File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.13: download - view: text, annotated - select for diffs
Thu Nov 16 11:58:30 2000 UTC (23 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Parameter mechanism changes

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

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