File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.114: download - view: text, annotated - select for diffs
Sat Oct 29 18:13:29 2022 UTC (18 months 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: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.114 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::lonpageflip;
   33: 
   34: use strict;
   35: use LONCAPA;
   36: use Apache::Constants qw(:common :http REDIRECT);
   37: use Apache::lonnet;
   38: use Apache::loncommon();
   39: use Apache::lonnavmaps();
   40: use Apache::lonuserstate;
   41: use Apache::lonlocal;
   42: use HTML::TokeParser;
   43: use GDBM_File;
   44: 
   45: # ========================================================== Module Global Hash
   46:   
   47: my %hash;
   48: 
   49: sub cleanup {
   50:     if (tied(%hash)){
   51: 	&Apache::lonnet::logthis('Cleanup pageflip: hash');
   52:         unless (untie(%hash)) {
   53: 	    &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
   54:         }
   55:     }
   56:     return OK;
   57: }
   58: 
   59: sub addrid {
   60:     my ($current,$new,$condid)=@_;
   61:     unless ($condid) { $condid=0; }
   62: 
   63: 	if ($current) {
   64: 	    $current.=','.$new;
   65:         } else {
   66:             $current=''.$new;
   67:         }
   68: 
   69:     return $current;
   70: }
   71: 
   72: sub fullmove {
   73:     my ($rid,$mapurl,$direction)=@_;
   74:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
   75:                         &GDBM_READER(),0640)) {
   76: 	($rid,$mapurl)=&move($rid,$mapurl,$direction);
   77:         untie(%hash);
   78:     }
   79:     return($rid,$mapurl);
   80: }
   81: 
   82: sub hash_src {
   83:     my ($id)=@_;
   84:     my ($mapid,$resid)=split(/\./,$id);
   85:     my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
   86: 					  $resid,$hash{'src_'.$id});
   87:     my $anchor;
   88:     if ($hash{'ext_'.$id} eq 'true:') {
   89:         if ($hash{'src_'.$id} =~ /(\#.+)$/) {
   90:             $anchor = $1;
   91:         }
   92:     }
   93:     if ($hash{'encrypted_'.$id}) {
   94: 	return (&Apache::lonenc::encrypted($hash{'src_'.$id}),
   95: 		&Apache::lonenc::encrypted($symb),
   96:                 $hash{'encrypted_'.$id},$anchor);
   97:     }
   98:     return ($hash{'src_'.$id},$symb,$hash{'encrypted_'.$id},$anchor);
   99: }
  100: 
  101: sub move {
  102:     my ($next,$endupmap,$direction,$firstres) = @_;
  103:     my $safecount=0;
  104:     my $allowed=0;
  105:     my $deeplinkonly=0;
  106:     my $deeplinkchecked;
  107:     my $deeplink_login_pc;
  108:     my $prev=$next;
  109:     my ($prevmapid)=split(/\./,$next);
  110:     do {
  111: 	($next,$endupmap)=&get_next_possible_move($next,$endupmap,$direction);
  112: 
  113: 	my $url = $hash{'src_'.$next};
  114: 	my ($mapid,$resid)=split(/\./,$next);
  115: 	my $symb = &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
  116: 						$resid,$url);
  117: 	if ($url eq '' || $symb eq '') {
  118: 	    $allowed = 0;
  119: 	} else {
  120:             my $nodeeplinkcheck = 0;
  121:             if ($hash{'is_map_'.$next}) {
  122:                 $nodeeplinkcheck = 1;
  123:             }
  124: 	    my $priv = &Apache::lonnet::allowed('bre',$url,$symb,'','','','',$nodeeplinkcheck);
  125: 	    $allowed = (($priv eq 'F') || ($priv eq '2') || ($priv eq 'A'));
  126: 	}
  127:         $deeplinkonly = 0;
  128:         if ($hash{'deeplinkonly_'.$next}) {
  129:             my ($value,$level) = map { &unescape($_); } split(/:/,$hash{'deeplinkonly_'.$next});
  130:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$value);
  131:             unless (($state eq 'both') || ($hash{'is_map_'.$next})) {
  132:                 if ($level eq 'resource') {
  133:                     $deeplinkonly = 1;
  134:                 } elsif ($level eq 'map') {
  135:                     if ($scope eq 'rec') {
  136:                         unless ($mapid == $prevmapid) {
  137:                             unless ($deeplinkchecked) {
  138:                                 $deeplink_login_pc = &get_deeplink_login_pc();
  139:                                 $deeplinkchecked = 1;
  140:                             }
  141:                             if ($deeplink_login_pc) {
  142:                                 my $poss_map_pc;
  143:                                 if ($hash{'is_map_'.$next}) {
  144:                                     $poss_map_pc = $hash{'map_pc_'.$url};
  145:                                 } else {
  146:                                     $poss_map_pc = $hash{'map_pc_'.$hash{'map_id_'.$mapid}};
  147:                                 }
  148:                                 unless ($deeplink_login_pc == $poss_map_pc) {
  149:                                     unless (grep(/^$deeplink_login_pc$/,split(/,/,$hash{'map_hierarchy_'.$poss_map_pc}))) {
  150:                                         $deeplinkonly = 1;
  151:                                     }
  152:                                 }
  153:                             } else {
  154:                                 $deeplinkonly = 1;
  155:                             }
  156:                         }
  157:                     } elsif ($mapid != $prevmapid) {
  158:                         $deeplinkonly = 1;
  159:                     }
  160:                 }
  161:             }
  162:         } elsif (($hash{'deeplinkonly_'.$prev}) && (!$firstres)) {
  163:             my ($value,$level) = map { &unescape($_); } split(/:/,$hash{'deeplinkonly_'.$prev});
  164:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$value);
  165:             unless (($state eq 'both') || ($hash{'is_map_'.$prev})) {
  166:                 if ($level eq 'resource') {
  167:                     $deeplinkonly = 1;
  168:                 } elsif ($level eq 'map') {
  169:                     if ($scope eq 'rec') {
  170:                         unless ($mapid == $prevmapid) {
  171:                             unless ($deeplinkchecked) {
  172:                                 $deeplink_login_pc = &get_deeplink_login_pc();
  173:                                 $deeplinkchecked = 1;
  174:                             }
  175:                             if ($deeplink_login_pc) {
  176:                                 my $poss_map_pc;
  177:                                 if ($hash{'is_map_'.$prev}) {
  178:                                     $poss_map_pc = $hash{'map_pc_'.$url};
  179:                                 } else {
  180:                                     $poss_map_pc = $hash{'map_pc_'.$hash{'map_id_'.$mapid}};
  181:                                 }
  182:                                 unless ($deeplink_login_pc == $poss_map_pc) {
  183:                                     unless (grep(/^$deeplink_login_pc$/,split(/,/,$hash{'map_hierarchy_'.$poss_map_pc}))) {
  184:                                         $deeplinkonly = 1;
  185:                                     }
  186:                                 }
  187:                             }
  188:                         }
  189:                     } else {
  190:                         if ($mapid != $prevmapid) {
  191:                             $deeplinkonly = 1;
  192:                         }
  193:                     }
  194:                 }
  195:             }
  196:         }
  197: 	$safecount++;
  198:     } while (   ($next)
  199: 	     && ($next!~/\,/)
  200: 	     && (
  201: 		    (!$hash{'src_'.$next})
  202: 		 || (
  203:                         (!$env{'request.role.adv'})
  204:                      && (($hash{'randomout_'.$next})
  205:                      ||  ($deeplinkonly)
  206:                      ||  ($hash{'deeplinkout_'.$next}))
  207: 		    )
  208: 		 || (!$allowed)
  209: 		)
  210: 	     && ($safecount<10000));
  211: 
  212:     return ($next,$endupmap);
  213: }
  214: 
  215: sub get_next_possible_move {
  216:     my ($rid,$mapurl,$direction)=@_;
  217:     my $startoutrid=$rid;
  218: 
  219:     my $next='';
  220: 
  221:               my $mincond=1;
  222:               my $posnext='';
  223:               if ($direction eq 'forward') {
  224: # --------------------------------------------------------------------- Forward
  225:                   while ($hash{'type_'.$rid} eq 'finish') {
  226: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  227:                   }
  228: 		  foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
  229: 		     my $condition= $hash{'conditions_'.$hash{'goesto_'.$id}};
  230: 		     my $rescond  = &Apache::lonnet::docondval($condition);
  231: 		     my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
  232: 		     my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
  233: 		     if ($thiscond>=$mincond) {
  234: 		          if ($posnext) {
  235: 		             $posnext.=','.$id.':'.$thiscond;
  236:                           } else {
  237:                              $posnext=$id.':'.$thiscond;
  238: 		          }
  239:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  240: 	              }
  241:                   }
  242: 		  foreach my $id (split(/\,/,$posnext))  {
  243:                       my ($linkid,$condval)=split(/\:/,$id);
  244:                       if ($condval>=$mincond) {
  245: 		          $next=&addrid($next,$hash{'goesto_'.$linkid},
  246:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  247:                       }
  248:                   }
  249:                   if ($hash{'is_map_'.$next}) {
  250: # This jumps to the beginning of a new map (going down level)
  251:                       if (
  252:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  253: 			  $mapurl=$hash{'src_'.$next};
  254: 			  $next=$hash{'map_start_'.$hash{'src_'.$next}};
  255:                      } elsif (
  256: # This jumps back up from an empty sequence, to a page up one level
  257:                          $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  258:                          $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  259:                      }
  260:                   } elsif 
  261:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  262: # This comes up from a map (coming up one level);
  263: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  264: 		  }
  265:               } elsif ($direction eq 'back') {
  266: # ------------------------------------------------------------------- Backwards
  267:                  while ($hash{'type_'.$rid} eq 'start') {
  268: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  269: 		 }
  270: 		 foreach my $id (split(/\,/,$hash{'from_'.$rid})) {
  271: 		     my $condition= $hash{'conditions_'.$hash{'comesfrom_'.$id}};
  272: 		     my $rescond  = &Apache::lonnet::docondval($condition);
  273: 		     my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
  274: 		     my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
  275: 		     if ($thiscond>=$mincond) {
  276: 			 if ($posnext) {
  277: 		             $posnext.=','.$id.':'.$thiscond;
  278: 			 } else {
  279:                              $posnext=$id.':'.$thiscond;
  280: 			 }
  281: 			 if ($thiscond>$mincond) { $mincond=$thiscond; }
  282: 		     }
  283: 		 }
  284: 		 foreach my $id (split(/\,/,$posnext)) {
  285: 		     my ($linkid,$condval)=split(/\:/,$id);
  286: 		     if ($condval>=$mincond) {
  287: 			 $next=&addrid($next,$hash{'comesfrom_'.$linkid},
  288: 				       $hash{'condid_'.$hash{'undercond_'.$linkid}});
  289: 		     }
  290: 		 }
  291:                   if ($hash{'is_map_'.$next}) {
  292: # This jumps to the end of a new map (going down one level)
  293:                       if (
  294:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  295: 			  $mapurl=$hash{'src_'.$next};
  296: 			  $next=$hash{'map_finish_'.$hash{'src_'.$next}};
  297:                       } elsif (
  298:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  299: # This jumps back up from an empty sequence, to a page up one level
  300:                           $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  301:                       }
  302:                   } elsif 
  303:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  304: # This comes back up from a map (going up one level);
  305: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  306:                   }
  307: 	      }
  308:               return ($next,$mapurl);
  309: }
  310: 
  311: sub first_accessible_resource {
  312:     my $furl;
  313:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  314: 	    &GDBM_READER(),0640)) {
  315: 	$furl=$hash{'first_url'};
  316: 	my (%args,$url,$argstr);
  317: 	if ($furl =~ m{^/enc/}) {
  318: 	    ($url,$argstr) = split(/\?/,&Apache::lonenc::unencrypted($furl));
  319: 	} else {
  320: 	    ($url,$argstr) = split(/\?/,$furl);
  321: 	}
  322: 	foreach my $pair (split(/\&/,$argstr)) {
  323: 	    my ($name,$value) = split(/=/,$pair);
  324: 	    $args{&unescape($name)} = &unescape($value);
  325: 	}
  326:         my $priv = &Apache::lonnet::allowed('bre',$url,$args{'symb'});
  327:         my $allowed = (($priv eq 'F') || ($priv eq '2') || ($priv eq 'A'));
  328:         if (!$allowed) {
  329: # Wow, we cannot see this ... move forward to the next one that we can see
  330: 	    my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward',1);
  331: # Build the new URL
  332:             if ($newrid eq '') {
  333:                 $furl = '/adm/navmaps';
  334:             } else {
  335: 	        my ($newmapid,$newresid)=split(/\./,$newrid);
  336: 	        my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
  337: 	        $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
  338: 	        if ($hash{'encrypted_'.$newrid}) {
  339: 		    $furl=&Apache::lonenc::encrypted($furl);
  340: 	        }
  341:             }
  342: 	}
  343: 	untie(%hash);
  344: 	return $furl;
  345:     } else {
  346: 	return '/adm/navmaps';
  347:     }
  348: }
  349: 
  350: sub first_answerable_ressymb {
  351:     my $navmap = Apache::lonnavmaps::navmap->new;
  352:     return unless (ref($navmap));
  353:     my $iterator = $navmap->getIterator(undef,undef,undef,1);
  354:     return unless (ref($iterator));
  355:     my ($curRes,$result);
  356:     while ($curRes = $iterator->next()) {
  357:         if (ref($curRes) && $curRes->is_problem()) {
  358:             foreach my $part (@{$curRes->parts()}) {
  359:                 if ($curRes->tries($part) < $curRes->maxtries($part)) {
  360:                     $result = $curRes->link().'?symb='.$curRes->shown_symb();
  361:                     last;
  362:                 }    
  363:             }
  364:         }
  365:     }
  366:     if ($result) {
  367:         return $result; 
  368:     } else {
  369:         return &first_accessible_resource(); 
  370:     }
  371: }
  372: 
  373: sub check_http_req {
  374:     my ($srcref,$hostname) = @_;
  375:     return unless (ref($srcref) eq 'SCALAR');
  376:     my $usehttp;
  377:     if ($env{'request.course.id'}) {
  378:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  379:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  380:         if (($$srcref =~ m{^\Q/public/$cdom/$cnum/syllabus\E($|\?)}) &&
  381:             ($ENV{'SERVER_PORT'} == 443) &&
  382:             ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
  383:             unless ((&Apache::lonnet::uses_sts()) ||
  384:                     (&Apache::lonnet::waf_allssl($hostname))) {
  385:                 $$srcref .= (($$srcref =~/\?/)? '&':'?') . 'usehttp=1';
  386:                 $usehttp = 1;
  387:             }
  388:         } elsif (($$srcref =~ m{^\Q/adm/wrapper/ext/\E(?!https:)}) &&
  389:                  ($ENV{'SERVER_PORT'} == 443)) {
  390:             unless ((&Apache::lonnet::uses_sts()) ||
  391:                     (&Apache::lonnet::waf_allssl($hostname))) {
  392:                 my ($url,$anchor) = ($$srcref =~ /^([^\#]+)(?:|(\#[^\#]+))$/);
  393:                 $$srcref = $url . (($$srcref =~/\?/)? '&':'?') . 'usehttp=1' .$anchor; 
  394:                 $usehttp = 1;
  395:             }
  396:         }
  397:     }
  398:     return $usehttp;
  399: }
  400: 
  401: sub reinited_js {
  402:     my ($url,$cid,$timeout) = @_;
  403:     if (!$timeout) {
  404:         $timeout = 0;
  405:     }
  406:     return <<"END";
  407: <script type="text/javascript">
  408: // <![CDATA[
  409: setTimeout(function() {
  410:     var newurl = '$url';
  411:     if (document.getElementById('LC_update_$cid')) {
  412:         document.getElementById('LC_update_$cid').style.display = 'none';
  413:     }
  414:     if ((newurl !== null) && (newurl !== '') && (newurl !== 'undefined')) {
  415:         window.location.href = "$url";
  416:     }
  417: }, $timeout);
  418: // ]]>
  419: </script>
  420: END
  421: }
  422: 
  423: sub get_deeplink_login_pc {
  424:     my $deeplink_login_pc;
  425:     if (($env{'request.deeplink.login'}) && ($env{'request.course.id'})) {
  426:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  427:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  428:         my $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
  429:         if ($deeplink_symb) {
  430:             my $loginmap;
  431:             if ($deeplink_symb =~ /\.(page|sequence)$/) {
  432:                 $loginmap = &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($deeplink_symb))[2]);
  433:             } else {
  434:                 $loginmap = &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($deeplink_symb))[0]);
  435:             }
  436:             $deeplink_login_pc = $hash{'map_pc_'.$loginmap};
  437:         }
  438:     }
  439:     return $deeplink_login_pc;
  440: }
  441: 
  442: # ================================================================ Main Handler
  443: 
  444: sub handler {
  445:    my $r=shift;
  446: 
  447: # ------------------------------------------- Set document type for header only
  448: 
  449:   if ($r->header_only) {
  450:       &Apache::loncommon::content_type($r,'text/html');
  451:       $r->send_http_header;
  452:       return OK;
  453:   }
  454: 
  455:   my %cachehash=(); 
  456:   my $multichoice=0;
  457:   my %multichoicehash=();
  458:   my %prog_state=();
  459:   my ($redirecturl,$redirectsymb,$enc,$anchor,$deeplinklevel);
  460:   my $next='';
  461:   my $hostname = $r->hostname();
  462:   my @possibilities=();
  463:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  464:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
  465:       my ($direction,$currenturl) = ($env{'form.postdata'}=~/(\w+)\:(.*)/);
  466:       if ($currenturl=~m|^/enc/|) {
  467:           $currenturl=&Apache::lonenc::unencrypted($currenturl);
  468:       }
  469:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  470:       $currenturl=~s/^https?\:\/\///;
  471:       $currenturl=~s/^[^\/]+//;
  472:       my ($preupdatepos,$last,$reinitcheck);
  473:       if ($direction eq 'return') {
  474:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  475:                     &GDBM_READER(),0640)) {
  476:               $last=$hash{'last_known'};
  477:               untie(%hash);
  478:           }
  479:       } elsif ($direction eq 'firstanswerable') {
  480:           my $furl = &first_answerable_ressymb();
  481:           my $usehttp = &check_http_req(\$furl,$hostname);
  482:           if (($usehttp) && ($hostname ne '')) {
  483:               $furl='http://'.$hostname.$furl;
  484:           } else {
  485:               $furl=&Apache::lonnet::absolute_url().$furl;
  486:           }
  487:           &Apache::loncommon::content_type($r,'text/html');
  488:           $r->header_out(Location => $furl);
  489:           return REDIRECT;
  490:       } elsif ($direction eq 'endplacement') {
  491:           &Apache::loncommon::content_type($r,'text/html');
  492:           $r->send_http_header;
  493:           $r->print(&Apache::lonplacementtest::showresult());
  494:           return OK;
  495:       }
  496:       if ($env{'request.course.id'}) {
  497:           # Check if course needs to be re-initialized
  498:           my $loncaparev = $r->dir_config('lonVersion');
  499:           ($reinitcheck,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  500:           if ($reinitcheck eq 'switch') {
  501:               &Apache::loncommon::content_type($r,'text/html');
  502:               $r->send_http_header;
  503:               $r->print(&Apache::loncommon::check_release_result(@reinit));
  504:               return OK;
  505:           }
  506:           my ($cnum,$cdom);
  507:           if ($reinitcheck) {
  508:               $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  509:               $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  510:           }
  511:           if (($reinitcheck eq 'main') || ($reinitcheck eq 'both')) {
  512:               $preupdatepos = &Apache::lonnet::symbread($currenturl);
  513:               unless ($direction eq 'return') {
  514:                   if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  515:                                 &GDBM_READER(),0640)) {
  516:                       $last=$hash{'last_known'};
  517:                       untie(%hash);
  518:                   }
  519:               }
  520:               &Apache::loncommon::content_type($r,'text/html');
  521:               $r->send_http_header;
  522:               $r->print(&Apache::loncommon::start_page('Content Changed'));
  523:               my $preamble = '<div id="LC_update_'.$env{'request.course.id'}.'" class="LC_info">'.
  524:                              '<br />'.
  525:                              &mt('Your course session is being updated because of recent changes by course personnel.').
  526:                              ' '.&mt('Please be patient').'.<br /></div>'.
  527:                              '<div style="padding:0;clear:both;margin:0;border:0"></div>';
  528:               %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
  529:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
  530:               my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
  531:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
  532:               if ($ferr) {
  533:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  534:                   my $requrl = $r->uri;
  535:                   $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  536:                   $env{'user.reinit'} = 1;
  537:                   return HTTP_NOT_ACCEPTABLE;
  538:               } else {
  539:                   if ($last) {
  540:                       my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  541:                       unless (&Apache::lonnet::symbverify($last,$fn)) {
  542:                           undef($last);
  543:                       }
  544:                   }
  545:               }
  546:           }
  547:           if (($reinitcheck eq 'both') || ($reinitcheck eq 'supp')) {
  548:               my $possdel;
  549:               if ($reinitcheck eq 'supp') {
  550:                   $possdel = 1;
  551:               }
  552:               my ($supplemental,$refs_updated) = &Apache::loncommon::get_supplemental($cnum,$cdom,'',$possdel);
  553:               unless ($refs_updated) {
  554:                   &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental);
  555:               }
  556:           }
  557:       }
  558:       if ($direction eq 'firstres') {
  559: 	  my $furl=&first_accessible_resource();
  560:           my $usehttp = &check_http_req(\$furl,$hostname);
  561:           if (($usehttp) && ($hostname ne '')) {
  562:               $furl='http://'.$hostname.$furl;
  563:           } else {
  564:               $furl=&Apache::lonnet::absolute_url().$furl;
  565:           }
  566:           if ($reinitcheck eq 'update') {
  567:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  568:               $r->print(&reinited_js($furl,$env{'request.course.id'},100));
  569:               $r->print(&Apache::loncommon::end_page());
  570:               return OK;
  571:           } else {
  572: 	      &Apache::loncommon::content_type($r,'text/html');
  573: 	      $r->header_out(Location => $furl);
  574: 	      return REDIRECT;
  575:           }
  576:       }
  577:       if ($direction eq 'return') {
  578: # -------------------------------------------------------- Return to last known
  579:          my ($newloc,$usehttp);
  580:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  581:                         &GDBM_READER(),0640))) {
  582:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  583: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  584: 	    $newloc=$hash{'src_'.$id};
  585: 	    if ($newloc) {
  586:                 $usehttp = &check_http_req(\$newloc,$hostname);
  587: 		if ($hash{'encrypted_'.$id}) { 
  588:                     $newloc=&Apache::lonenc::encrypted($newloc);
  589:                 }
  590:                 if ($newloc =~ m{^(/adm/wrapper/ext/[^\#]+)(?:|(\#[^\#]+))$}) {
  591:                     my ($url,$anchor) = ($1,$2);
  592:                     if ($anchor) {
  593:                         $newloc = $url.(($url=~/\?/)?'&':'?').'symb='.&escape($last).$anchor;
  594:                     }
  595:                 }
  596: 	    } else {
  597: 		$newloc='/adm/navmaps';
  598: 	    }
  599:             untie %hash;
  600:          } else {
  601: 	    $newloc='/adm/navmaps';
  602:          }
  603:          if (($usehttp) && ($hostname ne '')) {
  604:              $newloc='http://'.$hostname.$newloc;
  605:          } else {
  606:              $newloc=&Apache::lonnet::absolute_url().$newloc
  607:          }
  608:          if ($reinitcheck eq 'update') {
  609:              $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  610:              $r->print(&Apache::loncommon::end_page());
  611:              return OK;
  612:          } else {
  613: 	     &Apache::loncommon::content_type($r,'text/html');
  614: 	     $r->header_out(Location => $newloc);
  615: 	     return REDIRECT;
  616:          }
  617:       }
  618: #
  619: # Is the current URL on the map? If not, start with last known URL
  620: #
  621: 
  622:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  623:          if ($preupdatepos) {
  624:              undef($preupdatepos);
  625:          } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  626:                             &GDBM_READER(),0640)) {
  627:              $last=$hash{'last_known'};
  628:              untie(%hash);
  629:          }
  630:          if ($last) {
  631: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
  632: 	 } else {
  633:              my $newloc = &Apache::lonnet::absolute_url().
  634:                           '/adm/navmaps'; 
  635:              if ($reinitcheck eq 'update') {
  636:                  &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  637:                  $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  638:                  $r->print(&Apache::loncommon::end_page());
  639:                  return OK;
  640:              } else {
  641: 	         &Apache::loncommon::content_type($r,'text/html');
  642: 	         $r->header_out(Location => $newloc); 
  643: 	         return REDIRECT;
  644:              }
  645:          }
  646:       }
  647: # ------------------------------------------- Do we have any idea where we are?
  648:       my $position;
  649:       if ($preupdatepos) {
  650:           $position = $preupdatepos;
  651:       } else {
  652:           $position=Apache::lonnet::symbread($currenturl);
  653:       }
  654:       if ($position) {
  655: # ------------------------------------------------------------------------- Yes
  656: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  657:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
  658:           $cachehash{$startoutmap}{'last_known'}=
  659:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
  660: 
  661: # ============================================================ Tie the big hash
  662:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  663:                         &GDBM_READER(),0640)) {
  664:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  665:                       '.'.$mapnum;
  666: 
  667: # ------------------------------------------------- Move forward, backward, etc
  668:               my $endupmap;
  669:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  670: # -------------------------------------- Do we have one and only one empty URL?
  671: # We are now at at least one non-empty URL
  672: # ----------------------------------------------------- Check out possibilities
  673:               if ($next) {
  674:                   @possibilities=split(/\,/,$next);
  675:                   if ($#possibilities==0) {
  676: # ---------------------------------------------- Only one possibility, redirect
  677: 	              ($redirecturl,$redirectsymb,$enc,$anchor)=&hash_src($next);
  678:                       $cachehash{$endupmap}{$redirecturl}=
  679: 			  [$redirecturl,(split(/\./,$next))[1]];
  680:                   } else {
  681: # ------------------------ There are multiple possibilities for a next resource
  682:                       $multichoice=1;
  683: 		      foreach my $id (@possibilities) {
  684: 			  $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
  685:                           $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
  686:                           $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
  687:                           (my $first, my $second) = $id =~ /(\d+).(\d+)/;
  688:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
  689:                           $multichoicehash{'symb_'.$id} = 
  690:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  691:                                                         $second.'___'.$symbSrc);
  692:                                                          
  693:                           my ($choicemap,$choiceres)=split(/\./,$id);
  694: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
  695: 			  my $url=$multichoicehash{'src_'.$id};
  696:                           $cachehash{$map}{$url}=[$url,$choiceres];
  697:                       }
  698:                   }
  699: 	      } else {
  700: # -------------------------------------------------------------- No place to go
  701:                   $multichoice=-1;
  702:                   if ($position && $env{'request.deeplink.login'}) {
  703:                       my ($map,$resid,$url) = &Apache::lonnet::decode_symb($position);
  704:                       my $mapid = $hash{'map_pc_'.&Apache::lonnet::clutter($map)};
  705:                       my $position_deeplink = $hash{'deeplinkonly_'.$mapid.'.'.$resid};
  706:                       if ($position_deeplink) {
  707:                           (my $value,$deeplinklevel) = map { &unescape($_); } 
  708:                                                        split(/:/,$position_deeplink);
  709:                       }
  710:                   }
  711:               }
  712: # ----------------- The program must come past this point to untie the big hash
  713: 	      untie(%hash);
  714: # --------------------------------------------------------- Store position info
  715:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
  716:               foreach my $thismap (keys(%cachehash)) {
  717: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
  718: 		  delete($cachehash{$thismap}->{'mapnum'});
  719: 		  &Apache::lonnet::symblist($thismap,
  720: 					    %{$cachehash{$thismap}});
  721: 	      }
  722: # ============================================== Do not return before this line
  723:               if ($redirecturl) {
  724: # ----------------------------------------------------- There is a URL to go to
  725: 		  if ($direction eq 'forward') {
  726:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  727: 		  }
  728: 		  if ($direction eq 'back') {
  729:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  730: 		  }
  731: # ------------------------------------- Check for and display critical messages
  732:                   my ($redirect, $url) = &Apache::loncommon::critical_redirect(300,'flip');
  733:                   unless ($redirect) {
  734:                       my $usehttp = &check_http_req(\$redirecturl,$hostname);
  735:                       if (($usehttp) && ($hostname ne '')) {
  736:                           $url='http://'.$hostname.$redirecturl;
  737:                       } else {
  738:                           $url=&Apache::lonnet::absolute_url().$redirecturl;
  739:                       }
  740:                       my $addanchor;
  741:                       if (($anchor ne '') && (!$enc || $env{'request.role.adv'})) {
  742:                           $addanchor = 1;
  743:                           $url =~ s/\#.+$//;
  744:                       }
  745:                       $url = &add_get_param($url, { 'symb' => $redirectsymb});
  746:                       if ($addanchor) {
  747:                           $url .= $anchor;
  748:                       }
  749:                   }
  750:                   if ($reinitcheck eq 'update') {
  751:                       &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  752:                       $r->print(&reinited_js($url,$env{'request.course.id'},100)); 
  753:                       $r->print(&Apache::loncommon::end_page());
  754:                       return OK;
  755:                   } else {
  756:                       &Apache::loncommon::content_type($r,'text/html');
  757:                       $r->header_out(Location => $url);
  758:                       return REDIRECT;
  759:                   }
  760: 	      } else {
  761: # --------------------------------------------------------- There was a problem
  762:                   &Apache::loncommon::content_type($r,'text/html');
  763:                   $r->send_http_header;
  764: 		  my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
  765:                                                      'deeplink' => 'No link available',
  766:                                                      'deeplinkres' =>
  767:                                                      'Navigation to other content is unavailable when accessing content via deep-linking',
  768:                                                      'deeplinkmap' =>
  769:                                                      'You have reached the end of the sequence of available materials for access via deep-linking',
  770: 						     'explain' =>
  771: 						     'You have reached the end of the sequence of materials.',
  772: 						     'back' => 'Go Back',
  773: 						     'nav' => 'Course Contents',
  774: 						     'wherenext' =>
  775: 						     'There are several possibilities of where to go next',
  776: 						     'pick' =>
  777: 						     'Please click on the the resource you intend to access',
  778: 						     'titleheader' => 'Title',
  779: 						     'type' => 'Type',
  780:                                                      'update' => 'Content updated',
  781:                                                      'expupdate' => 'As a result of a recent update to the sequence of materials, it is not possible to complete the page flip.',
  782:                                                      'gonav' => 'Go to the Contents page to select a resource to display.',
  783:                                                      );
  784:                   if (&Apache::loncommon::course_type() eq 'Community') {
  785:                       $lt{'nav'} = &mt('Community Contents');
  786:                   }
  787:                   if ($#possibilities>0) {
  788: 		      my $start_page=
  789: 			  &Apache::loncommon::start_page('Multiple Resources');
  790:                      $r->print(<<ENDSTART);
  791: $start_page
  792: <h3>$lt{'wherenext'}</h3>
  793: <p>
  794: $lt{'pick'}:
  795: <p>
  796: <table border="2">
  797: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
  798: ENDSTART
  799:                      foreach my $id (@possibilities) {
  800:                         my $src = $multichoicehash{'src_'.$id};
  801:                         my $usehttp = &check_http_req(\$src,$hostname);
  802:                         if (($usehttp) && ($hostname ne '')) {
  803:                             $src = 'http://'.$hostname.$src;
  804:                         }
  805:                         $r->print(
  806:                               '<tr><td><a href="'.
  807: 				  &add_get_param($src,
  808: 						 {'symb' =>
  809: 						      $multichoicehash{'symb_'.$id},
  810: 						  }).'">'.
  811:                               $multichoicehash{'title_'.$id}.
  812:                               '</a></td><td>'.$multichoicehash{'type_'.$id}.
  813: 			      '</td></tr>');
  814:                      }
  815:                      $r->print('</table>');
  816:                   } else {
  817:                       if ($reinitcheck) {
  818:                           if (&Apache::loncommon::course_type() eq 'Community') {
  819:                               $r->print(
  820:                                   &Apache::loncommon::start_page('Community Contents Updated'));
  821:                           } else { 
  822:                               $r->print(
  823:                                   &Apache::loncommon::start_page('Course Contents Updated'));
  824:                           }
  825:                           $r->print('<h2>'.$lt{'update'}.'</h2>'
  826:                                   .'<p>'.$lt{'expupdate'}.'<br />'
  827:                                   .$lt{'gonav'}.'</p>');
  828:                       } else {
  829:                           if (($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') && 
  830:                               (!$env{'request.role.adv'})) {
  831:                               my ($score,$incomplete) = &Apache::lonplacementtest::check_completion(undef,undef,1); 
  832:                               if ($incomplete) {
  833:                                   $r->print(&Apache::lonplacementtest::showincomplete($incomplete)); 
  834:                               } else {
  835:                                   $r->print(&Apache::lonplacementtest::showresult(1));
  836:                               }
  837:                           } else {
  838:                               $r->print(
  839:                                   &Apache::loncommon::start_page('No Resource'));
  840:                               if ($deeplinklevel eq 'resource') {
  841:                                   $r->print('<h2>'.$lt{'deeplink'}.'</h2>'
  842:                                            .'<p>'.$lt{'deeplinkres'}.'</p>');
  843:                               } elsif ($deeplinklevel eq 'map') {
  844:                                   $r->print('<h2>'.$lt{'title'}.'</h2>'
  845:                                            .'<p>'.$lt{'deeplinkmap'}.'</p>');
  846:                               } else {
  847:                                   $r->print('<h2>'.$lt{'title'}.'</h2>'
  848:                                            .'<p>'.$lt{'explain'}.'</p>');
  849:                               }
  850:                           }
  851:                       }
  852: 		  }
  853:                   unless (($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') &&
  854:                           (!$env{'request.role.adv'})) {
  855:                       if ($deeplinklevel) {
  856:                           $r->print(
  857:                               &Apache::lonhtmlcommon::actionbox(
  858:                                   ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a>']));
  859:                       } elsif ((!@possibilities) && ($reinitcheck))  {
  860:                           $r->print(
  861:                               &Apache::lonhtmlcommon::actionbox(
  862:                                   ['<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  863:                                   ]));
  864:                       } else {
  865:                           $r->print(
  866:                               &Apache::lonhtmlcommon::actionbox(
  867:                                   ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a></li>',
  868:                                    '<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  869:                                   ]));
  870:                       }
  871: 
  872:                   }
  873:                   $r->print(&Apache::loncommon::end_page());
  874:       
  875:                   return OK;
  876: 	      }
  877: 	  } else {
  878: # ------------------------------------------------- Problem, could not tie hash
  879:               if ($reinitcheck eq 'update') {
  880:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  881:                   $r->print(&Apache::loncommon::end_page());
  882:               } 
  883:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  884:               return HTTP_NOT_ACCEPTABLE; 
  885:           }
  886:       } else {
  887: # ---------------------------------------- No, could not determine where we are
  888:           my $newloc = '/adm/ambiguous';
  889:           if ($reinitcheck eq 'update') {
  890:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  891:               $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  892:               $r->print(&Apache::loncommon::end_page());
  893:           } else {
  894: 	      $r->internal_redirect($newloc);
  895:           }
  896:           return OK;
  897:       }
  898:   } else {
  899: # -------------------------- Class was not initialized or page fliped strangely
  900:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  901:       return HTTP_NOT_ACCEPTABLE; 
  902:   } 
  903: }
  904: 
  905: 1;
  906: __END__
  907: 
  908: =pod
  909: 
  910: =head1 NAME
  911: 
  912: Apache::lonpageflip
  913: 
  914: =head1 SYNOPSIS
  915: 
  916: Deals with forward, backward, and other page flips.
  917: 
  918: This is part of the LearningOnline Network with CAPA project
  919: described at http://www.lon-capa.org.
  920: 
  921: =head1 OVERVIEW
  922: 
  923: (empty)
  924: 
  925: =head1 SUBROUTINES
  926: 
  927: =over cleanup()
  928: 
  929: =item addrid()
  930: 
  931: =item fullmove()
  932: 
  933: =item hash_src()
  934: 
  935: =item move()
  936: 
  937: =item get_next_possible_move()
  938: 
  939: =item first_accessible_resource()
  940: 
  941: =item handler()
  942: 
  943: =back
  944: 
  945: =cut
  946: 
  947: 
  948: 
  949: 
  950: 
  951: 

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