File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.105: download - view: text, annotated - select for diffs
Mon Jul 19 14:26:40 2021 UTC (2 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support case where first URL in course is an external resource and
  encrypturl in use.
- Use symb in first_url from lonuserstate::readmap() for other resources
  besides external resource.
- Retrieve symb in first_url in lonpageflip::first_accessible_resource()
  when encrypturl in use.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.105 2021/07/19 14:26:40 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: 
   31: 
   32: package Apache::lonpageflip;
   33: 
   34: use strict;
   35: use LONCAPA;
   36: use Apache::Constants qw(:common :http REDIRECT);
   37: use Apache::lonnet;
   38: use Apache::loncommon();
   39: use Apache::lonnavmaps();
   40: use Apache::lonuserstate;
   41: use Apache::lonlocal;
   42: use HTML::TokeParser;
   43: use GDBM_File;
   44: 
   45: # ========================================================== Module Global Hash
   46:   
   47: my %hash;
   48: 
   49: sub cleanup {
   50:     if (tied(%hash)){
   51: 	&Apache::lonnet::logthis('Cleanup pageflip: hash');
   52:         unless (untie(%hash)) {
   53: 	    &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
   54:         }
   55:     }
   56:     return OK;
   57: }
   58: 
   59: sub addrid {
   60:     my ($current,$new,$condid)=@_;
   61:     unless ($condid) { $condid=0; }
   62: 
   63: 	if ($current) {
   64: 	    $current.=','.$new;
   65:         } else {
   66:             $current=''.$new;
   67:         }
   68: 
   69:     return $current;
   70: }
   71: 
   72: sub fullmove {
   73:     my ($rid,$mapurl,$direction)=@_;
   74:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
   75:                         &GDBM_READER(),0640)) {
   76: 	($rid,$mapurl)=&move($rid,$mapurl,$direction);
   77:         untie(%hash);
   78:     }
   79:     return($rid,$mapurl);
   80: }
   81: 
   82: sub hash_src {
   83:     my ($id)=@_;
   84:     my ($mapid,$resid)=split(/\./,$id);
   85:     my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
   86: 					  $resid,$hash{'src_'.$id});
   87:     my $anchor;
   88:     if ($hash{'ext_'.$id} eq 'true:') {
   89:         if ($hash{'src_'.$id} =~ /(\#.+)$/) {
   90:             $anchor = $1;
   91:         }
   92:     }
   93:     if ($hash{'encrypted_'.$id}) {
   94: 	return (&Apache::lonenc::encrypted($hash{'src_'.$id}),
   95: 		&Apache::lonenc::encrypted($symb),
   96:                 $hash{'encrypted_'.$id},$anchor);
   97:     }
   98:     return ($hash{'src_'.$id},$symb,$hash{'encrypted_'.$id},$anchor);
   99: }
  100: 
  101: sub move {
  102:     my ($next,$endupmap,$direction,$firstres) = @_;
  103:     my $safecount=0;
  104:     my $allowed=0;
  105:     my $deeplinkonly=0;
  106:     my $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') || ($priv eq 'A'));
  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}) && (!$firstres)) {
  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,$url,$argstr);
  260: 	if ($furl =~ m{^/enc/}) {
  261: 	    ($url,$argstr) = split(/\?/,&Apache::lonenc::unencrypted($furl));
  262: 	} else {
  263: 	    ($url,$argstr) = split(/\?/,$furl);
  264: 	}
  265: 	foreach my $pair (split(/\&/,$argstr)) {
  266: 	    my ($name,$value) = split(/=/,$pair);
  267: 	    $args{&unescape($name)} = &unescape($value);
  268: 	}
  269:         my $priv = &Apache::lonnet::allowed('bre',$url,$args{'symb'});
  270:         my $allowed = (($priv eq 'F') || ($priv eq '2') || ($priv eq 'A'));
  271:         if (!$allowed) {
  272: # Wow, we cannot see this ... move forward to the next one that we can see
  273: 	    my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward',1);
  274: # Build the new URL
  275:             if ($newrid eq '') {
  276:                 $furl = '/adm/navmaps';
  277:             } else {
  278: 	        my ($newmapid,$newresid)=split(/\./,$newrid);
  279: 	        my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
  280: 	        $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
  281: 	        if ($hash{'encrypted_'.$newrid}) {
  282: 		    $furl=&Apache::lonenc::encrypted($furl);
  283: 	        }
  284: 	    }
  285:         }
  286: 	untie(%hash);
  287: 	return $furl;
  288:     } else {
  289: 	return '/adm/navmaps';
  290:     }
  291: }
  292: 
  293: sub first_answerable_ressymb {
  294:     my $navmap = Apache::lonnavmaps::navmap->new;
  295:     return unless (ref($navmap));
  296:     my $iterator = $navmap->getIterator(undef,undef,undef,1);
  297:     return unless (ref($iterator));
  298:     my ($curRes,$result);
  299:     while ($curRes = $iterator->next()) {
  300:         if (ref($curRes) && $curRes->is_problem()) {
  301:             foreach my $part (@{$curRes->parts()}) {
  302:                 if ($curRes->tries($part) < $curRes->maxtries($part)) {
  303:                     $result = $curRes->link().'?symb='.$curRes->shown_symb();
  304:                     last;
  305:                 }    
  306:             }
  307:         }
  308:     }
  309:     if ($result) {
  310:         return $result; 
  311:     } else {
  312:         return &first_accessible_resource(); 
  313:     }
  314: }
  315: 
  316: sub check_http_req {
  317:     my ($srcref,$hostname) = @_;
  318:     return unless (ref($srcref) eq 'SCALAR');
  319:     my $usehttp;
  320:     if ($env{'request.course.id'}) {
  321:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  322:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  323:         if (($$srcref =~ m{^\Q/public/$cdom/$cnum/syllabus\E($|\?)}) &&
  324:             ($ENV{'SERVER_PORT'} == 443) &&
  325:             ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
  326:             unless ((&Apache::lonnet::uses_sts()) ||
  327:                     (&Apache::lonnet::waf_allssl($hostname))) {
  328:                 $$srcref .= (($$srcref =~/\?/)? '&':'?') . 'usehttp=1';
  329:                 $usehttp = 1;
  330:             }
  331:         } elsif (($$srcref =~ m{^\Q/adm/wrapper/ext/\E(?!https:)}) &&
  332:                  ($ENV{'SERVER_PORT'} == 443)) {
  333:             unless ((&Apache::lonnet::uses_sts()) ||
  334:                     (&Apache::lonnet::waf_allssl($hostname))) {
  335:                 my ($url,$anchor) = ($$srcref =~ /^([^\#]+)(?:|(\#[^\#]+))$/);
  336:                 $$srcref = $url . (($$srcref =~/\?/)? '&':'?') . 'usehttp=1' .$anchor; 
  337:                 $usehttp = 1;
  338:             }
  339:         }
  340:     }
  341:     return $usehttp;
  342: }
  343: 
  344: sub reinited_js {
  345:     my ($url,$cid,$timeout) = @_;
  346:     if (!$timeout) {
  347:         $timeout = 0;
  348:     }
  349:     return <<"END";
  350: <script type="text/javascript">
  351: // <![CDATA[
  352: setTimeout(function() {
  353:     var newurl = '$url';
  354:     if (document.getElementById('LC_update_$cid')) {
  355:         document.getElementById('LC_update_$cid').style.display = 'none';
  356:     }
  357:     if ((newurl !== null) && (newurl !== '') && (newurl !== 'undefined')) {
  358:         window.location.href = "$url";
  359:     }
  360: }, $timeout);
  361: // ]]>
  362: </script>
  363: END
  364: }
  365: 
  366: # ================================================================ Main Handler
  367: 
  368: sub handler {
  369:    my $r=shift;
  370: 
  371: # ------------------------------------------- Set document type for header only
  372: 
  373:   if ($r->header_only) {
  374:       &Apache::loncommon::content_type($r,'text/html');
  375:       $r->send_http_header;
  376:       return OK;
  377:   }
  378: 
  379:   my %cachehash=(); 
  380:   my $multichoice=0;
  381:   my %multichoicehash=();
  382:   my %prog_state=();
  383:   my ($redirecturl,$redirectsymb,$enc,$anchor,$deeplinklevel);
  384:   my $next='';
  385:   my $hostname = $r->hostname();
  386:   my @possibilities=();
  387:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  388:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
  389:       my ($direction,$currenturl) = ($env{'form.postdata'}=~/(\w+)\:(.*)/);
  390:       if ($currenturl=~m|^/enc/|) {
  391:           $currenturl=&Apache::lonenc::unencrypted($currenturl);
  392:       }
  393:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  394:       $currenturl=~s/^https?\:\/\///;
  395:       $currenturl=~s/^[^\/]+//;
  396:       my ($preupdatepos,$last,$reinitcheck);
  397:       if ($direction eq 'return') {
  398:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  399:                     &GDBM_READER(),0640)) {
  400:               $last=$hash{'last_known'};
  401:               untie(%hash);
  402:           }
  403:       } elsif ($direction eq 'firstanswerable') {
  404:           my $furl = &first_answerable_ressymb();
  405:           my $usehttp = &check_http_req(\$furl,$hostname);
  406:           if (($usehttp) && ($hostname ne '')) {
  407:               $furl='http://'.$hostname.$furl;
  408:           } else {
  409:               $furl=&Apache::lonnet::absolute_url().$furl;
  410:           }
  411:           &Apache::loncommon::content_type($r,'text/html');
  412:           $r->header_out(Location => $furl);
  413:           return REDIRECT;
  414:       } elsif ($direction eq 'endplacement') {
  415:           &Apache::loncommon::content_type($r,'text/html');
  416:           $r->send_http_header;
  417:           $r->print(&Apache::lonplacementtest::showresult());
  418:           return OK;
  419:       }
  420:       if ($env{'request.course.id'}) {
  421:           # Check if course needs to be re-initialized
  422:           my $loncaparev = $r->dir_config('lonVersion');
  423:           ($reinitcheck,my @reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  424:           if ($reinitcheck eq 'switch') {
  425:               &Apache::loncommon::content_type($r,'text/html');
  426:               $r->send_http_header;
  427:               $r->print(&Apache::loncommon::check_release_result(@reinit));
  428:               return OK;
  429:           } elsif ($reinitcheck eq 'update') {
  430:               my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  431:               my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  432:               $preupdatepos = &Apache::lonnet::symbread($currenturl);
  433:               unless ($direction eq 'return') {
  434:                   if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  435:                                 &GDBM_READER(),0640)) {
  436:                       $last=$hash{'last_known'};
  437:                       untie(%hash);
  438:                   }
  439:               }
  440:               &Apache::loncommon::content_type($r,'text/html');
  441:               $r->send_http_header;
  442:               $r->print(&Apache::loncommon::start_page('Content Changed'));
  443:               my $preamble = '<div id="LC_update_'.$env{'request.course.id'}.'" class="LC_info">'.
  444:                              '<br />'.
  445:                              &mt('Your course session is being updated because of recent changes by course personnel.').
  446:                              ' '.&mt('Please be patient').'.<br /></div>'.
  447:                              '<div style="padding:0;clear:both;margin:0;border:0"></div>';
  448:               %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
  449:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
  450:               my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
  451:               &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
  452:               if ($ferr) {
  453:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  454:                   my $requrl = $r->uri;
  455:                   $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  456:                   $env{'user.reinit'} = 1;
  457:                   return HTTP_NOT_ACCEPTABLE;
  458:               } else {
  459:                   if ($last) {
  460:                       my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  461:                       unless (&Apache::lonnet::symbverify($last,$fn)) {
  462:                           undef($last);
  463:                       }
  464:                   }
  465:               }
  466:           }
  467:       }
  468:       if ($direction eq 'firstres') {
  469: 	  my $furl=&first_accessible_resource();
  470:           my $usehttp = &check_http_req(\$furl,$hostname);
  471:           if (($usehttp) && ($hostname ne '')) {
  472:               $furl='http://'.$hostname.$furl;
  473:           } else {
  474:               $furl=&Apache::lonnet::absolute_url().$furl;
  475:           }
  476:           if ($reinitcheck eq 'update') {
  477:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  478:               $r->print(&reinited_js($furl,$env{'request.course.id'},100));
  479:               $r->print(&Apache::loncommon::end_page());
  480:               return OK;
  481:           } else {
  482: 	      &Apache::loncommon::content_type($r,'text/html');
  483: 	      $r->header_out(Location => $furl);
  484: 	      return REDIRECT;
  485:           }
  486:       }
  487:       if ($direction eq 'return') {
  488: # -------------------------------------------------------- Return to last known
  489:          my ($newloc,$usehttp);
  490:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  491:                         &GDBM_READER(),0640))) {
  492:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  493: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  494: 	    $newloc=$hash{'src_'.$id};
  495: 	    if ($newloc) {
  496:                 $usehttp = &check_http_req(\$newloc,$hostname);
  497: 		if ($hash{'encrypted_'.$id}) { 
  498:                     $newloc=&Apache::lonenc::encrypted($newloc);
  499:                 }
  500:                 if ($newloc =~ m{^(/adm/wrapper/ext/[^\#]+)(?:|(\#[^\#]+))$}) {
  501:                     my ($url,$anchor) = ($1,$2);
  502:                     if ($anchor) {
  503:                         $newloc = $url.(($url=~/\?/)?'&':'?').'symb='.&escape($last).$anchor;
  504:                     }
  505:                 }
  506: 	    } else {
  507: 		$newloc='/adm/navmaps';
  508: 	    }
  509:             untie %hash;
  510:          } else {
  511: 	    $newloc='/adm/navmaps';
  512:          }
  513:          if (($usehttp) && ($hostname ne '')) {
  514:              $newloc='http://'.$hostname.$newloc;
  515:          } else {
  516:              $newloc=&Apache::lonnet::absolute_url().$newloc
  517:          }
  518:          if ($reinitcheck eq 'update') {
  519:              $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  520:              $r->print(&Apache::loncommon::end_page());
  521:              return OK;
  522:          } else {
  523: 	     &Apache::loncommon::content_type($r,'text/html');
  524: 	     $r->header_out(Location => $newloc);
  525: 	     return REDIRECT;
  526:          }
  527:       }
  528: #
  529: # Is the current URL on the map? If not, start with last known URL
  530: #
  531: 
  532:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  533:          if ($preupdatepos) {
  534:              undef($preupdatepos);
  535:          } elsif (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  536:                             &GDBM_READER(),0640)) {
  537:              $last=$hash{'last_known'};
  538:              untie(%hash);
  539:          }
  540:          if ($last) {
  541: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
  542: 	 } else {
  543:              my $newloc = &Apache::lonnet::absolute_url().
  544:                           '/adm/navmaps'; 
  545:              if ($reinitcheck eq 'update') {
  546:                  &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  547:                  $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  548:                  $r->print(&Apache::loncommon::end_page());
  549:                  return OK;
  550:              } else {
  551: 	         &Apache::loncommon::content_type($r,'text/html');
  552: 	         $r->header_out(Location => $newloc); 
  553: 	         return REDIRECT;
  554:              }
  555:          }
  556:       }
  557: # ------------------------------------------- Do we have any idea where we are?
  558:       my $position;
  559:       if ($preupdatepos) {
  560:           $position = $preupdatepos;
  561:       } else {
  562:           $position=Apache::lonnet::symbread($currenturl);
  563:       }
  564:       if ($position) {
  565: # ------------------------------------------------------------------------- Yes
  566: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  567:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
  568:           $cachehash{$startoutmap}{'last_known'}=
  569:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
  570: 
  571: # ============================================================ Tie the big hash
  572:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  573:                         &GDBM_READER(),0640)) {
  574:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  575:                       '.'.$mapnum;
  576: 
  577: # ------------------------------------------------- Move forward, backward, etc
  578:               my $endupmap;
  579:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  580: # -------------------------------------- Do we have one and only one empty URL?
  581: # We are now at at least one non-empty URL
  582: # ----------------------------------------------------- Check out possibilities
  583:               if ($next) {
  584:                   @possibilities=split(/\,/,$next);
  585:                   if ($#possibilities==0) {
  586: # ---------------------------------------------- Only one possibility, redirect
  587: 	              ($redirecturl,$redirectsymb,$enc,$anchor)=&hash_src($next);
  588:                       $cachehash{$endupmap}{$redirecturl}=
  589: 			  [$redirecturl,(split(/\./,$next))[1]];
  590:                   } else {
  591: # ------------------------ There are multiple possibilities for a next resource
  592:                       $multichoice=1;
  593: 		      foreach my $id (@possibilities) {
  594: 			  $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
  595:                           $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
  596:                           $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
  597:                           (my $first, my $second) = $id =~ /(\d+).(\d+)/;
  598:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
  599:                           $multichoicehash{'symb_'.$id} = 
  600:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  601:                                                         $second.'___'.$symbSrc);
  602:                                                          
  603:                           my ($choicemap,$choiceres)=split(/\./,$id);
  604: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
  605: 			  my $url=$multichoicehash{'src_'.$id};
  606:                           $cachehash{$map}{$url}=[$url,$choiceres];
  607:                       }
  608:                   }
  609: 	      } else {
  610: # -------------------------------------------------------------- No place to go
  611:                   $multichoice=-1;
  612:                   if ($hash{'deeplinkonly_'.$rid}) {
  613:                       (my $value,$deeplinklevel) = split(/:/,$hash{'deeplinkonly_'.$rid});
  614:                   }
  615:               }
  616: # ----------------- The program must come past this point to untie the big hash
  617: 	      untie(%hash);
  618: # --------------------------------------------------------- Store position info
  619:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
  620:               foreach my $thismap (keys(%cachehash)) {
  621: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
  622: 		  delete($cachehash{$thismap}->{'mapnum'});
  623: 		  &Apache::lonnet::symblist($thismap,
  624: 					    %{$cachehash{$thismap}});
  625: 	      }
  626: # ============================================== Do not return before this line
  627:               if ($redirecturl) {
  628: # ----------------------------------------------------- There is a URL to go to
  629: 		  if ($direction eq 'forward') {
  630:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  631: 		  }
  632: 		  if ($direction eq 'back') {
  633:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  634: 		  }
  635: # ------------------------------------- Check for and display critical messages
  636:                   my ($redirect, $url) = &Apache::loncommon::critical_redirect(300,'flip');
  637:                   unless ($redirect) {
  638:                       my $usehttp = &check_http_req(\$redirecturl,$hostname);
  639:                       if (($usehttp) && ($hostname ne '')) {
  640:                           $url='http://'.$hostname.$redirecturl;
  641:                       } else {
  642:                           $url=&Apache::lonnet::absolute_url().$redirecturl;
  643:                       }
  644:                       my $addanchor;
  645:                       if (($anchor ne '') && (!$enc || $env{'request.role.adv'})) {
  646:                           $addanchor = 1;
  647:                           $url =~ s/\#.+$//;
  648:                       }
  649:                       $url = &add_get_param($url, { 'symb' => $redirectsymb});
  650:                       if ($addanchor) {
  651:                           $url .= $anchor;
  652:                       }
  653:                   }
  654:                   if ($reinitcheck eq 'update') {
  655:                       &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  656:                       $r->print(&reinited_js($url,$env{'request.course.id'},100)); 
  657:                       $r->print(&Apache::loncommon::end_page());
  658:                       return OK;
  659:                   } else {
  660:                       &Apache::loncommon::content_type($r,'text/html');
  661:                       $r->header_out(Location => $url);
  662:                       return REDIRECT;
  663:                   }
  664: 	      } else {
  665: # --------------------------------------------------------- There was a problem
  666:                   &Apache::loncommon::content_type($r,'text/html');
  667:                   $r->send_http_header;
  668: 		  my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
  669:                                                      'deeplink' => 'No link available',
  670:                                                      'deeplinkres' =>
  671:                                                      'Navigation to other content is unavailable when accessing content via deep-linking',
  672:                                                      'deeplinkmap' =>
  673:                                                      'You have reached the end of the sequence of available materials for access via deep-linking',
  674: 						     'explain' =>
  675: 						     'You have reached the end of the sequence of materials.',
  676: 						     'back' => 'Go Back',
  677: 						     'nav' => 'Course Contents',
  678: 						     'wherenext' =>
  679: 						     'There are several possibilities of where to go next',
  680: 						     'pick' =>
  681: 						     'Please click on the the resource you intend to access',
  682: 						     'titleheader' => 'Title',
  683: 						     'type' => 'Type',
  684:                                                      'update' => 'Content updated',
  685:                                                      'expupdate' => 'As a result of a recent update to the sequence of materials, it is not possible to complete the page flip.',
  686:                                                      'gonav' => 'Go to the Contents page to select a resource to display.',
  687:                                                      );
  688:                   if (&Apache::loncommon::course_type() eq 'Community') {
  689:                       $lt{'nav'} = &mt('Community Contents');
  690:                   }
  691:                   if ($#possibilities>0) {
  692: 		      my $start_page=
  693: 			  &Apache::loncommon::start_page('Multiple Resources');
  694:                      $r->print(<<ENDSTART);
  695: $start_page
  696: <h3>$lt{'wherenext'}</h3>
  697: <p>
  698: $lt{'pick'}:
  699: <p>
  700: <table border="2">
  701: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
  702: ENDSTART
  703:                      foreach my $id (@possibilities) {
  704:                         my $src = $multichoicehash{'src_'.$id};
  705:                         my $usehttp = &check_http_req(\$src,$hostname);
  706:                         if (($usehttp) && ($hostname ne '')) {
  707:                             $src = 'http://'.$hostname.$src;
  708:                         }
  709:                         $r->print(
  710:                               '<tr><td><a href="'.
  711: 				  &add_get_param($src,
  712: 						 {'symb' =>
  713: 						      $multichoicehash{'symb_'.$id},
  714: 						  }).'">'.
  715:                               $multichoicehash{'title_'.$id}.
  716:                               '</a></td><td>'.$multichoicehash{'type_'.$id}.
  717: 			      '</td></tr>');
  718:                      }
  719:                      $r->print('</table>');
  720:                   } else {
  721:                       if ($reinitcheck) {
  722:                           if (&Apache::loncommon::course_type() eq 'Community') {
  723:                               $r->print(
  724:                                   &Apache::loncommon::start_page('Community Contents Updated'));
  725:                           } else { 
  726:                               $r->print(
  727:                                   &Apache::loncommon::start_page('Course Contents Updated'));
  728:                           }
  729:                           $r->print('<h2>'.$lt{'update'}.'</h2>'
  730:                                   .'<p>'.$lt{'expupdate'}.'<br />'
  731:                                   .$lt{'gonav'}.'</p>');
  732:                       } else {
  733:                           if (($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') && 
  734:                               (!$env{'request.role.adv'})) {
  735:                               my ($score,$incomplete) = &Apache::lonplacementtest::check_completion(undef,undef,1); 
  736:                               if ($incomplete) {
  737:                                   $r->print(&Apache::lonplacementtest::showincomplete($incomplete)); 
  738:                               } else {
  739:                                   $r->print(&Apache::lonplacementtest::showresult(1));
  740:                               }
  741:                           } else {
  742:                               $r->print(
  743:                                   &Apache::loncommon::start_page('No Resource'));
  744:                               if ($deeplinklevel eq 'resource') {
  745:                                   $r->print('<h2>'.$lt{'deeplink'}.'</h2>'
  746:                                            .'<p>'.$lt{'deeplinkres'}.'</p>');
  747:                               } elsif ($deeplinklevel eq 'map') {
  748:                                   $r->print('<h2>'.$lt{'title'}.'</h2>'
  749:                                            .'<p>'.$lt{'deeplinkmap'}.'</p>');
  750:                               } else {
  751:                                   $r->print('<h2>'.$lt{'title'}.'</h2>'
  752:                                            .'<p>'.$lt{'explain'}.'</p>');
  753:                               }
  754:                           }
  755:                       }
  756: 		  }
  757:                   unless (($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') ||
  758:                           ($env{'request.role.adv'})) {
  759:                       if ($deeplinklevel) {
  760:                           $r->print(
  761:                               &Apache::lonhtmlcommon::actionbox(
  762:                                   ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a>']));
  763:                       } elsif ((!@possibilities) && ($reinitcheck))  {
  764:                           $r->print(
  765:                               &Apache::lonhtmlcommon::actionbox(
  766:                                   ['<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  767:                                   ]));
  768:                       } else {
  769:                           $r->print(
  770:                               &Apache::lonhtmlcommon::actionbox(
  771:                                   ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a></li>',
  772:                                    '<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  773:                                   ]));
  774:                       }
  775: 
  776:                   }
  777:                   $r->print(&Apache::loncommon::end_page());
  778:       
  779:                   return OK;
  780: 	      }
  781: 	  } else {
  782: # ------------------------------------------------- Problem, could not tie hash
  783:               if ($reinitcheck eq 'update') {
  784:                   &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  785:                   $r->print(&Apache::loncommon::end_page());
  786:               } 
  787:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  788:               return HTTP_NOT_ACCEPTABLE; 
  789:           }
  790:       } else {
  791: # ---------------------------------------- No, could not determine where we are
  792:           my $newloc = '/adm/ambiguous';
  793:           if ($reinitcheck eq 'update') {
  794:               &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  795:               $r->print(&reinited_js($newloc,$env{'request.course.id'},100));
  796:               $r->print(&Apache::loncommon::end_page());
  797:           } else {
  798: 	      $r->internal_redirect($newloc);
  799:           }
  800:           return OK;
  801:       }
  802:   } else {
  803: # -------------------------- Class was not initialized or page fliped strangely
  804:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  805:       return HTTP_NOT_ACCEPTABLE; 
  806:   } 
  807: }
  808: 
  809: 1;
  810: __END__
  811: 
  812: =pod
  813: 
  814: =head1 NAME
  815: 
  816: Apache::lonpageflip
  817: 
  818: =head1 SYNOPSIS
  819: 
  820: Deals with forward, backward, and other page flips.
  821: 
  822: This is part of the LearningOnline Network with CAPA project
  823: described at http://www.lon-capa.org.
  824: 
  825: =head1 OVERVIEW
  826: 
  827: (empty)
  828: 
  829: =head1 SUBROUTINES
  830: 
  831: =over cleanup()
  832: 
  833: =item addrid()
  834: 
  835: =item fullmove()
  836: 
  837: =item hash_src()
  838: 
  839: =item move()
  840: 
  841: =item get_next_possible_move()
  842: 
  843: =item first_accessible_resource()
  844: 
  845: =item handler()
  846: 
  847: =back
  848: 
  849: =cut
  850: 
  851: 
  852: 
  853: 
  854: 
  855: 

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