File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.62: download - view: text, annotated - select for diffs
Mon Jan 9 18:51:26 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- eliminating more $_

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

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