Annotation of rat/lonpage.pm, revision 1.37
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
1.37 ! sakharuk 4: # $Id: lonpage.pm,v 1.36 2002/03/28 21:47:48 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:
1.37 ! sakharuk 155: my $requrl=$r->uri;
! 156: my $target = $ENV{'form.grade_target'};
1.1 www 157: # ----------------------------------------------------------------- Tie db file
158: if ($ENV{'request.course.fn'}) {
159: my $fn=$ENV{'request.course.fn'};
160: if (-e "$fn.db") {
1.7 www 161: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 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:
1.3 www 168: @rows=();
1.2 www 169:
170: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 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: }
1.2 www 178:
1.9 www 179: # ------------------------------------------------------------ Add to symb list
180:
1.2 www 181: my $i;
1.9 www 182: my %symbhash=();
183: for ($i=0;$i<=$#rows;$i++) {
184: if ($rows[$i]) {
185: my @colcont=split(/\&/,$rows[$i]);
1.30 harris41 186: foreach (@colcont) {
1.9 www 187: $symbhash{$hash{'src_'.$_}}='';
1.30 harris41 188: }
1.9 www 189: }
190: }
191: &Apache::lonnet::symblist($requrl,%symbhash);
192:
193: # ------------------------------------------------------------------ Page parms
194:
1.4 www 195: my $j;
1.6 www 196: my $lcm=1;
197: my $contents=0;
1.7 www 198: my $nforms=0;
1.6 www 199:
200: my %ssibody=();
201: my %ssibgcolor=();
202: my %ssitext=();
203: my %ssilink=();
204: my %ssivlink=();
205: my %ssialink=();
1.14 www 206:
207: my %metalink=();
208:
1.6 www 209: my %cellemb=();
1.3 www 210:
1.7 www 211: my $allscript='';
212: my $allmeta='';
213:
214: my $isxml=0;
215: my $xmlheader='';
216: my $xmlbody='';
217:
1.3 www 218: # --------------------------------------------- Get SSI output, post parameters
219:
1.2 www 220: for ($i=0;$i<=$#rows;$i++) {
1.4 www 221: if ($rows[$i]) {
1.6 www 222: $contents++;
1.3 www 223: my @colcont=split(/\&/,$rows[$i]);
1.6 www 224: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.30 harris41 225: foreach (@colcont) {
1.3 www 226: my $src=$hash{'src_'.$_};
1.5 www 227: $src=~/\.(\w+)$/;
1.14 www 228: $metalink{$_}=$src.'.meta';
1.30 harris41 229: $cellemb{$_}=Apache::loncommon::fileembstyle($1);
1.3 www 230: if ($cellemb{$_} eq 'ssi') {
231: # --------------------------------------------------------- This is an SSI cell
1.5 www 232: my $prefix=$_.'_';
233: my %posthash=('request.prefix' => $prefix);
1.8 www 234: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 235: || ($ENV{'form.all_submit'})) {
1.30 harris41 236: foreach (keys %ENV) {
1.5 www 237: if ($_=~/^form.$prefix/) {
238: my $name=$_;
239: $name=~s/^form.$prefix//;
240: $posthash{$name}=$ENV{$_};
241: }
1.30 harris41 242: }
1.7 www 243: }
1.5 www 244: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 245: my $parser=HTML::TokeParser->new(\$output);
246: my $token;
1.12 www 247: my $thisdir=$src;
1.6 www 248: my $bodydef=0;
1.7 www 249: my $thisxml=0;
1.12 www 250: my @rlinks=();
1.7 www 251: if ($output=~/\?xml/) {
252: $isxml=1;
253: $thisxml=1;
254: $output=~
255: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
256: $xmlheader=$1;
257: }
1.12 www 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') {
1.7 www 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: }
1.12 www 283: } elsif ($token->[1] eq 'meta') {
1.28 albertel 284: if ($token->[4] !~ m:/>$:) {
1.7 www 285: $allmeta.="\n".$token->[4].'</meta>';
1.28 albertel 286: } else {
287: $allmeta.="\n".$token->[4];
288: }
1.12 www 289: } elsif (($token->[1] eq 'script') &&
290: ($bodydef==0)) {
1.7 www 291: $allscript.="\n\n"
292: .$parser->get_text('/script');
1.6 www 293: }
1.12 www 294: }
295: }
1.6 www 296: if ($output=~/\<body[^\>]*\>(.*)/si) {
297: $output=$1;
298: }
299: $output=~s/\<\/body\>.*//si;
1.7 www 300: if ($output=~/\<form/si) {
301: $nforms++;
302: $output=~s/\<form[^\>]*\>//gsi;
303: $output=~s/\<\/form[^\>]*\>//gsi;
1.17 www 304: $output=~
1.18 www 305: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
1.7 www 306: }
1.12 www 307: $thisdir=~s/\/[^\/]*$//;
1.30 harris41 308: foreach (@rlinks) {
1.12 www 309: unless (($_=~/^http:\/\//i) ||
1.31 albertel 310: ($_=~/^\//) ||
311: ($_=~/^javascript:/i) ||
312: ($_=~/^mailto:/i) ||
313: ($_=~/^\#/)) {
1.12 www 314: my $newlocation=
315: &Apache::lonnet::hreflocation($thisdir,$_);
316: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
317: }
1.30 harris41 318: }
1.24 www 319: # -------------------------------------------------- Deal with Applet codebases
320: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
1.5 www 321: $ssibody{$_}=$output;
1.3 www 322: # ---------------------------------------------------------------- End SSI cell
323: }
1.30 harris41 324: }
1.4 www 325: }
1.2 www 326: }
1.6 www 327: unless ($contents) {
1.3 www 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
1.7 www 333:
334: # ---------------------------------------------------------------- Send headers
1.37 ! sakharuk 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: }
1.7 www 345: # ------------------------------------------------------------------------ Head
1.37 ! sakharuk 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");
1.7 www 355: # ------------------------------------------------------------------ Start body
1.37 ! sakharuk 356: if ($isxml) {
! 357: $r->print($xmlbody);
! 358: } else {
! 359: $r->print('<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.'" onUnload="'.&Apache::lonxml::unloadevents.'">');
! 360: }
1.7 www 361: # ------------------------------------------------------------------ Start form
1.37 ! sakharuk 362: if ($nforms) {
! 363: $r->print('<form method="post" action="'.
! 364: $requrl.'">');
! 365: }
1.7 www 366: # ----------------------------------------------------------------- Start table
1.37 ! sakharuk 367: $r->print('<table cols="'.$lcm.'" border="0">');
! 368: }
1.5 www 369: for ($i=0;$i<=$#rows;$i++) {
370: if ($rows[$i]) {
1.37 ! sakharuk 371: unless ($target eq 'tex') {
! 372: $r->print("\n<tr>");
! 373: }
1.4 www 374: my @colcont=split(/\&/,$rows[$i]);
1.6 www 375: my $avespan=$lcm/($#colcont+1);
376: for ($j=0;$j<=$#colcont;$j++) {
377: my $rid=$colcont[$j];
1.23 www 378: my $metainfo='<a href="'.
379: $metalink{$rid}.'" target="LONcatInfo">'.
380: '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
1.34 www 381: '</img></a><a href="/adm/evaluate?postdata='.
1.35 www 382: &Apache::lonnet::escape(&Apache::lonnet::declutter($hash{'src_'.$rid}))
383: .'" target="LONcatInfo">'.
1.33 www 384: '<img src="/adm/lonMisc/eval_button.gif" border=0>'.
385: '</img></a>';
1.27 www 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.=
1.36 www 395: '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
1.27 www 396: '&command=submission" target="LONcatInfo">'.
397: '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
398: '</img></a>'.
1.36 www 399: '<a href="/adm/grades?symb='.&Apache::lonnet::escape($symb).
1.27 www 400: '&command=viewgrades" target="LONcatInfo">'.
401: '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
402: '</img></a>'.
1.36 www 403: '<a href="/adm/parmset?symb='.&Apache::lonnet::escape($symb).
404: '" target="LONcatInfo">'.
1.27 www 405: '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
406: '</img></a>';
407: }
408: $metainfo.='<br></br>';
1.37 ! sakharuk 409: unless ($target eq 'tex') {
! 410: $r->print('<td colspan="'.$avespan.'"');
! 411: }
1.6 www 412: if ($cellemb{$rid} eq 'ssi') {
1.37 ! sakharuk 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') {
1.14 www 439: $r->print('>'.$metainfo.'<img src="'.
1.7 www 440: $hash{'src_'.$rid}.'"></img>');
1.13 www 441: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 442: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 443: $hash{'src_'.$rid}.'"></embed>');
444: }
1.37 ! sakharuk 445: unless ($target eq 'tex') {
! 446: $r->print('</td>');
! 447: }
1.4 www 448: }
1.37 ! sakharuk 449: unless ($target eq 'tex') {
! 450: $r->print('</tr>');
! 451: }
1.5 www 452: }
1.4 www 453: }
1.37 ! sakharuk 454: unless ($target eq 'tex') {
! 455: $r->print("\n</table>");
! 456: }
1.7 www 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: }
1.25 www 463: $r->print('</body>'.&Apache::lonxml::xmlend());
1.3 www 464: # -------------------------------------------------------------------- End page
465: }
1.1 www 466: # ------------------------------------------------------------- End render page
467: } else {
1.3 www 468: $r->content_type('text/html');
469: $r->send_http_header;
470: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 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: }
1.10 www 483: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 484: return HTTP_NOT_ACCEPTABLE;
485: }
486:
487: 1;
488: __END__
489:
1.30 harris41 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.
1.1 www 585:
1.30 harris41 586: =back
1.1 www 587:
1.30 harris41 588: =cut
1.1 www 589:
590:
591:
592:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>