File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.50: download - view: text, annotated - select for diffs
Thu Nov 11 19:43:22 2004 UTC (19 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_99_1, version_1_2_99_0, HEAD
- back/forward work

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.50 2004/11/11 19:43:22 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: # (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 hash_src {
   86:     my ($id)=@_;
   87:     if ($hash{'encrypted_'.$id}) {
   88: 	return &Apache::lonenc::encrypted($hash{'src_'.$id});
   89:     }
   90:     return $hash{'src_'.$id};
   91: }
   92: 
   93: sub move {
   94:     my ($rid,$mapurl,$direction)=@_;
   95:     my $startoutrid=$rid;
   96: 
   97:     my $next='';
   98: 
   99:               my $mincond=1;
  100:               my $posnext='';
  101:               if ($direction eq 'forward') {
  102: # --------------------------------------------------------------------- Forward
  103:                   while ($hash{'type_'.$rid} eq 'finish') {
  104: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  105:                   }
  106:                   map {
  107:                       my $thiscond=
  108:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  109:                       if ($thiscond>=$mincond) {
  110: 		          if ($posnext) {
  111: 		             $posnext.=','.$_.':'.$thiscond;
  112:                           } else {
  113:                              $posnext=$_.':'.$thiscond;
  114: 		          }
  115:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  116: 	              }
  117:                   } split(/\,/,$hash{'to_'.$rid});
  118:                   map {
  119:                       my ($linkid,$condval)=split(/\:/,$_);
  120:                       if ($condval>=$mincond) {
  121: 		          $next=&addrid($next,$hash{'goesto_'.$linkid},
  122:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  123:                       }
  124:                   } split(/\,/,$posnext);
  125:                   if ($hash{'is_map_'.$next}) {
  126: # This jumps to the beginning of a new map (going down level)
  127:                       if (
  128:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  129: 			  $mapurl=$hash{'src_'.$next};
  130: 			  $next=$hash{'map_start_'.$hash{'src_'.$next}};
  131:                      } elsif (
  132: # This jumps back up from an empty sequence, to a page up one level
  133:                          $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  134:                          $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  135:                      }
  136:                   } elsif 
  137:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  138: # This comes up from a map (coming up one level);
  139: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  140:                   }
  141:               } elsif ($direction eq 'back') {
  142: # ------------------------------------------------------------------- Backwards
  143:                  while ($hash{'type_'.$rid} eq 'start') {
  144: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
  145: 		 }
  146:                   map {
  147:                       my $thiscond=
  148:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  149:                       if ($thiscond>=$mincond) {
  150: 		          if ($posnext) {
  151: 		             $posnext.=','.$_.':'.$thiscond;
  152:                           } else {
  153:                              $posnext=$_.':'.$thiscond;
  154: 		          }
  155:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
  156: 	              }
  157:                   } split(/\,/,$hash{'from_'.$rid});
  158:                   map {
  159:                       my ($linkid,$condval)=split(/\:/,$_);
  160:                       if ($condval>=$mincond) {
  161: 		          $next=&addrid($next,$hash{'comesfrom_'.$linkid},
  162:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  163:                       }
  164:                   } split(/\,/,$posnext);
  165:                   if ($hash{'is_map_'.$next}) {
  166: # This jumps to the end of a new map (going down one level)
  167:                       if (
  168:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  169: 			  $mapurl=$hash{'src_'.$next};
  170: 			  $next=$hash{'map_finish_'.$hash{'src_'.$next}};
  171:                       } elsif (
  172:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
  173: # This jumps back up from an empty sequence, to a page up one level
  174:                           $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  175:                       }
  176:                   } elsif 
  177:                     ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
  178: # This comes back up from a map (going up one level);
  179: 		      $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
  180:                   }
  181: 	      }
  182:               return ($next,$mapurl);
  183: }
  184: 
  185: # ================================================================ Main Handler
  186: 
  187: sub handler {
  188:    my $r=shift;
  189: 
  190: # ------------------------------------------- Set document type for header only
  191: 
  192:   if ($r->header_only) {
  193:      $r->content_type('text/html');
  194:      $r->send_http_header;
  195:      return OK;
  196:   }
  197: 
  198:   my %cachehash=(); 
  199:   my $multichoice=0;
  200:   my %multichoicehash=();
  201:   my $redirecturl='';
  202:   my $next='';
  203:   my @possibilities=();
  204:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  205:   if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
  206:       $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
  207:       my $direction=$1;
  208:       my $currenturl=$2;
  209:       if ($currenturl=~m|^/enc/|) {
  210: 	  $currenturl=&Apache::lonenc::unencrypted($currenturl);
  211:       }
  212:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  213:       if ($direction eq 'return') {
  214: # -------------------------------------------------------- Return to last known
  215:          my $last;
  216:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  217:                     &GDBM_READER(),0640)) {
  218: 	     $last=$hash{'last_known'};
  219:              untie(%hash);
  220:          }
  221:          my $newloc;
  222:          if (($last) && (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  223:                         &GDBM_READER(),0640))) {
  224:             my ($murl,$fn)=&Apache::lonnet::decode_symb($last);
  225:             my $id;
  226:             ($murl,$id,$fn)=&Apache::lonnet::decode_symb(&Apache::lonnet::symbread($fn));
  227: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  228: 	    $newloc=$hash{'src_'.$id};
  229: 	    &Apache::lonnet::logthis(" hrrm 1 ");
  230: 	    if ($newloc) {
  231: 		if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
  232: 			      
  233: 	    } else {
  234: 		$newloc='/adm/noidea.html';
  235: 	    }
  236:             untie %hash;
  237:          } else {
  238: 	    $newloc='/adm/noidea.html';
  239:          }  
  240: 	 $r->content_type('text/html');
  241:          $r->header_out(Location => 
  242: 			'http://'.$ENV{'HTTP_HOST'}.$newloc);
  243:                                
  244:          return REDIRECT;
  245:       }
  246:       $currenturl=~s/^http\:\/\///;
  247:       $currenturl=~s/^[^\/]+//;
  248: #
  249: # Is the current URL on the map? If not, start with last known URL
  250: #
  251:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  252: 	 my $last;
  253:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  254:                     &GDBM_READER(),0640)) {
  255: 	     $last=$hash{'last_known'};
  256:              untie(%hash);
  257:          }
  258:          if ($last) {
  259: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[1]);
  260: 	 } else {
  261: 	     $r->content_type('text/html');
  262:              $r->header_out(Location => 
  263:                                'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
  264:              return REDIRECT;
  265:          }
  266:       }
  267: # ------------------------------------------- Do we have any idea where we are?
  268:       my $position;
  269:       if ($position=Apache::lonnet::symbread($currenturl)) {
  270: # ------------------------------------------------------------------------- Yes
  271: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  272:           $cachehash{$startoutmap}{$thisurl}=$mapnum;
  273:           $cachehash{$startoutmap}{'last_known'}=
  274:                                        &Apache::lonnet::declutter($currenturl);
  275: 
  276: # ============================================================ Tie the big hash
  277:           if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  278:                         &GDBM_READER(),0640)) {
  279:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  280:                       '.'.$mapnum;
  281: 
  282: # ------------------------------------------------- Move forward, backward, etc
  283:               my $endupmap;
  284:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  285: # -------------------------------------- Do we have one and only one empty URL?
  286:               my $safecount=0;
  287:               while (($next) && ($next!~/\,/) && 
  288:                      ((!$hash{'src_'.$next}) || 
  289: 		      ((!$ENV{'request.role.adv'}) && $hash{'randomout_'.$next}))
  290:                      && ($safecount<10000)) {
  291:                   ($next,$endupmap)=&move($next,$endupmap,$direction);
  292:                   $safecount++;
  293:               }
  294: # We are now at at least one non-empty URL
  295: # ----------------------------------------------------- Check out possibilities
  296:               if ($next) {
  297:                   @possibilities=split(/\,/,$next);
  298:                   if ($#possibilities==0) {
  299: # ---------------------------------------------- Only one possibility, redirect
  300: 	              $redirecturl=&hash_src($next);
  301:                       $cachehash{$endupmap}
  302:                                 {&Apache::lonnet::declutter($redirecturl)}
  303: 		                                 =(split(/\./,$next))[1];
  304:                   } else {
  305: # ------------------------ There are multiple possibilities for a next resource
  306:                       $multichoice=1;
  307:                       map {
  308: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
  309:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
  310:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
  311:                           (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
  312:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
  313:                           $multichoicehash{'symb_'.$_} = 
  314:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  315:                                                         $second.'___'.$symbSrc);
  316:                                                          
  317:                           my ($choicemap,$choiceres)=split(/\./,$_);
  318:                           $cachehash
  319: 			 {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
  320:                          {&Apache::lonnet::declutter(
  321: 						      $multichoicehash
  322:                                                          {'src_'.$_}
  323:                                                        )}
  324: 		                                 =$choiceres;
  325:                       } @possibilities;
  326:                   }
  327: 	      } else {
  328: # -------------------------------------------------------------- No place to go
  329:                   $multichoice=-1;
  330:               }
  331: # ----------------- The program must come past this point to untie the big hash
  332: 	      untie(%hash);
  333: # --------------------------------------------------------- Store position info
  334:               $cachehash{$startoutmap}{'last_direction'}=$direction;
  335:               foreach my $thismap (keys %cachehash) {
  336:                  &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
  337: 	      }
  338: # ============================================== Do not return before this line
  339:               if ($redirecturl) {
  340: # ----------------------------------------------------- There is a URL to go to
  341: 		  if ($direction eq 'forward') {
  342:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  343: 		  }
  344: 		  if ($direction eq 'back') {
  345:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  346: 		  }
  347: # ------------------------------------------------- Check for critical messages
  348: 		  if ((time-$ENV{'user.criticalcheck.time'})>300) {
  349:                      my @what=&Apache::lonnet::dump
  350:                                   ('critical',$ENV{'user.domain'},
  351:                                               $ENV{'user.name'});
  352:                      if ($what[0]) {
  353: 	                if (($what[0] ne 'con_lost') && 
  354:                             ($what[0]!~/^error\:/)) {
  355: 	                   $redirecturl='/adm/email?critical=display';
  356:                         }
  357:                      }
  358:                      &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
  359: 		  }
  360: 
  361: 		  $r->content_type('text/html');
  362:                   $r->header_out(Location => 
  363:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
  364:                   return REDIRECT;
  365: 	      } else {
  366: # --------------------------------------------------------- There was a problem
  367:                   $r->content_type('text/html');
  368:                   $r->send_http_header;
  369:                   if ($#possibilities>0) {
  370: 		      my $bodytag=
  371:                              &Apache::loncommon::bodytag('Multiple Resources');
  372:                      $r->print(<<ENDSTART);
  373: <head><title>Choose Next Location</title></head>
  374: $bodytag
  375: <h3>There are several possibilities of where to go next</h3>
  376: <p>
  377: Please click on the the resource you intend to access:
  378: <p>
  379: <table border=2>
  380: <tr><th>Title</th><th>Type</th></tr>
  381: ENDSTART
  382:                      foreach (@possibilities) {
  383:                         $r->print(
  384:                               '<tr><td><a href="'.
  385:                               $multichoicehash{'src_'.$_}.'?symb=' .
  386:                                   Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
  387:                               $multichoicehash{'title_'.$_}.
  388:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
  389: 			      '</td></tr>');
  390:                      }
  391:                      $r->print('</table></body></html>');
  392: 		     return OK;
  393:                   } else {
  394: 		      my $bodytag=&Apache::loncommon::bodytag('No Resource');
  395:                      $r->print(<<ENDNONE);
  396: <head><title>No Resource</title></head>
  397: $bodytag
  398: <h3>Next resource could not be identified.</h3>
  399: <p>You probably are at the <b>beginning</b> or the <b>end</b> of the 
  400: course.</p>
  401: <ul>
  402: <li><a href="/adm/flip?postdata=return:">Go Back</a></li>
  403: <li><a href="/adm/navmaps">Navigate Course Content</a></li>
  404: </ul>
  405: </body>
  406: </html>
  407: ENDNONE
  408:                      return OK;
  409: 	         }
  410: 	     }
  411: 	  } else {
  412: # ------------------------------------------------- Problem, could not tie hash
  413:               $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  414:               return HTTP_NOT_ACCEPTABLE; 
  415:           }
  416:       } else {
  417: # ---------------------------------------- No, could not determine where we are
  418: 	  if ( &Apache::lonnet::mod_perl_version() == 2 ) {
  419: 	      &Apache::lonnet::cleanenv();
  420: 	  }
  421: 	  $r->internal_redirect('/adm/ambiguous');
  422:       }
  423:   } else {
  424: # -------------------------- Class was not initialized or page fliped strangely
  425:       $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  426:       return HTTP_NOT_ACCEPTABLE; 
  427:   } 
  428: }
  429: 
  430: 1;
  431: __END__
  432: 
  433: 
  434: 
  435: 
  436: 
  437: 
  438: 

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