File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.31: download - view: text, annotated - select for diffs
Thu Oct 10 19:05:32 2002 UTC (21 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug 790

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

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