File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.10: download - view: text, annotated - select for diffs
Tue Oct 17 17:49:59 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Return location implemented

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

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