Annotation of rat/lonpageflip.pm, revision 1.44

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

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