File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.80.8.13: download - view: text, annotated - select for diffs
Tue Dec 14 21:34:45 2021 UTC (2 years, 4 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4
Diff to branchpoint 1.80: preferred, unified
- For 2.11
  Backport 1.103, 1.105

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.80.8.13 2021/12/14 21:34:45 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,$url,$argstr);
  235: 	if ($furl =~ m{^/enc/}) {
  236: 	    ($url,$argstr) = split(/\?/,&Apache::lonenc::unencrypted($furl));
  237: 	} else {
  238: 	    ($url,$argstr) = split(/\?/,$furl);
  239: 	}
  240: 	foreach my $pair (split(/\&/,$argstr)) {
  241: 	    my ($name,$value) = split(/=/,$pair);
  242: 	    $args{&unescape($name)} = &unescape($value);
  243: 	}
  244:         if (!&Apache::lonnet::allowed('bre',$url,$args{'symb'})) {
  245: # Wow, we cannot see this ... move forward to the next one that we can see
  246: 	    my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward');
  247: # Build the new URL
  248:             if ($newrid eq '') {
  249:                 $furl = '/adm/navmaps';
  250:             } else {
  251: 	        my ($newmapid,$newresid)=split(/\./,$newrid);
  252: 	        my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
  253: 	        $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
  254: 	        if ($hash{'encrypted_'.$newrid}) {
  255: 		    $furl=&Apache::lonenc::encrypted($furl);
  256: 	        }
  257: 	    }
  258:         }
  259: 	untie(%hash);
  260: 	return $furl;
  261:     } else {
  262: 	return '/adm/navmaps';
  263:     }
  264: }
  265: 
  266: sub check_http_req {
  267:     my ($srcref,$hostname) = @_;
  268:     return unless (ref($srcref) eq 'SCALAR');
  269:     my $usehttp;
  270:     if ($env{'request.course.id'}) {
  271:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  272:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  273:         if (($$srcref =~ m{^\Q/public/$cdom/$cnum/syllabus\E($|\?)}) &&
  274:             ($ENV{'SERVER_PORT'} == 443) &&
  275:             ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
  276:             unless ((&Apache::lonnet::uses_sts()) ||
  277:                     (&Apache::lonnet::waf_allssl($hostname))) {
  278:                 $$srcref .= (($$srcref =~/\?/)? '&':'?') . 'usehttp=1';
  279:                 $usehttp = 1;
  280:             }
  281:         } elsif (($$srcref =~ m{^\Q/adm/wrapper/ext/\E(?!https:)}) &&
  282:                  ($ENV{'SERVER_PORT'} == 443)) {
  283:             unless ((&Apache::lonnet::uses_sts()) ||
  284:                     (&Apache::lonnet::waf_allssl($hostname))) {
  285:                 my ($url,$anchor) = ($$srcref =~ /^([^\#]+)(?:|(\#[^\#]+))$/);
  286:                 $$srcref = $url . (($$srcref =~/\?/)? '&':'?') . 'usehttp=1' .$anchor;
  287:                 $usehttp = 1;
  288:             }
  289:         }
  290:     }
  291:     return $usehttp;
  292: }
  293: 
  294: sub reinited_js {
  295:     my ($url,$cid,$timeout) = @_;
  296:     if (!$timeout) {
  297:         $timeout = 0;
  298:     }
  299:     return <<"END";
  300: <script type="text/javascript">
  301: // <![CDATA[
  302: setTimeout(function() {
  303:     var newurl = '$url';
  304:     if (document.getElementById('LC_update_$cid')) {
  305:         document.getElementById('LC_update_$cid').style.display = 'none';
  306:     }
  307:     if ((newurl !== null) && (newurl !== '') && (newurl !== 'undefined')) {
  308:         window.location.href = "$url";
  309:     }
  310: }, $timeout);
  311: // ]]>
  312: </script>
  313: END
  314: }
  315: 
  316: # ================================================================ Main Handler
  317: 
  318: sub handler {
  319:    my $r=shift;
  320: 
  321: # ------------------------------------------- Set document type for header only
  322: 
  323:   if ($r->header_only) {
  324:       &Apache::loncommon::content_type($r,'text/html');
  325:       $r->send_http_header;
  326:       return OK;
  327:   }
  328: 
  329:   my %cachehash=(); 
  330:   my $multichoice=0;
  331:   my %multichoicehash=();
  332:   my %prog_state=(); 
  333:   my ($redirecturl,$redirectsymb,$enc,$anchor);
  334:   my $next='';
  335:   my $hostname = $r->hostname();
  336:   my @possibilities=();
  337:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  338:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
  339:       my ($direction,$currenturl) = ($env{'form.postdata'}=~/(\w+)\:(.*)/);
  340:       if ($currenturl=~m|^/enc/|) {
  341:           $currenturl=&Apache::lonenc::unencrypted($currenturl);
  342:       }
  343:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  344:       $currenturl=~s/^https?\:\/\///;
  345:       $currenturl=~s/^[^\/]+//;
  346:       my ($preupdatepos,$last,$reinitcheck);
  347:       if ($direction eq 'return') {
  348:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  349:                     &GDBM_READER(),0640)) {
  350:               $last=$hash{'last_known'};
  351:               untie(%hash);
  352:           }
  353:       }
  354:       if ($env{'request.course.id'}) {
  355:           # Check if course needs to be re-initialized
  356:           my $loncaparev = $r->dir_config('lonVersion');
  357:           ($reinitcheck,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  358:           if ($reinitcheck eq 'switch') {
  359:               &Apache::loncommon::content_type($r,'text/html');
  360:               $r->send_http_header;
  361:               $r->print(&Apache::loncommon::check_release_result(@reinit));
  362:               return OK;
  363:           } elsif ($reinitcheck eq 'update') {
  364:               my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  365:               my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  366:               $preupdatepos = &Apache::lonnet::symbread($currenturl);
  367:               unless ($direction eq 'return') {
  368:                   if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  369:                                 &GDBM_READER(),0640)) {
  370:                       $last=$hash{'last_known'};
  371:                       untie(%hash);
  372:                   }
  373:               }
  374:               &Apache::loncommon::content_type($r,'text/html');
  375:               $r->send_http_header;
  376:               $r->print(&Apache::loncommon::start_page('Content Changed'));
  377:               my $preamble = '<div id="LC_update_'.$env{'request.course.id'}.'" class="LC_info">'.
  378:                              '<br />'.
  379:                              &mt('Your course session is being updated because of recent changes by course personnel.').
  380:                              ' '.&mt('Please be patient').'.<br /></div>'.
  381:                              '<div style="padding:0;clear:both;margin:0;border:0"></div>';
  382:               %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
  383:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
  384:               my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
  385:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
  386:               if ($ferr) {
  387:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  388:                   my $requrl = $r->uri;
  389:                   $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  390:                   $env{'user.reinit'} = 1;
  391:                   return HTTP_NOT_ACCEPTABLE;
  392:               } else {
  393:                   if ($last) {
  394:                       my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  395:                       unless (&Apache::lonnet::symbverify($last,$fn)) {
  396:                           undef($last);
  397:                       }
  398:                   }
  399:               }
  400:           }
  401:       }
  402:       if ($direction eq 'firstres') {
  403: 	  my $furl=&first_accessible_resource();
  404:           my $usehttp = &check_http_req(\$furl,$hostname);
  405:           if (($usehttp) && ($hostname ne '')) {
  406:               $furl='http://'.$hostname.$furl;
  407:           } else {
  408:               $furl=&Apache::lonnet::absolute_url().$furl;
  409:           }
  410:           if ($reinitcheck eq 'update') {
  411:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  412:               $r->print(&reinited_js($furl,$env{'request.course.id'},100));
  413:               $r->print(&Apache::loncommon::end_page());
  414:               return OK;
  415:           } else {
  416: 	      &Apache::loncommon::content_type($r,'text/html');
  417: 	      $r->header_out(Location => $furl);
  418: 	      return REDIRECT;
  419:           }
  420:       }
  421:       if ($direction eq 'return') {
  422: # -------------------------------------------------------- Return to last known
  423:          my ($newloc,$usehttp);
  424:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  425:                         &GDBM_READER(),0640))) {
  426:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  427: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  428: 	    $newloc=$hash{'src_'.$id};
  429: 	    if ($newloc) {
  430:                 $usehttp = &check_http_req(\$newloc,$hostname);
  431:                 if ($hash{'encrypted_'.$id}) {
  432:                     $newloc=&Apache::lonenc::encrypted($newloc);
  433:                 }
  434:                 if ($newloc =~ m{^(/adm/wrapper/ext/[^\#]+)(?:|(\#[^\#]+))$}) {
  435:                     my ($url,$anchor) = ($1,$2);
  436:                     if ($anchor) {
  437:                         $newloc = $url.(($url=~/\?/)?'&':'?').'symb='.&escape($last).$anchor;
  438:                     }
  439:                 }
  440: 	    } else {
  441: 		$newloc='/adm/navmaps';
  442: 	    }
  443:             untie %hash;
  444:          } else {
  445: 	    $newloc='/adm/navmaps';
  446:          }
  447:          if (($usehttp) && ($hostname ne '')) {
  448:              $newloc='http://'.$hostname.$newloc;
  449:          } else {
  450:              $newloc=&Apache::lonnet::absolute_url().$newloc
  451:          }
  452:          if ($reinitcheck eq 'update') {
  453:              $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  454:              $r->print(&Apache::loncommon::end_page());
  455:              return OK;
  456:          } else {
  457:              &Apache::loncommon::content_type($r,'text/html');
  458:              $r->header_out(Location => $newloc);
  459:              return REDIRECT;
  460:          }
  461:       }
  462: #
  463: # Is the current URL on the map? If not, start with last known URL
  464: #
  465: 
  466:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  467:          if ($preupdatepos) {
  468:              undef($preupdatepos);
  469:          } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  470:                             &GDBM_READER(),0640)) {
  471:              $last=$hash{'last_known'};
  472:              untie(%hash);
  473:          }
  474:          if ($last) {
  475: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
  476: 	 } else {
  477:              my $newloc = &Apache::lonnet::absolute_url().
  478:                           '/adm/navmaps';
  479:              if ($reinitcheck eq 'update') {
  480:                  &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  481:                  $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  482:                  $r->print(&Apache::loncommon::end_page());
  483:                  return OK;
  484:              } else {
  485:                  &Apache::loncommon::content_type($r,'text/html');
  486: 	         $r->header_out(Location => $newloc);
  487: 	         return REDIRECT;
  488:              }
  489:          }
  490:       }
  491: # ------------------------------------------- Do we have any idea where we are?
  492:       my $position;
  493:       if ($preupdatepos) {
  494:           $position = $preupdatepos;
  495:       } else {
  496:           $position=Apache::lonnet::symbread($currenturl);
  497:       }
  498:       if ($position) {
  499: # ------------------------------------------------------------------------- Yes
  500: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  501:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
  502:           $cachehash{$startoutmap}{'last_known'}=
  503:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
  504: 
  505: # ============================================================ Tie the big hash
  506:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  507:                         &GDBM_READER(),0640)) {
  508:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  509:                       '.'.$mapnum;
  510: 
  511: # ------------------------------------------------- Move forward, backward, etc
  512:               my $endupmap;
  513:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  514: # -------------------------------------- Do we have one and only one empty URL?
  515: # We are now at at least one non-empty URL
  516: # ----------------------------------------------------- Check out possibilities
  517:               if ($next) {
  518:                   @possibilities=split(/\,/,$next);
  519:                   if ($#possibilities==0) {
  520: # ---------------------------------------------- Only one possibility, redirect
  521: 	              ($redirecturl,$redirectsymb,$enc,$anchor)=&hash_src($next);
  522:                       $cachehash{$endupmap}{$redirecturl}=
  523: 			  [$redirecturl,(split(/\./,$next))[1]];
  524:                   } else {
  525: # ------------------------ There are multiple possibilities for a next resource
  526:                       $multichoice=1;
  527: 		      foreach my $id (@possibilities) {
  528: 			  $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
  529:                           $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
  530:                           $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
  531:                           (my $first, my $second) = $id =~ /(\d+).(\d+)/;
  532:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
  533:                           $multichoicehash{'symb_'.$id} = 
  534:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  535:                                                         $second.'___'.$symbSrc);
  536:                                                          
  537:                           my ($choicemap,$choiceres)=split(/\./,$id);
  538: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
  539: 			  my $url=$multichoicehash{'src_'.$id};
  540:                           $cachehash{$map}{$url}=[$url,$choiceres];
  541:                       }
  542:                   }
  543: 	      } else {
  544: # -------------------------------------------------------------- No place to go
  545:                   $multichoice=-1;
  546:               }
  547: # ----------------- The program must come past this point to untie the big hash
  548: 	      untie(%hash);
  549: # --------------------------------------------------------- Store position info
  550:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
  551:               foreach my $thismap (keys(%cachehash)) {
  552: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
  553: 		  delete($cachehash{$thismap}->{'mapnum'});
  554: 		  &Apache::lonnet::symblist($thismap,
  555: 					    %{$cachehash{$thismap}});
  556: 	      }
  557: # ============================================== Do not return before this line
  558:               if ($redirecturl) {
  559: # ----------------------------------------------------- There is a URL to go to
  560: 		  if ($direction eq 'forward') {
  561:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  562: 		  }
  563: 		  if ($direction eq 'back') {
  564:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  565: 		  }
  566: # ------------------------------------- Check for and display critical messages
  567:                   my ($redirect, $url) = &Apache::loncommon::critical_redirect(300);
  568:                   unless ($redirect) {
  569:                       my $usehttp = &check_http_req(\$redirecturl,$hostname);
  570:                       if (($usehttp) && ($hostname ne '')) {
  571:                           $url='http://'.$hostname.$redirecturl;
  572:                       } else {
  573:                           $url=&Apache::lonnet::absolute_url().$redirecturl;
  574:                       }
  575:                       my $addanchor;
  576:                       if (($anchor ne '') && (!$enc || $env{'request.role.adv'})) {
  577:                           $addanchor = 1;
  578:                           $url =~ s/\#.+$//;
  579:                       }
  580:                       $url = &add_get_param($url, { 'symb' => $redirectsymb});
  581:                       if ($addanchor) {
  582:                           $url .= $anchor;
  583:                       }
  584:                   }
  585:                   if ($reinitcheck eq 'update') {
  586:                       &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  587:                       $r->print(&reinited_js($url,$env{'request.course.id'},100));
  588:                       $r->print(&Apache::loncommon::end_page());
  589:                       return OK;
  590:                   } else {
  591:                       &Apache::loncommon::content_type($r,'text/html');
  592:                       $r->header_out(Location => $url);
  593:                       return REDIRECT;
  594:                   }
  595: 	      } else {
  596: # --------------------------------------------------------- There was a problem
  597:                   &Apache::loncommon::content_type($r,'text/html');
  598:                   $r->send_http_header;
  599: 		  my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
  600: 						     'explain' =>
  601: 						     'You have reached the end of the sequence of materials.',
  602: 						     'back' => 'Go Back',
  603: 						     'nav' => 'Course Contents',
  604: 						     'wherenext' =>
  605: 						     'There are several possibilities of where to go next',
  606: 						     'pick' =>
  607: 						     'Please click on the the resource you intend to access',
  608: 						     'titleheader' => 'Title',
  609: 						     'type' => 'Type',
  610:                                                      'update' => 'Content updated',
  611:                                                      'expupdate' => 'As a result of a recent update to the sequence of materials, it is not possible to complete the page flip.',
  612:                                                      'gonav' => 'Go to the Contents page to select a resource to display.');
  613:                   if (&Apache::loncommon::course_type() eq 'Community') {
  614:                       $lt{'nav'} = &mt('Community Contents');
  615:                   }
  616:                   if ($#possibilities>0) {
  617: 		      my $start_page=
  618: 			  &Apache::loncommon::start_page('Multiple Resources');
  619:                      $r->print(<<ENDSTART);
  620: $start_page
  621: <h3>$lt{'wherenext'}</h3>
  622: <p>
  623: $lt{'pick'}:
  624: <p>
  625: <table border="2">
  626: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
  627: ENDSTART
  628:                      foreach my $id (@possibilities) {
  629:                         my $src = $multichoicehash{'src_'.$id};
  630:                         my $usehttp = &check_http_req(\$src,$hostname);
  631:                         if (($usehttp) && ($hostname ne '')) {
  632:                             $src = 'http://'.$hostname.$src;
  633:                         }
  634:                         $r->print(
  635:                               '<tr><td><a href="'.
  636: 				  &add_get_param($src,
  637: 						 {'symb' =>
  638: 						      $multichoicehash{'symb_'.$id},
  639: 						  }).'">'.
  640:                               $multichoicehash{'title_'.$id}.
  641:                               '</a></td><td>'.$multichoicehash{'type_'.$id}.
  642: 			      '</td></tr>');
  643:                      }
  644:                      $r->print('</table>');
  645:                   } else {
  646:                       if ($reinitcheck) {
  647:                           if (&Apache::loncommon::course_type() eq 'Community') {
  648:                               $r->print(
  649:                                   &Apache::loncommon::start_page('Community Contents Updated'));
  650:                           } else { 
  651:                               $r->print(
  652:                                   &Apache::loncommon::start_page('Course Contents Updated'));
  653:                           }
  654:                           $r->print('<h2>'.$lt{'update'}.'</h2>'
  655:                                   .'<p>'.$lt{'expupdate'}.'<br />'
  656:                                   .$lt{'gonav'}.'</p>');
  657:                       } else {
  658:                           $r->print(
  659:                               &Apache::loncommon::start_page('No Resource')
  660:                              .'<h2>'.$lt{'title'}.'</h2>'
  661:                              .'<p>'.$lt{'explain'}.'</p>');
  662:                       }
  663: 		  }
  664:                   if ((!@possibilities) && ($reinitcheck))  {
  665:                       $r->print(
  666:                           &Apache::lonhtmlcommon::actionbox(
  667:                               ['<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  668:                               ]));
  669:                   } else {
  670:                       $r->print(
  671:                           &Apache::lonhtmlcommon::actionbox(
  672:                               ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a></li>',
  673:                                '<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  674:                               ]));
  675:                   }
  676:                   $r->print(&Apache::loncommon::end_page());
  677:       
  678:                   return OK;
  679: 	      }
  680: 	  } else {
  681: # ------------------------------------------------- Problem, could not tie hash
  682:               if ($reinitcheck eq 'update') {
  683:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  684:                   $r->print(&Apache::loncommon::end_page());
  685:               }
  686:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  687:               return HTTP_NOT_ACCEPTABLE; 
  688:           }
  689:       } else {
  690: # ---------------------------------------- No, could not determine where we are
  691:           my $newloc = '/adm/ambiguous';
  692:           if ($reinitcheck eq 'update') {
  693:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  694:               $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  695:               $r->print(&Apache::loncommon::end_page());
  696:           } else {
  697: 	      $r->internal_redirect($newloc);
  698:           }
  699:           return OK;
  700:       }
  701:   } else {
  702: # -------------------------- Class was not initialized or page fliped strangely
  703:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  704:       return HTTP_NOT_ACCEPTABLE; 
  705:   } 
  706: }
  707: 
  708: 1;
  709: __END__
  710: 
  711: =pod
  712: 
  713: =head1 NAME
  714: 
  715: Apache::lonpageflip
  716: 
  717: =head1 SYNOPSIS
  718: 
  719: Deals with forward, backward, and other page flips.
  720: 
  721: This is part of the LearningOnline Network with CAPA project
  722: described at http://www.lon-capa.org.
  723: 
  724: =head1 OVERVIEW
  725: 
  726: (empty)
  727: 
  728: =head1 SUBROUTINES
  729: 
  730: =over cleanup()
  731: 
  732: =item addrid()
  733: 
  734: =item fullmove()
  735: 
  736: =item hash_src()
  737: 
  738: =item move()
  739: 
  740: =item get_next_possible_move()
  741: 
  742: =item first_accessible_resource()
  743: 
  744: =item handler()
  745: 
  746: =back
  747: 
  748: =cut
  749: 
  750: 
  751: 
  752: 
  753: 
  754: 

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