File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.29: download - view: text, annotated - select for diffs
Thu Nov 29 19:23:48 2001 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
GPL

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # $Id: lonpage.pm,v 1.29 2001/11/29 19:23:48 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: # 05/29/00,05/30 Gerd Kortemeyer)
   31: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   32: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
   33: # 08/13/01,08/30,10/1 Gerd Kortemeyer
   34: 
   35: package Apache::lonpage;
   36: 
   37: use strict;
   38: use Apache::Constants qw(:common :http);
   39: use Apache::lonnet();
   40: use Apache::lonxml();
   41: use HTML::TokeParser;
   42: use GDBM_File;
   43: 
   44: # -------------------------------------------------------------- Module Globals
   45: my %hash;
   46: my @rows;
   47: 
   48: # ------------------------------------------------------------------ Euclid gcd
   49: 
   50: sub euclid {
   51:     my ($e,$f)=@_;
   52:     my $a; my $b; my $r;
   53:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   54:     while ($r!=0) {
   55: 	$a=$b; $b=$r;
   56:         $r=$a%$b;
   57:     }
   58:     return $b;
   59: }
   60: 
   61: # ------------------------------------------------------------ Build page table
   62: 
   63: sub tracetable {
   64:     my ($sofar,$rid,$beenhere)=@_;
   65:     my $further=$sofar;
   66:     unless ($beenhere=~/\&$rid\&/) {
   67:        $beenhere.=$rid.'&';  
   68: 
   69:        if (defined($hash{'is_map_'.$rid})) {
   70:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   71:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   72:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   73: 	      $sofar=
   74:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   75:                 '&'.$frid.'&');
   76:               $sofar++;
   77:               if ($hash{'src_'.$frid}) {
   78:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   79:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   80:                  if (defined($rows[$sofar])) {
   81:                    $rows[$sofar].='&'.$frid;
   82:                  } else {
   83:                    $rows[$sofar]=$frid;
   84:                  }
   85: 	       }
   86: 	      }
   87: 	   }
   88:        } else {
   89:           $sofar++;
   90:           if ($hash{'src_'.$rid}) {
   91:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   92:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   93:              if (defined($rows[$sofar])) {
   94:                $rows[$sofar].='&'.$rid;
   95:              } else {
   96:                $rows[$sofar]=$rid;
   97:              }
   98: 	   }
   99:           }
  100:        }
  101: 
  102:        if (defined($hash{'to_'.$rid})) {
  103: 	  my $mincond=1;
  104:           my $next='';
  105:           map {
  106:               my $thiscond=
  107:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  108:               if ($thiscond>=$mincond) {
  109: 		  if ($next) {
  110: 		      $next.=','.$_.':'.$thiscond;
  111:                   } else {
  112:                       $next=$_.':'.$thiscond;
  113: 		  }
  114:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
  115: 	      }
  116:           } split(/\,/,$hash{'to_'.$rid});
  117:           map {
  118:               my ($linkid,$condval)=split(/\:/,$_);
  119:               if ($condval>=$mincond) {
  120:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  121:                 if ($now>$further) { $further=$now; }
  122: 	      }
  123:           } split(/\,/,$next);
  124: 
  125:        }
  126:     }
  127:     return $further;
  128: }
  129: 
  130: # ================================================================ Main Handler
  131: 
  132: sub handler {
  133:   my $r=shift;
  134: 
  135: # ------------------------------------------- Set document type for header only
  136: 
  137:   if ($r->header_only) {
  138:        if ($ENV{'browser.mathml'}) {
  139:            $r->content_type('text/xml');
  140:        } else {
  141:            $r->content_type('text/html');
  142:        }
  143:        $r->send_http_header;
  144:        return OK;
  145:    }
  146: 
  147:   my $requrl=$r->uri;
  148: # ----------------------------------------------------------------- Tie db file
  149:   if ($ENV{'request.course.fn'}) {
  150:       my $fn=$ENV{'request.course.fn'};
  151:       if (-e "$fn.db") {
  152:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  153: # ------------------------------------------------------------------- Hash tied
  154:               my $firstres=$hash{'map_start_'.$requrl};
  155:               my $lastres=$hash{'map_finish_'.$requrl};
  156:               if (($firstres) && ($lastres)) {
  157: # ----------------------------------------------------------------- Render page
  158: 
  159:                   @rows=();
  160: 
  161:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  162:                   if ($hash{'src_'.$lastres}) {
  163:                      my $brepriv=
  164:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  165:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  166:                         $rows[$#rows+1]=''.$lastres;
  167: 		     }
  168: 		  }
  169: 
  170: # ------------------------------------------------------------ Add to symb list
  171: 
  172:                   my $i;
  173:                   my %symbhash=();
  174:                   for ($i=0;$i<=$#rows;$i++) {
  175: 		     if ($rows[$i]) {
  176:                         my @colcont=split(/\&/,$rows[$i]);
  177:                         map {
  178:                            $symbhash{$hash{'src_'.$_}}='';
  179: 		        } @colcont;
  180: 		     }
  181: 		  }
  182:                   &Apache::lonnet::symblist($requrl,%symbhash);
  183: 
  184: # ------------------------------------------------------------------ Page parms
  185: 
  186:                   my $j;
  187:                   my $lcm=1;
  188:                   my $contents=0;
  189:                   my $nforms=0;
  190:                   
  191:                   my %ssibody=();
  192:                   my %ssibgcolor=();
  193:                   my %ssitext=();
  194:                   my %ssilink=();
  195:                   my %ssivlink=();
  196:                   my %ssialink=();
  197:      
  198:                   my %metalink=();
  199: 
  200:                   my %cellemb=();
  201: 
  202:                   my $allscript='';
  203:                   my $allmeta='';
  204: 
  205:                   my $isxml=0;
  206:                   my $xmlheader='';
  207:                   my $xmlbody='';
  208: 
  209: # --------------------------------------------- Get SSI output, post parameters
  210: 
  211:                   for ($i=0;$i<=$#rows;$i++) {
  212: 		     if ($rows[$i]) {
  213: 		      $contents++;
  214:                       my @colcont=split(/\&/,$rows[$i]);
  215:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  216:                       map {
  217:                           my $src=$hash{'src_'.$_};
  218:                           $src=~/\.(\w+)$/;
  219:                           $metalink{$_}=$src.'.meta';
  220:                           $cellemb{$_}=Apache::lonnet::fileembstyle($1);
  221:                           if ($cellemb{$_} eq 'ssi') {
  222: # --------------------------------------------------------- This is an SSI cell
  223: 			      my $prefix=$_.'_';
  224:                               my %posthash=('request.prefix' => $prefix);
  225:                               if (($ENV{'form.'.$prefix.'submit'}) 
  226:                                || ($ENV{'form.all_submit'})) {
  227:                                map {
  228: 				  if ($_=~/^form.$prefix/) {
  229: 				      my $name=$_;
  230:                                       $name=~s/^form.$prefix//;
  231:                                       $posthash{$name}=$ENV{$_};
  232:                                   }
  233:                                } keys %ENV;
  234: 			      }
  235:                               my $output=Apache::lonnet::ssi($src,%posthash);
  236:                               my $parser=HTML::TokeParser->new(\$output);
  237:                               my $token;
  238:                               my $thisdir=$src;
  239:                               my $bodydef=0;
  240:                               my $thisxml=0;
  241:                               my @rlinks=();
  242:                               if ($output=~/\?xml/) {
  243:                                  $isxml=1;
  244:                                  $thisxml=1;
  245:                                  $output=~
  246:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  247:                                  $xmlheader=$1;
  248: 			      }
  249:                               while ($token=$parser->get_token) {
  250: 				if ($token->[0] eq 'S') {
  251:                                   if ($token->[1] eq 'a') {
  252: 				      if ($token->[2]->{'href'}) {
  253:                                          $rlinks[$#rlinks+1]=
  254: 					     $token->[2]->{'href'};
  255: 				      }
  256: 				  } elsif ($token->[1] eq 'img') {
  257:                                          $rlinks[$#rlinks+1]=
  258: 					     $token->[2]->{'src'};
  259: 				  } elsif ($token->[1] eq 'embed') {
  260:                                          $rlinks[$#rlinks+1]=
  261: 					     $token->[2]->{'src'};
  262: 				  } elsif ($token->[1] eq 'base') {
  263: 				      $thisdir=$token->[2]->{'href'};
  264: 				  } elsif ($token->[1] eq 'body') {
  265: 				      $bodydef=1;
  266:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  267:                                       $ssitext{$_}=$token->[2]->{'text'};
  268:                                       $ssilink{$_}=$token->[2]->{'link'};
  269:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  270:                                       $ssialink{$_}=$token->[2]->{'alink'};
  271:                                       if ($thisxml) {
  272: 					  $xmlbody=$token->[4];
  273:                                       }
  274:                                   } elsif ($token->[1] eq 'meta') {
  275: 				    if ($token->[4] !~ m:/>$:) {
  276: 				      $allmeta.="\n".$token->[4].'</meta>';
  277: 				    } else {
  278: 				      $allmeta.="\n".$token->[4];
  279: 				    }
  280:                                   } elsif (($token->[1] eq 'script') &&
  281:                                            ($bodydef==0)) {
  282: 				      $allscript.="\n\n"
  283:                                                 .$parser->get_text('/script');
  284:                                   }
  285: 			        }
  286: 			      }
  287:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  288:                                  $output=$1; 
  289:                               }
  290:                               $output=~s/\<\/body\>.*//si;
  291:                               if ($output=~/\<form/si) {
  292: 				  $nforms++;
  293:                                   $output=~s/\<form[^\>]*\>//gsi;
  294:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  295:                                   $output=~
  296: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  297:                               }
  298:                               $thisdir=~s/\/[^\/]*$//;
  299: 			      map {
  300: 				  unless (($_=~/^http:\/\//i) ||
  301:                                           ($_=~/^\//)) {
  302: 				      my $newlocation=
  303: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  304:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  305: 				  }
  306: 			      } @rlinks;
  307: # -------------------------------------------------- Deal with Applet codebases
  308:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  309: 			      $ssibody{$_}=$output;
  310: # ---------------------------------------------------------------- End SSI cell
  311:                           }
  312:                       } @colcont;
  313:                      } 
  314:                   }
  315:                   unless ($contents) {
  316:                       $r->content_type('text/html');
  317:                       $r->send_http_header;
  318:                       $r->print('<html><body>Empty page.</body></html>');
  319:                   } else {
  320: # ------------------------------------------------------------------ Build page
  321: 
  322: # ---------------------------------------------------------------- Send headers
  323:                       if ($isxml) {
  324: 			  $r->content_type('text/xml');
  325:                           $r->send_http_header;
  326:                           $r->print($xmlheader);
  327: 		      } else {
  328:                           $r->content_type('text/html');
  329:                           $r->send_http_header;
  330:                           $r->print('<html>');
  331: 		      }
  332: # ------------------------------------------------------------------------ Head
  333:                       $r->print("\n<head>\n".$allmeta);
  334:                       $allscript=~
  335:        s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
  336:                       if ($allscript) {
  337: 			  $r->print("\n<script language='JavaScript'>\n".
  338:                                    $allscript."\n</script>\n");
  339:                       }
  340:                       $r->print(&Apache::lonxml::registerurl(1));
  341:                       $r->print("\n</head>\n");
  342: # ------------------------------------------------------------------ Start body
  343:                       if ($isxml) {
  344:                           $r->print($xmlbody);
  345:                       } else {
  346: 			  $r->print(
  347:  '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
  348:                      '" onUnload="'.&Apache::lonxml::unloadevents.'">');
  349:                       }
  350: # ------------------------------------------------------------------ Start form
  351:                       if ($nforms) {
  352: 			  $r->print('<form method="post" action="'.
  353: 				    $requrl.'">');
  354:                       }
  355: # ----------------------------------------------------------------- Start table
  356:                       $r->print('<table cols="'.$lcm.'" border="0">');
  357:                       for ($i=0;$i<=$#rows;$i++) {
  358: 			if ($rows[$i]) {
  359:                           $r->print("\n<tr>");
  360:                           my @colcont=split(/\&/,$rows[$i]);
  361:                           my $avespan=$lcm/($#colcont+1);
  362:                           for ($j=0;$j<=$#colcont;$j++) {
  363:                               my $rid=$colcont[$j];
  364:                               my $metainfo='<a href="'.
  365:                                     $metalink{$rid}.'" target="LONcatInfo">'.
  366:                           '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
  367: 			  '</img></a>';
  368:                               if (
  369:  ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
  370:  (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
  371: 				  my ($mapid,$resid)=split(/\./,$rid);
  372:                                  my $symb=
  373:                 &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  374:                 '___'.$resid.'___'.
  375: 		&Apache::lonnet::declutter($hash{'src_'.$rid});
  376:                                  $metainfo.=
  377:                   '<a href="/adm/grades?symb='.$symb.
  378:                   '&command=submission" target="LONcatInfo">'.
  379:                           '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
  380: 			  '</img></a>'.
  381:                   '<a href="/adm/grades?symb='.$symb.
  382:                   '&command=viewgrades" target="LONcatInfo">'.
  383:                           '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
  384: 			  '</img></a>'.
  385:                   '<a href="/adm/parmset?symb='.$symb.'" target="LONcatInfo">'.
  386:                           '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
  387: 			      '</img></a>';
  388:                               }
  389:                               $metainfo.='<br></br>';
  390:                               $r->print('<td colspan="'.$avespan.'"');
  391:                               if ($cellemb{$rid} eq 'ssi') {
  392: 				  if ($ssibgcolor{$rid}) {
  393:                                      $r->print(' bgcolor="'.
  394:                                                $ssibgcolor{$rid}.'"');
  395:                                   }
  396:                                   $r->print('>'.$metainfo.'<font');
  397:                                   if ($ssitext{$rid}) {
  398: 				     $r->print(' text="'.$ssitext{$rid}.'"');
  399:                                   }
  400:                                   if ($ssilink{$rid}) {
  401: 				     $r->print(' link="'.$ssilink{$rid}.'"');
  402:                                   }
  403:                                   if ($ssitext{$rid}) {
  404: 				     $r->print(' vlink="'.$ssivlink{$rid}.'"');
  405:                                   }
  406:                                   if ($ssialink{$rid}) {
  407: 				     $r->print(' alink="'.$ssialink{$rid}.'"');
  408:                                   }
  409:                             
  410:                                   $r->print('>'.$ssibody{$rid}.'</font>');
  411:                               } elsif ($cellemb{$rid} eq 'img') {
  412:                                   $r->print('>'.$metainfo.'<img src="'.
  413:                                     $hash{'src_'.$rid}.'"></img>');
  414: 			      } elsif ($cellemb{$rid} eq 'emb') {
  415:                                   $r->print('>'.$metainfo.'<embed src="'.
  416:                                     $hash{'src_'.$rid}.'"></embed>');
  417:                               }
  418:                               $r->print('</td>');
  419:                           }
  420:                           $r->print('</tr>');
  421: 		        }
  422:                       }
  423:                       $r->print("\n</table>");
  424: # ---------------------------------------------------------------- Submit, etc.
  425:                       if ($nforms) {
  426:                           $r->print(
  427: 	                  '<input name="all_submit" value="Submit All" type="'.
  428: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  429:                       }
  430:                       $r->print('</body>'.&Apache::lonxml::xmlend());
  431: # -------------------------------------------------------------------- End page
  432:                   }                  
  433: # ------------------------------------------------------------- End render page
  434:               } else {
  435:                   $r->content_type('text/html');
  436:                   $r->send_http_header;
  437: 		  $r->print('<html><body>Page undefined.</body></html>');
  438:               }
  439: # ------------------------------------------------------------------ Untie hash
  440:               unless (untie(%hash)) {
  441:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  442:                        "Could not untie coursemap $fn (browse).</font>"); 
  443:               }
  444: # -------------------------------------------------------------------- All done
  445: 	      return OK;
  446: # ----------------------------------------------- Errors, hash could no be tied
  447:           }
  448:       } 
  449:   }
  450:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  451:   return HTTP_NOT_ACCEPTABLE; 
  452: }
  453: 
  454: 1;
  455: __END__
  456: 
  457: 
  458: 
  459: 
  460: 
  461: 
  462: 

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