File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.16: download - view: text, annotated - select for diffs
Mon May 28 14:21:22 2001 UTC (22 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
No more up and down - makes no sense

    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 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: 	 my $last;
  164:          if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
  165:                     &GDBM_READER,0640)) {
  166: 	     $last=$hash{'last_known'};
  167:              untie(%hash);
  168:          }
  169:          if ($last) {
  170: 	     $currenturl='/res/'.(split(/\_\_\_/,$last))[1];
  171: 	 } else {
  172: 	     $r->content_type('text/html');
  173:              $r->header_out(Location => 
  174:                                'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
  175:              return REDIRECT;
  176:          }
  177:       }
  178: # ------------------------------------------- Do we have any idea where we are?
  179:       my $position;
  180:       if ($position=Apache::lonnet::symbread($currenturl)) {
  181: # ------------------------------------------------------------------------- Yes
  182: 	  my ($mapurl,$mapnum,$thisurl)=split(/\_\_\_/,$position);
  183:           $cachehash{$thisurl}=$mapnum;
  184: # ============================================================ Tie the big hash
  185:           if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  186:                         &GDBM_READER,0640)) {
  187:               my $rid=$hash{'map_pc_/res/'.$mapurl}.'.'.$mapnum;
  188: 
  189: # ------------------------------------------------- Move forward, backward, etc
  190: 
  191:               ($next,$mapurl)=&move($rid,$mapurl,$direction);
  192: # -------------------------------------- Do we have one and only one empty URL?
  193:               my $safecount=0;
  194:               while (($next) && ($next!~/\,/) && (!$hash{'src_'.$next})
  195:                      && ($safecount<25)) {
  196:                   ($next,$mapurl)=&move($next,$mapurl,$direction);
  197:                   $safecount++;
  198:               }
  199: # ----------------------------------------------------- Check out possibilities
  200:               if ($next) {
  201:                   @possibilities=split(/\,/,$next);
  202:                   if ($#possibilities==0) {
  203: # ---------------------------------------------- Only one possibility, redirect
  204: 	              $redirecturl=$hash{'src_'.$next};
  205:                       $cachehash{&Apache::lonnet::declutter($redirecturl)}
  206: 		                                 =(split(/\./,$next))[1];
  207:                   } else {
  208: # ------------------------ There are multiple possibilities for a next resource
  209:                       $multichoice=1;
  210:                       map {
  211: 			  $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
  212:                           $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
  213:                           $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
  214:                           $cachehash
  215:                             {&Apache::lonnet::declutter(
  216: 						      $multichoicehash
  217:                                                          {'src_'.$_}
  218:                                                        )}
  219: 		                                 =(split(/\./,$_))[1];
  220:                       } @possibilities;
  221:                   }
  222: 	      } else {
  223: # -------------------------------------------------------------- No place to go
  224:                   $multichoice=-1;
  225:               }
  226: # ----------------- The program must come past this point to untie the big hash
  227: 	      untie(%hash);
  228: # --------------------------------------------------------- Store position info
  229:               $cachehash{'last_direction'}=$direction;
  230:               $cachehash{'last_known'}=&Apache::lonnet::declutter($currenturl);
  231:               &Apache::lonnet::symblist($mapurl,%cachehash);
  232: # ============================================== Do not return before this line
  233:               if ($redirecturl) {
  234: # ----------------------------------------------------- There is a URL to go to
  235: 		  $r->content_type('text/html');
  236:                   $r->header_out(Location => 
  237:                                 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
  238:                   return REDIRECT;
  239: 	      } else {
  240: # --------------------------------------------------------- There was a problem
  241:                   $r->content_type('text/html');
  242:                   $r->send_http_header;
  243:                   if ($#possibilities>0) {
  244:                      $r->print(<<ENDSTART);
  245: <head><title>Choose Next Location</title></head>
  246: <body bgcolor="#FFFFFF">
  247: <h1>LON-CAPA</h1>
  248: There are several possibilities of where to go next.
  249: <p>
  250: Please click on the the resource you intend to access:
  251: <p>
  252: <table border=2>
  253: <tr><th>Title</th><th>Type</th></tr>
  254: ENDSTART
  255:                      map {
  256:                         $r->print(
  257:                               '<tr><td><a href="'.
  258:                               $multichoicehash{'src_'.$_}.'">'.
  259:                               $multichoicehash{'title_'.$_}.
  260:                               '</a></td><td>'.$multichoicehash{'type_'.$_}.
  261: 			      '</td></tr>');
  262:                      } @possibilities;
  263:                      $r->print('</table></body></html>');
  264: 		     return OK;
  265:                   } else {
  266:                      $r->print(<<ENDNONE);
  267: <head><title>Choose Next Location</title></head>
  268: <body bgcolor="#FFFFFF">
  269: <img src="/adm/lonKaputt/lonlogo_broken.gif" align=left>
  270: <h1>Sorry!</h1>
  271: <h2>Next resource could not be identified.</h2>
  272: </body>
  273: </html>
  274: ENDNONE
  275:                      return OK;
  276: 	         }
  277: 	     }
  278: 	  } else {
  279: # ------------------------------------------------- Problem, could not tie hash
  280:               $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
  281:               return HTTP_NOT_ACCEPTABLE; 
  282:           }
  283:       } else {
  284: # ---------------------------------------- No, could not determine where we are
  285:          $r->internal_redirect('/adm/ambiguous');
  286:       }
  287:   } else {
  288: # -------------------------- Class was not initialized or page fliped strangely
  289:       $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  290:       return HTTP_NOT_ACCEPTABLE; 
  291:   } 
  292: }
  293: 
  294: 1;
  295: __END__
  296: 
  297: 
  298: 
  299: 
  300: 
  301: 
  302: 

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