Annotation of rat/lonsequence.pm, revision 1.48

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

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