File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.104: download - view: text, annotated - select for diffs
Mon Jun 7 19:04:20 2021 UTC (2 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  If first resource in a course is deep-link only, and we are not accessing via
  deep-link, first accessible resource is one that is not deep-link only.

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

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