File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.107: download - view: text, annotated - select for diffs
Fri Aug 6 00:52:43 2021 UTC (2 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Fix typos in rev. 1.105

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

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