File:  [LON-CAPA] / rat / lonsequence.pm
Revision 1.59: download - view: text, annotated - select for diffs
Sat Oct 29 18:13:29 2022 UTC (16 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 6975
  - &get_supplemental() routine moved from lonnet.pm to loncommon.pm
  - use LONCAPA::map() moved out of conditional block in loncommon.pm
  - $keeporder arg for update_content_constraints() no longer needed

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Sequence Handler
    4: #
    5: # $Id: lonsequence.pm,v 1.59 2022/10/29 18:13:29 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: 
   31: 
   32: package Apache::lonsequence;
   33: 
   34: use strict;
   35: use Apache::lonnet;
   36: use Apache::Constants qw(:common :http REDIRECT);
   37: use GDBM_File;
   38: use LONCAPA::map();
   39: use LONCAPA::ltiutils;
   40: use LONCAPA;
   41: use Apache::lonpageflip();
   42: use Apache::loncommon();
   43: use Apache::groupsort();
   44: use Apache::lonlocal;
   45: use Apache::lonnavmaps();
   46: use Apache::lonenc();
   47: use HTML::Entities();
   48: 
   49: my %selhash;
   50: my $successtied;
   51: 
   52: # ----------------------------------------- Attempt to read from resource space
   53: 
   54: sub attemptread {
   55:     my ($fn,$unsorted)=@_;
   56:     &Apache::lonnet::repcopy($fn);
   57:     if (-e $fn) {
   58: 	return &LONCAPA::map::attemptread($fn,$unsorted);
   59:     } else {
   60:         return ();
   61:     }
   62: }
   63: 
   64: sub mapread {
   65:     my $fn=shift;
   66:     &Apache::lonnet::repcopy($fn);
   67:     if (-e $fn) {
   68: 	return &LONCAPA::map::mapread($fn,'');
   69:     } else {
   70:         return ();
   71:     }
   72: }
   73: 
   74: # ---------------------------------------------------------------- View Handler
   75: 
   76: sub viewmap {
   77:     my ($r,$url)=@_;
   78: 
   79:     my $js;
   80:     if ($env{'form.forceselect'}) {
   81: 	$js = (<<ENDSCRIPT);
   82: <script type="text/javascript">
   83: 
   84: function select_group() {
   85:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
   86: }
   87: 
   88: function queue(val) {
   89:     if (eval("document.forms."+val+".filelink.checked")) {
   90: 	var l=val.length;
   91: 	var v=val.substring(4,l);
   92: 	document.forms.fileattr.acts.value+='1a'+v+'b';
   93:     }
   94:     else {
   95: 	var l=val.length;
   96: 	var v=val.substring(4,l);
   97: 	document.forms.fileattr.acts.value+='0a'+v+'b';
   98:     }
   99: }
  100: 
  101: </script>
  102: ENDSCRIPT
  103:     }
  104: 
  105:     $r->print(&Apache::loncommon::start_page('Map Contents',$js).
  106: 	      '<h1>'.$url.'</h1>');
  107: # ------------------ This is trying to select. Provide buttons and tie %selhash
  108:     if ($env{'form.forceselect'}) { $r->print(<<ENDSELECT);
  109: <form name="fileattr"><input type="hidden" name="acts" value="" />
  110: <input type="button" name="close" value="CLOSE" onClick="self.close()" />
  111: <input type="button" name="groupimport" value="GROUP IMPORT"
  112: onClick="javascript:select_group()" />
  113: </form>   
  114: ENDSELECT
  115:     my $diropendb = 
  116:   LONCAPA::tempdir() .
  117:     "$env{'user.domain'}\_$env{'user.name'}_sel_res.db";
  118:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  119: 	    if ($env{'form.launch'} eq '1') {
  120: 	       &start_fresh_session();
  121: 	    }
  122:             $successtied=1;
  123: 
  124: # - Evaluate actions from previous page (both cumulatively and chronologically)
  125: 	    if ($env{'form.catalogmode'} eq 'import') {
  126: 		&Apache::groupsort::update_actions_hash(\%selhash);
  127: 	    }
  128: # -
  129:         }
  130:     }
  131: # ----------------------------- successtied is now '1' if in working selectmode
  132:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
  133:     if ($fatal==1) {
  134:        $r->print('<p class="LC_warning">'
  135:                 .&mt('Map contents are not shown in order.')
  136:                 .'</p><br />');
  137:     }
  138:     my $idx=0;
  139:     foreach my $entry (&attemptread(&Apache::lonnet::filelocation('',$url))) {
  140: 	if (defined($entry)) {
  141:             $idx++;
  142:             if ($successtied) { 
  143: 		$r->print('<form name="form'.$idx.'">');
  144:             }
  145: 	    my ($title,$url)=split(/\:/,$entry);
  146: 	    $title = &LONCAPA::map::qtescape($title);
  147: 	    unless ($title) { $title=(split(/\//,$url))[-1] };
  148:             my $enc_title = &HTML::Entities::encode($title,'\'"<>&');
  149: 	    unless ($title) {
  150: 		$title='<i>'.&mt('Empty').'</i>';
  151: 		$enc_title = &mt('Empty');
  152: 	    }
  153: 	    $url  = &LONCAPA::map::qtescape($url);
  154:             my $enc_url = &HTML::Entities::encode($url,'\'"<>&');
  155:             if ($url) {
  156: 		if ($successtied) {
  157: 		    my $checked='';
  158: 	           if ($selhash{'store_'.$url}) {
  159: 	       	      $checked=' checked="checked"';
  160: 	           }
  161: 	           $selhash{"pre_${idx}_link"}=$url;
  162: 	           $selhash{"pre_${idx}_title"}=$title;
  163: 		    
  164: 		    $url  = &HTML::Entities::encode($url, '\'"<>&');
  165: 		    $r->print(<<ENDCHECKBOX);
  166: <input type='checkbox' name='filelink' 
  167: value='$enc_url' onClick='javascript:queue("form$idx")'$checked />
  168: <input type='hidden' name='title' value='$enc_title' />
  169: ENDCHECKBOX
  170:                 }
  171: 		$r->print('<a href="'.$enc_url.'">');
  172:             }
  173:             $r->print($enc_title);
  174:             if ($url) { $r->print('</a>'); }
  175:             if ($successtied) {
  176: 		$r->print('</form>');
  177:             } else {
  178: 		$r->print('<br />');
  179:             }
  180:         }
  181:     }
  182:     $r->print(&Apache::loncommon::end_page());
  183:     if ($successtied) {
  184: 	untie %selhash;
  185:     }
  186: }
  187: 
  188: # ----------------------------------------------------------- Clean out selhash
  189: sub start_fresh_session {
  190:     foreach my $item (keys(%selhash)) {
  191: 	if ($item =~ /^pre_/) {
  192: 	    delete $selhash{$item};
  193: 	}
  194: 	if ($item =~ /^store/) {
  195: 	    delete $selhash{$item};
  196: 	}
  197:     }
  198: }
  199: 
  200: 
  201: # ================================================================ Main Handler
  202: 
  203: sub handler {
  204:    my $r=shift;
  205: 
  206:    if ($r->header_only) {
  207:       &Apache::loncommon::content_type($r,'text/html');
  208:       $r->send_http_header;
  209:       return OK;
  210:    }
  211: 
  212:    my $requrl=$r->uri;
  213:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  214:                                           ['forceselect','launch','navmap']);
  215: 
  216:    if (($env{'request.course.fn'}) && ($env{'form.navmap'}) && ($env{'request.course.id'})) {
  217:        my $crstype = &Apache::loncommon::course_type();
  218:        unless (($crstype eq 'Placement') && (!$env{'request.role.adv'})) {
  219: 
  220:            # Check for critical messages and redirect if present.
  221:            my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'contents');
  222:            if ($redirect) {
  223:                &Apache::loncommon::content_type($r,'text/html');
  224:                $r->header_out(Location => $url);
  225:                return REDIRECT;
  226:            }
  227: 
  228:            # Check if course needs to be re-initialized
  229:            my $loncaparev = $r->dir_config('lonVersion');
  230:            my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  231: 
  232:            if ($result eq 'switch') {
  233:                &Apache::loncommon::content_type($r,'text/html');
  234:                $r->send_http_header;
  235:                $r->print(&Apache::loncommon::check_release_result(@reinit));
  236:                return OK;
  237:            }
  238:            my ($cnum,$cdom);
  239:            if ($result) {
  240:                $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  241:                $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  242:            }
  243:            if (($result eq 'main') || ($result eq 'both')) {
  244:                my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
  245:                if ($ferr) {
  246:                    my $requrl = $r->uri;
  247:                    $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  248:                    $env{'user.reinit'} = 1;
  249:                    return HTTP_NOT_ACCEPTABLE;
  250:                }
  251:            }
  252:            if (($result eq 'both') || ($result eq 'supp')) {
  253:                my $possdel;
  254:                if ($result eq 'supp') {
  255:                    $possdel = 1;
  256:                }
  257:                my ($supplemental,$refs_updated) = &Apache::loncommon::get_supplemental($cnum,$cdom,'',$possdel);
  258:                unless ($refs_updated) {
  259:                    &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental,$possdel);
  260:                }
  261:            }
  262:            &Apache::loncommon::content_type($r,'text/html');
  263:            $r->send_http_header;
  264: 
  265:            my $mapurl = &Apache::lonnet::declutter($requrl);
  266:            my $maptitle = &Apache::lonnet::gettitle($mapurl);
  267:            my @crumbs = ({text => $maptitle, no_mt => 1});
  268:            my $args = {'bread_crumbs' => \@crumbs,
  269:                        'bread_crumbs_nomenu' => 1};
  270: 
  271:            # Create the nav map
  272:            my $navmap = Apache::lonnavmaps::navmap->new();
  273: 
  274:            if (ref($navmap)) {
  275:                # renderer call
  276:                if (&Apache::lonnet::is_on_map($requrl)) {
  277:                    my ($ltiscope,$ltiuri);
  278:                    if (($env{'request.lti.login'}) && ($env{'request.lti.uri'})) {
  279:                        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  280:                        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  281:                        ($ltiscope,$ltiuri) = &LONCAPA::ltiutils::lti_provider_scope($env{'request.lti.uri'},$cdom,$cnum);
  282:                    }
  283:                    @crumbs = ();
  284:                    unless ($ltiscope eq 'resource') {
  285:                        unless ($ltiscope eq 'map') {
  286:                            @crumbs = ({text  => $crstype.' Contents',
  287:                                        href  => "javascript:gopost('/adm/navmaps','')"});
  288:                        }
  289:                        my $res = $navmap->getResourceByUrl($mapurl);
  290:                        if (ref($res)) {
  291:                            my $symb = $res->symb();
  292:                            if ($symb) {
  293:                                my ($parent) = &Apache::lonnet::decode_symb($res->symb());
  294:                                if (($parent ne $env{'course.'.$env{'request.course.id'}.'.url'}) &&
  295:                                    !(($ltiscope eq 'map') && ($requrl eq $ltiuri))) {
  296:                                    my @mapcrumbs = $navmap->recursed_crumbs($parent);
  297:                                    if (@mapcrumbs) {
  298:                                        push(@crumbs,@mapcrumbs);
  299:                                    }
  300:                                }
  301:                                $env{'request.symb'} = $symb;
  302:                            }
  303:                        }
  304:                    }
  305:                    push(@crumbs,{text => $maptitle, no_mt => 1});
  306:                    $args = {'bread_crumbs' => \@crumbs,
  307:                             'bread_crumbs_nomenu' => 1,
  308:                             'no_auto_mt_title' => 1};
  309:                    $r->print(&Apache::loncommon::start_page($maptitle,undef,$args));
  310: 
  311:                    my $renderArgs = { 'cols'                    => [0,1,2,3],
  312:                                       'url'                     => $mapurl,
  313:                                       'navmap'                  => $navmap,
  314:                                       'suppressNavmap'          => 1,
  315:                                       'suppressEmptySequences'  => 1,
  316:                                       'filterFunc'              => undef,
  317:                                       'resource_no_folder_link' => 1,
  318:                                       'r'                       => $r,
  319:                                       'caller'                  => 'sequence',
  320:                                       'notools'                 => 1,
  321:                                       'iterator_map'            => $mapurl,
  322:                                     };
  323: 
  324:                    my $render = &Apache::lonnavmaps::render($renderArgs);
  325: 
  326:                    # If no resources were found let the user know.
  327:                    if ($renderArgs->{'counter'} == 0) {
  328:                        $r->print('<p class="LC_info">'.
  329:                                  &mt('No items found in folder').
  330:                                  '</p>');
  331:                    }
  332:                    $r->print(&Apache::loncommon::end_page());
  333:                } else {
  334:                    $r->print(&Apache::loncommon::start_page($maptitle,undef,$args).
  335:                              '<p class="LC_info">'.
  336:                              &mt('Folder no longer appears to be a part of the course').
  337:                              '</p>'.
  338:                              &Apache::loncommon::end_page());
  339:                }
  340:            } else {
  341:                $r->print(&Apache::loncommon::start_page($maptitle,undef,$args).
  342:                          '<p class="LC_warning">'.
  343:                          &mt('Error: could not determine contents of folder').
  344:                          '</p>'.
  345:                          &Apache::loncommon::end_page());
  346:            }
  347:            $r->rflush();
  348:            return OK;
  349:        }
  350:    }
  351: 
  352:    my %hash;
  353:    my %bighash;
  354: 
  355:    $successtied=0;
  356: # ------------------------------------------------------------ Tie symb db file
  357:   my $disurl='';
  358:   my $dismapid='';
  359:   my $exitdisid = '';
  360:   my $arrow_dir = '';
  361:   my $is_encrypted = '';
  362: 
  363:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  364:        my $last;
  365:        if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  366:                     &GDBM_READER(),0640)) {
  367: 	   $last=$hash{'last_direction'};
  368:            untie(%hash);
  369:        }
  370:        my $direction='';
  371:        my $prevmap='';
  372:        if ($last) {
  373: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
  374:        }
  375: # ------------------------------------------------------------- Tie big db file
  376:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  377:                     &GDBM_READER(),0640)) {
  378: 	   my $disid='';
  379:            my $randomout ='';
  380: 
  381:            if ($direction eq 'back') {
  382: 	       $disid=$bighash{'map_finish_'.$requrl};
  383:            } else {
  384:                $disid=$bighash{'map_start_'.$requrl};
  385:            }
  386:            if ($disid) {
  387: 	       $disurl=$bighash{'src_'.$disid};
  388:                $dismapid=(split(/\./,$disid))[1];
  389: 	       if (!$env{'request.role.adv'}) {
  390: 		   $randomout = $bighash{'randomout_'.$disid};
  391:                    $is_encrypted = $bighash{'encrypted_'.$disid};
  392:                }
  393:            } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  394:                     &GDBM_READER(),0640)) {
  395:                $last=$hash{'last_known'};
  396:                untie(%hash);
  397:            }
  398: 
  399: 
  400: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
  401:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
  402: 	       $direction=($direction?$direction:'forward');
  403:                ($disid,$requrl)=
  404:                          &Apache::lonpageflip::fullmove($disid,
  405:                            &Apache::lonnet::declutter($requrl),$direction);
  406:                if ($disid) {
  407: 	           $disurl=$bighash{'src_'.$disid};
  408:                    $dismapid=(split(/\./,$disid))[1];
  409: 		   if (!$env{'request.role.adv'}) {
  410: 		       $randomout = $bighash{'randomout_'.$disid};
  411:                        $is_encrypted = $bighash{'encrypted_'.$disid};
  412:                    }
  413:                }
  414:  	   }
  415:            $exitdisid = $disid;
  416:            $arrow_dir = $direction;
  417: 
  418: # --------------------------------------- Untie hash, make sure to come by here
  419:            untie(%bighash);
  420:        }
  421:    }
  422: 
  423: # now either disurl is set (going to first page), or we need another display
  424:    if ($disurl) {
  425: # -------------------------------------------------- Has first or last resource
  426:       my $showdisurl = $disurl;
  427:       if ($is_encrypted) {
  428:           $showdisurl = &Apache::lonenc::encrypted($disurl);
  429:       }
  430:       if ($disurl =~ m{^/adm/navmaps(\?|$)}) {
  431:           &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid]);
  432:       } else {
  433:           &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
  434:                                     'last_known' => [$disurl,$dismapid]);
  435:       }
  436:       &Apache::loncommon::content_type($r,'text/html');
  437:       $r->header_out(Location => &Apache::lonnet::absolute_url().
  438:                                  $showdisurl);
  439:       return REDIRECT;
  440:    } else {
  441:        &Apache::loncommon::content_type($r,'text/html');
  442:        $r->send_http_header;
  443:        if ($exitdisid eq '' && $arrow_dir ne '') {
  444:            my %lt =&Apache::lonlocal::texthash(
  445:                    'nere' => 'Next resource could not be displayed',
  446:                    'goba' => 'Go Back',
  447:                    'nacc' => 'Course Contents',
  448:                           );
  449:            if (&Apache::loncommon::course_type() eq 'Community') {
  450:                $lt{'nav'} = &mt('Community Contents');
  451:            }
  452:            my $warnmsg;
  453:            if ($arrow_dir eq 'forward') {
  454:                $warnmsg = &mt('As all folders and sequences '
  455:                              .'following the current resource were empty, '
  456:                              .'you have now reached the end of the course.');
  457:            } elsif ($arrow_dir eq 'back') {
  458:                $warnmsg = &mt('As all folders and sequences '
  459:                              .'preceding the current resource were empty, '
  460:                              .'you have now reached the beginning of the course.');
  461:            }
  462:            my $start_page=
  463: 	       &Apache::loncommon::start_page('Empty Folder/Sequence');
  464:            my $end_page=
  465: 	       &Apache::loncommon::end_page();
  466:            $r->print(<<ENDNONE);
  467: $start_page
  468: <h3>$lt{'nere'}</h3>
  469: <p>$warnmsg</p>
  470: <ul>
  471:   <li><a href="javascript:history.go(-1)">$lt{'goba'}</a></li>
  472:   <li><a href="/adm/navmaps">$lt{'nacc'}</a></li>
  473: </ul>
  474: $end_page
  475: ENDNONE
  476:        } else {
  477:            &viewmap($r,$requrl);
  478:        }
  479:        return OK;
  480:    }
  481: }
  482: 
  483: 1;
  484: __END__
  485: 
  486: =head1 NAME
  487: 
  488: Apache::lonsequence
  489: 
  490: =head1 SYNOPSIS
  491: 
  492: Handler for showing sequence objects of
  493: educational resources.
  494: 
  495: This is part of the LearningOnline Network with CAPA project
  496: described at http://www.lon-capa.org.
  497: 
  498: =head1 SUBROUTINES
  499: 
  500: =over
  501: 
  502: =item handler()
  503: 
  504: =item viewmap()
  505: 
  506: =item attemptread()
  507: 
  508: =item mapread()
  509: 
  510: =item start_fresh_session()
  511: 
  512: =back
  513: 
  514: =cut
  515: 
  516: 
  517: 
  518: 
  519: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>