Annotation of rat/lonsequence.pm, revision 1.50

1.1       www         1: # The LearningOnline Network with CAPA
                      2: #
                      3: # Sequence Handler
                      4: #
1.50    ! raeburn     5: # $Id: lonsequence.pm,v 1.49 2018/04/14 02:29:57 raeburn Exp $
1.5       www         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: #
1.1       www        29: 
1.33      jms        30: 
                     31: 
1.3       www        32: package Apache::lonsequence;
1.1       www        33: 
                     34: use strict;
                     35: use Apache::lonnet;
1.3       www        36: use Apache::Constants qw(:common :http REDIRECT);
1.1       www        37: use GDBM_File;
1.28      albertel   38: use LONCAPA::map();
1.49      raeburn    39: use LONCAPA::ltiutils;
1.41      foxr       40: use LONCAPA;
1.28      albertel   41: use Apache::lonpageflip();
                     42: use Apache::loncommon();
1.31      albertel   43: use Apache::groupsort();
1.16      www        44: use Apache::lonlocal;
1.46      raeburn    45: use Apache::lonnavmaps();
                     46: use Apache::lonenc();
1.30      albertel   47: use HTML::Entities();
1.1       www        48: 
1.12      www        49: my %selhash;
                     50: my $successtied;
1.8       www        51: 
                     52: # ----------------------------------------- Attempt to read from resource space
                     53: 
                     54: sub attemptread {
1.32      albertel   55:     my ($fn,$unsorted)=@_;
1.8       www        56:     &Apache::lonnet::repcopy($fn);
                     57:     if (-e $fn) {
1.32      albertel   58: 	return &LONCAPA::map::attemptread($fn,$unsorted);
1.8       www        59:     } else {
                     60:         return ();
                     61:     }
                     62: }
                     63: 
1.15      www        64: sub mapread {
                     65:     my $fn=shift;
                     66:     &Apache::lonnet::repcopy($fn);
                     67:     if (-e $fn) {
1.28      albertel   68: 	return &LONCAPA::map::mapread($fn,'');
1.15      www        69:     } else {
                     70:         return ();
                     71:     }
                     72: }
                     73: 
1.8       www        74: # ---------------------------------------------------------------- View Handler
                     75: 
                     76: sub viewmap {
1.9       www        77:     my ($r,$url)=@_;
1.26      albertel   78: 
                     79:     my $js;
                     80:     if ($env{'form.forceselect'}) {
                     81: 	$js = (<<ENDSCRIPT);
                     82: <script type="text/javascript">
1.8       www        83: 
                     84: function select_group() {
1.12      www        85:     window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
1.8       www        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:     }
1.26      albertel  104: 
                    105:     $r->print(&Apache::loncommon::start_page('Map Contents',$js).
                    106: 	      '<h1>'.$url.'</h1>');
1.12      www       107: # ------------------ This is trying to select. Provide buttons and tie %selhash
1.24      albertel  108:     if ($env{'form.forceselect'}) { $r->print(<<ENDSELECT);
1.38      bisitz    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()" />
1.8       www       113: </form>   
                    114: ENDSELECT
1.12      www       115:     my $diropendb = 
1.41      foxr      116:   LONCAPA::tempdir() .
                    117:     "$env{'user.domain'}\_$env{'user.name'}_sel_res.db";
1.13      albertel  118:         if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.24      albertel  119: 	    if ($env{'form.launch'} eq '1') {
1.12      www       120: 	       &start_fresh_session();
                    121: 	    }
                    122:             $successtied=1;
                    123: 
                    124: # - Evaluate actions from previous page (both cumulatively and chronologically)
1.31      albertel  125: 	    if ($env{'form.catalogmode'} eq 'import') {
                    126: 		&Apache::groupsort::update_actions_hash(\%selhash);
1.12      www       127: 	    }
                    128: # -
                    129:         }
1.8       www       130:     }
1.12      www       131: # ----------------------------- successtied is now '1' if in working selectmode
1.15      www       132:     my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
                    133:     if ($fatal==1) {
1.37      bisitz    134:        $r->print('<p class="LC_warning">'
                    135:                 .&mt('Map contents are not shown in order.')
                    136:                 .'</p><br />');
1.15      www       137:     }
1.8       www       138:     my $idx=0;
1.45      raeburn   139:     foreach my $entry (&attemptread(&Apache::lonnet::filelocation('',$url))) {
                    140: 	if (defined($entry)) {
1.8       www       141:             $idx++;
1.12      www       142:             if ($successtied) { 
1.8       www       143: 		$r->print('<form name="form'.$idx.'">');
                    144:             }
1.45      raeburn   145: 	    my ($title,$url)=split(/\:/,$entry);
1.30      albertel  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,'\'"<>&');
1.8       www       155:             if ($url) {
1.12      www       156: 		if ($successtied) {
                    157: 		    my $checked='';
                    158: 	           if ($selhash{'store_'.$url}) {
1.38      bisitz    159: 	       	      $checked=' checked="checked"';
1.12      www       160: 	           }
                    161: 	           $selhash{"pre_${idx}_link"}=$url;
                    162: 	           $selhash{"pre_${idx}_title"}=$title;
1.30      albertel  163: 		    
                    164: 		    $url  = &HTML::Entities::encode($url, '\'"<>&');
1.8       www       165: 		    $r->print(<<ENDCHECKBOX);
                    166: <input type='checkbox' name='filelink' 
1.30      albertel  167: value='$enc_url' onClick='javascript:queue("form$idx")'$checked />
                    168: <input type='hidden' name='title' value='$enc_title' />
1.8       www       169: ENDCHECKBOX
                    170:                 }
1.30      albertel  171: 		$r->print('<a href="'.$enc_url.'">');
1.8       www       172:             }
1.30      albertel  173:             $r->print($enc_title);
1.8       www       174:             if ($url) { $r->print('</a>'); }
1.12      www       175:             if ($successtied) {
1.8       www       176: 		$r->print('</form>');
                    177:             } else {
1.39      bisitz    178: 		$r->print('<br />');
1.8       www       179:             }
                    180:         }
                    181:     }
1.26      albertel  182:     $r->print(&Apache::loncommon::end_page());
1.12      www       183:     if ($successtied) {
                    184: 	untie %selhash;
                    185:     }
1.8       www       186: }
                    187: 
1.12      www       188: # ----------------------------------------------------------- Clean out selhash
                    189: sub start_fresh_session {
1.45      raeburn   190:     foreach my $item (keys(%selhash)) {
                    191: 	if ($item =~ /^pre_/) {
                    192: 	    delete $selhash{$item};
1.12      www       193: 	}
1.45      raeburn   194: 	if ($item =~ /^store/) {
                    195: 	    delete $selhash{$item};
1.12      www       196: 	}
                    197:     }
                    198: }
                    199: 
                    200: 
1.1       www       201: # ================================================================ Main Handler
                    202: 
                    203: sub handler {
                    204:    my $r=shift;
                    205: 
                    206:    if ($r->header_only) {
1.16      www       207:       &Apache::loncommon::content_type($r,'text/html');
1.1       www       208:       $r->send_http_header;
                    209:       return OK;
                    210:    }
1.46      raeburn   211: 
                    212:    my $requrl=$r->uri;
1.8       www       213:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.46      raeburn   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:            } elsif ($result eq 'update') {
                    238:                my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    239:                my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    240:                my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
                    241:                if ($ferr) {
                    242:                    my $requrl = $r->uri;
                    243:                    $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
                    244:                    $env{'user.reinit'} = 1;
                    245:                    return HTTP_NOT_ACCEPTABLE;
                    246:                }
                    247:            }
                    248: 
                    249:            # Create the nav map
                    250:            my $navmap = Apache::lonnavmaps::navmap->new();
                    251: 
                    252:            if (ref($navmap)) {
                    253:                # renderer call
                    254:                if (&Apache::lonnet::is_on_map($requrl)) {
1.50    ! raeburn   255:                    my ($ltiscope,$ltiuri);
1.49      raeburn   256:                    if (($env{'request.lti.login'}) && ($env{'request.lti.uri'})) {
                    257:                        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    258:                        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.50    ! raeburn   259:                        ($ltiscope,$ltiuri) = &LONCAPA::ltiutils::lti_provider_scope($env{'request.lti.uri'},$cdom,$cnum);
1.49      raeburn   260:                    }
1.46      raeburn   261:                    my $mapurl = &Apache::lonnet::declutter($requrl);
1.49      raeburn   262:                    my @crumbs;
1.50    ! raeburn   263:                    unless ($ltiscope eq 'resource') {
        !           264:                        unless ($ltiscope eq 'map') {
        !           265:                            @crumbs = ({text  => $crstype.' Contents',
        !           266:                                        href  => "javascript:gopost('/adm/navmaps','')"});
        !           267:                        }
1.49      raeburn   268:                        my $res = $navmap->getResourceByUrl($mapurl);
                    269:                        if (ref($res)) {
                    270:                            my $symb = $res->symb();
                    271:                            if ($symb) {
                    272:                                my ($parent) = &Apache::lonnet::decode_symb($res->symb());
1.50    ! raeburn   273:                                if (($parent ne $env{'course.'.$env{'request.course.id'}.'.url'}) &&
        !           274:                                    !(($ltiscope eq 'map') && ($requrl eq $ltiuri))) {
1.49      raeburn   275:                                    my @mapcrumbs = $navmap->recursed_crumbs($parent);
                    276:                                    if (@mapcrumbs) {
                    277:                                        push(@crumbs,@mapcrumbs);
                    278:                                    }
1.48      raeburn   279:                                }
1.49      raeburn   280:                                $env{'request.symb'} = $symb;
1.46      raeburn   281:                            }
                    282:                        }
                    283:                    }
                    284:                    my $maptitle = &Apache::lonnet::gettitle($mapurl);
                    285:                    push(@crumbs,{text => $maptitle, no_mt => 1});
                    286:                    my $args = {'bread_crumbs' => \@crumbs,
                    287:                                'bread_crumbs_nomenu' => 1};
                    288:                    &Apache::loncommon::content_type($r,'text/html');
                    289:                    $r->send_http_header;
                    290: 
                    291:                    $r->print(&Apache::loncommon::start_page($maptitle,undef,$args));
                    292: 
                    293:                    my $renderArgs = { 'cols'                    => [0,1,2,3],
                    294:                                       'url'                     => $mapurl,
                    295:                                       'navmap'                  => $navmap,
                    296:                                       'suppressNavmap'          => 1,
                    297:                                       'suppressEmptySequences'  => 1,
                    298:                                       'filterFunc'              => undef,
                    299:                                       'resource_no_folder_link' => 1,
                    300:                                       'r'                       => $r,
                    301:                                       'caller'                  => 'sequence',
                    302:                                       'notools'                 => 1,
                    303:                                       'iterator_map'            => $mapurl,
                    304:                                     };
                    305: 
                    306:                    my $render = &Apache::lonnavmaps::render($renderArgs);
                    307: 
                    308:                    # If no resources were found let the user know.
                    309:                    if ($renderArgs->{'counter'} == 0) {
                    310:                        $r->print("<p><span class=\"LC_info\">".
                    311:                                  &mt("This folder is empty.")."</span></p>");
                    312:                    }
                    313:                    $r->print(&Apache::loncommon::end_page());
                    314:                    $r->rflush();
                    315:                    return OK;
                    316:                }
                    317:            }
                    318:        }
                    319:    }
1.1       www       320: 
1.2       www       321:    my %hash;
1.1       www       322:    my %bighash;
1.2       www       323: 
1.12      www       324:    $successtied=0;
1.3       www       325: # ------------------------------------------------------------ Tie symb db file
1.9       www       326:   my $disurl='';
                    327:   my $dismapid='';
1.18      raeburn   328:   my $exitdisid = '';
                    329:   my $arrow_dir = '';
1.40      raeburn   330:   my $is_encrypted = '';
1.9       www       331: 
1.24      albertel  332:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
1.2       www       333:        my $last;
1.24      albertel  334:        if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.13      albertel  335:                     &GDBM_READER(),0640)) {
1.2       www       336: 	   $last=$hash{'last_direction'};
                    337:            untie(%hash);
                    338:        }
1.3       www       339:        my $direction='';
                    340:        my $prevmap='';
                    341:        if ($last) {
1.23      albertel  342: 	   ($prevmap,undef,$direction)=&Apache::lonnet::decode_symb($last);
1.3       www       343:        }
                    344: # ------------------------------------------------------------- Tie big db file
1.24      albertel  345:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.13      albertel  346:                     &GDBM_READER(),0640)) {
1.3       www       347: 	   my $disid='';
1.17      raeburn   348:            my $randomout ='';
1.10      www       349: 
1.3       www       350:            if ($direction eq 'back') {
                    351: 	       $disid=$bighash{'map_finish_'.$requrl};
                    352:            } else {
                    353:                $disid=$bighash{'map_start_'.$requrl};
1.18      raeburn   354:            }
1.3       www       355:            if ($disid) {
                    356: 	       $disurl=$bighash{'src_'.$disid};
1.4       www       357:                $dismapid=(split(/\./,$disid))[1];
1.25      albertel  358: 	       if (!$env{'request.role.adv'}) {
                    359: 		   $randomout = $bighash{'randomout_'.$disid};
                    360: 	       }
1.40      raeburn   361:                if (!$env{'request.role.adv'}) {
                    362:                    $is_encrypted = $bighash{'encrypted_'.$disid};
                    363:                }
1.24      albertel  364:            } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.18      raeburn   365:                     &GDBM_READER(),0640)) {
                    366:                $last=$hash{'last_known'};
                    367:                untie(%hash);
1.3       www       368:            }
1.18      raeburn   369: 
                    370: 
1.17      raeburn   371: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
1.18      raeburn   372:            while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
1.11      www       373: 	       $direction=($direction?$direction:'forward');
                    374:                ($disid,$requrl)=
                    375:                          &Apache::lonpageflip::fullmove($disid,
                    376:                            &Apache::lonnet::declutter($requrl),$direction);
                    377:                if ($disid) {
                    378: 	           $disurl=$bighash{'src_'.$disid};
                    379:                    $dismapid=(split(/\./,$disid))[1];
1.25      albertel  380: 		   if (!$env{'request.role.adv'}) {
                    381: 		       $randomout = $bighash{'randomout_'.$disid};
                    382: 		   }
1.40      raeburn   383:                    if (!$env{'request.role.adv'}) {
                    384:                        $is_encrypted = $bighash{'encrypted_'.$disid};
                    385:                    }
1.11      www       386:                }
                    387:  	   }
1.18      raeburn   388:            $exitdisid = $disid;
                    389:            $arrow_dir = $direction;
1.11      www       390: 
1.3       www       391: # --------------------------------------- Untie hash, make sure to come by here
                    392:            untie(%bighash);
1.9       www       393:        }
                    394:    }
                    395: 
                    396: # now either disurl is set (going to first page), or we need another display
                    397:    if ($disurl) {
1.3       www       398: # -------------------------------------------------- Has first or last resource
1.40      raeburn   399:       my $showdisurl = $disurl;
                    400:       if ($is_encrypted) {
                    401:           $showdisurl = &Apache::lonenc::encrypted($disurl);
                    402:       }
1.23      albertel  403:       &Apache::lonnet::symblist($requrl,$disurl => [$disurl,$dismapid],
                    404: 				'last_known' => [$disurl,$dismapid]); 
1.16      www       405:       &Apache::loncommon::content_type($r,'text/html');
1.36      raeburn   406:       $r->header_out(Location => &Apache::lonnet::absolute_url($ENV{'SERVER_NAME'}).
1.40      raeburn   407:                                  $showdisurl);
1.9       www       408:       return REDIRECT;
1.1       www       409:    } else {
1.16      www       410:        &Apache::loncommon::content_type($r,'text/html');
1.9       www       411:        $r->send_http_header;
1.21      albertel  412:        if ($exitdisid eq '' && $arrow_dir ne '') {
1.18      raeburn   413:            my %lt =&Apache::lonlocal::texthash(
                    414:                    'nere' => 'Next resource could not be displayed',
                    415:                    'goba' => 'Go Back',
1.44      raeburn   416:                    'nacc' => 'Course Contents',
1.18      raeburn   417:                           );
1.44      raeburn   418:            if (&Apache::loncommon::course_type() eq 'Community') {
                    419:                $lt{'nav'} = &mt('Community Contents');
                    420:            }
1.42      raeburn   421:            my $warnmsg;
1.18      raeburn   422:            if ($arrow_dir eq 'forward') {
1.42      raeburn   423:                $warnmsg = &mt('As all folders and sequences '
                    424:                              .'following the current resource were empty, '
                    425:                              .'you have now reached the end of the course.');
1.18      raeburn   426:            } elsif ($arrow_dir eq 'back') {
1.43      raeburn   427:                $warnmsg = &mt('As all folders and sequences '
1.42      raeburn   428:                              .'preceding the current resource were empty, '
                    429:                              .'you have now reached the beginning of the course.');
1.18      raeburn   430:            }
1.26      albertel  431:            my $start_page=
                    432: 	       &Apache::loncommon::start_page('Empty Folder/Sequence');
                    433:            my $end_page=
                    434: 	       &Apache::loncommon::end_page();
1.18      raeburn   435:            $r->print(<<ENDNONE);
1.26      albertel  436: $start_page
1.18      raeburn   437: <h3>$lt{'nere'}</h3>
                    438: <p>$warnmsg</p>
                    439: <ul>
                    440:   <li><a href="javascript:history.go(-1)">$lt{'goba'}</a></li>
                    441:   <li><a href="/adm/navmaps">$lt{'nacc'}</a></li>
                    442: </ul>
1.26      albertel  443: $end_page
1.18      raeburn   444: ENDNONE
                    445:        } else {
                    446:            &viewmap($r,$requrl);
                    447:        }
1.9       www       448:        return OK;
1.1       www       449:    }
                    450: }
                    451: 
                    452: 1;
                    453: __END__
                    454: 
1.35      jms       455: =head1 NAME
                    456: 
                    457: Apache::lonsequence
                    458: 
                    459: =head1 SYNOPSIS
                    460: 
                    461: Handler for showing sequence objects of
                    462: educational resources.
                    463: 
                    464: This is part of the LearningOnline Network with CAPA project
                    465: described at http://www.lon-capa.org.
                    466: 
                    467: =head1 SUBROUTINES
                    468: 
                    469: =over
                    470: 
                    471: =item handler()
                    472: 
                    473: =item viewmap()
                    474: 
                    475: =item attemptread()
1.1       www       476: 
1.35      jms       477: =item mapread()
                    478: 
                    479: =item start_fresh_session()
                    480: 
                    481: =back
                    482: 
                    483: =cut
1.1       www       484: 
                    485: 
                    486: 
                    487: 
                    488: 

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