Annotation of rat/lonpageflip.pm, revision 1.53

1.1       www         1: # The LearningOnline Network with CAPA
                      2: #
                      3: # Page flip handler
                      4: #
1.53    ! albertel    5: # $Id: lonpageflip.pm,v 1.52 2005/03/01 00:22:57 albertel Exp $
1.18      www         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: #
1.1       www        29: 
                     30: package Apache::lonpageflip;
                     31: 
                     32: use strict;
1.4       www        33: use Apache::Constants qw(:common :http REDIRECT);
1.53    ! albertel   34: use Apache::lonnet;
1.1       www        35: use HTML::TokeParser;
                     36: use GDBM_File;
                     37: 
1.3       www        38: # ========================================================== Module Global Hash
                     39:   
1.1       www        40: my %hash;
1.34      www        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: }
1.1       www        50: 
1.3       www        51: sub addrid {
1.4       www        52:     my ($current,$new,$condid)=@_;
                     53:     unless ($condid) { $condid=0; }
1.27      www        54: 
1.3       www        55: 	if ($current) {
1.5       www        56: 	    $current.=','.$new;
1.3       www        57:         } else {
1.5       www        58:             $current=''.$new;
1.3       www        59:         }
1.27      www        60: 
1.3       www        61:     return $current;
1.25      www        62: }
                     63: 
                     64: sub fullmove {
                     65:     my ($rid,$mapurl,$direction)=@_;
1.53    ! albertel   66:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.28      albertel   67:                         &GDBM_READER(),0640)) {
1.25      www        68: 	($rid,$mapurl)=&move($rid,$mapurl,$direction);
                     69:         untie(%hash);
                     70:     }
                     71:     return($rid,$mapurl);
1.1       www        72: }
                     73: 
1.50      albertel   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: 
1.15      www        82: sub move {
                     83:     my ($rid,$mapurl,$direction)=@_;
1.23      www        84:     my $startoutrid=$rid;
1.15      www        85: 
                     86:     my $next='';
                     87: 
                     88:               my $mincond=1;
                     89:               my $posnext='';
                     90:               if ($direction eq 'forward') {
                     91: # --------------------------------------------------------------------- Forward
1.33      www        92:                   while ($hash{'type_'.$rid} eq 'finish') {
                     93: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
1.15      www        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}) {
1.23      www       115: # This jumps to the beginning of a new map (going down level)
1.15      www       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}};
1.47      raeburn   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]};
1.15      www       124:                      }
1.23      www       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]};
1.15      www       129:                   }
                    130:               } elsif ($direction eq 'back') {
                    131: # ------------------------------------------------------------------- Backwards
1.33      www       132:                  while ($hash{'type_'.$rid} eq 'start') {
                    133: 	             $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
                    134: 		 }
1.15      www       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}) {
1.24      www       155: # This jumps to the end of a new map (going down one level)
1.15      www       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}};
1.47      raeburn   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:                       }
1.24      www       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]};
1.15      www       169:                   }
                    170: 	      }
                    171:               return ($next,$mapurl);
                    172: }
                    173: 
1.1       www       174: # ================================================================ Main Handler
                    175: 
                    176: sub handler {
1.2       www       177:    my $r=shift;
1.1       www       178: 
                    179: # ------------------------------------------- Set document type for header only
                    180: 
1.2       www       181:   if ($r->header_only) {
1.51      albertel  182:       &Apache::loncommon::content_type($r,'text/html');
                    183:       $r->send_http_header;
                    184:       return OK;
1.2       www       185:   }
                    186: 
1.5       www       187:   my %cachehash=(); 
                    188:   my $multichoice=0;
                    189:   my %multichoicehash=();
1.4       www       190:   my $redirecturl='';
                    191:   my $next='';
                    192:   my @possibilities=();
1.37      www       193:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
1.53    ! albertel  194:   if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
        !           195:       $env{'form.postdata'}=~/(\w+)\:(.*)/;
1.2       www       196:       my $direction=$1;
1.40      www       197:       my $currenturl=$2;
1.50      albertel  198:       if ($currenturl=~m|^/enc/|) {
                    199: 	  $currenturl=&Apache::lonenc::unencrypted($currenturl);
                    200:       }
1.46      www       201:       $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
1.10      www       202:       if ($direction eq 'return') {
                    203: # -------------------------------------------------------- Return to last known
                    204:          my $last;
1.53    ! albertel  205:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.28      albertel  206:                     &GDBM_READER(),0640)) {
1.10      www       207: 	     $last=$hash{'last_known'};
                    208:              untie(%hash);
                    209:          }
                    210:          my $newloc;
1.53    ! albertel  211:          if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.36      www       212:                         &GDBM_READER(),0640))) {
1.52      albertel  213:             my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
1.50      albertel  214: 	    $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
                    215: 	    $newloc=$hash{'src_'.$id};
                    216: 	    if ($newloc) {
                    217: 		if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
                    218: 			      
                    219: 	    } else {
                    220: 		$newloc='/adm/noidea.html';
                    221: 	    }
1.36      www       222:             untie %hash;
1.10      www       223:          } else {
                    224: 	    $newloc='/adm/noidea.html';
                    225:          }  
1.51      albertel  226: 	 &Apache::loncommon::content_type($r,'text/html');
1.10      www       227:          $r->header_out(Location => 
                    228: 			'http://'.$ENV{'HTTP_HOST'}.$newloc);
                    229:                                
                    230:          return REDIRECT;
                    231:       }
1.2       www       232:       $currenturl=~s/^http\:\/\///;
                    233:       $currenturl=~s/^[^\/]+//;
1.35      www       234: #
                    235: # Is the current URL on the map? If not, start with last known URL
                    236: #
                    237:       unless (&Apache::lonnet::is_on_map($currenturl)) {
1.7       www       238: 	 my $last;
1.53    ! albertel  239:          if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.28      albertel  240:                     &GDBM_READER(),0640)) {
1.7       www       241: 	     $last=$hash{'last_known'};
                    242:              untie(%hash);
                    243:          }
                    244:          if ($last) {
1.52      albertel  245: 	     $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
1.7       www       246: 	 } else {
1.51      albertel  247: 	     &Apache::loncommon::content_type($r,'text/html');
1.7       www       248:              $r->header_out(Location => 
                    249:                                'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
1.9       www       250:              return REDIRECT;
1.7       www       251:          }
                    252:       }
1.3       www       253: # ------------------------------------------- Do we have any idea where we are?
                    254:       my $position;
                    255:       if ($position=Apache::lonnet::symbread($currenturl)) {
                    256: # ------------------------------------------------------------------------- Yes
1.41      www       257: 	  my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
1.52      albertel  258:           $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
1.23      www       259:           $cachehash{$startoutmap}{'last_known'}=
1.52      albertel  260:                              [&Apache::lonnet::declutter($currenturl),$mapnum];
1.20      albertel  261: 
1.5       www       262: # ============================================================ Tie the big hash
1.53    ! albertel  263:           if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.28      albertel  264:                         &GDBM_READER(),0640)) {
1.29      www       265:               my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
                    266:                       '.'.$mapnum;
1.14      www       267: 
1.15      www       268: # ------------------------------------------------- Move forward, backward, etc
1.22      www       269:               my $endupmap;
                    270:               ($next,$endupmap)=&move($rid,$startoutmap,$direction);
1.15      www       271: # -------------------------------------- Do we have one and only one empty URL?
                    272:               my $safecount=0;
1.26      www       273:               while (($next) && ($next!~/\,/) && 
1.48      albertel  274:                      ((!$hash{'src_'.$next}) || 
1.53    ! albertel  275: 		      ((!$env{'request.role.adv'}) && $hash{'randomout_'.$next}))
1.26      www       276:                      && ($safecount<10000)) {
1.22      www       277:                   ($next,$endupmap)=&move($next,$endupmap,$direction);
1.15      www       278:                   $safecount++;
                    279:               }
1.22      www       280: # We are now at at least one non-empty URL
1.4       www       281: # ----------------------------------------------------- Check out possibilities
                    282:               if ($next) {
                    283:                   @possibilities=split(/\,/,$next);
                    284:                   if ($#possibilities==0) {
1.5       www       285: # ---------------------------------------------- Only one possibility, redirect
1.50      albertel  286: 	              $redirecturl=&hash_src($next);
1.52      albertel  287:                       $cachehash{$endupmap}{$redirecturl}=
                    288: 			  [$redirecturl,(split(/\./,$next))[1]];
1.4       www       289:                   } else {
1.5       www       290: # ------------------------ There are multiple possibilities for a next resource
                    291:                       $multichoice=1;
                    292:                       map {
                    293: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
                    294:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
1.6       www       295:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
1.32      bowersj2  296:                           (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
                    297:                           my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
                    298:                           $multichoicehash{'symb_'.$_} = 
                    299:                               Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
                    300:                                                         $second.'___'.$symbSrc);
                    301:                                                          
1.22      www       302:                           my ($choicemap,$choiceres)=split(/\./,$_);
1.52      albertel  303: 			  my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
                    304: 			  my $url=$multichoicehash{'src_'.$_};
                    305:                           $cachehash{$map}{$url}=[$url,$choiceres];
1.5       www       306:                       } @possibilities;
1.4       www       307:                   }
1.5       www       308: 	      } else {
                    309: # -------------------------------------------------------------- No place to go
                    310:                   $multichoice=-1;
1.4       www       311:               }
1.5       www       312: # ----------------- The program must come past this point to untie the big hash
1.3       www       313: 	      untie(%hash);
1.5       www       314: # --------------------------------------------------------- Store position info
1.52      albertel  315:               $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
1.19      www       316:               foreach my $thismap (keys %cachehash) {
1.52      albertel  317: 		  my $mapnum=$cachehash{$thismap}->{'mapnum'};
                    318: 		  delete($cachehash{$thismap}->{'mapnum'});
                    319: 		  &Apache::lonnet::symblist($thismap,
                    320: 					    %{$cachehash{$thismap}});
1.19      www       321: 	      }
1.5       www       322: # ============================================== Do not return before this line
1.4       www       323:               if ($redirecturl) {
1.5       www       324: # ----------------------------------------------------- There is a URL to go to
1.38      www       325: 		  if ($direction eq 'forward') {
                    326:                      &Apache::lonnet::linklog($currenturl,$redirecturl);
                    327: 		  }
                    328: 		  if ($direction eq 'back') {
                    329:                      &Apache::lonnet::linklog($redirecturl,$currenturl);
                    330: 		  }
1.31      www       331: # ------------------------------------------------- Check for critical messages
1.53    ! albertel  332: 		  if ((time-$env{'user.criticalcheck.time'})>300) {
1.31      www       333:                      my @what=&Apache::lonnet::dump
1.53    ! albertel  334:                                   ('critical',$env{'user.domain'},
        !           335:                                               $env{'user.name'});
1.31      www       336:                      if ($what[0]) {
                    337: 	                if (($what[0] ne 'con_lost') && 
                    338:                             ($what[0]!~/^error\:/)) {
                    339: 	                   $redirecturl='/adm/email?critical=display';
                    340:                         }
                    341:                      }
                    342:                      &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
                    343: 		  }
                    344: 
1.51      albertel  345: 		  &Apache::loncommon::content_type($r,'text/html');
1.4       www       346:                   $r->header_out(Location => 
                    347:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
                    348:                   return REDIRECT;
1.5       www       349: 	      } else {
                    350: # --------------------------------------------------------- There was a problem
1.51      albertel  351:                   &Apache::loncommon::content_type($r,'text/html');
1.8       www       352:                   $r->send_http_header;
                    353:                   if ($#possibilities>0) {
1.37      www       354: 		      my $bodytag=
                    355:                              &Apache::loncommon::bodytag('Multiple Resources');
1.8       www       356:                      $r->print(<<ENDSTART);
                    357: <head><title>Choose Next Location</title></head>
1.37      www       358: $bodytag
                    359: <h3>There are several possibilities of where to go next</h3>
1.8       www       360: <p>
                    361: Please click on the the resource you intend to access:
                    362: <p>
                    363: <table border=2>
                    364: <tr><th>Title</th><th>Type</th></tr>
                    365: ENDSTART
1.26      www       366:                      foreach (@possibilities) {
1.8       www       367:                         $r->print(
                    368:                               '<tr><td><a href="'.
1.32      bowersj2  369:                               $multichoicehash{'src_'.$_}.'?symb=' .
                    370:                                   Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
1.8       www       371:                               $multichoicehash{'title_'.$_}.
                    372:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
                    373: 			      '</td></tr>');
1.26      www       374:                      }
1.8       www       375:                      $r->print('</table></body></html>');
                    376: 		     return OK;
                    377:                   } else {
1.37      www       378: 		      my $bodytag=&Apache::loncommon::bodytag('No Resource');
1.8       www       379:                      $r->print(<<ENDNONE);
1.37      www       380: <head><title>No Resource</title></head>
                    381: $bodytag
                    382: <h3>Next resource could not be identified.</h3>
                    383: <p>You probably are at the <b>beginning</b> or the <b>end</b> of the 
                    384: course.</p>
                    385: <ul>
                    386: <li><a href="/adm/flip?postdata=return:">Go Back</a></li>
                    387: <li><a href="/adm/navmaps">Navigate Course Content</a></li>
                    388: </ul>
1.8       www       389: </body>
                    390: </html>
                    391: ENDNONE
                    392:                      return OK;
                    393: 	         }
                    394: 	     }
1.5       www       395: 	  } else {
                    396: # ------------------------------------------------- Problem, could not tie hash
1.53    ! albertel  397:               $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
1.5       www       398:               return HTTP_NOT_ACCEPTABLE; 
1.3       www       399:           }
1.5       www       400:       } else {
                    401: # ---------------------------------------- No, could not determine where we are
1.45      albertel  402: 	  if ( &Apache::lonnet::mod_perl_version() == 2 ) {
1.42      albertel  403: 	      &Apache::lonnet::cleanenv();
                    404: 	  }
                    405: 	  $r->internal_redirect('/adm/ambiguous');
1.2       www       406:       }
1.5       www       407:   } else {
1.2       www       408: # -------------------------- Class was not initialized or page fliped strangely
1.53    ! albertel  409:       $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
1.2       www       410:       return HTTP_NOT_ACCEPTABLE; 
                    411:   } 
1.1       www       412: }
                    413: 
                    414: 1;
                    415: __END__
                    416: 
                    417: 
                    418: 
                    419: 
                    420: 
                    421: 
                    422: 

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