Annotation of rat/lonpageflip.pm, revision 1.24

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

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