File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.51: download - view: text, annotated - select for diffs
Thu Feb 17 09:09:57 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- more $r->content_type eradication

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

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