1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.155 2002/03/06 20:28:19 matthew 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: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39: # last modified 06/26/00 by Alexander Sakharuk
40: # 11/6 Gerd Kortemeyer
41: # 6/1/1 Gerd Kortemeyer
42: # 2/21,3/13 Guy
43: # 3/29,5/4 Gerd Kortemeyer
44: # 5/10 Scott Harrison
45: # 5/26 Gerd Kortemeyer
46: # 5/27 H. K. Ng
47: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
48: # 6/12,6/13 H. K. Ng
49: # 6/16 Gerd Kortemeyer
50: # 7/27 H. K. Ng
51: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
52: # Guy Albertelli
53: # 9/26 Gerd Kortemeyer
54: # Dec Guy Albertelli
55: # YEAR=2002
56: # 1/1 Gerd Kortemeyer
57: # 1/2 Matthew Hall
58: # 1/3 Gerd Kortemeyer
59: #
60:
61: package Apache::lonxml;
62: use vars
63: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
64: use strict;
65: use HTML::TokeParser;
66: use HTML::TreeBuilder;
67: use Safe;
68: use Safe::Hole;
69: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
70: use Math::Random qw(:all);
71: use Opcode;
72:
73: sub register {
74: my ($space,@taglist) = @_;
75: foreach my $temptag (@taglist) {
76: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
77: }
78: }
79:
80: sub deregister {
81: my ($space,@taglist) = @_;
82: foreach my $temptag (@taglist) {
83: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
84: if ($tempspace eq $space) {
85: pop(@{ $Apache::lonxml::alltags{$temptag} });
86: }
87: }
88: #&printalltags();
89: }
90:
91: use Apache::Constants qw(:common);
92: use Apache::lontexconvert;
93: use Apache::style;
94: use Apache::run;
95: use Apache::londefdef;
96: use Apache::scripttag;
97: use Apache::edit;
98: use Apache::lonnet;
99: use Apache::File;
100: use Apache::loncommon;
101:
102: #================================================== Main subroutine: xmlparse
103: #debugging control, to turn on debugging modify the correct handler
104: $Apache::lonxml::debug=0;
105:
106: #path to the directory containing the file currently being processed
107: @pwd=();
108:
109: #these two are used for capturing a subset of the output for later processing,
110: #don't touch them directly use &startredirection and &endredirection
111: @outputstack = ();
112: $redirection = 0;
113:
114: #controls wheter the <import> tag actually does
115: $import = 1;
116: @extlinks=();
117:
118: # meta mode is a bit weird only some output is to be turned off
119: #<output> tag turns metamode off (defined in londefdef.pm)
120: $metamode = 0;
121:
122: # turns on and of run::evaluate actually derefencing var refs
123: $evaluate = 1;
124:
125: # data structure for eidt mode, determines what tags can go into what other tags
126: %insertlist=();
127:
128: # stores the list of active tag namespaces
129: @namespace=();
130:
131: # has the dynamic menu been updated to know about this resource
132: $Apache::lonxml::registered=0;
133:
134: sub xmlbegin {
135: my $output='';
136: if ($ENV{'browser.mathml'}) {
137: $output='<?xml version="1.0"?>'
138: .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
139: .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
140: .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
141: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
142: .'xmlns="http://www.w3.org/TR/REC-html40">';
143: } else {
144: $output='<html>';
145: }
146: return $output;
147: }
148:
149: sub xmlend {
150: my $discussion='';
151: if ($ENV{'request.course.id'}) {
152: my $crs='/'.$ENV{'request.course.id'};
153: if ($ENV{'request.course.sec'}) {
154: $crs.='_'.$ENV{'request.course.sec'};
155: }
156: $crs=~s/\_/\//g;
157: my $seeid=&Apache::lonnet::allowed('rin',$crs);
158: my $symb=&Apache::lonnet::symbread();
159: if ($symb) {
160: my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
161: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
162: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
163: if ($contrib{'version'}) {
164: $discussion.=
165: '<address><hr /><h2>Course Discussion of Resource</h2>';
166: my $idx;
167: for ($idx=1;$idx<=$contrib{'version'};$idx++) {
168: my $hidden=($contrib{'hidden'}=~/\.$idx\./);
169: unless (($hidden) && (!$seeid)) {
170: my $message=$contrib{$idx.':message'};
171: $message=~s/\n/\<br \/\>/g;
172: if ($message) {
173: if ($hidden) {
174: $message='<font color="#888888">'.$message.'</font>';
175: }
176: my $sender='Anonymous';
177: if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
178: $sender=$contrib{$idx.':sendername'}.' at '.
179: $contrib{$idx.':senderdomain'};
180: if ($contrib{$idx.':anonymous'}) {
181: $sender.=' (anonymous)';
182: }
183: if ($seeid) {
184: if ($hidden) {
185: $sender.=' <a href="/adm/feedback?unhide='.
186: $symb.':::'.$idx.'">Make Visible</a>';
187: } else {
188: $sender.=' <a href="/adm/feedback?hide='.
189: $symb.':::'.$idx.'">Hide</a>';
190: }
191: }
192: }
193: $discussion.='<p><b>'.$sender.'</b> ('.
194: localtime($contrib{$idx.':timestamp'}).
195: '):<blockquote>'.$message.
196: '</blockquote></p>';
197: }
198: }
199: }
200: $discussion.='</address>';
201: }
202: }
203: }
204: return $discussion.'</html>';
205: }
206:
207: sub tokeninputfield {
208: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
209: $defhost=~tr/a-z/A-Z/;
210: return (<<ENDINPUTFIELD)
211: <script>
212: function updatetoken() {
213: var comp=new Array;
214: var barcode=unescape(document.tokeninput.barcode.value);
215: comp=barcode.split('*');
216: if (typeof(comp[0])!="undefined") {
217: document.tokeninput.codeone.value=comp[0];
218: }
219: if (typeof(comp[1])!="undefined") {
220: document.tokeninput.codetwo.value=comp[1];
221: }
222: if (typeof(comp[2])!="undefined") {
223: comp[2]=comp[2].toUpperCase();
224: document.tokeninput.codethree.value=comp[2];
225: }
226: document.tokeninput.barcode.value='';
227: }
228: </script>
229: <form method="post" name="tokeninput">
230: <table border="2" bgcolor="#FFFFBB">
231: <tr><th>DocID Checkin</th></tr>
232: <tr><td>
233: <table>
234: <tr>
235: <td>Scan in Barcode</td>
236: <td><input type="text" size="22" name="barcode"
237: onChange="updatetoken()"/></td>
238: </tr>
239: <tr><td><i>or</i> Type in DocID</td>
240: <td>
241: <input type="text" size="5" name="codeone" />
242: <b><font size="+2">*</font></b>
243: <input type="text" size="5" name="codetwo" />
244: <b><font size="+2">*</font></b>
245: <input type="text" size="10" name="codethree" value="$defhost"
246: onChange="this.value=this.value.toUpperCase()" />
247: </td></tr>
248: </table>
249: </td></tr>
250: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
251: </table>
252: </form>
253: ENDINPUTFIELD
254: }
255:
256: sub maketoken {
257: my ($symb,$tuname,$tudom,$tcrsid)=@_;
258: unless ($symb) {
259: $symb=&Apache::lonnet::symbread();
260: }
261: unless ($tuname) {
262: $tuname=$ENV{'user.name'};
263: $tudom=$ENV{'user.domain'};
264: $tcrsid=$ENV{'request.course.id'};
265: }
266:
267: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
268: }
269:
270: sub printtokenheader {
271: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
272: unless ($token) { return ''; }
273:
274: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
275: unless ($tsymb) {
276: $tsymb=$symb;
277: }
278: unless ($tuname) {
279: $tuname=$name;
280: $tudom=$domain;
281: $tcrsid=$courseid;
282: }
283:
284: my %reply=&Apache::lonnet::get('environment',
285: ['firstname','middlename','lastname','generation'],
286: $tudom,$tuname);
287: my $plainname=$reply{'firstname'}.' '.
288: $reply{'middlename'}.' '.
289: $reply{'lastname'}.' '.
290: $reply{'generation'};
291:
292: if ($target eq 'web') {
293: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
294: return
295: '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
296: 'Checked out for '.$plainname.
297: '<br />User: '.$tuname.' at '.$tudom.
298: '<br />ID: '.$idhash{$tuname}.
299: '<br />CourseID: '.$tcrsid.
300: '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
301: '<br />DocID: '.$token.
302: '<br />Time: '.localtime().'<hr />';
303: } else {
304: return $token;
305: }
306: }
307:
308: sub fontsettings() {
309: my $headerstring='';
310: if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {
311: $headerstring.=
312: '<meta Content-Type="text/html; charset=x-mac-roman">';
313: }
314: return $headerstring;
315: }
316:
317: sub registerurl {
318: my $forcereg=shift;
319: my $target = shift;
320: my $result = '';
321: if ($ENV{'request.publicaccess'}) {
322: return
323: '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
324: }
325: if ($Apache::lonxml::registered && !$forcereg) { return ''; }
326: $Apache::lonxml::registered=1;
327: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
328: my $hwkadd='';
329: if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
330: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
331: $hwkadd.=(<<ENDSUBM);
332: menu.switchbutton
333: (7,1,'subm.gif','view sub','missions',
334: 'gocmd("/adm/grades","submission")');
335: ENDSUBM
336: }
337: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
338: $hwkadd.=(<<ENDGRDS);
339: menu.switchbutton
340: (7,2,'pgrd.gif','problem','grades',
341: 'gocmd("/adm/grades","viewgrades")');
342: ENDGRDS
343: }
344: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
345: $hwkadd.=(<<ENDPARM);
346: menu.switchbutton
347: (7,3,'pparm.gif','problem','parms',
348: 'gocmd("/adm/parmset","set")');
349: ENDPARM
350: }
351: }
352: $result = (<<ENDREGTHIS);
353:
354: <script language="JavaScript">
355: // BEGIN LON-CAPA Internal
356:
357: function LONCAPAreg() {
358: menu=window.open("","LONCAPAmenu");
359: menu.clearTimeout(menu.menucltim);
360: menu.currentURL=window.location.pathname;
361: menu.currentStale=0;
362: menu.clearbut(3,1);
363: menu.switchbutton
364: (6,3,'catalog.gif','catalog','info','catalog_info()');
365: menu.switchbutton
366: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
367: menu.switchbutton
368: (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
369: menu.switchbutton
370: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
371: menu.switchbutton
372: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
373: menu.switchbutton
374: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
375: menu.switchbutton
376: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
377: menu.switchbutton
378: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
379: menu.switchbutton
380: (9,3,'anot.gif','anno-','tations','annotate()');
381: $hwkadd
382: }
383:
384: function LONCAPAstale() {
385: menu=window.open("","LONCAPAmenu");
386: menu.currentStale=1;
387: menu.switchbutton
388: (3,1,'reload.gif','return','location','go(currentURL)');
389: menu.clearbut(7,1);
390: menu.clearbut(7,2);
391: menu.clearbut(7,3);
392: menu.menucltim=menu.setTimeout(
393: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
394: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
395: 2000);
396:
397: }
398:
399: // END LON-CAPA Internal
400: </script>
401: ENDREGTHIS
402:
403: } else {
404: $result = (<<ENDDONOTREGTHIS);
405:
406: <script language="JavaScript">
407: // BEGIN LON-CAPA Internal
408:
409: function LONCAPAreg() {
410: menu=window.open("","LONCAPAmenu");
411: menu.currentStale=1;
412: menu.clearbut(2,1);
413: menu.clearbut(2,3);
414: menu.clearbut(8,1);
415: menu.clearbut(8,2);
416: menu.clearbut(8,3);
417: if (menu.currentURL) {
418: menu.switchbutton
419: (3,1,'reload.gif','return','location','go(currentURL)');
420: } else {
421: menu.clearbut(3,1);
422: }
423: }
424:
425: function LONCAPAstale() {
426: }
427:
428: // END LON-CAPA Internal
429: </script>
430: ENDDONOTREGTHIS
431: }
432: if ($target eq 'edit') {
433: $result .=<<"ENDBROWSERSCRIPT";
434: <script>
435: var editform;
436: function openbrowser(formname,elementname) {
437: var url = '/res/?';
438: if (editform == null) {
439: url += 'launch=1&';
440: }
441: url += 'catalogmode=interactive&';
442: url += 'mode=edit&';
443: url += 'form=' + formname + '&';
444: url += 'element=' + elementname + '';
445: var title = 'Browser';
446: var options = 'scrollbars=1,resizable=1,menubar=0';
447: options += ',width=700,height=600';
448: editform = open(url,title,options,'1');
449: editform.focus();
450: }
451: </script>
452: ENDBROWSERSCRIPT
453: }
454: return $result;
455: }
456:
457: sub loadevents() {
458: return 'LONCAPAreg();';
459: }
460:
461: sub unloadevents() {
462: return 'LONCAPAstale();';
463: }
464:
465: sub printalltags {
466: my $temp;
467: foreach $temp (sort keys %Apache::lonxml::alltags) {
468: &Apache::lonxml::debug("$temp -- ".
469: join(',',@{ $Apache::lonxml::alltags{$temp} }));
470: }
471: }
472:
473: sub xmlparse {
474: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
475:
476: &setup_globals($target);
477: #&printalltags();
478: my @pars = ();
479: my $pwd=$ENV{'request.filename'};
480: $pwd =~ s:/[^/]*$::;
481: &newparser(\@pars,\$content_file_string,$pwd);
482:
483: my $safeeval = new Safe;
484: my $safehole = new Safe::Hole;
485: &init_safespace($target,$safeeval,$safehole,$safeinit);
486: #-------------------- Redefinition of the target in the case of compound target
487:
488: ($target, my @tenta) = split('&&',$target);
489:
490: my @stack = ();
491: my @parstack = ();
492: &initdepth;
493:
494: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
495: $safeeval,\%style_for_target);
496: if ($ENV{'request.uri'}) {
497: &writeallows($ENV{'request.uri'});
498: }
499: return $finaloutput;
500: }
501:
502: sub htmlclean {
503: my ($raw,$full)=@_;
504:
505: my $tree = HTML::TreeBuilder->new;
506: $tree->ignore_unknown(0);
507:
508: $tree->parse($raw);
509:
510: my $output= $tree->as_HTML(undef,' ');
511:
512: $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
513: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
514: unless ($full) {
515: $output=~s/\<[\/]*(body|head|html)\>//gis;
516: }
517:
518: $tree = $tree->delete;
519:
520: return $output;
521: }
522:
523: sub inner_xmlparse {
524: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
525: my $finaloutput = '';
526: my $result;
527: my $token;
528: while ( $#$pars > -1 ) {
529: while ($token = $$pars['-1']->get_token) {
530: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
531: if ($metamode<1) {
532: $result=$token->[1];
533: }
534: } elsif ($token->[0] eq 'PI') {
535: if ($metamode<1) {
536: $result=$token->[2];
537: }
538: } elsif ($token->[0] eq 'S') {
539: # add tag to stack
540: push (@$stack,$token->[1]);
541: # add parameters list to another stack
542: push (@$parstack,&parstring($token));
543: &increasedepth($token);
544: if (exists $$style_for_target{$token->[1]}) {
545: if ($Apache::lonxml::redirection) {
546: $Apache::lonxml::outputstack['-1'] .=
547: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
548: $style_for_target,@$parstack);
549: } else {
550: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
551: $safeeval,$style_for_target,@$parstack);
552: }
553: } else {
554: $result = &callsub("start_$token->[1]", $target, $token, $stack,
555: $parstack, $pars, $safeeval, $style_for_target);
556: }
557: } elsif ($token->[0] eq 'E') {
558: #clear out any tags that didn't end
559: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
560: my $lasttag=$$stack[-1];
561: if ($token->[1] =~ /^$lasttag$/i) {
562: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
563: last;
564: } else {
565: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
566: &end_tag($stack,$parstack,$token);
567: }
568: }
569:
570: if (exists($$style_for_target{'/'."$token->[1]"})) {
571: if ($Apache::lonxml::redirection) {
572: $Apache::lonxml::outputstack['-1'] .=
573: &recurse($$style_for_target{'/'."$token->[1]"},
574: $target,$safeeval,$style_for_target,@$parstack);
575: } else {
576: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
577: $target,$safeeval,$style_for_target,
578: @$parstack);
579: }
580: } else {
581: $result = &callsub("end_$token->[1]", $target, $token, $stack,
582: $parstack, $pars,$safeeval, $style_for_target);
583: }
584: } else {
585: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
586: }
587: #evaluate variable refs in result
588: if ($result ne "") {
589: if ( $#$parstack > -1 ) {
590: if ($Apache::lonxml::redirection) {
591: $Apache::lonxml::outputstack['-1'] .=
592: &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
593: } else {
594: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
595: $$parstack['-1']);
596: }
597: } else {
598: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
599: }
600: $result = '';
601: }
602: if ($token->[0] eq 'E') {
603: &end_tag($stack,$parstack,$token);
604: }
605: }
606: pop @$pars;
607: pop @Apache::lonxml::pwd;
608: }
609:
610: # if ($target eq 'meta') {
611: # $finaloutput.=&endredirection;
612: # }
613:
614: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
615: $finaloutput=&afterburn($finaloutput);
616: }
617: return $finaloutput;
618: }
619:
620: sub recurse {
621: my @innerstack = ();
622: my @innerparstack = ();
623: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
624: my @pat = ();
625: &newparser(\@pat,\$newarg);
626: my $tokenpat;
627: my $partstring = '';
628: my $output='';
629: my $decls='';
630: &Apache::lonxml::debug("Recursing");
631: while ( $#pat > -1 ) {
632: while ($tokenpat = $pat[$#pat]->get_token) {
633: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
634: if ($metamode<1) { $partstring=$tokenpat->[1]; }
635: } elsif ($tokenpat->[0] eq 'PI') {
636: if ($metamode<1) { $partstring=$tokenpat->[2]; }
637: } elsif ($tokenpat->[0] eq 'S') {
638: push (@innerstack,$tokenpat->[1]);
639: push (@innerparstack,&parstring($tokenpat));
640: &increasedepth($tokenpat);
641: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
642: \@innerstack, \@innerparstack, \@pat,
643: $safeeval, $style_for_target);
644: } elsif ($tokenpat->[0] eq 'E') {
645: #clear out any tags that didn't end
646: while ($tokenpat->[1] ne $innerstack[$#innerstack]
647: && ($#innerstack > -1)) {
648: my $lasttag=$innerstack[-1];
649: if ($tokenpat->[1] =~ /^$lasttag$/i) {
650: &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
651: last;
652: } else {
653: &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
654: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
655: }
656: }
657: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
658: \@innerstack, \@innerparstack, \@pat,
659: $safeeval, $style_for_target);
660: } else {
661: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
662: }
663: #pass both the variable to the style tag, and the tag we
664: #are processing inside the <definedtag>
665: if ( $partstring ne "" ) {
666: if ( $#parstack > -1 ) {
667: if ( $#innerparstack > -1 ) {
668: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
669: } else {
670: $decls= $parstack[$#parstack];
671: }
672: } else {
673: if ( $#innerparstack > -1 ) {
674: $decls=$innerparstack[$#innerparstack];
675: } else {
676: $decls='';
677: }
678: }
679: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
680: $partstring = '';
681: }
682: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
683: &decreasedepth($tokenpat);}
684: }
685: pop @pat;
686: pop @Apache::lonxml::pwd;
687: }
688: &Apache::lonxml::debug("Exiting Recursing");
689: return $output;
690: }
691:
692: sub callsub {
693: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
694: my $currentstring='';
695: my $nodefault;
696: {
697: my $sub1;
698: no strict 'refs';
699: my $tag=$token->[1];
700: my $space=$Apache::lonxml::alltags{$tag}[-1];
701: if (!$space) {
702: $tag=~tr/A-Z/a-z/;
703: $sub=~tr/A-Z/a-z/;
704: $space=$Apache::lonxml::alltags{$tag}[-1]
705: }
706:
707: my $deleted=0;
708: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
709: if (($token->[0] eq 'S') && ($target eq 'modified')) {
710: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
711: $parstack,$parser,$safeeval,
712: $style);
713: }
714: if (!$deleted) {
715: if ($space) {
716: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
717: $sub1="$space\:\:$sub";
718: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
719: $parstack,$parser,$safeeval,
720: $style);
721: } else {
722: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
723: if ($metamode <1) {
724: if (defined($token->[4]) && ($metamode < 1)) {
725: $currentstring = $token->[4];
726: } else {
727: $currentstring = $token->[2];
728: }
729: }
730: }
731: # &Apache::lonxml::debug("nodefalt:$nodefault:");
732: if ($currentstring eq '' && $nodefault eq '') {
733: if ($target eq 'edit') {
734: &Apache::lonxml::debug("doing default edit for $token->[1]");
735: if ($token->[0] eq 'S') {
736: $currentstring = &Apache::edit::tag_start($target,$token);
737: } elsif ($token->[0] eq 'E') {
738: $currentstring = &Apache::edit::tag_end($target,$token);
739: }
740: } elsif ($target eq 'modified') {
741: if ($token->[0] eq 'S') {
742: $currentstring = $token->[4];
743: $currentstring.=&Apache::edit::handle_insert();
744: } else {
745: $currentstring = $token->[2];
746: }
747: }
748: }
749: }
750: use strict 'refs';
751: }
752: return $currentstring;
753: }
754:
755: sub setup_globals {
756: my ($target)=@_;
757: $Apache::lonxml::registered = 0;
758: @Apache::lonxml::pwd=();
759: @Apache::lonxml::extlinks=();
760: if ($target eq 'meta') {
761: $Apache::lonxml::redirection = 0;
762: $Apache::lonxml::metamode = 1;
763: $Apache::lonxml::evaluate = 1;
764: $Apache::lonxml::import = 0;
765: } elsif ($target eq 'answer') {
766: $Apache::lonxml::redirection = 0;
767: $Apache::lonxml::metamode = 1;
768: $Apache::lonxml::evaluate = 1;
769: $Apache::lonxml::import = 1;
770: } elsif ($target eq 'grade') {
771: &startredirection;
772: $Apache::lonxml::metamode = 0;
773: $Apache::lonxml::evaluate = 1;
774: $Apache::lonxml::import = 1;
775: } elsif ($target eq 'modified') {
776: $Apache::lonxml::redirection = 0;
777: $Apache::lonxml::metamode = 0;
778: $Apache::lonxml::evaluate = 0;
779: $Apache::lonxml::import = 0;
780: } elsif ($target eq 'edit') {
781: $Apache::lonxml::redirection = 0;
782: $Apache::lonxml::metamode = 0;
783: $Apache::lonxml::evaluate = 0;
784: $Apache::lonxml::import = 0;
785: } else {
786: $Apache::lonxml::redirection = 0;
787: $Apache::lonxml::metamode = 0;
788: $Apache::lonxml::evaluate = 1;
789: $Apache::lonxml::import = 1;
790: }
791: }
792:
793: sub init_safespace {
794: my ($target,$safeeval,$safehole,$safeinit) = @_;
795: $safeeval->permit("entereval");
796: $safeeval->permit(":base_math");
797: $safeeval->permit("sort");
798: $safeeval->deny(":base_io");
799: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
800: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
801:
802: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
803: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
804: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
805: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
806: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
807: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
808: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
809: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
810: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
811: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
812: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
813: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
814: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
815: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
816: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
817: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
818: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
819: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
820: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
821: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
822: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
823: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
824: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
825: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
826: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
827: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
828: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
829: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
830: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
831: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
832: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
833: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
834: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
835: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
836: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
837: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
838: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
839: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
840: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
841: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
842:
843: #need to inspect this class of ops
844: # $safeeval->deny(":base_orig");
845: $safeinit .= ';$external::target="'.$target.'";';
846: my $rndseed;
847: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
848: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
849: $safeinit .= ';$external::randomseed='.$rndseed.';';
850: &Apache::run::run($safeinit,$safeeval);
851: }
852:
853: sub startredirection {
854: $Apache::lonxml::redirection++;
855: push (@Apache::lonxml::outputstack, '');
856: }
857:
858: sub endredirection {
859: if (!$Apache::lonxml::redirection) {
860: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
861: return '';
862: }
863: $Apache::lonxml::redirection--;
864: pop @Apache::lonxml::outputstack;
865: }
866:
867: sub end_tag {
868: my ($tagstack,$parstack,$token)=@_;
869: pop(@$tagstack);
870: pop(@$parstack);
871: &decreasedepth($token);
872: }
873:
874: sub initdepth {
875: @Apache::lonxml::depthcounter=();
876: $Apache::lonxml::depth=-1;
877: $Apache::lonxml::olddepth=-1;
878: }
879:
880: sub increasedepth {
881: my ($token) = @_;
882: $Apache::lonxml::depth++;
883: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
884: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
885: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
886: }
887: my $curdepth=join('_',@Apache::lonxml::depthcounter);
888: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
889: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
890: }
891:
892: sub decreasedepth {
893: my ($token) = @_;
894: $Apache::lonxml::depth--;
895: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
896: $#Apache::lonxml::depthcounter--;
897: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
898: }
899: if ( $Apache::lonxml::depth < -1) {
900: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
901: $Apache::lonxml::depth='-1';
902: }
903: my $curdepth=join('_',@Apache::lonxml::depthcounter);
904: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
905: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
906: }
907:
908: sub get_all_text {
909:
910: my($tag,$pars)= @_;
911: my $depth=0;
912: my $token;
913: my $result='';
914: if ( $tag =~ m:^/: ) {
915: my $tag=substr($tag,1);
916: # &Apache::lonxml::debug("have:$tag:");
917: while (($depth >=0) && ($token = $pars->get_token)) {
918: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
919: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
920: $result.=$token->[1];
921: } elsif ($token->[0] eq 'PI') {
922: $result.=$token->[2];
923: } elsif ($token->[0] eq 'S') {
924: if ($token->[1] =~ /^$tag$/i) { $depth++; }
925: $result.=$token->[4];
926: } elsif ($token->[0] eq 'E') {
927: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
928: #skip sending back the last end tag
929: if ($depth > -1) { $result.=$token->[2]; } else {
930: $pars->unget_token($token);
931: }
932: }
933: }
934: } else {
935: while ($token = $pars->get_token) {
936: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
937: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
938: $result.=$token->[1];
939: } elsif ($token->[0] eq 'PI') {
940: $result.=$token->[2];
941: } elsif ($token->[0] eq 'S') {
942: if ( $token->[1] =~ /^$tag$/i) {
943: $pars->unget_token($token); last;
944: } else {
945: $result.=$token->[4];
946: }
947: } elsif ($token->[0] eq 'E') {
948: $result.=$token->[2];
949: }
950: }
951: }
952: # &Apache::lonxml::debug("Exit:$result:");
953: return $result
954: }
955:
956: sub newparser {
957: my ($parser,$contentref,$dir) = @_;
958: push (@$parser,HTML::TokeParser->new($contentref));
959: $$parser['-1']->xml_mode('1');
960: if ( $dir eq '' ) {
961: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
962: } else {
963: push (@Apache::lonxml::pwd, $dir);
964: }
965: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
966: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
967: }
968:
969: sub parstring {
970: my ($token) = @_;
971: my $temp='';
972: foreach (@{$token->[3]}) {
973: unless ($_=~/\W/) {
974: my $val=$token->[2]->{$_};
975: $val =~ s/([\%\@\\\"])/\\$1/g;
976: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
977: $temp .= "my \$$_=\"$val\";"
978: }
979: }
980: return $temp;
981: }
982:
983: sub writeallows {
984: unless ($#extlinks>=0) { return; }
985: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
986: if ($ENV{'httpref.'.$thisurl}) {
987: $thisurl=$ENV{'httpref.'.$thisurl};
988: }
989: my $thisdir=$thisurl;
990: $thisdir=~s/\/[^\/]+$//;
991: my %httpref=();
992: foreach (@extlinks) {
993: $httpref{'httpref.'.
994: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
995: }
996: @extlinks=();
997: &Apache::lonnet::appenv(%httpref);
998: }
999:
1000: #
1001: # Afterburner handles anchors, highlights and links
1002: #
1003: sub afterburn {
1004: my $result=shift;
1005: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1006: ['highlight','anchor','link']);
1007: if ($ENV{'form.highlight'}) {
1008: foreach (split(/\,/,$ENV{'form.highlight'})) {
1009: my $anchorname=$_;
1010: my $matchthis=$anchorname;
1011: $matchthis=~s/\_+/\\s\+/g;
1012: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1013: }
1014: }
1015: if ($ENV{'form.link'}) {
1016: foreach (split(/\,/,$ENV{'form.link'})) {
1017: my ($anchorname,$linkurl)=split(/\>/,$_);
1018: my $matchthis=$anchorname;
1019: $matchthis=~s/\_+/\\s\+/g;
1020: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1021: }
1022: }
1023: if ($ENV{'form.anchor'}) {
1024: my $anchorname=$ENV{'form.anchor'};
1025: my $matchthis=$anchorname;
1026: $matchthis=~s/\_+/\\s\+/g;
1027: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1028: $result.=(<<"ENDSCRIPT");
1029: <script>
1030: document.location.hash='$anchorname';
1031: </script>
1032: ENDSCRIPT
1033: }
1034: return $result;
1035: }
1036:
1037: sub storefile {
1038: my ($file,$contents)=@_;
1039: if (my $fh=Apache::File->new('>'.$file)) {
1040: print $fh $contents;
1041: $fh->close();
1042: } else {
1043: &warning("Unable to save file $file");
1044: }
1045: }
1046:
1047: sub createnewhtml {
1048: my $filecontents=(<<SIMPLECONTENT);
1049: <html>
1050: <head>
1051: <title>
1052: Title of Document Goes Here
1053: </title>
1054: </head>
1055: <body bgcolor="#FFFFFF">
1056:
1057: Body of Document Goes Here
1058:
1059: </body>
1060: </html>
1061: SIMPLECONTENT
1062: return $filecontents;
1063: }
1064:
1065:
1066: sub inserteditinfo {
1067: my ($result,$filecontents)=@_;
1068: $filecontents =~ s:</textarea>:</textarea>:ig;
1069: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1070: my $editfooter=(<<ENDFOOTER);
1071: <hr />
1072: <a name="editsection" />
1073: <form method="post">
1074: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1075: <br />
1076: <input type="hidden" name="showmode" value="Edit" />
1077: <input type="submit" name="attemptclean"
1078: value="Save and then attempt to clean HTML" />
1079: <input type="submit" name="savethisfile" value="Save this" />
1080: <input type="submit" name="showmode" value="View" />
1081: </form>
1082: ENDFOOTER
1083: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1084: $result=~s/(\<\/body\>)/$editfooter/is;
1085: return $result;
1086: }
1087:
1088: sub get_target {
1089: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1090: if ( $ENV{'request.state'} eq 'published') {
1091: if ( defined($ENV{'form.grade_target'})
1092: && ($viewgrades == 'F' )) {
1093: return ($ENV{'form.grade_target'});
1094: } elsif (defined($ENV{'form.grade_target'})) {
1095: if (($ENV{'form.grade_target'} eq 'web') ||
1096: ($ENV{'form.grade_target'} eq 'tex') ) {
1097: return $ENV{'form.grade_target'}
1098: } else {
1099: return 'web';
1100: }
1101: } else {
1102: return 'web';
1103: }
1104: } elsif ($ENV{'request.state'} eq 'construct') {
1105: if ( defined($ENV{'form.grade_target'})) {
1106: return ($ENV{'form.grade_target'});
1107: } else {
1108: return 'web';
1109: }
1110: } else {
1111: return 'web';
1112: }
1113: }
1114:
1115: sub handler {
1116: my $request=shift;
1117:
1118: my $target=&get_target();
1119:
1120: $Apache::lonxml::debug=0;
1121:
1122: if ($ENV{'browser.mathml'}) {
1123: $request->content_type('text/xml');
1124: } else {
1125: $request->content_type('text/html');
1126: }
1127: &Apache::loncommon::no_cache($request);
1128: $request->send_http_header;
1129:
1130: return OK if $request->header_only;
1131:
1132:
1133: my $file=&Apache::lonnet::filelocation("",$request->uri);
1134: #
1135: # Edit action? Save file.
1136: #
1137: unless ($ENV{'request.state'} eq 'published') {
1138: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1139: &storefile($file,$ENV{'form.filecont'});
1140: }
1141: }
1142: my %mystyle;
1143: my $result = '';
1144: my $filecontents=&Apache::lonnet::getfile($file);
1145: if ($filecontents == -1) {
1146: $result=(<<ENDNOTFOUND);
1147: <html>
1148: <head>
1149: <title>File not found</title>
1150: </head>
1151: <body bgcolor="#FFFFFF">
1152: <b>File not found: $file</b>
1153: </body>
1154: </html>
1155: ENDNOTFOUND
1156: $filecontents='';
1157: if ($ENV{'request.state'} ne 'published') {
1158: $filecontents=&createnewhtml();
1159: $ENV{'form.showmode'}='Edit'; #force edit mode
1160: }
1161: } else {
1162: unless ($ENV{'request.state'} eq 'published') {
1163: if ($ENV{'form.attemptclean'}) {
1164: $filecontents=&htmlclean($filecontents,1);
1165: }
1166: }
1167: if ($ENV{'form.showmode'} ne 'Edit') {
1168: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1169: }
1170: }
1171:
1172: #
1173: # Edit action? Insert editing commands
1174: #
1175: unless ($ENV{'request.state'} eq 'published') {
1176: if ($ENV{'form.showmode'} eq 'Edit') {
1177: $result='<html><body bgcolor="#FFFFFF"></body></html>';
1178: $result=&inserteditinfo($result,$filecontents);
1179: }
1180: }
1181:
1182: writeallows($request->uri);
1183:
1184: $request->print($result);
1185:
1186: return OK;
1187: }
1188:
1189: sub debug {
1190: if ($Apache::lonxml::debug eq 1) {
1191: $|=1;
1192: print("DEBUG:".join('<br />',@_)."<br />\n");
1193: }
1194: }
1195:
1196: sub error {
1197: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1198: print "<b>ERROR:</b>".join('<br />',@_)."<br />\n";
1199: } else {
1200: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1201: #notify author
1202: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1203: #notify course
1204: if ( $ENV{'request.course.id'} ) {
1205: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1206: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1207: foreach my $user (split /\,/, $users) {
1208: ($user,my $domain) = split /:/, $user;
1209: &Apache::lonmsg::user_normal_msg($user,$domain,
1210: "Error [$declutter]",join('<br />',@_));
1211: }
1212: }
1213:
1214: #FIXME probably shouldn't have me get everything forever.
1215: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1216: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1217: }
1218: }
1219:
1220: sub warning {
1221: if ($ENV{'request.state'} eq 'construct') {
1222: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1223: }
1224: }
1225:
1226: sub get_param {
1227: my ($param,$parstack,$safeeval,$context) = @_;
1228: if ( ! $context ) { $context = -1; }
1229: my $args ='';
1230: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1231: if ( $args =~ /my \$$param=\"/ ) {
1232: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1233: } else {
1234: return undef;
1235: }
1236: }
1237:
1238: sub get_param_var {
1239: my ($param,$parstack,$safeeval,$context) = @_;
1240: if ( ! $context ) { $context = -1; }
1241: my $args ='';
1242: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1243: if ( $args !~ /my \$$param=\"/ ) { return undef; }
1244: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1245: if ($value =~ /^[\$\@\%]/) {
1246: return &Apache::run::run("return $value",$safeeval,1);
1247: } else {
1248: return $value;
1249: }
1250: }
1251:
1252: sub register_insert {
1253: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1254: my $i;
1255: my $tagnum=0;
1256: my @order;
1257: for ($i=0;$i < $#data; $i++) {
1258: my $line = $data[$i];
1259: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1260: if ( $line =~ /TABLE/ ) { last; }
1261: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1262: if ($tag) {
1263: $insertlist{"$tagnum.tag"} = $tag;
1264: $insertlist{"$tagnum.description"} = $descrip;
1265: $insertlist{"$tagnum.color"} = $color;
1266: $insertlist{"$tagnum.function"} = $function;
1267: if (!defined($show)) { $show='yes'; }
1268: $insertlist{"$tagnum.show"}= $show;
1269: $insertlist{"$tag.num"}=$tagnum;
1270: $tagnum++;
1271: }
1272: }
1273: $i++; #skipping TABLE line
1274: $tagnum = 0;
1275: for (;$i < $#data;$i++) {
1276: my $line = $data[$i];
1277: my ($mnemonic,@which) = split(/ +/,$line);
1278: my $tag = $insertlist{"$tagnum.tag"};
1279: for (my $j=0;$j <=$#which;$j++) {
1280: if ( $which[$j] eq 'Y' ) {
1281: if ($insertlist{"$j.show"} ne 'no') {
1282: push(@{ $insertlist{"$tag.which"} },$j);
1283: }
1284: }
1285: }
1286: $tagnum++;
1287: }
1288: }
1289:
1290: sub description {
1291: my ($token)=@_;
1292: my $tagnum;
1293: my $tag=$token->[1];
1294: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1295: my $testtag=$namespace.'::'.$tag;
1296: $tagnum=$insertlist{"$testtag.num"};
1297: if (defined($tagnum)) { last; }
1298: }
1299: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1300: return $insertlist{$tagnum.'.description'};
1301: }
1302:
1303: # ----------------------------------------------------------------- whichuser
1304: # returns a list of $symb, $courseid, $domain, $name that is correct for
1305: # calls to lonnet functions for this setup.
1306: # - looks for form.grade_ parameters
1307: sub whichuser {
1308: my ($symb,$courseid,$domain,$name);
1309: if (defined($ENV{'form.grade_symb'})) {
1310: my $tmp_courseid=$ENV{'form.grade_courseid'};
1311: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1312: if ($allowed) {
1313: $symb=$ENV{'form.grade_symb'};
1314: $courseid=$ENV{'form.grade_courseid'};
1315: $domain=$ENV{'form.grade_domain'};
1316: $name=$ENV{'form.grade_username'};
1317: }
1318: } else {
1319: $symb=&Apache::lonnet::symbread();
1320: $courseid=$ENV{'request.course.id'};
1321: $domain=$ENV{'user.domain'};
1322: $name=$ENV{'user.name'};
1323: }
1324: return ($symb,$courseid,$domain,$name);
1325: }
1326:
1327: 1;
1328: __END__
1329:
1330:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>