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