File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.32: download - view: text, annotated - select for diffs
Wed Mar 6 15:00:55 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor change for lonxml::registerurl calls.  This should affect nothing!

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # $Id: lonpage.pm,v 1.32 2002/03/06 15:00:55 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (TeX Content Handler
   29: #
   30: # YEAR=2000
   31: # 05/29/00,05/30 Gerd Kortemeyer)
   32: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   33: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
   34: # YEAR=2001
   35: # 08/13/01,08/30,10/1 Gerd Kortemeyer
   36: # 12/16 Scott Harrison
   37: #
   38: ###
   39: 
   40: package Apache::lonpage;
   41: 
   42: use strict;
   43: use Apache::Constants qw(:common :http);
   44: use Apache::lonnet();
   45: use Apache::loncommon();
   46: use Apache::lonxml();
   47: use HTML::TokeParser;
   48: use GDBM_File;
   49: 
   50: # -------------------------------------------------------------- Module Globals
   51: my %hash;
   52: my @rows;
   53: 
   54: # ------------------------------------------------------------------ Euclid gcd
   55: 
   56: sub euclid {
   57:     my ($e,$f)=@_;
   58:     my $a; my $b; my $r;
   59:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   60:     while ($r!=0) {
   61: 	$a=$b; $b=$r;
   62:         $r=$a%$b;
   63:     }
   64:     return $b;
   65: }
   66: 
   67: # ------------------------------------------------------------ Build page table
   68: 
   69: sub tracetable {
   70:     my ($sofar,$rid,$beenhere)=@_;
   71:     my $further=$sofar;
   72:     unless ($beenhere=~/\&$rid\&/) {
   73:        $beenhere.=$rid.'&';  
   74: 
   75:        if (defined($hash{'is_map_'.$rid})) {
   76:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   77:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   78:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   79: 	      $sofar=
   80:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   81:                 '&'.$frid.'&');
   82:               $sofar++;
   83:               if ($hash{'src_'.$frid}) {
   84:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   85:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   86:                  if (defined($rows[$sofar])) {
   87:                    $rows[$sofar].='&'.$frid;
   88:                  } else {
   89:                    $rows[$sofar]=$frid;
   90:                  }
   91: 	       }
   92: 	      }
   93: 	   }
   94:        } else {
   95:           $sofar++;
   96:           if ($hash{'src_'.$rid}) {
   97:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   98:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   99:              if (defined($rows[$sofar])) {
  100:                $rows[$sofar].='&'.$rid;
  101:              } else {
  102:                $rows[$sofar]=$rid;
  103:              }
  104: 	   }
  105:           }
  106:        }
  107: 
  108:        if (defined($hash{'to_'.$rid})) {
  109: 	  my $mincond=1;
  110:           my $next='';
  111:           foreach (split(/\,/,$hash{'to_'.$rid})) {
  112:               my $thiscond=
  113:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  114:               if ($thiscond>=$mincond) {
  115: 		  if ($next) {
  116: 		      $next.=','.$_.':'.$thiscond;
  117:                   } else {
  118:                       $next=$_.':'.$thiscond;
  119: 		  }
  120:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
  121: 	      }
  122:           }
  123:           foreach (split(/\,/,$next)) {
  124:               my ($linkid,$condval)=split(/\:/,$_);
  125:               if ($condval>=$mincond) {
  126:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  127:                 if ($now>$further) { $further=$now; }
  128: 	      }
  129:           }
  130: 
  131:        }
  132:     }
  133:     return $further;
  134: }
  135: 
  136: # ================================================================ Main Handler
  137: 
  138: sub handler {
  139:   my $r=shift;
  140: 
  141: # ------------------------------------------- Set document type for header only
  142: 
  143:   if ($r->header_only) {
  144:        if ($ENV{'browser.mathml'}) {
  145:            $r->content_type('text/xml');
  146:        } else {
  147:            $r->content_type('text/html');
  148:        }
  149:        $r->send_http_header;
  150:        return OK;
  151:    }
  152: 
  153:   my $requrl=$r->uri;
  154: # ----------------------------------------------------------------- Tie db file
  155:   if ($ENV{'request.course.fn'}) {
  156:       my $fn=$ENV{'request.course.fn'};
  157:       if (-e "$fn.db") {
  158:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  159: # ------------------------------------------------------------------- Hash tied
  160:               my $firstres=$hash{'map_start_'.$requrl};
  161:               my $lastres=$hash{'map_finish_'.$requrl};
  162:               if (($firstres) && ($lastres)) {
  163: # ----------------------------------------------------------------- Render page
  164: 
  165:                   @rows=();
  166: 
  167:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  168:                   if ($hash{'src_'.$lastres}) {
  169:                      my $brepriv=
  170:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  171:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  172:                         $rows[$#rows+1]=''.$lastres;
  173: 		     }
  174: 		  }
  175: 
  176: # ------------------------------------------------------------ Add to symb list
  177: 
  178:                   my $i;
  179:                   my %symbhash=();
  180:                   for ($i=0;$i<=$#rows;$i++) {
  181: 		     if ($rows[$i]) {
  182:                         my @colcont=split(/\&/,$rows[$i]);
  183:                         foreach (@colcont) {
  184:                            $symbhash{$hash{'src_'.$_}}='';
  185: 		        }
  186: 		     }
  187: 		  }
  188:                   &Apache::lonnet::symblist($requrl,%symbhash);
  189: 
  190: # ------------------------------------------------------------------ Page parms
  191: 
  192:                   my $j;
  193:                   my $lcm=1;
  194:                   my $contents=0;
  195:                   my $nforms=0;
  196:                   
  197:                   my %ssibody=();
  198:                   my %ssibgcolor=();
  199:                   my %ssitext=();
  200:                   my %ssilink=();
  201:                   my %ssivlink=();
  202:                   my %ssialink=();
  203:      
  204:                   my %metalink=();
  205: 
  206:                   my %cellemb=();
  207: 
  208:                   my $allscript='';
  209:                   my $allmeta='';
  210: 
  211:                   my $isxml=0;
  212:                   my $xmlheader='';
  213:                   my $xmlbody='';
  214: 
  215: # --------------------------------------------- Get SSI output, post parameters
  216: 
  217:                   for ($i=0;$i<=$#rows;$i++) {
  218: 		     if ($rows[$i]) {
  219: 		      $contents++;
  220:                       my @colcont=split(/\&/,$rows[$i]);
  221:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  222:                       foreach (@colcont) {
  223:                           my $src=$hash{'src_'.$_};
  224:                           $src=~/\.(\w+)$/;
  225:                           $metalink{$_}=$src.'.meta';
  226:                           $cellemb{$_}=Apache::loncommon::fileembstyle($1);
  227:                           if ($cellemb{$_} eq 'ssi') {
  228: # --------------------------------------------------------- This is an SSI cell
  229: 			      my $prefix=$_.'_';
  230:                               my %posthash=('request.prefix' => $prefix);
  231:                               if (($ENV{'form.'.$prefix.'submit'}) 
  232:                                || ($ENV{'form.all_submit'})) {
  233:                                foreach (keys %ENV) {
  234: 				  if ($_=~/^form.$prefix/) {
  235: 				      my $name=$_;
  236:                                       $name=~s/^form.$prefix//;
  237:                                       $posthash{$name}=$ENV{$_};
  238:                                   }
  239:                                }
  240: 			      }
  241:                               my $output=Apache::lonnet::ssi($src,%posthash);
  242:                               my $parser=HTML::TokeParser->new(\$output);
  243:                               my $token;
  244:                               my $thisdir=$src;
  245:                               my $bodydef=0;
  246:                               my $thisxml=0;
  247:                               my @rlinks=();
  248:                               if ($output=~/\?xml/) {
  249:                                  $isxml=1;
  250:                                  $thisxml=1;
  251:                                  $output=~
  252:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  253:                                  $xmlheader=$1;
  254: 			      }
  255:                               while ($token=$parser->get_token) {
  256: 				if ($token->[0] eq 'S') {
  257:                                   if ($token->[1] eq 'a') {
  258: 				      if ($token->[2]->{'href'}) {
  259:                                          $rlinks[$#rlinks+1]=
  260: 					     $token->[2]->{'href'};
  261: 				      }
  262: 				  } elsif ($token->[1] eq 'img') {
  263:                                          $rlinks[$#rlinks+1]=
  264: 					     $token->[2]->{'src'};
  265: 				  } elsif ($token->[1] eq 'embed') {
  266:                                          $rlinks[$#rlinks+1]=
  267: 					     $token->[2]->{'src'};
  268: 				  } elsif ($token->[1] eq 'base') {
  269: 				      $thisdir=$token->[2]->{'href'};
  270: 				  } elsif ($token->[1] eq 'body') {
  271: 				      $bodydef=1;
  272:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  273:                                       $ssitext{$_}=$token->[2]->{'text'};
  274:                                       $ssilink{$_}=$token->[2]->{'link'};
  275:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  276:                                       $ssialink{$_}=$token->[2]->{'alink'};
  277:                                       if ($thisxml) {
  278: 					  $xmlbody=$token->[4];
  279:                                       }
  280:                                   } elsif ($token->[1] eq 'meta') {
  281: 				    if ($token->[4] !~ m:/>$:) {
  282: 				      $allmeta.="\n".$token->[4].'</meta>';
  283: 				    } else {
  284: 				      $allmeta.="\n".$token->[4];
  285: 				    }
  286:                                   } elsif (($token->[1] eq 'script') &&
  287:                                            ($bodydef==0)) {
  288: 				      $allscript.="\n\n"
  289:                                                 .$parser->get_text('/script');
  290:                                   }
  291: 			        }
  292: 			      }
  293:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  294:                                  $output=$1; 
  295:                               }
  296:                               $output=~s/\<\/body\>.*//si;
  297:                               if ($output=~/\<form/si) {
  298: 				  $nforms++;
  299:                                   $output=~s/\<form[^\>]*\>//gsi;
  300:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  301:                                   $output=~
  302: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  303:                               }
  304:                               $thisdir=~s/\/[^\/]*$//;
  305: 			      foreach (@rlinks) {
  306: 				  unless (($_=~/^http:\/\//i) ||
  307: 					  ($_=~/^\//) ||
  308: 					  ($_=~/^javascript:/i) ||
  309: 					  ($_=~/^mailto:/i) ||
  310: 					  ($_=~/^\#/)) {
  311: 				      my $newlocation=
  312: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  313:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  314: 				  }
  315: 			      }
  316: # -------------------------------------------------- Deal with Applet codebases
  317:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  318: 			      $ssibody{$_}=$output;
  319: # ---------------------------------------------------------------- End SSI cell
  320:                           }
  321:                       }
  322:                      } 
  323:                   }
  324:                   unless ($contents) {
  325:                       $r->content_type('text/html');
  326:                       $r->send_http_header;
  327:                       $r->print('<html><body>Empty page.</body></html>');
  328:                   } else {
  329: # ------------------------------------------------------------------ Build page
  330: 
  331: # ---------------------------------------------------------------- Send headers
  332:                       if ($isxml) {
  333: 			  $r->content_type('text/xml');
  334:                           $r->send_http_header;
  335:                           $r->print($xmlheader);
  336: 		      } else {
  337:                           $r->content_type('text/html');
  338:                           $r->send_http_header;
  339:                           $r->print('<html>');
  340: 		      }
  341: # ------------------------------------------------------------------------ Head
  342:                       $r->print("\n<head>\n".$allmeta);
  343:                       $allscript=~
  344:        s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
  345:                       if ($allscript) {
  346: 			  $r->print("\n<script language='JavaScript'>\n".
  347:                                    $allscript."\n</script>\n");
  348:                       }
  349:                       $r->print(&Apache::lonxml::registerurl(1,undef));
  350:                       $r->print("\n</head>\n");
  351: # ------------------------------------------------------------------ Start body
  352:                       if ($isxml) {
  353:                           $r->print($xmlbody);
  354:                       } else {
  355: 			  $r->print(
  356:  '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
  357:                      '" onUnload="'.&Apache::lonxml::unloadevents.'">');
  358:                       }
  359: # ------------------------------------------------------------------ Start form
  360:                       if ($nforms) {
  361: 			  $r->print('<form method="post" action="'.
  362: 				    $requrl.'">');
  363:                       }
  364: # ----------------------------------------------------------------- Start table
  365:                       $r->print('<table cols="'.$lcm.'" border="0">');
  366:                       for ($i=0;$i<=$#rows;$i++) {
  367: 			if ($rows[$i]) {
  368:                           $r->print("\n<tr>");
  369:                           my @colcont=split(/\&/,$rows[$i]);
  370:                           my $avespan=$lcm/($#colcont+1);
  371:                           for ($j=0;$j<=$#colcont;$j++) {
  372:                               my $rid=$colcont[$j];
  373:                               my $metainfo='<a href="'.
  374:                                     $metalink{$rid}.'" target="LONcatInfo">'.
  375:                           '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
  376: 			  '</img></a>';
  377:                               if (
  378:  ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
  379:  (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
  380: 				  my ($mapid,$resid)=split(/\./,$rid);
  381:                                  my $symb=
  382:                 &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  383:                 '___'.$resid.'___'.
  384: 		&Apache::lonnet::declutter($hash{'src_'.$rid});
  385:                                  $metainfo.=
  386:                   '<a href="/adm/grades?symb='.$symb.
  387:                   '&command=submission" target="LONcatInfo">'.
  388:                           '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
  389: 			  '</img></a>'.
  390:                   '<a href="/adm/grades?symb='.$symb.
  391:                   '&command=viewgrades" target="LONcatInfo">'.
  392:                           '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
  393: 			  '</img></a>'.
  394:                   '<a href="/adm/parmset?symb='.$symb.'" target="LONcatInfo">'.
  395:                           '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
  396: 			      '</img></a>';
  397:                               }
  398:                               $metainfo.='<br></br>';
  399:                               $r->print('<td colspan="'.$avespan.'"');
  400:                               if ($cellemb{$rid} eq 'ssi') {
  401: 				  if ($ssibgcolor{$rid}) {
  402:                                      $r->print(' bgcolor="'.
  403:                                                $ssibgcolor{$rid}.'"');
  404:                                   }
  405:                                   $r->print('>'.$metainfo.'<font');
  406:                                   if ($ssitext{$rid}) {
  407: 				     $r->print(' text="'.$ssitext{$rid}.'"');
  408:                                   }
  409:                                   if ($ssilink{$rid}) {
  410: 				     $r->print(' link="'.$ssilink{$rid}.'"');
  411:                                   }
  412:                                   if ($ssitext{$rid}) {
  413: 				     $r->print(' vlink="'.$ssivlink{$rid}.'"');
  414:                                   }
  415:                                   if ($ssialink{$rid}) {
  416: 				     $r->print(' alink="'.$ssialink{$rid}.'"');
  417:                                   }
  418:                             
  419:                                   $r->print('>'.$ssibody{$rid}.'</font>');
  420:                               } elsif ($cellemb{$rid} eq 'img') {
  421:                                   $r->print('>'.$metainfo.'<img src="'.
  422:                                     $hash{'src_'.$rid}.'"></img>');
  423: 			      } elsif ($cellemb{$rid} eq 'emb') {
  424:                                   $r->print('>'.$metainfo.'<embed src="'.
  425:                                     $hash{'src_'.$rid}.'"></embed>');
  426:                               }
  427:                               $r->print('</td>');
  428:                           }
  429:                           $r->print('</tr>');
  430: 		        }
  431:                       }
  432:                       $r->print("\n</table>");
  433: # ---------------------------------------------------------------- Submit, etc.
  434:                       if ($nforms) {
  435:                           $r->print(
  436: 	                  '<input name="all_submit" value="Submit All" type="'.
  437: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  438:                       }
  439:                       $r->print('</body>'.&Apache::lonxml::xmlend());
  440: # -------------------------------------------------------------------- End page
  441:                   }                  
  442: # ------------------------------------------------------------- End render page
  443:               } else {
  444:                   $r->content_type('text/html');
  445:                   $r->send_http_header;
  446: 		  $r->print('<html><body>Page undefined.</body></html>');
  447:               }
  448: # ------------------------------------------------------------------ Untie hash
  449:               unless (untie(%hash)) {
  450:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  451:                        "Could not untie coursemap $fn (browse).</font>"); 
  452:               }
  453: # -------------------------------------------------------------------- All done
  454: 	      return OK;
  455: # ----------------------------------------------- Errors, hash could no be tied
  456:           }
  457:       } 
  458:   }
  459:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  460:   return HTTP_NOT_ACCEPTABLE; 
  461: }
  462: 
  463: 1;
  464: __END__
  465: 
  466: =head1 NAME
  467: 
  468: Apache::lonpage - Page Handler
  469: 
  470: =head1 SYNOPSIS
  471: 
  472: Invoked by /etc/httpd/conf/srm.conf:
  473: 
  474:  <LocationMatch "^/res/.*\.page$>
  475:  SetHandler perl-script
  476:  PerlHandler Apache::lonpage
  477:  </LocationMatch>
  478: 
  479: =head1 INTRODUCTION
  480: 
  481: This module renders a .page resource.
  482: 
  483: This is part of the LearningOnline Network with CAPA project
  484: described at http://www.lon-capa.org.
  485: 
  486: =head1 HANDLER SUBROUTINE
  487: 
  488: This routine is called by Apache and mod_perl.
  489: 
  490: =over 4
  491: 
  492: =item *
  493: 
  494: set document type for header only
  495: 
  496: =item *
  497: 
  498: tie db file
  499: 
  500: =item *
  501: 
  502: render page
  503: 
  504: =item *
  505: 
  506: add to symb list
  507: 
  508: =item *
  509: 
  510: page parms
  511: 
  512: =item *
  513: 
  514: Get SSI output, post parameters
  515: 
  516: =item *
  517: 
  518: SSI cell rendering
  519: 
  520: =item *
  521: 
  522: Deal with Applet codebases
  523: 
  524: =item *
  525: 
  526: Build page
  527: 
  528: =item *
  529: 
  530: send headers
  531: 
  532: =item *
  533: 
  534: start body
  535: 
  536: =item *
  537: 
  538: start form
  539: 
  540: =item *
  541: 
  542: start table
  543: 
  544: =item *
  545: 
  546: submit element, etc, render page, untie hash
  547: 
  548: =back
  549: 
  550: =head1 OTHER SUBROUTINES
  551: 
  552: =over 4
  553: 
  554: =item *
  555: 
  556: euclid() : Euclid's method for determining the greatest common denominator.
  557: 
  558: =item *
  559: 
  560: tracetable() : Build page table.
  561: 
  562: =back
  563: 
  564: =cut
  565: 
  566: 
  567: 
  568: 

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