File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.47: download - view: text, annotated - select for diffs
Thu Apr 8 09:14:41 2004 UTC (20 years ago) by raeburn
Branches: MAIN
CVS tags: version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, HEAD
Accommodate case where we are moving forwards (or backwards) and encounter an empty folder, followed (or preceded) by a page. This situation can occur for a course structure created from an IMS import.

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

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