File:  [LON-CAPA] / rat / lonpageflip.pm
Revision 1.2: download - view: text, annotated - select for diffs
Thu Oct 5 22:14:11 2000 UTC (23 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Further work on lonpageflip

    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 Gerd Kortemeyer
   14: 
   15: package Apache::lonpageflip;
   16: 
   17: use strict;
   18: use Apache::Constants qw(:common :http);
   19: use Apache::lonnet();
   20: use HTML::TokeParser;
   21: use GDBM_File;
   22: 
   23: # -------------------------------------------------------------- Module Globals
   24: my %hash;
   25: my @rows;
   26: 
   27: # ------------------------------------------------------------------ Euclid gcd
   28: 
   29: sub euclid {
   30:     my ($e,$f)=@_;
   31:     my $a; my $b; my $r;
   32:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   33:     while ($r!=0) {
   34: 	$a=$b; $b=$r;
   35:         $r=$a%$b;
   36:     }
   37:     return $b;
   38: }
   39: 
   40: # ------------------------------------------------------------ Build page table
   41: 
   42: sub tracetable {
   43:     my ($sofar,$rid,$beenhere)=@_;
   44:     my $further=$sofar;
   45:     unless ($beenhere=~/\&$rid\&/) {
   46:        $beenhere.=$rid.'&';  
   47: 
   48:        if (defined($hash{'is_map_'.$rid})) {
   49:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   50:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   51:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   52: 	      $sofar=
   53:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   54:                 '&'.$frid.'&');
   55:               $sofar++;
   56:               if ($hash{'src_'.$frid}) {
   57:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   58:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   59:                  if (defined($rows[$sofar])) {
   60:                    $rows[$sofar].='&'.$frid;
   61:                  } else {
   62:                    $rows[$sofar]=$frid;
   63:                  }
   64: 	       }
   65: 	      }
   66: 	   }
   67:        } else {
   68:           $sofar++;
   69:           if ($hash{'src_'.$rid}) {
   70:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   71:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   72:              if (defined($rows[$sofar])) {
   73:                $rows[$sofar].='&'.$rid;
   74:              } else {
   75:                $rows[$sofar]=$rid;
   76:              }
   77: 	   }
   78:           }
   79:        }
   80: 
   81:        if (defined($hash{'to_'.$rid})) {
   82:           map {
   83:               my $now=&tracetable($sofar,$hash{'goesto_'.$_},$beenhere);
   84:               if ($now>$further) { $further=$now; }
   85:           } split(/\,/,$hash{'to_'.$rid});
   86:        }
   87:     }
   88:     return $further;
   89: }
   90: 
   91: # ================================================================ Main Handler
   92: 
   93: sub handler {
   94:    my $r=shift;
   95: 
   96: # ------------------------------------------- Set document type for header only
   97: 
   98:   if ($r->header_only) {
   99:      $r->content_type('text/html');
  100:      $r->send_http_header;
  101:      return OK;
  102:   }
  103: 
  104: 
  105: # --------BEGIN DEBUG ONLY TRASH
  106:     $r->content_type('text/html');
  107:      $r->send_http_header;
  108: 
  109:   $r->print('<html><body>');
  110: # --------END DEBUG ONLY TRASH
  111: 
  112: 
  113:   if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
  114:       $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
  115:       my $direction=$1;
  116:       my $currenturl=$2;
  117:       my $redirecturl=$currenturl;
  118:       $currenturl=~s/^http\:\/\///;
  119:       $currenturl=~s/^[^\/]+//;
  120:       $currenturl=Apache::lonnet::declutter($currenturl);
  121: # ---------------------------------------------------------------- Tie database
  122:       if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
  123:                       &GDBM_READER,0640)) {
  124: # ---------------------------------------------- Get ID(s) for current resource
  125:          my $syval=$hash{'ids_/res/'.$currenturl};
  126:          if ($syval) {
  127: # ------------------------------------------------------------------- Has ID(s)
  128:              my @possiblities=split(/\,/,$syval);
  129:              $r->print($direction.' '.$currenturl.' '.$syval);
  130: 
  131:          } else {
  132: # --------------------------------------------------------- Does not have ID(s)
  133:              $r->print('Weird');
  134:          }
  135: # ------------------------------------- Program needs to get here to untie hash
  136:          unless(untie(%hash)) {
  137:             $r->log_reason('Could not untie database hash '.
  138: 			 $ENV{'request.course.fn'}.' for '.$ENV{'user.name'}.
  139:                          ' at '.$ENV{'user.domain'});
  140:             return HTTP_SERVICE_UNAVAILABLE;
  141: 	 }
  142: # ------------------------------------------------------------ Now do something
  143:       } else {
  144: # ----------------------------------------- Serious problem, could not tie hash
  145:           $r->log_reason('Could not tie database hash '.
  146: 			 $ENV{'request.course.fn'}.' for '.$ENV{'user.name'}.
  147:                          ' at '.$ENV{'user.domain'});
  148:           return HTTP_SERVICE_UNAVAILABLE;
  149:       }
  150: # -------------------------- Class was not initialized or page fliped strangely
  151:   } else {
  152:       $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
  153:       return HTTP_NOT_ACCEPTABLE; 
  154:   } 
  155: 
  156: 
  157: 
  158: 
  159: # ------TRASH BELOW
  160: # --------------------------- TRASH
  161: 
  162:   $r->print('</body></html>');
  163:   return OK;
  164: 
  165: 
  166: # ========================================================= TOTAL TRASH
  167: 
  168:   my $requrl=$r->uri;
  169: # ----------------------------------------------------------------- Tie db file
  170:   if ($ENV{'request.course.fn'}) {
  171:       my $fn=$ENV{'request.course.fn'};
  172:       if (-e "$fn.db") {
  173:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  174: # ------------------------------------------------------------------- Hash tied
  175:               my $firstres=$hash{'map_start_'.$requrl};
  176:               my $lastres=$hash{'map_finish_'.$requrl};
  177:               if (($firstres) && ($lastres)) {
  178: # ----------------------------------------------------------------- Render page
  179: 
  180:                   @rows=();
  181: 
  182:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  183:                   if ($hash{'src_'.$lastres}) {
  184:                      my $brepriv=
  185:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  186:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  187:                         $rows[$#rows+1]=''.$lastres;
  188: 		     }
  189: 		  }
  190: 
  191: # ------------------------------------------------------------ Add to symb list
  192: 
  193:                   my $i;
  194:                   my %symbhash=();
  195:                   for ($i=0;$i<=$#rows;$i++) {
  196: 		     if ($rows[$i]) {
  197:                         my @colcont=split(/\&/,$rows[$i]);
  198:                         map {
  199:                            $symbhash{$hash{'src_'.$_}}='';
  200: 		        } @colcont;
  201: 		     }
  202: 		  }
  203:                   &Apache::lonnet::symblist($requrl,%symbhash);
  204: 
  205: # ------------------------------------------------------------------ Page parms
  206: 
  207:                   my $j;
  208:                   my $lcm=1;
  209:                   my $contents=0;
  210:                   my $nforms=0;
  211:                   
  212:                   my %ssibody=();
  213:                   my %ssibgcolor=();
  214:                   my %ssitext=();
  215:                   my %ssilink=();
  216:                   my %ssivlink=();
  217:                   my %ssialink=();
  218:                   my %cellemb=();
  219: 
  220:                   my $allscript='';
  221:                   my $allmeta='';
  222: 
  223:                   my $isxml=0;
  224:                   my $xmlheader='';
  225:                   my $xmlbody='';
  226: 
  227: # --------------------------------------------- Get SSI output, post parameters
  228: 
  229:                   for ($i=0;$i<=$#rows;$i++) {
  230: 		     if ($rows[$i]) {
  231: 		      $contents++;
  232:                       my @colcont=split(/\&/,$rows[$i]);
  233:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  234:                       map {
  235:                           my $src=$hash{'src_'.$_};
  236:                           $src=~/\.(\w+)$/;
  237:                           $cellemb{$_}=Apache::lonnet::fileembstyle($1);
  238:                           if ($cellemb{$_} eq 'ssi') {
  239: # --------------------------------------------------------- This is an SSI cell
  240: 			      my $prefix=$_.'_';
  241:                               my %posthash=('request.prefix' => $prefix);
  242:                               if (($ENV{'form.'.$prefix.'submit'}) 
  243:                                || ($ENV{'form.all_submit'})) {
  244:                                map {
  245: 				  if ($_=~/^form.$prefix/) {
  246: 				      my $name=$_;
  247:                                       $name=~s/^form.$prefix//;
  248:                                       $posthash{$name}=$ENV{$_};
  249:                                   }
  250:                                } keys %ENV;
  251: 			      }
  252:                               my $output=Apache::lonnet::ssi($src,%posthash);
  253:                               my $parser=HTML::TokeParser->new(\$output);
  254:                               my $token;
  255:                               my $bodydef=0;
  256:                               my $thisxml=0;
  257:                               if ($output=~/\?xml/) {
  258:                                  $isxml=1;
  259:                                  $thisxml=1;
  260:                                  $output=~
  261:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  262:                                  $xmlheader=$1;
  263: 			      }
  264:                               while (($bodydef==0) &&
  265:                                      ($token=$parser->get_token)) {
  266: 				  if ($token->[1] eq 'body') {
  267: 				      $bodydef=1;
  268:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  269:                                       $ssitext{$_}=$token->[2]->{'text'};
  270:                                       $ssilink{$_}=$token->[2]->{'link'};
  271:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  272:                                       $ssialink{$_}=$token->[2]->{'alink'};
  273:                                       if ($thisxml) {
  274: 					  $xmlbody=$token->[4];
  275:                                       }
  276:                                   }
  277:                                   if ($token->[1] eq 'meta') {
  278: 				      $allmeta.="\n".$token->[4].'</meta>';
  279:                                   }
  280:                                   if ($token->[1] eq 'script') {
  281: 				      $allscript.="\n\n"
  282:                                                 .$parser->get_text('/script');
  283:                                   }
  284:                               }
  285:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  286:                                  $output=$1; 
  287:                               }
  288:                               $output=~s/\<\/body\>.*//si;
  289:                               if ($output=~/\<form/si) {
  290: 				  $nforms++;
  291:                                   $output=~s/\<form[^\>]*\>//gsi;
  292:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  293:                               }
  294: 			      $ssibody{$_}=$output;
  295: 
  296: # ---------------------------------------------------------------- End SSI cell
  297:                           }
  298:                       } @colcont;
  299:                      } 
  300:                   }
  301:                   unless ($contents) {
  302:                       $r->content_type('text/html');
  303:                       $r->send_http_header;
  304:                       $r->print('<html><body>Empty page.</body></html>');
  305:                   } else {
  306: # ------------------------------------------------------------------ Build page
  307: 
  308: # ---------------------------------------------------------------- Send headers
  309:                       if ($isxml) {
  310: 			  $r->content_type('text/xml');
  311:                           $r->send_http_header;
  312:                           $r->print($xmlheader);
  313: 		      } else {
  314:                           $r->content_type('text/html');
  315:                           $r->send_http_header;
  316:                           $r->print('<html>');
  317: 		      }
  318: # ------------------------------------------------------------------------ Head
  319:                       $r->print("\n<head>\n".$allmeta);
  320:                       if ($allscript) {
  321: 			  $r->print("\n<script>\n".$allscript."\n</script>\n");
  322:                       }
  323:                       $r->print("\n</head>\n");
  324: # ------------------------------------------------------------------ Start body
  325:                       if ($isxml) {
  326:                           $r->print($xmlbody);
  327:                       } else {
  328: 			  $r->print('<body bgcolor="#FFFFFF">');
  329:                       }
  330: # ------------------------------------------------------------------ Start form
  331:                       if ($nforms) {
  332: 			  $r->print('<form method="post" action="'.
  333: 				    $requrl.'">');
  334:                       }
  335: # ----------------------------------------------------------------- Start table
  336:                       $r->print('<table cols="'.$lcm.'" border="0">');
  337:                       for ($i=0;$i<=$#rows;$i++) {
  338: 			if ($rows[$i]) {
  339:                           $r->print("\n<tr>");
  340:                           my @colcont=split(/\&/,$rows[$i]);
  341:                           my $avespan=$lcm/($#colcont+1);
  342:                           for ($j=0;$j<=$#colcont;$j++) {
  343:                               my $rid=$colcont[$j];
  344:                               $r->print('<td colspan="'.$avespan.'"');
  345:                               if ($cellemb{$rid} eq 'ssi') {
  346: 				  if ($ssibgcolor{$rid}) {
  347:                                      $r->print(' bgcolor="'.
  348:                                                $ssibgcolor{$rid}.'"');
  349:                                   }
  350:                                   $r->print('><font');
  351:                                   if ($ssitext{$rid}) {
  352: 				     $r->print(' text="'.$ssitext{$rid}.'"');
  353:                                   }
  354:                                   if ($ssilink{$rid}) {
  355: 				     $r->print(' link="'.$ssilink{$rid}.'"');
  356:                                   }
  357:                                   if ($ssitext{$rid}) {
  358: 				     $r->print(' vlink="'.$ssivlink{$rid}.'"');
  359:                                   }
  360:                                   if ($ssialink{$rid}) {
  361: 				     $r->print(' alink="'.$ssialink{$rid}.'"');
  362:                                   }
  363:                             
  364:                                   $r->print('>'.$ssibody{$rid}.'</font>');
  365:                               } elsif ($cellemb{$rid} eq 'img') {
  366:                                   $r->print('><img src="'.
  367:                                     $hash{'src_'.$rid}.'"></img>');
  368: 			      }
  369:                               $r->print('</td>');
  370:                           }
  371:                           $r->print('</tr>');
  372: 		        }
  373:                       }
  374:                       $r->print("\n</table>");
  375: # ---------------------------------------------------------------- Submit, etc.
  376:                       if ($nforms) {
  377:                           $r->print(
  378: 	                  '<input name="all_submit" value="Submit All" type="'.
  379: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  380:                       }
  381:                       $r->print('</body></html>');
  382: # -------------------------------------------------------------------- End page
  383:                   }                  
  384: # ------------------------------------------------------------- End render page
  385:               } else {
  386:                   $r->content_type('text/html');
  387:                   $r->send_http_header;
  388: 		  $r->print('<html><body>Page undefined.</body></html>');
  389:               }
  390: # ------------------------------------------------------------------ Untie hash
  391:               unless (untie(%hash)) {
  392:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  393:                        "Could not untie coursemap $fn (browse).</font>"); 
  394:               }
  395: # -------------------------------------------------------------------- All done
  396: 	      return OK;
  397: # ----------------------------------------------- Errors, hash could no be tied
  398:           }
  399:       } 
  400:   }
  401:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  402:   return HTTP_NOT_ACCEPTABLE; 
  403: }
  404: 
  405: 1;
  406: __END__
  407: 
  408: 
  409: 
  410: 
  411: 
  412: 
  413: 

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