File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.98: download - view: text, annotated - select for diffs
Tue Nov 13 03:59:17 2018 UTC (5 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6400
  - deeplink parameter determines whether deep-linked items are listed in
    and/or linked to in Course Contents and student's view of Grades

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

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