File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.101: download - view: text, annotated - select for diffs
Sun Dec 30 19:47:06 2018 UTC (5 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6806 Use of an External Resource URL which includes an anchor will
  cause page to be loaded in iframe at anchor position.

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

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