File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.80.8.11.2.2: download - view: text, annotated - select for diffs
Thu Mar 4 13:56:51 2021 UTC (3 years, 1 month ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in rev. 1.102

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

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