File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.34: download - view: text, annotated - select for diffs
Tue Mar 26 16:14:51 2002 UTC (22 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Should be postdata instead of feedurl to emulate Remote

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

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