Annotation of rat/lonpage.pm, revision 1.29
1.1 www 1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
1.29 ! www 4: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 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: #
1.1 www 28: # (TeX Content Handler
29: #
30: # 05/29/00,05/30 Gerd Kortemeyer)
1.10 www 31: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
1.24 www 32: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
1.27 www 33: # 08/13/01,08/30,10/1 Gerd Kortemeyer
1.1 www 34:
35: package Apache::lonpage;
36:
37: use strict;
38: use Apache::Constants qw(:common :http);
39: use Apache::lonnet();
1.21 www 40: use Apache::lonxml();
1.6 www 41: use HTML::TokeParser;
1.1 www 42: use GDBM_File;
43:
1.2 www 44: # -------------------------------------------------------------- Module Globals
45: my %hash;
46: my @rows;
1.6 www 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: }
1.2 www 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}}))) {
1.4 www 72: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
73: $sofar=
1.2 www 74: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
1.3 www 75: '&'.$frid.'&');
1.4 www 76: $sofar++;
77: if ($hash{'src_'.$frid}) {
1.3 www 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: }
1.4 www 86: }
1.2 www 87: }
88: } else {
1.4 www 89: $sofar++;
90: if ($hash{'src_'.$rid}) {
1.3 www 91: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
92: if (($brepriv eq '2') || ($brepriv eq 'F')) {
93: if (defined($rows[$sofar])) {
1.2 www 94: $rows[$sofar].='&'.$rid;
1.3 www 95: } else {
1.2 www 96: $rows[$sofar]=$rid;
1.3 www 97: }
98: }
1.4 www 99: }
1.2 www 100: }
101:
102: if (defined($hash{'to_'.$rid})) {
1.11 www 103: my $mincond=1;
104: my $next='';
1.2 www 105: map {
1.11 www 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: }
1.2 www 116: } split(/\,/,$hash{'to_'.$rid});
1.11 www 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:
1.2 www 125: }
126: }
127: return $further;
128: }
129:
1.1 www 130: # ================================================================ Main Handler
131:
132: sub handler {
133: my $r=shift;
134:
1.3 www 135: # ------------------------------------------- Set document type for header only
1.1 www 136:
1.3 www 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: }
1.1 www 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") {
1.7 www 152: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
1.1 www 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:
1.3 www 159: @rows=();
1.2 www 160:
161: &tracetable(0,$firstres,'&'.$lastres.'&');
1.4 www 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: }
1.2 www 169:
1.9 www 170: # ------------------------------------------------------------ Add to symb list
171:
1.2 www 172: my $i;
1.9 www 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:
1.4 www 186: my $j;
1.6 www 187: my $lcm=1;
188: my $contents=0;
1.7 www 189: my $nforms=0;
1.6 www 190:
191: my %ssibody=();
192: my %ssibgcolor=();
193: my %ssitext=();
194: my %ssilink=();
195: my %ssivlink=();
196: my %ssialink=();
1.14 www 197:
198: my %metalink=();
199:
1.6 www 200: my %cellemb=();
1.3 www 201:
1.7 www 202: my $allscript='';
203: my $allmeta='';
204:
205: my $isxml=0;
206: my $xmlheader='';
207: my $xmlbody='';
208:
1.3 www 209: # --------------------------------------------- Get SSI output, post parameters
210:
1.2 www 211: for ($i=0;$i<=$#rows;$i++) {
1.4 www 212: if ($rows[$i]) {
1.6 www 213: $contents++;
1.3 www 214: my @colcont=split(/\&/,$rows[$i]);
1.6 www 215: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
1.3 www 216: map {
217: my $src=$hash{'src_'.$_};
1.5 www 218: $src=~/\.(\w+)$/;
1.14 www 219: $metalink{$_}=$src.'.meta';
1.3 www 220: $cellemb{$_}=Apache::lonnet::fileembstyle($1);
221: if ($cellemb{$_} eq 'ssi') {
222: # --------------------------------------------------------- This is an SSI cell
1.5 www 223: my $prefix=$_.'_';
224: my %posthash=('request.prefix' => $prefix);
1.8 www 225: if (($ENV{'form.'.$prefix.'submit'})
1.7 www 226: || ($ENV{'form.all_submit'})) {
227: map {
1.5 www 228: if ($_=~/^form.$prefix/) {
229: my $name=$_;
230: $name=~s/^form.$prefix//;
231: $posthash{$name}=$ENV{$_};
232: }
1.7 www 233: } keys %ENV;
234: }
1.5 www 235: my $output=Apache::lonnet::ssi($src,%posthash);
1.6 www 236: my $parser=HTML::TokeParser->new(\$output);
237: my $token;
1.12 www 238: my $thisdir=$src;
1.6 www 239: my $bodydef=0;
1.7 www 240: my $thisxml=0;
1.12 www 241: my @rlinks=();
1.7 www 242: if ($output=~/\?xml/) {
243: $isxml=1;
244: $thisxml=1;
245: $output=~
246: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
247: $xmlheader=$1;
248: }
1.12 www 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') {
1.7 www 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: }
1.12 www 274: } elsif ($token->[1] eq 'meta') {
1.28 albertel 275: if ($token->[4] !~ m:/>$:) {
1.7 www 276: $allmeta.="\n".$token->[4].'</meta>';
1.28 albertel 277: } else {
278: $allmeta.="\n".$token->[4];
279: }
1.12 www 280: } elsif (($token->[1] eq 'script') &&
281: ($bodydef==0)) {
1.7 www 282: $allscript.="\n\n"
283: .$parser->get_text('/script');
1.6 www 284: }
1.12 www 285: }
286: }
1.6 www 287: if ($output=~/\<body[^\>]*\>(.*)/si) {
288: $output=$1;
289: }
290: $output=~s/\<\/body\>.*//si;
1.7 www 291: if ($output=~/\<form/si) {
292: $nforms++;
293: $output=~s/\<form[^\>]*\>//gsi;
294: $output=~s/\<\/form[^\>]*\>//gsi;
1.17 www 295: $output=~
1.18 www 296: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
1.7 www 297: }
1.12 www 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;
1.24 www 307: # -------------------------------------------------- Deal with Applet codebases
308: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
1.5 www 309: $ssibody{$_}=$output;
1.3 www 310: # ---------------------------------------------------------------- End SSI cell
311: }
312: } @colcont;
1.4 www 313: }
1.2 www 314: }
1.6 www 315: unless ($contents) {
1.3 www 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
1.7 www 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);
1.21 www 334: $allscript=~
335: s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1.7 www 336: if ($allscript) {
1.21 www 337: $r->print("\n<script language='JavaScript'>\n".
338: $allscript."\n</script>\n");
1.7 www 339: }
1.26 www 340: $r->print(&Apache::lonxml::registerurl(1));
1.7 www 341: $r->print("\n</head>\n");
342: # ------------------------------------------------------------------ Start body
343: if ($isxml) {
344: $r->print($xmlbody);
345: } else {
1.21 www 346: $r->print(
347: '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
348: '" onUnload="'.&Apache::lonxml::unloadevents.'">');
1.7 www 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">');
1.5 www 357: for ($i=0;$i<=$#rows;$i++) {
358: if ($rows[$i]) {
1.4 www 359: $r->print("\n<tr>");
360: my @colcont=split(/\&/,$rows[$i]);
1.6 www 361: my $avespan=$lcm/($#colcont+1);
362: for ($j=0;$j<=$#colcont;$j++) {
363: my $rid=$colcont[$j];
1.23 www 364: my $metainfo='<a href="'.
365: $metalink{$rid}.'" target="LONcatInfo">'.
366: '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
1.27 www 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>';
1.6 www 390: $r->print('<td colspan="'.$avespan.'"');
391: if ($cellemb{$rid} eq 'ssi') {
1.7 www 392: if ($ssibgcolor{$rid}) {
393: $r->print(' bgcolor="'.
394: $ssibgcolor{$rid}.'"');
395: }
1.14 www 396: $r->print('>'.$metainfo.'<font');
1.7 www 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>');
1.6 www 411: } elsif ($cellemb{$rid} eq 'img') {
1.14 www 412: $r->print('>'.$metainfo.'<img src="'.
1.7 www 413: $hash{'src_'.$rid}.'"></img>');
1.13 www 414: } elsif ($cellemb{$rid} eq 'emb') {
1.14 www 415: $r->print('>'.$metainfo.'<embed src="'.
1.13 www 416: $hash{'src_'.$rid}.'"></embed>');
417: }
1.6 www 418: $r->print('</td>');
1.4 www 419: }
420: $r->print('</tr>');
1.5 www 421: }
1.4 www 422: }
423: $r->print("\n</table>");
1.7 www 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: }
1.25 www 430: $r->print('</body>'.&Apache::lonxml::xmlend());
1.3 www 431: # -------------------------------------------------------------------- End page
432: }
1.1 www 433: # ------------------------------------------------------------- End render page
434: } else {
1.3 www 435: $r->content_type('text/html');
436: $r->send_http_header;
437: $r->print('<html><body>Page undefined.</body></html>');
1.1 www 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: }
1.10 www 450: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
1.1 www 451: return HTTP_NOT_ACCEPTABLE;
452: }
453:
454: 1;
455: __END__
456:
457:
458:
459:
460:
461:
462:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>