1: # The LearningOnline Network with CAPA
2: # Page Handler
3: #
4: # $Id: lonpage.pm,v 1.120.4.3 2017/10/14 22:16:59 raeburn 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: #
28: ###
29:
30:
31:
32:
33: package Apache::lonpage;
34:
35: use strict;
36: use Apache::Constants qw(:common :http);
37: use Apache::lonnet;
38: use Apache::loncommon();
39: use Apache::lonhtmlcommon;
40: use Apache::lonxml();
41: use Apache::lonlocal;
42: use Apache::lonmenu;
43: use Apache::lonhomework;
44: use Apache::lonparmset;
45: use Apache::lonenc();
46: use HTML::TokeParser;
47: use GDBM_File;
48: use Apache::lonsequence;
49: use lib '/home/httpd/lib/perl/';
50: use LONCAPA;
51:
52:
53: # -------------------------------------------------------------- Module Globals
54: my %hash;
55: my @rows;
56:
57: # ------------------------------------------------------------------ Euclid gcd
58:
59: sub euclid {
60: my ($e,$f)=@_;
61: my $a; my $b; my $r;
62: if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
63: while ($r!=0) {
64: $a=$b; $b=$r;
65: $r=$a%$b;
66: }
67: return $b;
68: }
69:
70: # ------------------------------------------------------------ Build page table
71:
72: sub tracetable {
73: my ($sofar,$rid,$beenhere)=@_;
74: my $further=$sofar;
75: my $randomout=0;
76: unless ($env{'request.role.adv'}) {
77: $randomout = $hash{'randomout_'.$rid};
78: }
79: unless ($beenhere=~/\&$rid\&/) {
80: $beenhere.=$rid.'&';
81: unless ($randomout) {
82: if (defined($hash{'is_map_'.$rid})) {
83: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
84: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
85: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
86: $sofar=
87: &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
88: '&'.$frid.$beenhere);
89: $sofar++;
90: if ($hash{'src_'.$frid}) {
91: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
92: if (($brepriv eq '2') || ($brepriv eq 'F')) {
93: if (defined($rows[$sofar])) {
94: $rows[$sofar].='&'.$frid;
95: } else {
96: $rows[$sofar]=$frid;
97: }
98: }
99: }
100: }
101: } else {
102: $sofar++;
103: if ($hash{'src_'.$rid}) {
104: my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
105: if (($brepriv eq '2') || ($brepriv eq 'F')) {
106: if (defined($rows[$sofar])) {
107: $rows[$sofar].='&'.$rid;
108: } else {
109: $rows[$sofar]=$rid;
110: }
111: }
112: }
113: }
114: }
115:
116: if (defined($hash{'to_'.$rid})) {
117: my $mincond=1;
118: my $next='';
119: foreach (split(/\,/,$hash{'to_'.$rid})) {
120: my $thiscond=
121: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
122: if ($thiscond>=$mincond) {
123: if ($next) {
124: $next.=','.$_.':'.$thiscond;
125: } else {
126: $next=$_.':'.$thiscond;
127: }
128: if ($thiscond>$mincond) { $mincond=$thiscond; }
129: }
130: }
131: foreach (split(/\,/,$next)) {
132: my ($linkid,$condval)=split(/\:/,$_);
133: if ($condval>=$mincond) {
134: my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
135: if ($now>$further) { $further=$now; }
136: }
137: }
138: }
139: }
140: return $further;
141: }
142:
143: # ================================================================ Main Handler
144:
145: sub handler {
146: my $r=shift;
147:
148: # ------------------------------------------- Set document type for header only
149:
150: if ($r->header_only) {
151: if ($env{'browser.mathml'}) {
152: &Apache::loncommon::content_type($r,'text/xml');
153: } else {
154: &Apache::loncommon::content_type($r,'text/html');
155: }
156: $r->send_http_header;
157: return OK;
158: }
159:
160: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
161: ['forceselect','launch']);
162: my $number_of_columns = 1;
163: my $requrl=$r->uri;
164: my $target = $env{'form.grade_target'};
165:
166: # Short term solution: define target as 'tex_answer' when retrieving answers
167: # for resources in a .page when generating printouts.
168: # A better long-term fix would be to modify the way problem rendering, and
169: # answer rendering are retrieved for individual resources when printing a .page,
170: # so rendered problem and answer are sequential for individual resources in
171: # the .page
172: #
173: if ($target eq 'answer') {
174: if ($env{'form.answer_output_mode'} eq 'tex') {
175: $target = 'tex_answer';
176: }
177: }
178: # &Apache::lonnet::logthis("Got a target of $target");
179: if ($target eq 'meta') {
180: &Apache::loncommon::content_type($r,'text/html');
181: $r->send_http_header;
182: return OK;
183: }
184: # ----------------------------------------------------------------- Tie db file
185: if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
186: my $fn=$env{'request.course.fn'};
187: if (-e "$fn.db") {
188: my %buttonshide;
189: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
190: # ------------------------------------------------------------------- Hash tied
191: my $firstres=$hash{'map_start_'.$requrl};
192: my $lastres=$hash{'map_finish_'.$requrl};
193: if (($firstres) && ($lastres)) {
194: # ------------------------------------------------------------- Countdown Timer
195: my $now = time;
196: my ($pagefirstaccess,%hastimeleft,%countdowndisp,%donebutton,
197: %donebtnextra,%buttonbytime,$donetime,$symbtosetdone);
198: my ($pagesymb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
199: unless ($pagesymb) {
200: $pagesymb=&Apache::lonnet::symbread($requrl);
201: }
202: if ($pagesymb && ($courseid ne '') && ($domain ne '') && ($name ne '')) {
203: my %times=&Apache::lonnet::get('firstaccesstimes',
204: [$courseid."\0".$pagesymb],
205: $domain,$name);
206: if ($times{$courseid."\0".$pagesymb} =~ /^\d+$/) {
207: $pagefirstaccess = $times{$courseid."\0".$pagesymb};
208: if ($pagefirstaccess && $env{'form.LC_interval_done'} eq 'true') {
209: $donetime = $now - $pagefirstaccess;
210: }
211: }
212: }
213:
214: # ----------------------------------------------------------------- Render page
215:
216: @rows=();
217:
218: &tracetable(0,$firstres,'&');
219:
220: # ------------------------------------------------------------ Add to symb list
221:
222: my $i;
223: my %symbhash=();
224: for ($i=0;$i<=$#rows;$i++) {
225: if ($rows[$i]) {
226: my @colcont=split(/\&/,$rows[$i]);
227: foreach my $rid (@colcont) {
228: my ($mapid,$resid)=split(/\./,$rid);
229: $symbhash{$hash{'src_'.$rid}}=
230: [$hash{'src_'.$rid},$resid];
231: if (($donetime) && ($symbtosetdone eq '')) {
232: my $src = $hash{'src_'.$rid};
233: if ($hash{'encrypted_'.$rid}) {
234: $src=&Apache::lonenc::encrypted($src);
235: }
236: my ($mapid,$resid)=split(/\./,$rid);
237: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$src);
238: if ($src =~ /$LONCAPA::assess_re/) {
239: my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
240: if (@interval > 1) {
241: if (($interval[1] eq 'map') && ($pagefirstaccess)) {
242: my ($timelimit) = ($interval[0] =~ /^(\d+)/);
243: if ($timelimit) {
244: if ($pagefirstaccess + $timelimit > $now) {
245: $symbtosetdone = $symb;
246: }
247: }
248: }
249: }
250: }
251: }
252: }
253: }
254: }
255: &Apache::lonnet::symblist($requrl,%symbhash);
256:
257: # ------------------------------------------------------------------ Page parms
258:
259: my $j;
260: my $lcm=1;
261: my $contents=0;
262: my $nforms=0;
263: my $nuploads=0;
264: my $ntimers=0;
265: my %turninpaths;
266: my %multiresps;
267: my $turninparent;
268:
269: my %ssibody=();
270: my %ssibgcolor=();
271: my %ssitext=();
272: my %ssilink=();
273: my %ssivlink=();
274: my %ssialink=();
275:
276: my %cellemb=();
277: my %cellexternal=();
278:
279: my $allscript='';
280: my $allmeta='';
281:
282: my $isxml=0;
283: my $xmlheader='';
284: my $xmlbody='';
285:
286: # ---------------------------------------------------------- Handle Done button
287:
288: # Set the event timer to zero if the "done button" was clicked.
289: if ($donetime && $symbtosetdone) {
290: &Apache::lonparmset::storeparm_by_symb_inner($symbtosetdone,'0_interval',
291: 2,$donetime,'date_interval',
292: $name,$domain);
293: undef($env{'form.LC_interval_done'});
294: }
295:
296: # --------------------------------------------- Get SSI output, post parameters
297:
298: for ($i=0;$i<=$#rows;$i++) {
299: if ($rows[$i]) {
300: $contents++;
301: my @colcont=split(/\&/,$rows[$i]);
302: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
303: foreach (@colcont) {
304: my $src=$hash{'src_'.$_};
305: my $plainsrc = $src;
306: my ($extension)=($src=~/\.(\w+)$/);
307: $cellexternal{$_}=($hash{'ext_'.$_} eq 'true:');
308: if ($hash{'encrypted_'.$_}) {
309: $src=&Apache::lonenc::encrypted($src);
310: }
311: my ($mapid,$resid)=split(/\./,$_);
312: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$src);
313: unless ($env{'request.role.adv'}) {
314: $buttonshide{$symb} = &Apache::lonnet::EXT("resource.0.buttonshide",$symb);
315: }
316: $cellemb{$_}=
317: &Apache::loncommon::fileembstyle($extension);
318: if ($cellexternal{$_}) {
319: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
320: $ssibody{$_} = <<ENDEXT;
321: <iframe src="$src" width="100%">No iframe support!</iframe>
322: ENDEXT
323: }
324: } elsif ($cellemb{$_} eq 'ssi') {
325: # --------------------------------------------------------- This is an SSI cell
326: my $prefix=$_.'_';
327: my $idprefix= join('_',($mapid,$resid,''));
328: my %posthash=('request.prefix' => $prefix,
329: 'LONCAPA_INTERNAL_no_discussion' => 'true',
330: 'symb' => $symb);
331: if (($env{'form.grade_target'} eq 'tex') ||
332: ($env{'form.answer_output_mode'} eq 'tex')) {
333: $posthash{'grade_target'}=$env{'form.grade_target'};
334: $posthash{'textwidth'}=$env{'form.textwidth'};
335: $posthash{'problem_split'}=$env{'form.problem_split'};
336: $posthash{'latex_type'}=$env{'form.latex_type'};
337: $posthash{'rndseed'}=$env{'form.rndseed'};
338: $posthash{'answer_output_mode'} = $env{'form.answer_output_mode'};
339: }
340: my $submitted=exists($env{'form.all_submit'});
341: if (!$submitted) {
342: foreach my $key (keys(%env)) {
343: if ($key=~/^form.\Q$prefix\Esubmit_/) {
344: $submitted=1;last;
345: }
346: }
347: }
348: if ($submitted) {
349: foreach my $key (keys(%env)) {
350: if ($key=~/^form.\Q$prefix\E/) {
351: my $name=$key;
352: $name=~s/^form.\Q$prefix\E//;
353: $posthash{$name}=$env{$key};
354: }
355: }
356: if (exists($env{'form.all_submit'})) {
357: $posthash{'all_submit'}='yes';
358: }
359: }
360: if ($env{'environment.remote'} eq 'on') {
361: $posthash{'inhibitmenu'} = 'yes';
362: }
363: my $output=Apache::lonnet::ssi($src,%posthash);
364: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
365: if (($target eq 'tex') || ($target eq 'tex_answer')) {
366: $output =~ s/^([^&]+)\\begin\{document}//;
367: $output =~ s/\\end\{document}//;
368: # $output = '\parbox{\minipagewidth}{ '.$output.' }';
369: #some additional cleanup necessary for LateX (due to limitations of table environment
370: $output =~ s/(\\vskip\s*\d+mm)\s*(\\\\)+/$1/g;
371: }
372: my $matheditor;
373: if ($output =~ /\Qjavascript:LC_mathedit_HWVAL_\E/) {
374: $matheditor = 'dragmath';
375: } elsif ($output =~ /LCmathField/) {
376: $matheditor = 'lcmath';
377: }
378: my $parser=HTML::TokeParser->new(\$output);
379: my $token;
380: my $thisdir=$src;
381: my $bodydef=0;
382: my $thisxml=0;
383: my @rlinks=();
384: if ($output=~/\?xml/) {
385: $isxml=1;
386: $thisxml=1;
387: $output=~
388: /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
389: $xmlheader=$1;
390: }
391: while ($token=$parser->get_token) {
392: if ($token->[0] eq 'S') {
393: if ($token->[1] eq 'a') {
394: if ($token->[2]->{'href'}) {
395: $rlinks[$#rlinks+1]=
396: $token->[2]->{'href'};
397: }
398: } elsif ($token->[1] eq 'img') {
399: $rlinks[$#rlinks+1]=
400: $token->[2]->{'src'};
401: } elsif ($token->[1] eq 'embed') {
402: $rlinks[$#rlinks+1]=
403: $token->[2]->{'src'};
404: } elsif ($token->[1] eq 'base') {
405: $thisdir=$token->[2]->{'href'};
406: } elsif ($token->[1] eq 'body') {
407: $bodydef=1;
408: $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
409: $ssitext{$_}=$token->[2]->{'text'};
410: $ssilink{$_}=$token->[2]->{'link'};
411: $ssivlink{$_}=$token->[2]->{'vlink'};
412: $ssialink{$_}=$token->[2]->{'alink'};
413: if ($thisxml) {
414: $xmlbody=$token->[4];
415: }
416: } elsif ($token->[1] eq 'meta') {
417: if ($token->[4] !~ m:/>$:) {
418: $allmeta.="\n".$token->[4].'</meta>';
419: } else {
420: $allmeta.="\n".$token->[4];
421: }
422: } elsif (($token->[1] eq 'script') &&
423: ($bodydef==0)) {
424: $allscript.="\n\n"
425: .$parser->get_text('/script');
426: }
427: }
428: }
429: if ($output=~/\<body[^\>]*\>(.*)/si) {
430: $output=$1;
431: }
432: $output=~s/\<\/body\>.*//si;
433: if ($output=~/\<form/si) {
434: my $hastimer;
435: $nforms++;
436: $output=~s/\<form[^\>]*\>//gsi;
437: $output=~s/\<\/form[^\>]*\>//gsi;
438: if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*HWFILE/) {
439: $nuploads++;
440: }
441: if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*accessbutton/) {
442: $ntimers++;
443: $hastimer = 1;
444: }
445: $output=~
446: s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
447: $output=~
448: s/\<((?:input|select|button|textarea)[^\>]+)id\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 id="$idprefix$2" $3\>/gsi;
449: $output=~
450: s/(\Qthis.form.elements['\E)(HW(?:VAL|CHK)_[^']+\'\]\.(?:value=\'|checked))/$1$prefix$2/gsi;
451: if ($hastimer) {
452: $output=~
453: s/\<(input[^\>]+name=\Q"$prefix\Eaccessbutton"[^\>]+)(?:\Qdocument.markaccess.submit();\E)([^\>]*)\>/\<$1pageTimer(this.form,'$prefix')$2\>/gsi;
454: $output=~ s/\<(input[^\>]+name=\Q"$prefix\Emarkaccess"[^\>]+value=["'])(?:yes)(['"][^\>]*)\>/\<$1$2\>/gsi;
455: }
456: if ($matheditor eq 'dragmath') {
457: $output=~
458: s/(\Qjavascript:LC_mathedit_\E)(HWVAL_)([^'"]+?)(\(['"]*)(\QHWVAL_\E\3['"]\)\;void\(0\)\;)/$1$idprefix$2$3$4$idprefix$5/g;
459: $output=~
460: s/(function\s+LC_mathedit_)(HWVAL_)([^'"]+?)(\s+\(LCtextline\))/$1$idprefix$2$3$4/g;
461: } elsif ($matheditor eq 'lcmath') {
462: $output=~
463: s/(var\s+LCmathField\s+=\s+document\.getElementById\(['"])([^'"]+?)(['"]\)\;)/$1$idprefix$2$3/g;
464: }
465: $output=~
466: s/(\Q<div id="msg_\E)(\Qsubmit_\E)([^"]*)(\Q" style="display:none">\E)/<input type="hidden" name="$prefix$2$3_pressed" id="$idprefix$2$3_pressed" value="" \/>$1$idprefix$2$3$4/g;
467: $output=~
468: s/(\Q<td class="LC_status_\E)(\Qsubmit_\E)([^\"]*)(\s*[^\"]*"\>)/$1$idprefix$2$3$4/g;
469: if ($nuploads) {
470: $output=~
471: s/\<(input[^\>]+name=\"\Q$prefix\EHWFILE[^\>]+)\s*id\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\)]*)\>/\<$1 id="$prefix$2" $3\>/gsi;
472: ($turninpaths{$prefix},$multiresps{$prefix}) =
473: &Apache::loncommon::get_turnedin_filepath($symb,$env{'user.name'},$env{'user.domain'});
474: if ($turninparent eq '') {
475: $turninparent = $turninpaths{$prefix};
476: $turninparent =~ s{(/[^/]+)$}{};
477: }
478: }
479: $output=~
480: s/\<((?:input|select)[^\>]+\Qjavascript:setSubmittedPart\E)\(\s*[\'\"]([^\'\"]+)[\'\"]*\s*\)/\<$1('$2','$prefix')/gsi;
481: $output=~
482: s/\<(input[^\>]+\Qonfocus=\"javascript:disableAutoComplete\E)\(\'([^\']+)\'\)(;\")/\<$1('$idprefix$2')$3/gsi;
483: unless ($hastimer) {
484: if ($plainsrc =~ /$LONCAPA::assess_re/) {
485: %Apache::lonhomework::history =
486: &Apache::lonnet::restore($symb,$courseid,$domain,$name);
487: my $type = 'problem';
488: if ($extension eq 'task') {
489: $type = 'Task';
490: }
491: my ($status,$accessmsg,$slot_name,$slot) =
492: &Apache::lonhomework::check_slot_access('0',$type,$symb);
493: undef(%Apache::lonhomework::history);
494: my $probstatus = &Apache::lonnet::EXT("resource.0.problemstatus",$symb);
495: if (($status eq 'CAN_ANSWER') || (($status eq 'CANNOT_ANSWER') &&
496: (($probstatus eq 'no') || ($probstatus eq 'no_feedback_ever'))) ||
497: (($status eq 'NOT_YET_VIEWED') && ($posthash{'markaccess'} eq 'yes'))) {
498: my ($slothastime,$timerhastime);
499: if ($slot_name ne '') {
500: if (ref($slot) eq 'HASH') {
501: if (($slot->{'starttime'} < $now) &&
502: ($slot->{'endtime'} > $now)) {
503: $slothastime = $now - $slot->{'endtime'};
504: }
505: }
506: }
507: my $duedate = &Apache::lonnet::EXT("resource.0.duedate",$symb);
508: my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
509: if (@interval > 1) {
510: my $first_access;
511: if ($interval[1] eq 'map') {
512: my $ignorecache;
513: if ($env{'form.'.$prefix.'markaccess'} eq 'yes') {
514: $ignorecache = 1;
515: }
516: $first_access=&Apache::lonnet::get_first_access($interval[1],undef,$pagesymb,$ignorecache);
517: if (($first_access) && (!$pagefirstaccess)) {
518: $pagefirstaccess = $first_access;
519: }
520: } else {
521: $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
522: }
523: if ($first_access > 0) {
524: my ($timelimit) = ($interval[0] =~ /^(\d+)/);
525: if ($timelimit) {
526: my $timeremains = $timelimit + $first_access - $now;
527: if ($timeremains > 0) {
528: $timerhastime = $timeremains;
529: }
530: }
531: }
532: }
533: if (($duedate && $duedate > $now) ||
534: (!$duedate && $timerhastime > 0) ||
535: ($slot_name ne '' && $slothastime)) {
536: if ((@interval > 1 && $timerhastime) ||
537: ($type eq 'Task' && $slothastime)) {
538: $countdowndisp{$symb} = 'inline';
539: if ((@interval > 1) && ($timerhastime)) {
540: $hastimeleft{$symb} = $timerhastime;
541: if ($pagefirstaccess) {
542: my ($timelimit,$usesdone,$donebuttontext,$proctor,$secret);
543: ($timelimit,my $donesuffix) = split(/_/,$interval[0],2);
544: if ($donesuffix =~ /^done\:([^\:]+)\:(.*)$/) {
545: $usesdone = 'done';
546: $donebuttontext = $1;
547: (undef,$proctor,$secret) = split(/_/,$2);
548: } elsif ($donesuffix =~ /^done(|_.+)$/) {
549: $donebuttontext = &mt('Done');
550: ($usesdone,$proctor,$secret) = split(/_/,$donesuffix);
551: }
552: if ($usesdone eq 'done') {
553: $donebutton{$symb} = $timelimit;
554: push(@{$buttonbytime{$timelimit}},$symb);
555: $donebtnextra{$symb} = {
556: text => $donebuttontext,
557: proctor => $proctor,
558: secret => $secret,
559: type => $interval[1],
560: };
561: }
562: }
563: } else {
564: $hastimeleft{$symb} = $slothastime;
565: }
566: } else {
567: $hastimeleft{$symb} = $duedate - $now;
568: $countdowndisp{$symb} = 'none';
569: }
570: unless ($donebutton{$symb}) {
571: $donebutton{$symb} = 0;
572: }
573: }
574: }
575: }
576: }
577: }
578: $thisdir=~s/\/[^\/]*$//;
579: foreach (@rlinks) {
580: unless (($_=~/^https?\:\/\//i) ||
581: ($_=~/^\//) ||
582: ($_=~/^javascript:/i) ||
583: ($_=~/^mailto:/i) ||
584: ($_=~/^\#/)) {
585: my $newlocation=
586: &Apache::lonnet::hreflocation($thisdir,$_);
587: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
588: }
589: }
590: # -------------------------------------------------- Deal with Applet codebases
591: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
592: $ssibody{$_}=$output;
593: # ---------------------------------------------------------------- End SSI cell
594: }
595: }
596: }
597: }
598: unless ($contents) {
599: &Apache::loncommon::content_type($r,'text/html');
600: $r->send_http_header;
601: $r->print(&Apache::loncommon::start_page(undef,undef,
602: {'force_register' => 1,}));
603: $r->print(&mt('This page is either empty or it only contains resources that are currently hidden').'. ');
604: $r->print('<br /><br />'.&mt('Please use the LON-CAPA navigation arrows to move to another item in the course').
605: &Apache::loncommon::end_page());
606: } else {
607: # ------------------------------------------------------------------ Build page
608:
609: # ---------------------------------------------------------------- Send headers
610: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
611: if ($isxml) {
612: &Apache::loncommon::content_type($r,'text/xml');
613: } else {
614: &Apache::loncommon::content_type($r,'text/html');
615: }
616: $r->send_http_header;
617: # ------------------------------------------------------------------------ Head
618: if ($allscript) {
619: $allscript =
620: "\n".'<script type="text/javascript">'."\n".
621: $allscript.
622: "\n</script>\n";
623: }
624: if (($nforms) && ($nuploads)) {
625: $allscript .= &Apache::lonhtmlcommon::file_submissionchk_js(\%turninpaths,\%multiresps);
626: }
627: if (($nforms) && (&Apache::lonhtmlcommon::htmlareabrowser())) {
628: my %textarea_args = (
629: dragmath => 'math',
630: );
631: $allscript .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args);
632: }
633: if ($ntimers) {
634: $allscript .= '<script type="text/javascript">'."\n".
635: '// <![CDATA['."\n".
636: 'function pageTimer(form,prefix) {'."\n".
637: " form.elements[prefix+'markaccess'].value = 'yes';\n".
638: " form.submit();\n".
639: '}'."\n".
640: '// ]]>'.
641: "\n</script>\n";
642: }
643: if (keys(%hastimeleft)) {
644: my (%uniquetimes,%uniquedisplays);
645: foreach my $item (values(%hastimeleft)) {
646: if (exists($uniquetimes{$item})) {
647: $uniquetimes{$item} ++;
648: } else {
649: $uniquetimes{$item} = 1;
650: }
651: }
652: if (scalar(keys(%uniquetimes)) == 1) {
653: my (%uniquedisplays,%uniquedones,$currdisp,$donebuttontime,
654: $donebuttonextras);
655: if (keys(%countdowndisp)) {
656: foreach my $item (values(%countdowndisp)) {
657: if (exists($uniquedisplays{$item})) {
658: $uniquedisplays{$item} ++;
659: } else {
660: $uniquedisplays{$item} = 1;
661: }
662: }
663: my @countdowndisplay = keys(%uniquedisplays);
664: if (scalar(@countdowndisplay) == 1) {
665: $currdisp = $countdowndisplay[0];
666: }
667: }
668: if (keys(%donebutton)) {
669: foreach my $item (values(%donebutton)) {
670: if (exists($uniquedones{$item})) {
671: $uniquedones{$item} ++;
672: } else {
673: $uniquedones{$item} = 1;
674: }
675: }
676: my @donebuttons = sort { $ <=> $b } (keys(%uniquedones));
677: if (scalar(@donebuttons) == 1) {
678: if ($donebuttons[0]) {
679: $donebuttontime = $donebuttons[0];
680: if (ref($buttonbytime{$donebuttontime}) eq 'ARRAY') {
681: $donebuttonextras = $donebtnextra{$buttonbytime{$donebuttontime}->[0]};
682: }
683: }
684: }
685: }
686: &add_countdown_timer($currdisp,$donebuttontime,$donebuttonextras);
687: }
688: }
689: my $pagebuttonshide;
690: if (keys(%buttonshide)) {
691: my %uniquebuttonhide;
692: foreach my $item (values(%buttonshide)) {
693: if (exists($uniquebuttonhide{$item})) {
694: $uniquebuttonhide{$item} ++;
695: } else {
696: $uniquebuttonhide{$item} = 1;
697: }
698: }
699: if (keys(%uniquebuttonhide) == 1) {
700: if (lc((keys(%uniquebuttonhide))[0]) eq 'yes') {
701: $pagebuttonshide = 'yes';
702: }
703: }
704: }
705: # ------------------------------------------------------------------ Start body
706: $r->print(&Apache::loncommon::start_page(undef,$allscript,
707: {'force_register' => 1,
708: 'bgcolor' => '#ffffff',
709: 'hide_buttons' => $pagebuttonshide}));
710: # ------------------------------------------------------------------ Start form
711: if ($nforms) {
712: my $fmtag = '<form name="lonhomework" method="post" enctype="multipart/form-data"';
713: if ($nuploads) {
714: my $multi;
715: if ($nuploads > 1) {
716: $multi = 1;
717: }
718: $fmtag .= 'onsubmit="return file_submission_check(this,'."'$turninparent','$multi'".');"';
719: }
720: $fmtag .= ' action="'.
721: &Apache::lonenc::check_encrypt($requrl)
722: .'" id="LC_page">';
723: $r->print($fmtag);
724: }
725: } elsif (($target eq 'tex') || ($target eq 'tex_answer')) {
726: # I think this is not needed as the header
727: # will be put in for each of the page parts
728: # by the londefdef.pm now that we are opening up
729: # the parts of a page.
730: #$r->print('\documentclass{article}
731: # \newcommand{\keephidden}[1]{}
732: # \usepackage[dvips]{graphicx}
733: # \usepackage{epsfig}
734: # \usepackage{calc}
735: # \usepackage{longtable}
736: # \begin{document}');
737: }
738: # ----------------------------------------------------------------- Start table
739: if (($target eq 'tex') || ($target eq 'tex_answer')) {
740: # # $r->print('\begin{longtable}INSERTTHEHEADOFLONGTABLE\endfirsthead\endhead ');
741: if ($number_of_columns le $lcm) {$number_of_columns=$lcm;};
742: } else {
743: $r->print('<table width="100%" cols="'.$lcm.'" border="0">');
744: }
745: # generate rows
746: for ($i=0;$i<=$#rows;$i++) {
747: if ($rows[$i]) {
748: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
749: $r->print("\n<tr>");
750: }
751: my @colcont=split(/\&/,$rows[$i]);
752: my $avespan=$lcm/($#colcont+1);
753: for ($j=0;$j<=$#colcont;$j++) {
754: my $rid=$colcont[$j];
755: my $metainfo =&get_buttons(\%hash,$rid,\%buttonshide).'<br />';
756: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
757: $r->print('<td colspan="'.$avespan.'"');
758: }
759: if (($cellemb{$rid} eq 'ssi') || ($cellexternal{$rid})) {
760: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
761: if ($ssibgcolor{$rid}) {
762: $r->print(' bgcolor="'.
763: $ssibgcolor{$rid}.'"');
764: }
765: $r->print('>'.$metainfo.'<font');
766:
767: if ($ssitext{$rid}) {
768: $r->print(' text="'.$ssitext{$rid}.'"');
769: }
770: if ($ssilink{$rid}) {
771: $r->print(' link="'.$ssilink{$rid}.'"');
772: }
773: if ($ssitext{$rid}) {
774: $r->print(' vlink="'.$ssivlink{$rid}.'"');
775: }
776: if ($ssialink{$rid}) {
777: $r->print(' alink="'.$ssialink{$rid}.'"');
778: }
779: $r->print('>');
780: }
781: unless (($cellexternal{$rid}) &&
782: ($target eq 'tex') && ($target eq 'tex_answer')) {
783: $r->print($ssibody{$rid});
784: }
785: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
786: $r->print('</font>');
787: }
788: if ($env{'course.'.
789: $env{'request.course.id'}.
790: '.pageseparators'} eq 'yes') {
791: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
792: $r->print('<hr />');
793: }
794: }
795: } elsif ($cellemb{$rid} eq 'img') {
796: $r->print('>'.$metainfo.'<img src="'.
797: $hash{'src_'.$rid}.'" />');
798: } elsif ($cellemb{$rid} eq 'emb') {
799: $r->print('>'.$metainfo.'<embed src="'.
800: $hash{'src_'.$rid}.'"></embed>');
801: } elsif (&Apache::lonnet::declutter($hash{'src_'.$rid}) !~/\.(sequence|page)$/) {
802: $r->print($metainfo.'<b>'.$hash{'title_'.$rid}.'</b><br />');
803: unless ($cellemb{$rid} eq 'wrp') {
804: $r->print(&mt('It is recommended that you use an up-to-date virus scanner before handling this file.'));
805: }
806: $r->print('</p><p><table>'.
807: &Apache::londocs::entryline(0,
808: &mt("Click to download or use your browser's Save Link function"),
809: '/'.&Apache::lonnet::declutter($hash{'src_'.$rid})).
810: '</table></p><br />');
811: }
812: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
813: $r->print('</td>');
814: } else {
815: # for (my $incol=1;$incol<=$avespan;$incol++) {
816: # $r->print(' & ');
817: # }
818: }
819: }
820: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
821: $r->print('</tr>');
822: } else {
823: # $r->print('REMOVETHEHEADOFLONGTABLE\\\\');
824: }
825: }
826: }
827: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
828: $r->print("\n</table>");
829: } else {
830: # $r->print('\end{longtable}\strut');
831: }
832: # ---------------------------------------------------------------- Submit, etc.
833: if ($nforms) {
834: my $class;
835: if ($nforms > 1) {
836: $class = ' class="LC_hwk_submit"';
837: if ($ntimers) {
838: $nforms = 1;
839: $class = '';
840: }
841: }
842: $r->print(
843: '<input name="all_submit" value="'.&mt('Submit All').'" type="'.
844: (($nforms>1)?'submit':'hidden').'"'.$class.' id="all_submit" />'.
845: '<div id="msg_all_submit" style="display:none">'.
846: &mt('Processing your submission ...').'</div></form>');
847: }
848: unless (($target eq 'tex') || ($target eq 'tex_answer')) {
849: $r->print(&Apache::loncommon::end_page({'discussion'
850: => 1,}));
851: } else {
852: $r->print('\end{document}'.$number_of_columns);
853: }
854: &Apache::lonnet::symblist($requrl,%symbhash);
855: my ($map,$id,$url)=&Apache::lonnet::decode_symb(&Apache::lonnet::symbread());
856: &Apache::lonnet::symblist($map,'last_known'=>[$url,$id]);
857: # -------------------------------------------------------------------- End page
858: }
859: # ------------------------------------------------------------- End render page
860: } else {
861: &Apache::loncommon::content_type($r,'text/html');
862: $r->send_http_header;
863: &Apache::lonsequence::viewmap($r,$requrl);
864: }
865: # ------------------------------------------------------------------ Untie hash
866: unless (untie(%hash)) {
867: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
868: "Could not untie coursemap $fn (browse).</font>");
869: }
870: # -------------------------------------------------------------------- All done
871: return OK;
872: # ----------------------------------------------- Errors, hash could no be tied
873: }
874: }
875: }
876: &Apache::loncommon::content_type($r,'text/html');
877: $r->send_http_header;
878: &Apache::lonsequence::viewmap($r,$requrl);
879: return OK;
880: }
881:
882: sub get_buttons {
883: my ($hash,$rid,$buttonshide) = @_;
884:
885: my $metainfo = '';
886: my $esrc=&Apache::lonnet::declutter($hash->{'src_'.$rid});
887: my ($mapid,$resid)=split(/\./,$rid);
888: my $symb=&Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
889: $resid,
890: $hash->{'src_'.$rid});
891: unless ($env{'request.role.adv'}) {
892: if ($buttonshide->{$symb} eq 'yes') {
893: return;
894: }
895: }
896: if ($hash->{'encrypted_'.$rid}) {
897: $symb=&Apache::lonenc::encrypted($symb);
898: $esrc=&Apache::lonenc::encrypted($esrc);
899: }
900: if ($hash->{'src_'.$rid} !~ m-^/uploaded/-
901: && $hash->{'src_'.$rid} !~ m{^https?://}
902: && !$env{'request.enc'}
903: && ($env{'request.role.adv'}
904: || !$hash->{'encrypted_'.$rid})) {
905: $metainfo .='<a name="'.&escape($symb).'" />'.
906: '<a href="'.$hash->{'src_'.$rid}.'.meta'.'" target="LONcatInfo">'.
907: '<img src="/res/adm/pages/catalog.png" class="LC_icon"'.
908: ' alt="'.&mt('Show Metadata').'"'.
909: ' title="'.&mt('Show Metadata').'" />'.
910: '</a>';
911: }
912: if (($hash->{'src_'.$rid} !~ m{^/uploaded/}) &&
913: ($hash->{'src_'.$rid} !~ m{^https?://})) {
914: $metainfo .= '<a href="/adm/evaluate?postdata='.
915: &escape($esrc).
916: '" target="LONcatInfo">'.
917: '<img src="/res/adm/pages/eval.png" class="LC_icon"'.
918: ' alt="'.&mt('Provide my evaluation of this resource').'"'.
919: ' title="'.&mt('Provide my evaluation of this resource').'" />'.
920: '</a>';
921: }
922: if (($hash->{'src_'.$rid}=~/$LONCAPA::assess_re/) &&
923: ($hash->{'src_'.$rid} !~ m-^/uploaded/-)) {
924:
925: if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'})) {
926: $metainfo.=
927: '<a href="/adm/grades?symb='.&escape($symb).
928: # '&command=submission" target="LONcatInfo">'.
929: '&command=submission">'.
930: '<img src="/adm/lonMisc/subm_button.png" class="LC_icon"'.
931: ' alt="'.&mt('View Submissions for a Student or a Group of Students').'"'.
932: ' title="'.&mt('View Submissions for a Student or a Group of Students').'" />'.
933: '</a>'.
934: '<a href="/adm/grades?symb='.&escape($symb).
935: # '&command=gradingmenu" target="LONcatInfo">'.
936: '&command=gradingmenu">'.
937: '<img src="/res/adm/pages/pgrd.png" class="LC_icon"'.
938: ' alt="'.&mt('Content Grades').'"'.
939: ' title="'.&mt('Content Grades').'" />'.
940: '</a>';
941: }
942: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
943: $metainfo.=
944: '<a href="/adm/parmset?symb='.&escape($symb).
945: # '" target="LONcatInfo">'.
946: '" >'.
947: '<img src="/adm/lonMisc/pprm_button.png" class="LC_icon"'.
948: ' alt="'.&mt('Content Settings').'"'.
949: ' title="'.&mt('Content Settings').'" />'.
950: '</a>';
951: }
952: }
953: if ($env{'request.course.id'}) {
954: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
955: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
956: my $file=&Apache::lonnet::declutter($hash->{'src_'.$rid});
957: my $editbutton = '';
958: if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
959: my ($cfile,$home,$switchserver,$forceedit,$forceview) =
960: &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,$hash->{'src_'.$rid},$symb);
961: if ($cfile ne '') {
962: my $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver,
963: $forceedit,1,$symb,undef,
964: &escape($env{'form.title'}));
965: if ($jscall) {
966: $editbutton = 1;
967: my $icon = 'pcstr.png';
968: my $label = &mt('Edit');
969: my $title = &mt('Edit this resource');
970: my $pic = '<img src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$icon).'"'.
971: ' class="LC_icon" alt="'.$label.'" title="'.$title.'" />';
972: $metainfo .= ' <a href="javascript:'.$jscall.';">'.$pic.'</a>';
973: }
974: }
975: }
976: if ((!$editbutton) && ($file=~/$LONCAPA::assess_re/)) {
977: my $url = &Apache::lonnet::clutter($file);
978: my $viewsrcbutton;
979: if ((&Apache::lonnet::allowed('cre','/')) &&
980: (&Apache::lonnet::metadata($url,'sourceavail') eq 'open')) {
981: $viewsrcbutton = 1;
982: } elsif (&Apache::lonnet::allowed('vxc',$env{'request.course.id'})) {
983: if ($url =~ m{^\Q/res/$cdom/\E($LONCAPA::match_username)/}) {
984: my $auname = $1;
985: if (($env{'request.course.adhocsrcaccess'} ne '') &&
986: (grep(/^\Q$auname\E$/,split(/,/,$env{'request.course.adhocsrcaccess'})))) {
987: $viewsrcbutton = 1;
988: }
989: }
990: }
991: if ($viewsrcbutton) {
992: my $icon = 'pcstr.png';
993: my $label = &mt('View Source');
994: my $title = &mt('View source code');
995: my $jsrid = $rid;
996: $jsrid =~ s/\./_/g;
997: my $showurl = &escape(&Apache::lonenc::check_encrypt($url));
998: my $pic = '<img src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$icon).'"'.
999: ' class="LC_icon" alt="'.$label.'" title="'.$title.'" />';
1000: $metainfo .= ' <a href="javascript:open_source_'.$jsrid.'();">'.$pic.'</a>'."\n".
1001: '<script type="text/javascript">'."\n".
1002: "function open_source_$jsrid() {\n".
1003: " sourcewin=window.open('/adm/source?inhibitmenu=yes&viewonly=1&filename=$showurl','LONsource',".
1004: "'height=500,width=600,resizable=yes,location=no,menubar=no,toolbar=no,scrollbars=yes');\n".
1005: "}\n".
1006: "</script>\n";
1007: }
1008: }
1009: }
1010: return $metainfo;
1011: }
1012:
1013: sub add_countdown_timer {
1014: my ($currdisp,$donebuttontime,$donebuttonextras) = @_;
1015: my ($collapse,$expand,$alttxt,$title,$donebutton);
1016: if ($currdisp eq 'inline') {
1017: $collapse = '► ';
1018: } else {
1019: $expand = '◄ ';
1020: }
1021: if ($donebuttontime) {
1022: my ($type,$proctor,$donebuttontext);
1023: if (ref($donebuttonextras) eq 'HASH') {
1024: $proctor = $donebuttonextras->{'proctor'};
1025: $donebuttontext = $donebuttonextras->{'text'};
1026: $type = $donebuttonextras->{'type'};
1027: } else {
1028: $donebuttontext = &mt('Done');
1029: $type = 'map';
1030: }
1031: $donebutton =
1032: &Apache::lonmenu::done_button_js($type,'','',$proctor,$donebuttontext);
1033: }
1034: unless ($env{'environment.icons'} eq 'iconsonly') {
1035: $alttxt = &mt('Timer');
1036: $title = $alttxt.' ';
1037: }
1038: my $desc = &mt('Countdown to due date/time');
1039: my $output = <<END;
1040: $donebutton
1041: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
1042: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
1043: $collapse
1044: </span></a>
1045: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
1046: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
1047: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
1048: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
1049: END
1050: &Apache::lonhtmlcommon::clear_breadcrumb_tools();
1051: &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$output);
1052: return;
1053: }
1054:
1055:
1056: 1;
1057: __END__
1058:
1059:
1060: =head1 NAME
1061:
1062: Apache::lonpage - Page Handler
1063:
1064: =head1 SYNOPSIS
1065:
1066: Invoked by /etc/httpd/conf/srm.conf:
1067:
1068: <LocationMatch "^/res/.*\.page$>
1069: SetHandler perl-script
1070: PerlHandler Apache::lonpage
1071: </LocationMatch>
1072:
1073: =head1 INTRODUCTION
1074:
1075: This module renders a .page resource.
1076:
1077: This is part of the LearningOnline Network with CAPA project
1078: described at http://www.lon-capa.org.
1079:
1080: =head1 HANDLER SUBROUTINE
1081:
1082: This routine is called by Apache and mod_perl.
1083:
1084: =over 4
1085:
1086: =item *
1087:
1088: set document type for header only
1089:
1090: =item *
1091:
1092: tie db file
1093:
1094: =item *
1095:
1096: render page
1097:
1098: =item *
1099:
1100: add to symb list
1101:
1102: =item *
1103:
1104: page parms
1105:
1106: =item *
1107:
1108: Get SSI output, post parameters
1109:
1110: =item *
1111:
1112: SSI cell rendering
1113:
1114: =item *
1115:
1116: Deal with Applet codebases
1117:
1118: =item *
1119:
1120: Build page
1121:
1122: =item *
1123:
1124: send headers
1125:
1126: =item *
1127:
1128: start body
1129:
1130: =item *
1131:
1132: start form
1133:
1134: =item *
1135:
1136: start table
1137:
1138: =item *
1139:
1140: submit element, etc, render page, untie hash
1141:
1142: =back
1143:
1144: =head1 OTHER SUBROUTINES
1145:
1146: =over 4
1147:
1148: =item *
1149:
1150: euclid() : Euclid's method for determining the greatest common denominator.
1151:
1152: =item *
1153:
1154: tracetable() : Build page table.
1155:
1156: =back
1157:
1158: =cut
1159:
1160:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>