File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.97: download - view: text, annotated - select for diffs
Thu Nov 16 13:42:01 2017 UTC (6 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- If user's course session is stale, and needs updating when page flip,
  student's view of grades or course contents page were requested, show
  message and "indeterminate" progressbar until reinitialization complete.

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

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