File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Jul 5 19:38:23 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
Handles /adm/wrapper and gives friendlier message and beginning and end
of course.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Page flip handler
    4: #
    5: # (Page Handler
    6: #
    7: # (TeX Content Handler
    8: #
    9: # 05/29/00,05/30 Gerd Kortemeyer)
   10: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   11: # 10/02 Gerd Kortemeyer)
   12: #
   13: # 10/03,10/05,10/06,10/07,10/09,10/10,10/11,10/16,10/17,
   14: # 11/14,11/16,
   15: # 10/01/01,05/01,05/28,07/05 Gerd Kortemeyer
   16: 
   17: package Apache::lonpageflip;
   18: 
   19: use strict;
   20: use Apache::Constants qw(:common :http REDIRECT);
   21: use Apache::lonnet();
   22: use HTML::TokeParser;
   23: use GDBM_File;
   24: 
   25: # ========================================================== Module Global Hash
   26:   
   27: my %hash;
   28: 
   29: sub addrid {
   30:     my ($current,$new,$condid)=@_;
   31:     unless ($condid) { $condid=0; }
   32:     if (&Apache::lonnet::allowed('bre',$hash{'src_'.$new})) {
   33: 	if ($current) {
   34: 	    $current.=','.$new;
   35:         } else {
   36:             $current=''.$new;
   37:         }
   38:     }
   39:     return $current;
   40: }
   41: 
   42: sub move {
   43:     my ($rid,$mapurl,$direction)=@_;
   44: 
   45:     my $next='';
   46: 
   47:               my $mincond=1;
   48:               my $posnext='';
   49:               if ($direction eq 'forward') {
   50: # --------------------------------------------------------------------- Forward
   51:                   if ($hash{'type_'.$rid} eq 'finish') {
   52: 	             $rid=$hash{'ids_/res/'.$mapurl}; 
   53:                   }
   54:                   map {
   55:                       my $thiscond=
   56:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
   57:                       if ($thiscond>=$mincond) {
   58: 		          if ($posnext) {
   59: 		             $posnext.=','.$_.':'.$thiscond;
   60:                           } else {
   61:                              $posnext=$_.':'.$thiscond;
   62: 		          }
   63:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
   64: 	              }
   65:                   } split(/\,/,$hash{'to_'.$rid});
   66:                   map {
   67:                       my ($linkid,$condval)=split(/\:/,$_);
   68:                       if ($condval>=$mincond) {
   69: 		          $next=&addrid($next,$hash{'goesto_'.$linkid},
   70:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
   71:                       }
   72:                   } split(/\,/,$posnext);
   73:                   if ($hash{'is_map_'.$next}) {
   74:                       if (
   75:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
   76: 			  $mapurl=$hash{'src_'.$next};
   77: 			  $next=$hash{'map_start_'.$hash{'src_'.$next}};
   78:                      }
   79:                   }
   80:               } elsif ($direction eq 'back') {
   81: # ------------------------------------------------------------------- Backwards
   82:                   if ($hash{'type_'.$rid} eq 'start') {
   83: 	             $rid=$hash{'ids_/res/'.$mapurl};
   84:                   }
   85:                   map {
   86:                       my $thiscond=
   87:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
   88:                       if ($thiscond>=$mincond) {
   89: 		          if ($posnext) {
   90: 		             $posnext.=','.$_.':'.$thiscond;
   91:                           } else {
   92:                              $posnext=$_.':'.$thiscond;
   93: 		          }
   94:                           if ($thiscond>$mincond) { $mincond=$thiscond; }
   95: 	              }
   96:                   } split(/\,/,$hash{'from_'.$rid});
   97:                   map {
   98:                       my ($linkid,$condval)=split(/\:/,$_);
   99:                       if ($condval>=$mincond) {
  100: 		          $next=&addrid($next,$hash{'comesfrom_'.$linkid},
  101:                                 $hash{'condid_'.$hash{'undercond_'.$linkid}});
  102:                       }
  103:                   } split(/\,/,$posnext);
  104:                   if ($hash{'is_map_'.$next}) {
  105:                       if (
  106:       $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
  107: 			  $mapurl=$hash{'src_'.$next};
  108: 			  $next=$hash{'map_finish_'.$hash{'src_'.$next}};
  109:                      }
  110:                   }
  111: 	      }
  112: 
  113:               return ($next,$mapurl);
  114: }
  115: 
  116: # ================================================================ Main Handler
  117: 
  118: sub handler {
  119:    my $r=shift;
  120: 
  121: # ------------------------------------------- Set document type for header only
  122: 
  123:   if ($r->header_only) {
  124:      $r->content_type('text/html');
  125:      $r->send_http_header;
  126:      return OK;
  127:   }
  128: 
  129:   my %cachehash=(); 
  130:   my $multichoice=0;
  131:   my %multichoicehash=();
  132:   my $redirecturl='';
  133:   my $next='';
  134:   my @possibilities=();
  135: 
  136:   if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
  137:       $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
  138:       my $direction=$1;
  139:       my $currenturl=$2;
  140:       if ($direction eq 'return') {
  141: # -------------------------------------------------------- Return to last known
  142:          my $last;
  143:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  144:                     &GDBM_READER,0640)) {
  145: 	     $last=$hash{'last_known'};
  146:              untie(%hash);
  147:          }
  148:          my $newloc;
  149:          if ($last) {
  150:             $newloc='/res/'.(split(/\_\_\_/,$last))[1];
  151:          } else {
  152: 	    $newloc='/adm/noidea.html';
  153:          }  
  154: 	 $r->content_type('text/html');
  155:          $r->header_out(Location => 
  156: 			'http://'.$ENV{'HTTP_HOST'}.$newloc);
  157:                                
  158:          return REDIRECT;
  159:       }
  160:       $currenturl=~s/^http\:\/\///;
  161:       $currenturl=~s/^[^\/]+//;
  162:       unless (($currenturl=~/^\/res\//) || 
  163:               ($currenturl=~/^\/adm\/wrapper\//))  {
  164: 	 my $last;
  165:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  166:                     &GDBM_READER,0640)) {
  167: 	     $last=$hash{'last_known'};
  168:              untie(%hash);
  169:          }
  170:          if ($last) {
  171: 	     $currenturl='/res/'.(split(/\_\_\_/,$last))[1];
  172: 	 } else {
  173: 	     $r->content_type('text/html');
  174:              $r->header_out(Location => 
  175:                                'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
  176:              return REDIRECT;
  177:          }
  178:       }
  179: # ------------------------------------------- Do we have any idea where we are?
  180:       my $position;
  181:       if ($position=Apache::lonnet::symbread($currenturl)) {
  182: # ------------------------------------------------------------------------- Yes
  183: 	  my ($mapurl,$mapnum,$thisurl)=split(/\_\_\_/,$position);
  184:           $cachehash{$thisurl}=$mapnum;
  185: # ============================================================ Tie the big hash
  186:           if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  187:                         &GDBM_READER,0640)) {
  188:               my $rid=$hash{'map_pc_/res/'.$mapurl}.'.'.$mapnum;
  189: 
  190: # ------------------------------------------------- Move forward, backward, etc
  191: 
  192:               ($next,$mapurl)=&move($rid,$mapurl,$direction);
  193: # -------------------------------------- Do we have one and only one empty URL?
  194:               my $safecount=0;
  195:               while (($next) && ($next!~/\,/) && (!$hash{'src_'.$next})
  196:                      && ($safecount<25)) {
  197:                   ($next,$mapurl)=&move($next,$mapurl,$direction);
  198:                   $safecount++;
  199:               }
  200: # ----------------------------------------------------- Check out possibilities
  201:               if ($next) {
  202:                   @possibilities=split(/\,/,$next);
  203:                   if ($#possibilities==0) {
  204: # ---------------------------------------------- Only one possibility, redirect
  205: 	              $redirecturl=$hash{'src_'.$next};
  206:                       $cachehash{&Apache::lonnet::declutter($redirecturl)}
  207: 		                                 =(split(/\./,$next))[1];
  208:                   } else {
  209: # ------------------------ There are multiple possibilities for a next resource
  210:                       $multichoice=1;
  211:                       map {
  212: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
  213:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
  214:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
  215:                           $cachehash
  216:                             {&Apache::lonnet::declutter(
  217: 						      $multichoicehash
  218:                                                          {'src_'.$_}
  219:                                                        )}
  220: 		                                 =(split(/\./,$_))[1];
  221:                       } @possibilities;
  222:                   }
  223: 	      } else {
  224: # -------------------------------------------------------------- No place to go
  225:                   $multichoice=-1;
  226:               }
  227: # ----------------- The program must come past this point to untie the big hash
  228: 	      untie(%hash);
  229: # --------------------------------------------------------- Store position info
  230:               $cachehash{'last_direction'}=$direction;
  231:               $cachehash{'last_known'}=&Apache::lonnet::declutter($currenturl);
  232:               &Apache::lonnet::symblist($mapurl,%cachehash);
  233: # ============================================== Do not return before this line
  234:               if ($redirecturl) {
  235: # ----------------------------------------------------- There is a URL to go to
  236: 		  $r->content_type('text/html');
  237:                   $r->header_out(Location => 
  238:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
  239:                   return REDIRECT;
  240: 	      } else {
  241: # --------------------------------------------------------- There was a problem
  242:                   $r->content_type('text/html');
  243:                   $r->send_http_header;
  244:                   if ($#possibilities>0) {
  245:                      $r->print(<<ENDSTART);
  246: <head><title>Choose Next Location</title></head>
  247: <body bgcolor="#FFFFFF">
  248: <h1>LON-CAPA</h1>
  249: There are several possibilities of where to go next.
  250: <p>
  251: Please click on the the resource you intend to access:
  252: <p>
  253: <table border=2>
  254: <tr><th>Title</th><th>Type</th></tr>
  255: ENDSTART
  256:                      map {
  257:                         $r->print(
  258:                               '<tr><td><a href="'.
  259:                               $multichoicehash{'src_'.$_}.'">'.
  260:                               $multichoicehash{'title_'.$_}.
  261:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
  262: 			      '</td></tr>');
  263:                      } @possibilities;
  264:                      $r->print('</table></body></html>');
  265: 		     return OK;
  266:                   } else {
  267:                      $r->print(<<ENDNONE);
  268: <head><title>Choose Next Location</title></head>
  269: <body bgcolor="#FFFFFF">
  270: <img src="/adm/lonIcons/lonlogos.gif" align=right>
  271: <h1>Sorry!</h1>
  272: <h2>Next resource could not be identified.</h2>
  273: <h3>You probably are at the beginning or the end of the course.</h3>
  274: </body>
  275: </html>
  276: ENDNONE
  277:                      return OK;
  278: 	         }
  279: 	     }
  280: 	  } else {
  281: # ------------------------------------------------- Problem, could not tie hash
  282:               $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  283:               return HTTP_NOT_ACCEPTABLE; 
  284:           }
  285:       } else {
  286: # ---------------------------------------- No, could not determine where we are
  287:          $r->internal_redirect('/adm/ambiguous');
  288:       }
  289:   } else {
  290: # -------------------------- Class was not initialized or page fliped strangely
  291:       $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  292:       return HTTP_NOT_ACCEPTABLE; 
  293:   } 
  294: }
  295: 
  296: 1;
  297: __END__
  298: 
  299: 
  300: 
  301: 
  302: 
  303: 
  304: 

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