File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.36: download - view: text, annotated - select for diffs
Mon Mar 3 20:24:25 2003 UTC (21 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
URLs to go to should not be pulled from the symb, since that will lack
query strings and public/wrapper prefixes.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.36 2003/03/03 20:24:25 www 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: # (Page Handler
   30: #
   31: # (TeX Content Handler
   32: #
   33: # 05/29/00,05/30 Gerd Kortemeyer)
   34: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   35: # 10/02 Gerd Kortemeyer)
   36: #
   37: # 10/03,10/05,10/06,10/07,10/09,10/10,10/11,10/16,10/17,
   38: # 11/14,11/16,
   39: # 10/01/01,05/01,05/28,07/05 Gerd Kortemeyer
   40: 
   41: package Apache::lonpageflip;
   42: 
   43: use strict;
   44: use Apache::Constants qw(:common :http REDIRECT);
   45: use Apache::lonnet();
   46: use HTML::TokeParser;
   47: use GDBM_File;
   48: 
   49: # ========================================================== Module Global Hash
   50:   
   51: my %hash;
   52: 
   53: sub cleanup {
   54:     if (tied(%hash)){
   55: 	&Apache::lonnet::logthis('Cleanup pageflip: hash');
   56:         unless (untie(%hash)) {
   57: 	    &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
   58:         }
   59:     }
   60: }
   61: 
   62: sub addrid {
   63:     my ($current,$new,$condid)=@_;
   64:     unless ($condid) { $condid=0; }
   65: 
   66: 	if ($current) {
   67: 	    $current.=','.$new;
   68:         } else {
   69:             $current=''.$new;
   70:         }
   71: 
   72:     return $current;
   73: }
   74: 
   75: sub fullmove {
   76:     my ($rid,$mapurl,$direction)=@_;
   77:     if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
   78:                         &GDBM_READER(),0640)) {
   79: 	($rid,$mapurl)=&move($rid,$mapurl,$direction);
   80:         untie(%hash);
   81:     }
   82:     return($rid,$mapurl);
   83: }
   84: 
   85: sub move {
   86:     my ($rid,$mapurl,$direction)=@_;
   87:     my $startoutrid=$rid;
   88: 
   89:     my $next='';
   90: 
   91:               my $mincond=1;
   92:               my $posnext='';
   93:               if ($direction eq 'forward') {
   94: # --------------------------------------------------------------------- Forward
   95:                   while ($hash{'type_'.$rid} eq 'finish') {
   96: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
   97:                   }
   98:                   map {
   99:                       my $thiscond=
  100:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  101:                       if ($thiscond>=$mincond) {
  102: 		          if ($posnext) {
  103: 		             $posnext.=','.$_.':'.$thiscond;
  104:                           } else {
  105:                              $posnext=$_.':'.$thiscond;
  106: 		          }
  107:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  108: 	              }
  109:                   } split(/\,/,$hash{'to_'.$rid});
  110:                   map {
  111:                       my ($linkid,$condval)=split(/\:/,$_);
  112:                       if ($condval>=$mincond) {
  113: 		          $next=&addrid($next,$hash{'goesto_'.$linkid},
  114:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  115:                       }
  116:                   } split(/\,/,$posnext);
  117:                   if ($hash{'is_map_'.$next}) {
  118: # This jumps to the beginning of a new map (going down level)
  119:                       if (
  120:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  121: 			  $mapurl=$hash{'src_'.$next};
  122: 			  $next=$hash{'map_start_'.$hash{'src_'.$next}};
  123:                      }
  124:                   } elsif 
  125:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  126: # This comes up from a map (coming up one level);
  127: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  128:                   }
  129:               } elsif ($direction eq 'back') {
  130: # ------------------------------------------------------------------- Backwards
  131:                  while ($hash{'type_'.$rid} eq 'start') {
  132: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  133: 		 }
  134:                   map {
  135:                       my $thiscond=
  136:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  137:                       if ($thiscond>=$mincond) {
  138: 		          if ($posnext) {
  139: 		             $posnext.=','.$_.':'.$thiscond;
  140:                           } else {
  141:                              $posnext=$_.':'.$thiscond;
  142: 		          }
  143:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  144: 	              }
  145:                   } split(/\,/,$hash{'from_'.$rid});
  146:                   map {
  147:                       my ($linkid,$condval)=split(/\:/,$_);
  148:                       if ($condval>=$mincond) {
  149: 		          $next=&addrid($next,$hash{'comesfrom_'.$linkid},
  150:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  151:                       }
  152:                   } split(/\,/,$posnext);
  153:                   if ($hash{'is_map_'.$next}) {
  154: # This jumps to the end of a new map (going down one level)
  155:                       if (
  156:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  157: 			  $mapurl=$hash{'src_'.$next};
  158: 			  $next=$hash{'map_finish_'.$hash{'src_'.$next}};
  159:                      }
  160:                   } elsif 
  161:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  162: # This comes back up from a map (going up one level);
  163: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  164:                   }
  165: 	      }
  166:               return ($next,$mapurl);
  167: }
  168: 
  169: # ================================================================ Main Handler
  170: 
  171: sub handler {
  172:    my $r=shift;
  173: 
  174: # ------------------------------------------- Set document type for header only
  175: 
  176:   if ($r->header_only) {
  177:      $r->content_type('text/html');
  178:      $r->send_http_header;
  179:      return OK;
  180:   }
  181: 
  182:   my %cachehash=(); 
  183:   my $multichoice=0;
  184:   my %multichoicehash=();
  185:   my $redirecturl='';
  186:   my $next='';
  187:   my @possibilities=();
  188:   if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
  189:       $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
  190:       my $direction=$1;
  191:       my $currenturl=$2;
  192:       if ($direction eq 'return') {
  193: # -------------------------------------------------------- Return to last known
  194:          my $last;
  195:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  196:                     &GDBM_READER(),0640)) {
  197: 	     $last=$hash{'last_known'};
  198:              untie(%hash);
  199:          }
  200:          my $newloc;
  201:          if (($last) && (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  202:                         &GDBM_READER(),0640))) {
  203:             my ($murl,$mid,$fn)=split(/\_\_\_/,$last);
  204:             $newloc=$hash{'src_'.
  205:                       $hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.
  206:                       '.'.$mid};
  207:             untie %hash;
  208:          } else {
  209: 	    $newloc='/adm/noidea.html';
  210:          }  
  211: 	 $r->content_type('text/html');
  212:          $r->header_out(Location => 
  213: 			'http://'.$ENV{'HTTP_HOST'}.$newloc);
  214:                                
  215:          return REDIRECT;
  216:       }
  217:       $currenturl=~s/^http\:\/\///;
  218:       $currenturl=~s/^[^\/]+//;
  219: #
  220: # Is the current URL on the map? If not, start with last known URL
  221: #
  222:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  223: 	 my $last;
  224:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  225:                     &GDBM_READER(),0640)) {
  226: 	     $last=$hash{'last_known'};
  227:              untie(%hash);
  228:          }
  229:          if ($last) {
  230: 	     $currenturl=&Apache::lonnet::clutter((split(/\_\_\_/,$last))[1]);
  231: 	 } else {
  232: 	     $r->content_type('text/html');
  233:              $r->header_out(Location => 
  234:                                'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
  235:              return REDIRECT;
  236:          }
  237:       }
  238: # ------------------------------------------- Do we have any idea where we are?
  239:       my $position;
  240:       if ($position=Apache::lonnet::symbread($currenturl)) {
  241: # ------------------------------------------------------------------------- Yes
  242: 	  my ($startoutmap,$mapnum,$thisurl)=split(/\_\_\_/,$position);
  243:           $cachehash{$startoutmap}{$thisurl}=$mapnum;
  244:           $cachehash{$startoutmap}{'last_known'}=
  245:                                        &Apache::lonnet::declutter($currenturl);
  246: 
  247: # ============================================================ Tie the big hash
  248:           if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  249:                         &GDBM_READER(),0640)) {
  250:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  251:                       '.'.$mapnum;
  252: 
  253: # ------------------------------------------------- Move forward, backward, etc
  254:               my $endupmap;
  255:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  256: # -------------------------------------- Do we have one and only one empty URL?
  257:               my $safecount=0;
  258:               while (($next) && ($next!~/\,/) && 
  259:                      ((!$hash{'src_'.$next}) || ($hash{'randomout_'.$next}))
  260:                      && ($safecount<10000)) {
  261:                   ($next,$endupmap)=&move($next,$endupmap,$direction);
  262:                   $safecount++;
  263:               }
  264: # We are now at at least one non-empty URL
  265: # ----------------------------------------------------- Check out possibilities
  266:               if ($next) {
  267:                   @possibilities=split(/\,/,$next);
  268:                   if ($#possibilities==0) {
  269: # ---------------------------------------------- Only one possibility, redirect
  270: 	              $redirecturl=$hash{'src_'.$next};
  271:                       $cachehash{$endupmap}
  272:                                 {&Apache::lonnet::declutter($redirecturl)}
  273: 		                                 =(split(/\./,$next))[1];
  274:                   } else {
  275: # ------------------------ There are multiple possibilities for a next resource
  276:                       $multichoice=1;
  277:                       map {
  278: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
  279:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
  280:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
  281:                           (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
  282:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
  283:                           $multichoicehash{'symb_'.$_} = 
  284:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  285:                                                         $second.'___'.$symbSrc);
  286:                                                          
  287:                           my ($choicemap,$choiceres)=split(/\./,$_);
  288:                           $cachehash
  289: 			 {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
  290:                          {&Apache::lonnet::declutter(
  291: 						      $multichoicehash
  292:                                                          {'src_'.$_}
  293:                                                        )}
  294: 		                                 =$choiceres;
  295:                       } @possibilities;
  296:                   }
  297: 	      } else {
  298: # -------------------------------------------------------------- No place to go
  299:                   $multichoice=-1;
  300:               }
  301: # ----------------- The program must come past this point to untie the big hash
  302: 	      untie(%hash);
  303: # --------------------------------------------------------- Store position info
  304:               $cachehash{$startoutmap}{'last_direction'}=$direction;
  305:               foreach my $thismap (keys %cachehash) {
  306:                  &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
  307: 	      }
  308: # ============================================== Do not return before this line
  309:               if ($redirecturl) {
  310: # ----------------------------------------------------- There is a URL to go to
  311: # ------------------------------------------------- Check for critical messages
  312: 		  if ((time-$ENV{'user.criticalcheck.time'})>300) {
  313:                      my @what=&Apache::lonnet::dump
  314:                                   ('critical',$ENV{'user.domain'},
  315:                                               $ENV{'user.name'});
  316:                      if ($what[0]) {
  317: 	                if (($what[0] ne 'con_lost') && 
  318:                             ($what[0]!~/^error\:/)) {
  319: 	                   $redirecturl='/adm/email?critical=display';
  320:                         }
  321:                      }
  322:                      &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
  323: 		  }
  324: 
  325: 		  $r->content_type('text/html');
  326:                   $r->header_out(Location => 
  327:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
  328:                   return REDIRECT;
  329: 	      } else {
  330: # --------------------------------------------------------- There was a problem
  331:                   $r->content_type('text/html');
  332:                   $r->send_http_header;
  333:                   if ($#possibilities>0) {
  334:                      $r->print(<<ENDSTART);
  335: <head><title>Choose Next Location</title></head>
  336: <body bgcolor="#FFFFFF">
  337: <h1>LON-CAPA</h1>
  338: There are several possibilities of where to go next.
  339: <p>
  340: Please click on the the resource you intend to access:
  341: <p>
  342: <table border=2>
  343: <tr><th>Title</th><th>Type</th></tr>
  344: ENDSTART
  345:                      foreach (@possibilities) {
  346:                         $r->print(
  347:                               '<tr><td><a href="'.
  348:                               $multichoicehash{'src_'.$_}.'?symb=' .
  349:                                   Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
  350:                               $multichoicehash{'title_'.$_}.
  351:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
  352: 			      '</td></tr>');
  353:                      }
  354:                      $r->print('</table></body></html>');
  355: 		     return OK;
  356:                   } else {
  357:                      $r->print(<<ENDNONE);
  358: <head><title>Choose Next Location</title></head>
  359: <body bgcolor="#FFFFFF">
  360: <img src="/adm/lonIcons/lonlogos.gif" align=right>
  361: <h1>Sorry!</h1>
  362: <h2>Next resource could not be identified.</h2>
  363: <h3>You probably are at the beginning or the end of the course.</h3>
  364: </body>
  365: </html>
  366: ENDNONE
  367:                      return OK;
  368: 	         }
  369: 	     }
  370: 	  } else {
  371: # ------------------------------------------------- Problem, could not tie hash
  372:               $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  373:               return HTTP_NOT_ACCEPTABLE; 
  374:           }
  375:       } else {
  376: # ---------------------------------------- No, could not determine where we are
  377:          $r->internal_redirect('/adm/ambiguous');
  378:       }
  379:   } else {
  380: # -------------------------- Class was not initialized or page fliped strangely
  381:       $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  382:       return HTTP_NOT_ACCEPTABLE; 
  383:   } 
  384: }
  385: 
  386: 1;
  387: __END__
  388: 
  389: 
  390: 
  391: 
  392: 
  393: 
  394: 

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