File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.11: download - view: text, annotated - select for diffs
Tue Oct 10 20:34:09 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Now tries to respect link priorities

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

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