Annotation of rat/lonpage.pm, revision 1.36
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
1.36 ! www 4: # $Id: lonpage.pm,v 1.35 2002/03/26 23:12:57 www 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.34 www 378: '</img></a><a href="/adm/evaluate?postdata='.
1.35 www 379: &Apache::lonnet::escape(&Apache::lonnet::declutter($hash{'src_'.$rid}))
380: .'" target="LONcatInfo">'.
1.33 www 381: '<img src="/adm/lonMisc/eval_button.gif" border=0>'.
382: '</img></a>';
1.27 www 383: if (
384: ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
385: (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
386: my ($mapid,$resid)=split(/\./,$rid);
387: my $symb=
388: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
389: '___'.$resid.'___'.
390: &Apache::lonnet::declutter($hash{'src_'.$rid});
391: $metainfo.=
1.36 ! www 392: '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
1.27 www 393: '&command=submission" target="LONcatInfo">'.
394: '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
395: '</img></a>'.
1.36 ! www 396: '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
1.27 www 397: '&command=viewgrades" target="LONcatInfo">'.
398: '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
399: '</img></a>'.
1.36 ! www 400: '<a href="/adm/parmset?symb='.&Apache::lonnet::escape($symb).
! 401: '" target="LONcatInfo">'.
1.27 www 402: '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
403: '</img></a>';
404: }
405: $metainfo.='<br></br>';
1.6 www 406: $r->print('<td colspan="'.$avespan.'"');
407: if ($cellemb{$rid} eq 'ssi') {
1.7 www 408: if ($ssibgcolor{$rid}) {
409: $r->print(' bgcolor="'.
410: $ssibgcolor{$rid}.'"');
411: }
1.14 www 412: $r->print('>'.$metainfo.'<font');
1.7 www 413: if ($ssitext{$rid}) {
414: $r->print(' text="'.$ssitext{$rid}.'"');
415: }
416: if ($ssilink{$rid}) {
417: $r->print(' link="'.$ssilink{$rid}.'"');
418: }
419: if ($ssitext{$rid}) {
420: $r->print(' vlink="'.$ssivlink{$rid}.'"');
421: }
422: if ($ssialink{$rid}) {
423: $r->print(' alink="'.$ssialink{$rid}.'"');
424: }
425:
426: $r->print('>'.$ssibody{$rid}.'</font>');
1.6 www 427: } elsif ($cellemb{$rid} eq 'img') {
1.14 www 428: $r->print('>'.$metainfo.'<img src="'.
1.7 www 429: $hash{'src_'.$rid}.'"></img>');
1.13 www 430: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 431: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 432: $hash{'src_'.$rid}.'"></embed>');
433: }
1.6 www 434: $r->print('</td>');
1.4 www 435: }
436: $r->print('</tr>');
1.5 www 437: }
1.4 www 438: }
439: $r->print("\n</table>");
1.7 www 440: # ---------------------------------------------------------------- Submit, etc.
441: if ($nforms) {
442: $r->print(
443: '<input name="all_submit" value="Submit All" type="'.
444: (($nforms>1)?'submit':'hidden').'"></input></form>');
445: }
1.25 www 446: $r->print('</body>'.&Apache::lonxml::xmlend());
1.3 www 447: # -------------------------------------------------------------------- End page
448: }
1.1 www 449: # ------------------------------------------------------------- End render page
450: } else {
1.3 www 451: $r->content_type('text/html');
452: $r->send_http_header;
453: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 454: }
455: # ------------------------------------------------------------------ Untie hash
456: unless (untie(%hash)) {
457: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
458: "Could not untie coursemap $fn (browse).</font>");
459: }
460: # -------------------------------------------------------------------- All done
461: return OK;
462: # ----------------------------------------------- Errors, hash could no be tied
463: }
464: }
465: }
1.10 www 466: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 467: return HTTP_NOT_ACCEPTABLE;
468: }
469:
470: 1;
471: __END__
472:
1.30 harris41 473: =head1 NAME
474:
475: Apache::lonpage - Page Handler
476:
477: =head1 SYNOPSIS
478:
479: Invoked by /etc/httpd/conf/srm.conf:
480:
481: <LocationMatch "^/res/.*\.page$>
482: SetHandler perl-script
483: PerlHandler Apache::lonpage
484: </LocationMatch>
485:
486: =head1 INTRODUCTION
487:
488: This module renders a .page resource.
489:
490: This is part of the LearningOnline Network with CAPA project
491: described at http://www.lon-capa.org.
492:
493: =head1 HANDLER SUBROUTINE
494:
495: This routine is called by Apache and mod_perl.
496:
497: =over 4
498:
499: =item *
500:
501: set document type for header only
502:
503: =item *
504:
505: tie db file
506:
507: =item *
508:
509: render page
510:
511: =item *
512:
513: add to symb list
514:
515: =item *
516:
517: page parms
518:
519: =item *
520:
521: Get SSI output, post parameters
522:
523: =item *
524:
525: SSI cell rendering
526:
527: =item *
528:
529: Deal with Applet codebases
530:
531: =item *
532:
533: Build page
534:
535: =item *
536:
537: send headers
538:
539: =item *
540:
541: start body
542:
543: =item *
544:
545: start form
546:
547: =item *
548:
549: start table
550:
551: =item *
552:
553: submit element, etc, render page, untie hash
554:
555: =back
556:
557: =head1 OTHER SUBROUTINES
558:
559: =over 4
560:
561: =item *
562:
563: euclid() : Euclid's method for determining the greatest common denominator.
564:
565: =item *
566:
567: tracetable() : Build page table.
1.1 www 568:
1.30 harris41 569: =back
1.1 www 570:
1.30 harris41 571: =cut
1.1 www 572:
573:
574:
575:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>