File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.23: download - view: text, annotated - select for diffs
Fri Apr 12 13:47:21 2002 UTC (22 years ago) by www
Branches: MAIN
CVS tags: HEAD
Hopefully fixed "forward"

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

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