File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.86: download - view: text, annotated - select for diffs
Thu Dec 11 13:16:49 2014 UTC (9 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Coding style: keys()

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.86 2014/12/11 13:16:49 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::lonlocal;
   40: use HTML::TokeParser;
   41: use GDBM_File;
   42: 
   43: # ========================================================== Module Global Hash
   44:   
   45: my %hash;
   46: 
   47: sub cleanup {
   48:     if (tied(%hash)){
   49: 	&Apache::lonnet::logthis('Cleanup pageflip: hash');
   50:         unless (untie(%hash)) {
   51: 	    &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
   52:         }
   53:     }
   54:     return OK;
   55: }
   56: 
   57: sub addrid {
   58:     my ($current,$new,$condid)=@_;
   59:     unless ($condid) { $condid=0; }
   60: 
   61: 	if ($current) {
   62: 	    $current.=','.$new;
   63:         } else {
   64:             $current=''.$new;
   65:         }
   66: 
   67:     return $current;
   68: }
   69: 
   70: sub fullmove {
   71:     my ($rid,$mapurl,$direction)=@_;
   72:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
   73:                         &GDBM_READER(),0640)) {
   74: 	($rid,$mapurl)=&move($rid,$mapurl,$direction);
   75:         untie(%hash);
   76:     }
   77:     return($rid,$mapurl);
   78: }
   79: 
   80: sub hash_src {
   81:     my ($id)=@_;
   82:     my ($mapid,$resid)=split(/\./,$id);
   83:     my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
   84: 					  $resid,$hash{'src_'.$id});
   85:     if ($hash{'encrypted_'.$id}) {
   86: 	return (&Apache::lonenc::encrypted($hash{'src_'.$id}),
   87: 		&Apache::lonenc::encrypted($symb));
   88:     }
   89:     return ($hash{'src_'.$id},$symb);
   90: }
   91: 
   92: sub move {
   93:     my ($next,$endupmap,$direction) = @_;
   94:     my $safecount=0;
   95:     my $allowed=0;
   96:     do {
   97: 	($next,$endupmap)=&get_next_possible_move($next,$endupmap,$direction);
   98: 
   99: 	my $url = $hash{'src_'.$next};
  100: 	my ($mapid,$resid)=split(/\./,$next);
  101: 	my $symb = &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
  102: 						$resid,$url);
  103: 	if ($url eq '' || $symb eq '') {
  104: 	    $allowed = 0;
  105: 	} else {
  106: 	    my $priv = &Apache::lonnet::allowed('bre',$url,$symb);
  107: 	    $allowed = (($priv eq 'F') || ($priv eq '2'));
  108: 	}
  109: 	$safecount++;
  110:     } while (   ($next)
  111: 	     && ($next!~/\,/)
  112: 	     && (
  113: 		    (!$hash{'src_'.$next})
  114: 		 || (
  115: 		        (!$env{'request.role.adv'})
  116: 		     &&  $hash{'randomout_'.$next}
  117: 		    )
  118: 		 || (!$allowed)
  119: 		)
  120: 	     && ($safecount<10000));
  121: 
  122:     return ($next,$endupmap);
  123: }
  124: 
  125: sub get_next_possible_move {
  126:     my ($rid,$mapurl,$direction)=@_;
  127:     my $startoutrid=$rid;
  128: 
  129:     my $next='';
  130: 
  131:               my $mincond=1;
  132:               my $posnext='';
  133:               if ($direction eq 'forward') {
  134: # --------------------------------------------------------------------- Forward
  135:                   while ($hash{'type_'.$rid} eq 'finish') {
  136: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  137:                   }
  138: 		  foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
  139: 		     my $condition= $hash{'conditions_'.$hash{'goesto_'.$id}};
  140: 		     my $rescond  = &Apache::lonnet::docondval($condition);
  141: 		     my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
  142: 		     my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
  143: 		     if ($thiscond>=$mincond) {
  144: 		          if ($posnext) {
  145: 		             $posnext.=','.$id.':'.$thiscond;
  146:                           } else {
  147:                              $posnext=$id.':'.$thiscond;
  148: 		          }
  149:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  150: 	              }
  151:                   } 
  152: 		  foreach my $id (split(/\,/,$posnext))  {
  153:                       my ($linkid,$condval)=split(/\:/,$id);
  154:                       if ($condval>=$mincond) {
  155: 		          $next=&addrid($next,$hash{'goesto_'.$linkid},
  156:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  157:                       }
  158:                   }
  159:                   if ($hash{'is_map_'.$next}) {
  160: # This jumps to the beginning of a new map (going down level)
  161:                       if (
  162:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  163: 			  $mapurl=$hash{'src_'.$next};
  164: 			  $next=$hash{'map_start_'.$hash{'src_'.$next}};
  165:                      } elsif (
  166: # This jumps back up from an empty sequence, to a page up one level
  167:                          $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  168:                          $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  169:                      }
  170:                   } elsif 
  171:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  172: # This comes up from a map (coming up one level);
  173: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  174: 		  }
  175:               } elsif ($direction eq 'back') {
  176: # ------------------------------------------------------------------- Backwards
  177:                  while ($hash{'type_'.$rid} eq 'start') {
  178: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  179: 		 }
  180: 		 foreach my $id (split(/\,/,$hash{'from_'.$rid})) {
  181: 		     my $condition= $hash{'conditions_'.$hash{'comesfrom_'.$id}};
  182: 		     my $rescond  = &Apache::lonnet::docondval($condition);
  183: 		     my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
  184: 		     my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
  185: 		     if ($thiscond>=$mincond) {
  186: 			 if ($posnext) {
  187: 		             $posnext.=','.$id.':'.$thiscond;
  188: 			 } else {
  189:                              $posnext=$id.':'.$thiscond;
  190: 			 }
  191: 			 if ($thiscond>$mincond) { $mincond=$thiscond; }
  192: 		     }
  193: 		 } 
  194: 		 foreach my $id (split(/\,/,$posnext)) {
  195: 		     my ($linkid,$condval)=split(/\:/,$id);
  196: 		     if ($condval>=$mincond) {
  197: 			 $next=&addrid($next,$hash{'comesfrom_'.$linkid},
  198: 				       $hash{'condid_'.$hash{'undercond_'.$linkid}});
  199: 		     }
  200: 		 }
  201:                   if ($hash{'is_map_'.$next}) {
  202: # This jumps to the end of a new map (going down one level)
  203:                       if (
  204:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  205: 			  $mapurl=$hash{'src_'.$next};
  206: 			  $next=$hash{'map_finish_'.$hash{'src_'.$next}};
  207:                       } elsif (
  208:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  209: # This jumps back up from an empty sequence, to a page up one level
  210:                           $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  211:                       }
  212:                   } elsif 
  213:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  214: # This comes back up from a map (going up one level);
  215: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  216:                   }
  217: 	      }
  218:               return ($next,$mapurl);
  219: }
  220: 
  221: sub first_accessible_resource {
  222:     my $furl;
  223:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  224: 	    &GDBM_READER(),0640)) {
  225: 	$furl=$hash{'first_url'};
  226: 	my %args;
  227: 	my ($url,$args) = split(/\?/,$furl);
  228: 	foreach my $pair (split(/\&/,$args)) {
  229: 	    my ($name,$value) = split(/=/,$pair);
  230: 	    $args{&unescape($name)} = &unescape($value);
  231: 	}
  232:         if (!&Apache::lonnet::allowed('bre',$url,$args{'symb'})) {
  233: # Wow, we cannot see this ... move forward to the next one that we can see
  234: 	    my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward');
  235: # Build the new URL
  236: 	    my ($newmapid,$newresid)=split(/\./,$newrid);
  237: 	    my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
  238: 	    $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
  239: 	    if ($hash{'encrypted_'.$newrid}) {
  240: 		$furl=&Apache::lonenc::encrypted($furl);
  241: 	    }
  242: 	}
  243: 	untie(%hash);
  244: 	return $furl;
  245:     } else {
  246: 	return '/adm/navmaps';
  247:     }
  248: }
  249: 
  250: # ================================================================ Main Handler
  251: 
  252: sub handler {
  253:    my $r=shift;
  254: 
  255: # ------------------------------------------- Set document type for header only
  256: 
  257:   if ($r->header_only) {
  258:       &Apache::loncommon::content_type($r,'text/html');
  259:       $r->send_http_header;
  260:       return OK;
  261:   }
  262: 
  263:   my %cachehash=(); 
  264:   my $multichoice=0;
  265:   my %multichoicehash=();
  266:   my ($redirecturl,$redirectsymb);
  267:   my $next='';
  268:   my @possibilities=();
  269:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  270:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
  271:       $env{'form.postdata'}=~/(\w+)\:(.*)/;
  272:       my $direction=$1;
  273:       my $currenturl=$2;
  274:       if ($currenturl=~m|^/enc/|) {
  275: 	  $currenturl=&Apache::lonenc::unencrypted($currenturl);
  276:       }
  277:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  278:       if ($direction eq 'firstres') {
  279: 	  my $furl=&first_accessible_resource();
  280: 	  &Apache::loncommon::content_type($r,'text/html');
  281: 	  $r->header_out(Location => 
  282: 			 &Apache::lonnet::absolute_url().$furl);
  283: 	     
  284: 	  return REDIRECT;
  285:       }
  286:       if ($direction eq 'return') {
  287: # -------------------------------------------------------- Return to last known
  288:          my $last;
  289:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  290:                     &GDBM_READER(),0640)) {
  291: 	     $last=$hash{'last_known'};
  292:              untie(%hash);
  293:          }
  294:          my $newloc;
  295:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  296:                         &GDBM_READER(),0640))) {
  297:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  298: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  299: 	    $newloc=$hash{'src_'.$id};
  300: 	    if ($newloc) {
  301: 		if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
  302: 			      
  303: 	    } else {
  304: 		$newloc='/adm/navmaps';
  305: 	    }
  306:             untie %hash;
  307:          } else {
  308: 	    $newloc='/adm/navmaps';
  309:          }  
  310: 	     &Apache::loncommon::content_type($r,'text/html');
  311: 	     $r->header_out(Location => 
  312: 			    &Apache::lonnet::absolute_url().$newloc);
  313: 	     
  314: 	     return REDIRECT;
  315:       }
  316:       $currenturl=~s/^https?\:\/\///;
  317:       $currenturl=~s/^[^\/]+//;
  318: #
  319: # Is the current URL on the map? If not, start with last known URL
  320: #
  321:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  322: 	 my $last;
  323:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  324:                     &GDBM_READER(),0640)) {
  325: 	     $last=$hash{'last_known'};
  326:              untie(%hash);
  327:          }
  328:          if ($last) {
  329: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
  330: 	 } else {
  331: 		 &Apache::loncommon::content_type($r,'text/html');
  332: 		 $r->header_out(Location => 
  333: 				&Apache::lonnet::absolute_url().
  334: 				'/adm/noidea.html');
  335: 		 return REDIRECT;
  336:          }
  337:       }
  338: # ------------------------------------------- Do we have any idea where we are?
  339:       my $position;
  340:       if ($position=Apache::lonnet::symbread($currenturl)) {
  341: # ------------------------------------------------------------------------- Yes
  342: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  343:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
  344:           $cachehash{$startoutmap}{'last_known'}=
  345:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
  346: 
  347: # ============================================================ Tie the big hash
  348:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  349:                         &GDBM_READER(),0640)) {
  350:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  351:                       '.'.$mapnum;
  352: 
  353: # ------------------------------------------------- Move forward, backward, etc
  354:               my $endupmap;
  355:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  356: # -------------------------------------- Do we have one and only one empty URL?
  357: # We are now at at least one non-empty URL
  358: # ----------------------------------------------------- Check out possibilities
  359:               if ($next) {
  360:                   @possibilities=split(/\,/,$next);
  361:                   if ($#possibilities==0) {
  362: # ---------------------------------------------- Only one possibility, redirect
  363: 	              ($redirecturl,$redirectsymb)=&hash_src($next);
  364:                       $cachehash{$endupmap}{$redirecturl}=
  365: 			  [$redirecturl,(split(/\./,$next))[1]];
  366:                   } else {
  367: # ------------------------ There are multiple possibilities for a next resource
  368:                       $multichoice=1;
  369: 		      foreach my $id (@possibilities) {
  370: 			  $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
  371:                           $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
  372:                           $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
  373:                           (my $first, my $second) = $id =~ /(\d+).(\d+)/;
  374:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
  375:                           $multichoicehash{'symb_'.$id} = 
  376:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  377:                                                         $second.'___'.$symbSrc);
  378:                                                          
  379:                           my ($choicemap,$choiceres)=split(/\./,$id);
  380: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
  381: 			  my $url=$multichoicehash{'src_'.$id};
  382:                           $cachehash{$map}{$url}=[$url,$choiceres];
  383:                       }
  384:                   }
  385: 	      } else {
  386: # -------------------------------------------------------------- No place to go
  387:                   $multichoice=-1;
  388:               }
  389: # ----------------- The program must come past this point to untie the big hash
  390: 	      untie(%hash);
  391: # --------------------------------------------------------- Store position info
  392:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
  393:               foreach my $thismap (keys(%cachehash)) {
  394: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
  395: 		  delete($cachehash{$thismap}->{'mapnum'});
  396: 		  &Apache::lonnet::symblist($thismap,
  397: 					    %{$cachehash{$thismap}});
  398: 	      }
  399: # ============================================== Do not return before this line
  400:               if ($redirecturl) {
  401: # ----------------------------------------------------- There is a URL to go to
  402: 		  if ($direction eq 'forward') {
  403:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  404: 		  }
  405: 		  if ($direction eq 'back') {
  406:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  407: 		  }
  408: # ------------------------------------- Check for and display critical messages
  409:                   my ($redirect, $url) = &Apache::loncommon::critical_redirect(300);
  410:                   unless ($redirect) { 
  411:                       $url=&Apache::lonnet::absolute_url().$redirecturl;
  412: 		      $url = &add_get_param($url, { 'symb' => $redirectsymb});
  413:                   }
  414: 		  &Apache::loncommon::content_type($r,'text/html');
  415:                   $r->header_out(Location => $url);
  416:                   return REDIRECT;
  417: 	      } else {
  418: # --------------------------------------------------------- There was a problem
  419:                   &Apache::loncommon::content_type($r,'text/html');
  420:                   $r->send_http_header;
  421: 		  my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
  422: 						     'explain' =>
  423: 						     'You have reached the end of the sequence of materials.',
  424: 						     'back' => 'Go Back',
  425: 						     'nav' => 'Course Contents',
  426: 						     'wherenext' =>
  427: 						     'There are several possibilities of where to go next',
  428: 						     'pick' =>
  429: 						     'Please click on the the resource you intend to access',
  430: 						     'titleheader' => 'Title',
  431: 						     'type' => 'Type');
  432:                   if (&Apache::loncommon::course_type() eq 'Community') {
  433:                       $lt{'nav'} = &mt('Community Contents');
  434:                   }
  435:                   if ($#possibilities>0) {
  436: 		      my $start_page=
  437: 			  &Apache::loncommon::start_page('Multiple Resources');
  438:                      $r->print(<<ENDSTART);
  439: $start_page
  440: <h3>$lt{'wherenext'}</h3>
  441: <p>
  442: $lt{'pick'}:
  443: <p>
  444: <table border="2">
  445: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
  446: ENDSTART
  447:                      foreach my $id (@possibilities) {
  448:                         $r->print(
  449:                               '<tr><td><a href="'.
  450: 				  &add_get_param($multichoicehash{'src_'.$id},
  451: 						 {'symb' =>
  452: 						      $multichoicehash{'symb_'.$id},
  453: 						  }).'">'.
  454:                               $multichoicehash{'title_'.$id}.
  455:                               '</a></td><td>'.$multichoicehash{'type_'.$id}.
  456: 			      '</td></tr>');
  457:                      }
  458:                      $r->print('</table>');
  459:                   } else {
  460:                       $r->print(
  461:                           &Apache::loncommon::start_page('No Resource')
  462:                          .'<h2>'.$lt{'title'}.'</h2>'
  463:                          .'<p>'.$lt{'explain'}.'</p>');
  464:                   }
  465:                   $r->print(
  466:                       &Apache::lonhtmlcommon::actionbox(
  467:                           ['<a href="/adm/flip?postdata=return:">'.$lt{'back'}.'</a></li>',
  468:                            '<a href="/adm/navmaps">'.$lt{'nav'}.'</a></li>'
  469:                           ])
  470:                      .&Apache::loncommon::end_page());
  471:                   return OK;
  472: 	      }
  473: 	  } else {
  474: # ------------------------------------------------- Problem, could not tie hash
  475:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  476:               return HTTP_NOT_ACCEPTABLE; 
  477:           }
  478:       } else {
  479: # ---------------------------------------- No, could not determine where we are
  480: 	  $r->internal_redirect('/adm/ambiguous');
  481:           return OK;
  482:       }
  483:   } else {
  484: # -------------------------- Class was not initialized or page fliped strangely
  485:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  486:       return HTTP_NOT_ACCEPTABLE; 
  487:   } 
  488: }
  489: 
  490: 1;
  491: __END__
  492: 
  493: =pod
  494: 
  495: =head1 NAME
  496: 
  497: Apache::lonpageflip
  498: 
  499: =head1 SYNOPSIS
  500: 
  501: Deals with forward, backward, and other page flips.
  502: 
  503: This is part of the LearningOnline Network with CAPA project
  504: described at http://www.lon-capa.org.
  505: 
  506: =head1 OVERVIEW
  507: 
  508: (empty)
  509: 
  510: =head1 SUBROUTINES
  511: 
  512: =over cleanup()
  513: 
  514: =item addrid()
  515: 
  516: =item fullmove()
  517: 
  518: =item hash_src()
  519: 
  520: =item move()
  521: 
  522: =item get_next_possible_move()
  523: 
  524: =item first_accessible_resource()
  525: 
  526: =item handler()
  527: 
  528: =back
  529: 
  530: =cut
  531: 
  532: 
  533: 
  534: 
  535: 
  536: 

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