Annotation of rat/lonpage.pm, revision 1.33
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
1.33 ! www 4: # $Id: lonpage.pm,v 1.32 2002/03/06 15:00:55 matthew Exp $
1.29 www 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: #
1.1 www 28: # (TeX Content Handler
29: #
1.30 harris41 30: # YEAR=2000
1.1 www 31: # 05/29/00,05/30 Gerd Kortemeyer)
1.10 www 32: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
1.24 www 33: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
1.30 harris41 34: # YEAR=2001
1.27 www 35: # 08/13/01,08/30,10/1 Gerd Kortemeyer
1.30 harris41 36: # 12/16 Scott Harrison
1.33 ! www 37: # YEAR=2002
! 38: # 03/19 Gerd Kortemeyer
1.30 harris41 39: #
40: ###
1.1 www 41:
42: package Apache::lonpage;
43:
44: use strict;
45: use Apache::Constants qw(:common :http);
46: use Apache::lonnet();
1.30 harris41 47: use Apache::loncommon();
1.21 www 48: use Apache::lonxml();
1.6 www 49: use HTML::TokeParser;
1.1 www 50: use GDBM_File;
51:
1.2 www 52: # -------------------------------------------------------------- Module Globals
53: my %hash;
54: my @rows;
1.6 www 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: }
1.2 www 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}}))) {
1.4 www 80: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
81: $sofar=
1.2 www 82: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
1.3 www 83: '&'.$frid.'&');
1.4 www 84: $sofar++;
85: if ($hash{'src_'.$frid}) {
1.3 www 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: }
1.4 www 94: }
1.2 www 95: }
96: } else {
1.4 www 97: $sofar++;
98: if ($hash{'src_'.$rid}) {
1.3 www 99: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
100: if (($brepriv eq '2') || ($brepriv eq 'F')) {
101: if (defined($rows[$sofar])) {
1.2 www 102: $rows[$sofar].='&'.$rid;
1.3 www 103: } else {
1.2 www 104: $rows[$sofar]=$rid;
1.3 www 105: }
106: }
1.4 www 107: }
1.2 www 108: }
109:
110: if (defined($hash{'to_'.$rid})) {
1.11 www 111: my $mincond=1;
112: my $next='';
1.30 harris41 113: foreach (split(/\,/,$hash{'to_'.$rid})) {
1.11 www 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: }
1.30 harris41 124: }
125: foreach (split(/\,/,$next)) {
1.11 www 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: }
1.30 harris41 131: }
1.11 www 132:
1.2 www 133: }
134: }
135: return $further;
136: }
137:
1.1 www 138: # ================================================================ Main Handler
139:
140: sub handler {
141: my $r=shift;
142:
1.3 www 143: # ------------------------------------------- Set document type for header only
1.1 www 144:
1.3 www 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: }
1.1 www 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") {
1.7 www 160: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 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:
1.3 www 167: @rows=();
1.2 www 168:
169: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 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: }
1.2 www 177:
1.9 www 178: # ------------------------------------------------------------ Add to symb list
179:
1.2 www 180: my $i;
1.9 www 181: my %symbhash=();
182: for ($i=0;$i<=$#rows;$i++) {
183: if ($rows[$i]) {
184: my @colcont=split(/\&/,$rows[$i]);
1.30 harris41 185: foreach (@colcont) {
1.9 www 186: $symbhash{$hash{'src_'.$_}}='';
1.30 harris41 187: }
1.9 www 188: }
189: }
190: &Apache::lonnet::symblist($requrl,%symbhash);
191:
192: # ------------------------------------------------------------------ Page parms
193:
1.4 www 194: my $j;
1.6 www 195: my $lcm=1;
196: my $contents=0;
1.7 www 197: my $nforms=0;
1.6 www 198:
199: my %ssibody=();
200: my %ssibgcolor=();
201: my %ssitext=();
202: my %ssilink=();
203: my %ssivlink=();
204: my %ssialink=();
1.14 www 205:
206: my %metalink=();
207:
1.6 www 208: my %cellemb=();
1.3 www 209:
1.7 www 210: my $allscript='';
211: my $allmeta='';
212:
213: my $isxml=0;
214: my $xmlheader='';
215: my $xmlbody='';
216:
1.3 www 217: # --------------------------------------------- Get SSI output, post parameters
218:
1.2 www 219: for ($i=0;$i<=$#rows;$i++) {
1.4 www 220: if ($rows[$i]) {
1.6 www 221: $contents++;
1.3 www 222: my @colcont=split(/\&/,$rows[$i]);
1.6 www 223: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.30 harris41 224: foreach (@colcont) {
1.3 www 225: my $src=$hash{'src_'.$_};
1.5 www 226: $src=~/\.(\w+)$/;
1.14 www 227: $metalink{$_}=$src.'.meta';
1.30 harris41 228: $cellemb{$_}=Apache::loncommon::fileembstyle($1);
1.3 www 229: if ($cellemb{$_} eq 'ssi') {
230: # --------------------------------------------------------- This is an SSI cell
1.5 www 231: my $prefix=$_.'_';
232: my %posthash=('request.prefix' => $prefix);
1.8 www 233: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 234: || ($ENV{'form.all_submit'})) {
1.30 harris41 235: foreach (keys %ENV) {
1.5 www 236: if ($_=~/^form.$prefix/) {
237: my $name=$_;
238: $name=~s/^form.$prefix//;
239: $posthash{$name}=$ENV{$_};
240: }
1.30 harris41 241: }
1.7 www 242: }
1.5 www 243: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 244: my $parser=HTML::TokeParser->new(\$output);
245: my $token;
1.12 www 246: my $thisdir=$src;
1.6 www 247: my $bodydef=0;
1.7 www 248: my $thisxml=0;
1.12 www 249: my @rlinks=();
1.7 www 250: if ($output=~/\?xml/) {
251: $isxml=1;
252: $thisxml=1;
253: $output=~
254: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
255: $xmlheader=$1;
256: }
1.12 www 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') {
1.7 www 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: }
1.12 www 282: } elsif ($token->[1] eq 'meta') {
1.28 albertel 283: if ($token->[4] !~ m:/>$:) {
1.7 www 284: $allmeta.="\n".$token->[4].'</meta>';
1.28 albertel 285: } else {
286: $allmeta.="\n".$token->[4];
287: }
1.12 www 288: } elsif (($token->[1] eq 'script') &&
289: ($bodydef==0)) {
1.7 www 290: $allscript.="\n\n"
291: .$parser->get_text('/script');
1.6 www 292: }
1.12 www 293: }
294: }
1.6 www 295: if ($output=~/\<body[^\>]*\>(.*)/si) {
296: $output=$1;
297: }
298: $output=~s/\<\/body\>.*//si;
1.7 www 299: if ($output=~/\<form/si) {
300: $nforms++;
301: $output=~s/\<form[^\>]*\>//gsi;
302: $output=~s/\<\/form[^\>]*\>//gsi;
1.17 www 303: $output=~
1.18 www 304: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
1.7 www 305: }
1.12 www 306: $thisdir=~s/\/[^\/]*$//;
1.30 harris41 307: foreach (@rlinks) {
1.12 www 308: unless (($_=~/^http:\/\//i) ||
1.31 albertel 309: ($_=~/^\//) ||
310: ($_=~/^javascript:/i) ||
311: ($_=~/^mailto:/i) ||
312: ($_=~/^\#/)) {
1.12 www 313: my $newlocation=
314: &Apache::lonnet::hreflocation($thisdir,$_);
315: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
316: }
1.30 harris41 317: }
1.24 www 318: # -------------------------------------------------- Deal with Applet codebases
319: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
1.5 www 320: $ssibody{$_}=$output;
1.3 www 321: # ---------------------------------------------------------------- End SSI cell
322: }
1.30 harris41 323: }
1.4 www 324: }
1.2 www 325: }
1.6 www 326: unless ($contents) {
1.3 www 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
1.7 www 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);
1.21 www 345: $allscript=~
346: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1.7 www 347: if ($allscript) {
1.21 www 348: $r->print("\n<script language='JavaScript'>\n".
349: $allscript."\n</script>\n");
1.7 www 350: }
1.32 matthew 351: $r->print(&Apache::lonxml::registerurl(1,undef));
1.7 www 352: $r->print("\n</head>\n");
353: # ------------------------------------------------------------------ Start body
354: if ($isxml) {
355: $r->print($xmlbody);
356: } else {
1.21 www 357: $r->print(
358: '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
359: '" onUnload="'.&Apache::lonxml::unloadevents.'">');
1.7 www 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">');
1.5 www 368: for ($i=0;$i<=$#rows;$i++) {
369: if ($rows[$i]) {
1.4 www 370: $r->print("\n<tr>");
371: my @colcont=split(/\&/,$rows[$i]);
1.6 www 372: my $avespan=$lcm/($#colcont+1);
373: for ($j=0;$j<=$#colcont;$j++) {
374: my $rid=$colcont[$j];
1.23 www 375: my $metainfo='<a href="'.
376: $metalink{$rid}.'" target="LONcatInfo">'.
377: '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
1.33 ! www 378: '</img></a><a href="/adm/evaluate?feedurl='.
! 379: &Apache::lonnet::declutter($hash{'src_'.$rid}).'" target="LONcatInfo">'.
! 380: '<img src="/adm/lonMisc/eval_button.gif" border=0>'.
! 381: '</img></a>';
1.27 www 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>';
1.6 www 404: $r->print('<td colspan="'.$avespan.'"');
405: if ($cellemb{$rid} eq 'ssi') {
1.7 www 406: if ($ssibgcolor{$rid}) {
407: $r->print(' bgcolor="'.
408: $ssibgcolor{$rid}.'"');
409: }
1.14 www 410: $r->print('>'.$metainfo.'<font');
1.7 www 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>');
1.6 www 425: } elsif ($cellemb{$rid} eq 'img') {
1.14 www 426: $r->print('>'.$metainfo.'<img src="'.
1.7 www 427: $hash{'src_'.$rid}.'"></img>');
1.13 www 428: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 429: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 430: $hash{'src_'.$rid}.'"></embed>');
431: }
1.6 www 432: $r->print('</td>');
1.4 www 433: }
434: $r->print('</tr>');
1.5 www 435: }
1.4 www 436: }
437: $r->print("\n</table>");
1.7 www 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: }
1.25 www 444: $r->print('</body>'.&Apache::lonxml::xmlend());
1.3 www 445: # -------------------------------------------------------------------- End page
446: }
1.1 www 447: # ------------------------------------------------------------- End render page
448: } else {
1.3 www 449: $r->content_type('text/html');
450: $r->send_http_header;
451: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 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: }
1.10 www 464: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 465: return HTTP_NOT_ACCEPTABLE;
466: }
467:
468: 1;
469: __END__
470:
1.30 harris41 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.
1.1 www 566:
1.30 harris41 567: =back
1.1 www 568:
1.30 harris41 569: =cut
1.1 www 570:
571:
572:
573:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>