File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.38: download - view: text, annotated - select for diffs
Tue May 21 02:26:16 2002 UTC (21 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- multiple grading possibilities, go to the menu

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # $Id: lonpage.pm,v 1.38 2002/05/21 02:26:16 albertel 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:   my $target = $ENV{'form.grade_target'};
  157: # ----------------------------------------------------------------- Tie db file
  158:   if ($ENV{'request.course.fn'}) {
  159:       my $fn=$ENV{'request.course.fn'};
  160:       if (-e "$fn.db") {
  161:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  162: # ------------------------------------------------------------------- Hash tied
  163:               my $firstres=$hash{'map_start_'.$requrl};
  164:               my $lastres=$hash{'map_finish_'.$requrl};
  165:               if (($firstres) && ($lastres)) {
  166: # ----------------------------------------------------------------- Render page
  167: 
  168:                   @rows=();
  169: 
  170:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  171:                   if ($hash{'src_'.$lastres}) {
  172:                      my $brepriv=
  173:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  174:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  175:                         $rows[$#rows+1]=''.$lastres;
  176: 		     }
  177: 		  }
  178: 
  179: # ------------------------------------------------------------ Add to symb list
  180: 
  181:                   my $i;
  182:                   my %symbhash=();
  183:                   for ($i=0;$i<=$#rows;$i++) {
  184: 		     if ($rows[$i]) {
  185:                         my @colcont=split(/\&/,$rows[$i]);
  186:                         foreach (@colcont) {
  187:                            $symbhash{$hash{'src_'.$_}}='';
  188: 		        }
  189: 		     }
  190: 		  }
  191:                   &Apache::lonnet::symblist($requrl,%symbhash);
  192: 
  193: # ------------------------------------------------------------------ Page parms
  194: 
  195:                   my $j;
  196:                   my $lcm=1;
  197:                   my $contents=0;
  198:                   my $nforms=0;
  199:                   
  200:                   my %ssibody=();
  201:                   my %ssibgcolor=();
  202:                   my %ssitext=();
  203:                   my %ssilink=();
  204:                   my %ssivlink=();
  205:                   my %ssialink=();
  206:      
  207:                   my %metalink=();
  208: 
  209:                   my %cellemb=();
  210: 
  211:                   my $allscript='';
  212:                   my $allmeta='';
  213: 
  214:                   my $isxml=0;
  215:                   my $xmlheader='';
  216:                   my $xmlbody='';
  217: 
  218: # --------------------------------------------- Get SSI output, post parameters
  219: 
  220:                   for ($i=0;$i<=$#rows;$i++) {
  221: 		     if ($rows[$i]) {
  222: 		      $contents++;
  223:                       my @colcont=split(/\&/,$rows[$i]);
  224:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  225:                       foreach (@colcont) {
  226:                           my $src=$hash{'src_'.$_};
  227:                           $src=~/\.(\w+)$/;
  228:                           $metalink{$_}=$src.'.meta';
  229:                           $cellemb{$_}=Apache::loncommon::fileembstyle($1);
  230:                           if ($cellemb{$_} eq 'ssi') {
  231: # --------------------------------------------------------- This is an SSI cell
  232: 			      my $prefix=$_.'_';
  233:                               my %posthash=('request.prefix' => $prefix);
  234:                               if (($ENV{'form.'.$prefix.'submit'}) 
  235:                                || ($ENV{'form.all_submit'})) {
  236:                                foreach (keys %ENV) {
  237: 				  if ($_=~/^form.$prefix/) {
  238: 				      my $name=$_;
  239:                                       $name=~s/^form.$prefix//;
  240:                                       $posthash{$name}=$ENV{$_};
  241:                                   }
  242:                                }
  243: 			      }
  244:                               my $output=Apache::lonnet::ssi($src,%posthash);
  245:                               my $parser=HTML::TokeParser->new(\$output);
  246:                               my $token;
  247:                               my $thisdir=$src;
  248:                               my $bodydef=0;
  249:                               my $thisxml=0;
  250:                               my @rlinks=();
  251:                               if ($output=~/\?xml/) {
  252:                                  $isxml=1;
  253:                                  $thisxml=1;
  254:                                  $output=~
  255:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  256:                                  $xmlheader=$1;
  257: 			      }
  258:                               while ($token=$parser->get_token) {
  259: 				if ($token->[0] eq 'S') {
  260:                                   if ($token->[1] eq 'a') {
  261: 				      if ($token->[2]->{'href'}) {
  262:                                          $rlinks[$#rlinks+1]=
  263: 					     $token->[2]->{'href'};
  264: 				      }
  265: 				  } elsif ($token->[1] eq 'img') {
  266:                                          $rlinks[$#rlinks+1]=
  267: 					     $token->[2]->{'src'};
  268: 				  } elsif ($token->[1] eq 'embed') {
  269:                                          $rlinks[$#rlinks+1]=
  270: 					     $token->[2]->{'src'};
  271: 				  } elsif ($token->[1] eq 'base') {
  272: 				      $thisdir=$token->[2]->{'href'};
  273: 				  } elsif ($token->[1] eq 'body') {
  274: 				      $bodydef=1;
  275:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  276:                                       $ssitext{$_}=$token->[2]->{'text'};
  277:                                       $ssilink{$_}=$token->[2]->{'link'};
  278:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  279:                                       $ssialink{$_}=$token->[2]->{'alink'};
  280:                                       if ($thisxml) {
  281: 					  $xmlbody=$token->[4];
  282:                                       }
  283:                                   } elsif ($token->[1] eq 'meta') {
  284: 				    if ($token->[4] !~ m:/>$:) {
  285: 				      $allmeta.="\n".$token->[4].'</meta>';
  286: 				    } else {
  287: 				      $allmeta.="\n".$token->[4];
  288: 				    }
  289:                                   } elsif (($token->[1] eq 'script') &&
  290:                                            ($bodydef==0)) {
  291: 				      $allscript.="\n\n"
  292:                                                 .$parser->get_text('/script');
  293:                                   }
  294: 			        }
  295: 			      }
  296:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  297:                                  $output=$1; 
  298:                               }
  299:                               $output=~s/\<\/body\>.*//si;
  300:                               if ($output=~/\<form/si) {
  301: 				  $nforms++;
  302:                                   $output=~s/\<form[^\>]*\>//gsi;
  303:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  304:                                   $output=~
  305: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  306:                               }
  307:                               $thisdir=~s/\/[^\/]*$//;
  308: 			      foreach (@rlinks) {
  309: 				  unless (($_=~/^http:\/\//i) ||
  310: 					  ($_=~/^\//) ||
  311: 					  ($_=~/^javascript:/i) ||
  312: 					  ($_=~/^mailto:/i) ||
  313: 					  ($_=~/^\#/)) {
  314: 				      my $newlocation=
  315: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  316:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  317: 				  }
  318: 			      }
  319: # -------------------------------------------------- Deal with Applet codebases
  320:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  321: 			      $ssibody{$_}=$output;
  322: # ---------------------------------------------------------------- End SSI cell
  323:                           }
  324:                       }
  325:                      } 
  326:                   }
  327:                   unless ($contents) {
  328:                       $r->content_type('text/html');
  329:                       $r->send_http_header;
  330:                       $r->print('<html><body>Empty page.</body></html>');
  331:                   } else {
  332: # ------------------------------------------------------------------ Build page
  333: 
  334: # ---------------------------------------------------------------- Send headers
  335: 		      unless ($target eq 'tex') {
  336: 			  if ($isxml) {
  337: 			      $r->content_type('text/xml');
  338: 			      $r->send_http_header;
  339: 			      $r->print($xmlheader);
  340: 			  } else {
  341: 			      $r->content_type('text/html');
  342: 			      $r->send_http_header;
  343: 			      $r->print('<html>');
  344: 			  }
  345: # ------------------------------------------------------------------------ Head
  346: 			  $r->print("\n<head>\n".$allmeta);
  347: 			  $allscript=~
  348: 			      s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
  349: 			  if ($allscript) {
  350: 			      $r->print("\n<script language='JavaScript'>\n".
  351: 					$allscript."\n</script>\n");
  352: 			  }
  353: 			  $r->print(&Apache::lonxml::registerurl(1,undef));
  354: 			  $r->print("\n</head>\n");
  355: # ------------------------------------------------------------------ Start body
  356: 			  if ($isxml) {
  357: 			      $r->print($xmlbody);
  358: 			  } else {
  359: 			      $r->print('<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.'" 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: 		      }
  369:                       for ($i=0;$i<=$#rows;$i++) {
  370: 			if ($rows[$i]) {
  371: 			    unless ($target eq 'tex') {
  372: 				$r->print("\n<tr>");
  373: 			    }
  374:                           my @colcont=split(/\&/,$rows[$i]);
  375:                           my $avespan=$lcm/($#colcont+1);
  376:                           for ($j=0;$j<=$#colcont;$j++) {
  377:                               my $rid=$colcont[$j];
  378:                               my $metainfo='<a href="'.
  379:                                     $metalink{$rid}.'" target="LONcatInfo">'.
  380:                           '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
  381: 			  '</img></a><a href="/adm/evaluate?postdata='.
  382:       &Apache::lonnet::escape(&Apache::lonnet::declutter($hash{'src_'.$rid}))
  383:       .'" target="LONcatInfo">'.
  384:                           '<img src="/adm/lonMisc/eval_button.gif" border=0>'.
  385:                           '</img></a>';
  386:                               if (
  387:  ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
  388:  (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
  389: 				  my ($mapid,$resid)=split(/\./,$rid);
  390:                                  my $symb=
  391:                 &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  392:                 '___'.$resid.'___'.
  393: 		&Apache::lonnet::declutter($hash{'src_'.$rid});
  394:                                  $metainfo.=
  395:                   '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
  396:                   '&command=submission" target="LONcatInfo">'.
  397:                           '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
  398: 			  '</img></a>'.
  399:                   '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
  400:                   '&command=gradingmenu" target="LONcatInfo">'.
  401:                           '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
  402: 			  '</img></a>'.
  403:                   '<a href="/adm/parmset?symb='.&Apache::lonnet::escape($symb).
  404:                           '" target="LONcatInfo">'.
  405:                           '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
  406: 			      '</img></a>';
  407:                               }
  408:                               $metainfo.='<br></br>';
  409: 			    unless ($target eq 'tex') {
  410: 				$r->print('<td colspan="'.$avespan.'"');
  411: 			    }
  412:                               if ($cellemb{$rid} eq 'ssi') {
  413: 				  unless ($target eq 'tex') {
  414: 				      if ($ssibgcolor{$rid}) {
  415: 					  $r->print(' bgcolor="'.
  416: 						    $ssibgcolor{$rid}.'"');
  417: 				      }
  418: 				      $r->print('>'.$metainfo.'<font');
  419: 		    
  420: 				      if ($ssitext{$rid}) {
  421: 					  $r->print(' text="'.$ssitext{$rid}.'"');
  422: 				      }
  423: 				      if ($ssilink{$rid}) {
  424: 					  $r->print(' link="'.$ssilink{$rid}.'"');
  425: 				      }
  426: 				      if ($ssitext{$rid}) {
  427: 					  $r->print(' vlink="'.$ssivlink{$rid}.'"');
  428: 				      }
  429: 				      if ($ssialink{$rid}) {
  430: 					  $r->print(' alink="'.$ssialink{$rid}.'"');
  431: 				      }             
  432: 				      $r->print('>');
  433: 				  }
  434:                                   $r->print($ssibody{$rid});	
  435: 				  unless ($target eq 'tex') {
  436: 				      $r->print('</font>');
  437: 				  }
  438: 			      } elsif ($cellemb{$rid} eq 'img') {
  439:                                   $r->print('>'.$metainfo.'<img src="'.
  440:                                     $hash{'src_'.$rid}.'"></img>');
  441: 			      } elsif ($cellemb{$rid} eq 'emb') {
  442:                                   $r->print('>'.$metainfo.'<embed src="'.
  443:                                     $hash{'src_'.$rid}.'"></embed>');
  444:                               }
  445: 			      unless ($target eq 'tex') {
  446: 				  $r->print('</td>');
  447: 			      }
  448:                           }
  449: 			      unless ($target eq 'tex') {
  450: 				  $r->print('</tr>');
  451: 			      }
  452: 		        }
  453:                       }
  454: 		      unless ($target eq 'tex') {
  455: 			  $r->print("\n</table>");
  456: 		      }
  457: # ---------------------------------------------------------------- Submit, etc.
  458:                       if ($nforms) {
  459:                           $r->print(
  460: 	                  '<input name="all_submit" value="Submit All" type="'.
  461: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  462:                       }
  463:                       $r->print('</body>'.&Apache::lonxml::xmlend());
  464: # -------------------------------------------------------------------- End page
  465:                   }                  
  466: # ------------------------------------------------------------- End render page
  467:               } else {
  468:                   $r->content_type('text/html');
  469:                   $r->send_http_header;
  470: 		  $r->print('<html><body>Page undefined.</body></html>');
  471:               }
  472: # ------------------------------------------------------------------ Untie hash
  473:               unless (untie(%hash)) {
  474:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  475:                        "Could not untie coursemap $fn (browse).</font>"); 
  476:               }
  477: # -------------------------------------------------------------------- All done
  478: 	      return OK;
  479: # ----------------------------------------------- Errors, hash could no be tied
  480:           }
  481:       } 
  482:   }
  483:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  484:   return HTTP_NOT_ACCEPTABLE; 
  485: }
  486: 
  487: 1;
  488: __END__
  489: 
  490: =head1 NAME
  491: 
  492: Apache::lonpage - Page Handler
  493: 
  494: =head1 SYNOPSIS
  495: 
  496: Invoked by /etc/httpd/conf/srm.conf:
  497: 
  498:  <LocationMatch "^/res/.*\.page$>
  499:  SetHandler perl-script
  500:  PerlHandler Apache::lonpage
  501:  </LocationMatch>
  502: 
  503: =head1 INTRODUCTION
  504: 
  505: This module renders a .page resource.
  506: 
  507: This is part of the LearningOnline Network with CAPA project
  508: described at http://www.lon-capa.org.
  509: 
  510: =head1 HANDLER SUBROUTINE
  511: 
  512: This routine is called by Apache and mod_perl.
  513: 
  514: =over 4
  515: 
  516: =item *
  517: 
  518: set document type for header only
  519: 
  520: =item *
  521: 
  522: tie db file
  523: 
  524: =item *
  525: 
  526: render page
  527: 
  528: =item *
  529: 
  530: add to symb list
  531: 
  532: =item *
  533: 
  534: page parms
  535: 
  536: =item *
  537: 
  538: Get SSI output, post parameters
  539: 
  540: =item *
  541: 
  542: SSI cell rendering
  543: 
  544: =item *
  545: 
  546: Deal with Applet codebases
  547: 
  548: =item *
  549: 
  550: Build page
  551: 
  552: =item *
  553: 
  554: send headers
  555: 
  556: =item *
  557: 
  558: start body
  559: 
  560: =item *
  561: 
  562: start form
  563: 
  564: =item *
  565: 
  566: start table
  567: 
  568: =item *
  569: 
  570: submit element, etc, render page, untie hash
  571: 
  572: =back
  573: 
  574: =head1 OTHER SUBROUTINES
  575: 
  576: =over 4
  577: 
  578: =item *
  579: 
  580: euclid() : Euclid's method for determining the greatest common denominator.
  581: 
  582: =item *
  583: 
  584: tracetable() : Build page table.
  585: 
  586: =back
  587: 
  588: =cut
  589: 
  590: 
  591: 
  592: 

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