File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.55: download - view: text, annotated - select for diffs
Tue Aug 2 21:15:52 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_0_0, version_1_99_3, HEAD
- make sure "What's New" shows up if it is supposed to

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # $Id: lonpageflip.pm,v 1.55 2005/08/02 21:15:52 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: sub navlaunch {
  175:     my ($r)=@_;
  176:     &Apache::loncommon::content_type($r,'text/html');
  177:     &Apache::loncommon::no_cache($r);
  178:     $r->send_http_header;
  179:     my $html=&Apache::lonxml::xmlbegin();
  180:     $r->print("$html<head>\n");
  181:     $r->print('</head>'.
  182: 	      &Apache::loncommon::bodytag('Launched'));   
  183:     $r->print(<<ENDNAV);
  184:     <p><a href="/adm/flip?postdata=firstres%3a">Goto first resource</a></p>
  185:     <script type="text/javascript">
  186: 	function collapse() {
  187: 	    menu=window.open("/adm/navmaps?collapseExternal","loncapanav",
  188: 			     "height=600,width=400,scrollbars=1");
  189: 	    this.document.location='/adm/navmaps?turningOffExternal';
  190: 	}
  191:     </script>
  192:     <p><a href="javascript:collapse();">Collapse external navigation window</a></p>
  193: ENDNAV
  194:     $r->print(&Apache::loncommon::endbodytag().'</html>');
  195: }
  196: # ================================================================ Main Handler
  197: 
  198: sub handler {
  199:    my $r=shift;
  200: 
  201: # ------------------------------------------- Set document type for header only
  202: 
  203:   if ($r->header_only) {
  204:       &Apache::loncommon::content_type($r,'text/html');
  205:       $r->send_http_header;
  206:       return OK;
  207:   }
  208: 
  209:   my %cachehash=(); 
  210:   my $multichoice=0;
  211:   my %multichoicehash=();
  212:   my $redirecturl='';
  213:   my $next='';
  214:   my @possibilities=();
  215:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
  216:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
  217:       $env{'form.postdata'}=~/(\w+)\:(.*)/;
  218:       my $direction=$1;
  219:       my $currenturl=$2;
  220:       if ($currenturl=~m|^/enc/|) {
  221: 	  $currenturl=&Apache::lonenc::unencrypted($currenturl);
  222:       }
  223:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
  224:       if ($direction eq 'firstres') {
  225: 	  my $furl;
  226: 	  if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  227: 		  &GDBM_READER(),0640)) {
  228: 	      $furl=$bighash{'first_url'};
  229: 	      untie(%bighash);
  230: 	  }
  231: 	  &Apache::loncommon::content_type($r,'text/html');
  232: 	  $r->header_out(Location => 
  233: 			 'http://'.$ENV{'HTTP_HOST'}.$furl);
  234: 	     
  235: 	  return REDIRECT;
  236:       }
  237:       if ($direction eq 'return' || $direction eq 'navlaunch') {
  238: # -------------------------------------------------------- Return to last known
  239:          my $last;
  240:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  241:                     &GDBM_READER(),0640)) {
  242: 	     $last=$hash{'last_known'};
  243:              untie(%hash);
  244:          }
  245:          my $newloc;
  246:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  247:                         &GDBM_READER(),0640))) {
  248:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
  249: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
  250: 	    $newloc=$hash{'src_'.$id};
  251: 	    if ($newloc) {
  252: 		if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
  253: 			      
  254: 	    } else {
  255: 		$newloc='/adm/noidea.html';
  256: 	    }
  257:             untie %hash;
  258:          } else {
  259: 	    $newloc='/adm/noidea.html';
  260:          }  
  261: 	 if ($newloc eq '/adm/noidea.html' && $direction eq 'navlaunch') {
  262: 	     &navlaunch($r);
  263: 	     return OK;
  264: 	 } else {
  265: 	     &Apache::loncommon::content_type($r,'text/html');
  266: 	     $r->header_out(Location => 
  267: 			    'http://'.$ENV{'HTTP_HOST'}.$newloc);
  268: 	     
  269: 	     return REDIRECT;
  270: 	 }
  271:       }
  272:       $currenturl=~s/^http\:\/\///;
  273:       $currenturl=~s/^[^\/]+//;
  274: #
  275: # Is the current URL on the map? If not, start with last known URL
  276: #
  277:       unless (&Apache::lonnet::is_on_map($currenturl)) {
  278: 	 my $last;
  279:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  280:                     &GDBM_READER(),0640)) {
  281: 	     $last=$hash{'last_known'};
  282:              untie(%hash);
  283:          }
  284:          if ($last) {
  285: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
  286: 	 } else {
  287: 	     if ($direction eq 'return') {
  288: 		 &Apache::loncommon::content_type($r,'text/html');
  289: 		 $r->header_out(Location => 
  290: 				'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
  291: 		 return REDIRECT;
  292: 	     } else {
  293: 		 &navlaunch($r);
  294: 		 return OK;
  295: 	     }
  296:          }
  297:       }
  298: # ------------------------------------------- Do we have any idea where we are?
  299:       my $position;
  300:       if ($position=Apache::lonnet::symbread($currenturl)) {
  301: # ------------------------------------------------------------------------- Yes
  302: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
  303:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
  304:           $cachehash{$startoutmap}{'last_known'}=
  305:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
  306: 
  307: # ============================================================ Tie the big hash
  308:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
  309:                         &GDBM_READER(),0640)) {
  310:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
  311:                       '.'.$mapnum;
  312: 
  313: # ------------------------------------------------- Move forward, backward, etc
  314:               my $endupmap;
  315:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
  316: # -------------------------------------- Do we have one and only one empty URL?
  317:               my $safecount=0;
  318:               while (($next) && ($next!~/\,/) && 
  319:                      ((!$hash{'src_'.$next}) || 
  320: 		      ((!$env{'request.role.adv'}) && $hash{'randomout_'.$next}))
  321:                      && ($safecount<10000)) {
  322:                   ($next,$endupmap)=&move($next,$endupmap,$direction);
  323:                   $safecount++;
  324:               }
  325: # We are now at at least one non-empty URL
  326: # ----------------------------------------------------- Check out possibilities
  327:               if ($next) {
  328:                   @possibilities=split(/\,/,$next);
  329:                   if ($#possibilities==0) {
  330: # ---------------------------------------------- Only one possibility, redirect
  331: 	              $redirecturl=&hash_src($next);
  332:                       $cachehash{$endupmap}{$redirecturl}=
  333: 			  [$redirecturl,(split(/\./,$next))[1]];
  334:                   } else {
  335: # ------------------------ There are multiple possibilities for a next resource
  336:                       $multichoice=1;
  337:                       map {
  338: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
  339:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
  340:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
  341:                           (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
  342:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
  343:                           $multichoicehash{'symb_'.$_} = 
  344:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
  345:                                                         $second.'___'.$symbSrc);
  346:                                                          
  347:                           my ($choicemap,$choiceres)=split(/\./,$_);
  348: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
  349: 			  my $url=$multichoicehash{'src_'.$_};
  350:                           $cachehash{$map}{$url}=[$url,$choiceres];
  351:                       } @possibilities;
  352:                   }
  353: 	      } else {
  354: # -------------------------------------------------------------- No place to go
  355:                   $multichoice=-1;
  356:               }
  357: # ----------------- The program must come past this point to untie the big hash
  358: 	      untie(%hash);
  359: # --------------------------------------------------------- Store position info
  360:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
  361:               foreach my $thismap (keys %cachehash) {
  362: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
  363: 		  delete($cachehash{$thismap}->{'mapnum'});
  364: 		  &Apache::lonnet::symblist($thismap,
  365: 					    %{$cachehash{$thismap}});
  366: 	      }
  367: # ============================================== Do not return before this line
  368:               if ($redirecturl) {
  369: # ----------------------------------------------------- There is a URL to go to
  370: 		  if ($direction eq 'forward') {
  371:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
  372: 		  }
  373: 		  if ($direction eq 'back') {
  374:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
  375: 		  }
  376: # ------------------------------------------------- Check for critical messages
  377: 		  if ((time-$env{'user.criticalcheck.time'})>300) {
  378:                      my @what=&Apache::lonnet::dump
  379:                                   ('critical',$env{'user.domain'},
  380:                                               $env{'user.name'});
  381:                      if ($what[0]) {
  382: 	                if (($what[0] ne 'con_lost') && 
  383:                             ($what[0]!~/^error\:/)) {
  384: 	                   $redirecturl='/adm/email?critical=display';
  385:                         }
  386:                      }
  387:                      &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
  388: 		  }
  389: 
  390: 		  &Apache::loncommon::content_type($r,'text/html');
  391:                   $r->header_out(Location => 
  392:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
  393:                   return REDIRECT;
  394: 	      } else {
  395: # --------------------------------------------------------- There was a problem
  396:                   &Apache::loncommon::content_type($r,'text/html');
  397:                   $r->send_http_header;
  398:                   if ($#possibilities>0) {
  399: 		      my $bodytag=
  400:                              &Apache::loncommon::bodytag('Multiple Resources');
  401:                      $r->print(<<ENDSTART);
  402: <head><title>Choose Next Location</title></head>
  403: $bodytag
  404: <h3>There are several possibilities of where to go next</h3>
  405: <p>
  406: Please click on the the resource you intend to access:
  407: <p>
  408: <table border=2>
  409: <tr><th>Title</th><th>Type</th></tr>
  410: ENDSTART
  411:                      foreach (@possibilities) {
  412:                         $r->print(
  413:                               '<tr><td><a href="'.
  414:                               $multichoicehash{'src_'.$_}.'?symb=' .
  415:                                   Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
  416:                               $multichoicehash{'title_'.$_}.
  417:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
  418: 			      '</td></tr>');
  419:                      }
  420:                      $r->print('</table></body></html>');
  421: 		     return OK;
  422:                   } else {
  423: 		      my $bodytag=&Apache::loncommon::bodytag('No Resource');
  424:                      $r->print(<<ENDNONE);
  425: <head><title>No Resource</title></head>
  426: $bodytag
  427: <h3>Next resource could not be identified.</h3>
  428: <p>You probably are at the <b>beginning</b> or the <b>end</b> of the 
  429: course.</p>
  430: <ul>
  431: <li><a href="/adm/flip?postdata=return:">Go Back</a></li>
  432: <li><a href="/adm/navmaps">Navigate Course Content</a></li>
  433: </ul>
  434: </body>
  435: </html>
  436: ENDNONE
  437:                      return OK;
  438: 	         }
  439: 	     }
  440: 	  } else {
  441: # ------------------------------------------------- Problem, could not tie hash
  442:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  443:               return HTTP_NOT_ACCEPTABLE; 
  444:           }
  445:       } else {
  446: # ---------------------------------------- No, could not determine where we are
  447: 	  if ( &Apache::lonnet::mod_perl_version() == 2 ) {
  448: 	      &Apache::lonnet::cleanenv();
  449: 	  }
  450: 	  $r->internal_redirect('/adm/ambiguous');
  451:       }
  452:   } else {
  453: # -------------------------- Class was not initialized or page fliped strangely
  454:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  455:       return HTTP_NOT_ACCEPTABLE; 
  456:   } 
  457: }
  458: 
  459: 1;
  460: __END__
  461: 
  462: 
  463: 
  464: 
  465: 
  466: 
  467: 

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