File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.103: download - view: text, annotated - select for diffs
Thu Apr 29 17:45:25 2021 UTC (3 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
  Update domain config UI to support settings documented in
  Domain_Configuration_WAF_Proxy.tex

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

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