1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.410 2006/04/18 22:35:55 albertel 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:
40:
41: package Apache::lonxml;
42: use vars
43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
44: use strict;
45: use HTML::LCParser();
46: use HTML::TreeBuilder();
47: use HTML::Entities();
48: use Safe();
49: use Safe::Hole();
50: use Math::Cephes();
51: use Math::Random();
52: use Opcode();
53: use POSIX qw(strftime);
54: use Time::HiRes qw( gettimeofday tv_interval );
55: use Symbol();
56:
57: sub register {
58: my ($space,@taglist) = @_;
59: foreach my $temptag (@taglist) {
60: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
61: }
62: }
63:
64: sub deregister {
65: my ($space,@taglist) = @_;
66: foreach my $temptag (@taglist) {
67: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
68: if ($tempspace eq $space) {
69: pop(@{ $Apache::lonxml::alltags{$temptag} });
70: }
71: }
72: #&printalltags();
73: }
74:
75: use Apache::Constants qw(:common);
76: use Apache::lontexconvert();
77: use Apache::style();
78: use Apache::run();
79: use Apache::londefdef();
80: use Apache::scripttag();
81: use Apache::languagetags();
82: use Apache::edit();
83: use Apache::inputtags();
84: use Apache::outputtags();
85: use Apache::lonnet;
86: use Apache::File();
87: use Apache::loncommon();
88: use Apache::lonfeedback();
89: use Apache::lonmsg();
90: use Apache::loncacc();
91: use Apache::lonlocal;
92:
93: #================================================== Main subroutine: xmlparse
94: #debugging control, to turn on debugging modify the correct handler
95: $Apache::lonxml::debug=0;
96:
97: # keeps count of the number of warnings and errors generated in a parse
98: $warningcount=0;
99: $errorcount=0;
100:
101: #path to the directory containing the file currently being processed
102: @pwd=();
103:
104: #these two are used for capturing a subset of the output for later processing,
105: #don't touch them directly use &startredirection and &endredirection
106: @outputstack = ();
107: $redirection = 0;
108:
109: #controls wheter the <import> tag actually does
110: $import = 1;
111: @extlinks=();
112:
113: # meta mode is a bit weird only some output is to be turned off
114: #<output> tag turns metamode off (defined in londefdef.pm)
115: $metamode = 0;
116:
117: # turns on and of run::evaluate actually derefencing var refs
118: $evaluate = 1;
119:
120: # data structure for eidt mode, determines what tags can go into what other tags
121: %insertlist=();
122:
123: # stores the list of active tag namespaces
124: @namespace=();
125:
126: # has the dynamic menu been updated to know about this resource
127: $Apache::lonxml::registered=0;
128:
129: # a pointer the the Apache request object
130: $Apache::lonxml::request='';
131:
132: # a problem number counter, and check on ether it is used
133: $Apache::lonxml::counter=1;
134: $Apache::lonxml::counter_changed=0;
135:
136: #internal check on whether to look at style defs
137: $Apache::lonxml::usestyle=1;
138:
139: #locations used to store the parameter string for style substitutions
140: $Apache::lonxml::style_values='';
141: $Apache::lonxml::style_end_values='';
142:
143: #array of ssi calls that need to occur after we are done parsing
144: @Apache::lonxml::ssi_info=();
145:
146: #should we do the postag variable interpolation
147: $Apache::lonxml::post_evaluate=1;
148:
149: #a header message to emit in the case of any generated warning or errors
150: $Apache::lonxml::warnings_error_header='';
151:
152: # Control whether or not LaTeX symbols should be substituted for their
153: # \ style equivalents...this may be turned off e.g. in an verbatim
154: # environment.
155:
156: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
157:
158: sub enable_LaTeX_substitutions {
159: $Apache::lonxml::substitute_LaTeX_symbols = 1;
160: }
161: sub disable_LaTeX_substitutions {
162: $Apache::lonxml::substitute_LaTeX_symbols = 0;
163: }
164:
165: sub xmlend {
166: my ($target,$parser)=@_;
167: my $mode='xml';
168: my $status='OPEN';
169: if ($Apache::lonhomework::parsing_a_problem ||
170: $Apache::lonhomework::parsing_a_task ) {
171: $mode='problem';
172: $status=$Apache::inputtags::status[-1];
173: }
174: my $discussion;
175: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
176: ['LONCAPA_INTERNAL_no_discussion']);
177: if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
178: $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
179: $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
180: }
181: if ($target eq 'tex') {
182: $discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
183: &Apache::lonxml::newparser($parser,\$discussion,'');
184: return '';
185: }
186:
187: return $discussion;
188: }
189:
190: sub tokeninputfield {
191: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
192: $defhost=~tr/a-z/A-Z/;
193: return (<<ENDINPUTFIELD)
194: <script type="text/javascript">
195: function updatetoken() {
196: var comp=new Array;
197: var barcode=unescape(document.tokeninput.barcode.value);
198: comp=barcode.split('*');
199: if (typeof(comp[0])!="undefined") {
200: document.tokeninput.codeone.value=comp[0];
201: }
202: if (typeof(comp[1])!="undefined") {
203: document.tokeninput.codetwo.value=comp[1];
204: }
205: if (typeof(comp[2])!="undefined") {
206: comp[2]=comp[2].toUpperCase();
207: document.tokeninput.codethree.value=comp[2];
208: }
209: document.tokeninput.barcode.value='';
210: }
211: </script>
212: <form method="post" name="tokeninput">
213: <table border="2" bgcolor="#FFFFBB">
214: <tr><th>DocID Checkin</th></tr>
215: <tr><td>
216: <table>
217: <tr>
218: <td>Scan in Barcode</td>
219: <td><input type="text" size="22" name="barcode"
220: onChange="updatetoken()"/></td>
221: </tr>
222: <tr><td><i>or</i> Type in DocID</td>
223: <td>
224: <input type="text" size="5" name="codeone" />
225: <b><font size="+2">*</font></b>
226: <input type="text" size="5" name="codetwo" />
227: <b><font size="+2">*</font></b>
228: <input type="text" size="10" name="codethree" value="$defhost"
229: onChange="this.value=this.value.toUpperCase()" />
230: </td></tr>
231: </table>
232: </td></tr>
233: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
234: </table>
235: </form>
236: ENDINPUTFIELD
237: }
238:
239: sub maketoken {
240: my ($symb,$tuname,$tudom,$tcrsid)=@_;
241: unless ($symb) {
242: $symb=&Apache::lonnet::symbread();
243: }
244: unless ($tuname) {
245: $tuname=$env{'user.name'};
246: $tudom=$env{'user.domain'};
247: $tcrsid=$env{'request.course.id'};
248: }
249:
250: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
251: }
252:
253: sub printtokenheader {
254: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
255: unless ($token) { return ''; }
256:
257: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
258: unless ($tsymb) {
259: $tsymb=$symb;
260: }
261: unless ($tuname) {
262: $tuname=$name;
263: $tudom=$domain;
264: $tcrsid=$courseid;
265: }
266:
267: my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
268:
269: if ($target eq 'web') {
270: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
271: return
272: '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
273: &mt('Checked out for').' '.$plainname.
274: '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
275: '<br />'.&mt('ID').': '.$idhash{$tuname}.
276: '<br />'.&mt('CourseID').': '.$tcrsid.
277: '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
278: '<br />'.&mt('DocID').': '.$token.
279: '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
280: } else {
281: return $token;
282: }
283: }
284:
285: sub printalltags {
286: my $temp;
287: foreach $temp (sort keys %Apache::lonxml::alltags) {
288: &Apache::lonxml::debug("$temp -- ".
289: join(',',@{ $Apache::lonxml::alltags{$temp} }));
290: }
291: }
292:
293: sub xmlparse {
294: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
295:
296: &setup_globals($request,$target);
297: &Apache::inputtags::initialize_inputtags();
298: &Apache::bridgetask::initialize_bridgetask();
299: &Apache::outputtags::initialize_outputtags();
300: &Apache::edit::initialize_edit();
301: &Apache::londefdef::initialize_londefdef();
302:
303: #
304: # do we have a course style file?
305: #
306:
307: if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
308: my $bodytext=
309: $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
310: if ($bodytext) {
311: foreach my $file (split(',',$bodytext)) {
312: my $location=&Apache::lonnet::filelocation('',$file);
313: my $styletext=&Apache::lonnet::getfile($location);
314: if ($styletext ne '-1') {
315: %style_for_target = (%style_for_target,
316: &Apache::style::styleparser($target,$styletext));
317: }
318: }
319: }
320: } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
321: my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
322: my $styletext=&Apache::lonnet::getfile($location);
323: if ($styletext ne '-1') {
324: %style_for_target = (%style_for_target,
325: &Apache::style::styleparser($target,$styletext));
326: }
327: }
328: #&printalltags();
329: my @pars = ();
330: my $pwd=$env{'request.filename'};
331: $pwd =~ s:/[^/]*$::;
332: &newparser(\@pars,\$content_file_string,$pwd);
333:
334: my $safeeval = new Safe;
335: my $safehole = new Safe::Hole;
336: &init_safespace($target,$safeeval,$safehole,$safeinit);
337: #-------------------- Redefinition of the target in the case of compound target
338:
339: ($target, my @tenta) = split('&&',$target);
340:
341: my @stack = ();
342: my @parstack = ();
343: &initdepth();
344: &init_alarm();
345: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
346: $safeeval,\%style_for_target,1);
347:
348: if ($env{'request.uri'}) {
349: &writeallows($env{'request.uri'});
350: }
351: &do_registered_ssi();
352: if ($Apache::lonxml::counter_changed) { &store_counter() }
353:
354: &clean_safespace($safeeval);
355:
356: if ($env{'form.return_only_error_and_warning_counts'}) {
357: return "$errorcount:$warningcount";
358: }
359: return $finaloutput;
360: }
361:
362: sub latex_special_symbols {
363: my ($string,$where)=@_;
364: #
365: # If e.g. in verbatim mode, then don't substitute.
366: # but return original string.
367: #
368: if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
369: return $string;
370: }
371: if ($where eq 'header') {
372: $string =~ s/(\\|_|\^)/ /g;
373: $string =~ s/(\$|%|\{|\})/\\$1/g;
374: $string =~ s/_/ /g;
375: $string=&Apache::lonprintout::character_chart($string);
376: # any & or # leftover should be safe to just escape
377: $string=~s/([^\\])\&/$1\\\&/g;
378: $string=~s/([^\\])\#/$1\\\#/g;
379: } else {
380: $string=~s/\\/\\ensuremath{\\backslash}/g;
381: $string=~s/\\\%|\%/\\\%/g;
382: $string=~s/\\{|{/\\{/g;
383: $string=~s/\\}|}/\\}/g;
384: $string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
385: $string=~s/\\\$|\$/\\\$/g;
386: $string=~s/\\\_|\_/\\\_/g;
387: $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
388: $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
389: $string=&Apache::lonprintout::character_chart($string);
390: # any & or # leftover should be safe to just escape
391: $string=~s/\\\&|\&/\\\&/g;
392: $string=~s/\\\#|\#/\\\#/g;
393: $string=~s/\|/\$\\mid\$/g;
394: #single { or } How to escape?
395: }
396: return $string;
397: }
398:
399: sub inner_xmlparse {
400: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
401: my $finaloutput = '';
402: my $result;
403: my $token;
404: my $dontpop=0;
405: my $startredirection = $Apache::lonxml::redirection;
406: while ( $#$pars > -1 ) {
407: while ($token = $$pars['-1']->get_token) {
408: if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
409: if ($metamode<1) {
410: my $text=$token->[1];
411: if ($token->[0] eq 'C' && $target eq 'tex') {
412: $text = '';
413: # $text = '%'.$text."\n";
414: }
415: $result.=$text;
416: }
417: } elsif (($token->[0] eq 'D')) {
418: if ($metamode<1 && $target eq 'web') {
419: my $text=$token->[1];
420: $result.=$text;
421: }
422: } elsif ($token->[0] eq 'PI') {
423: if ($metamode<1 && $target eq 'web') {
424: $result=$token->[2];
425: }
426: } elsif ($token->[0] eq 'S') {
427: # add tag to stack
428: push (@$stack,$token->[1]);
429: # add parameters list to another stack
430: push (@$parstack,&parstring($token));
431: &increasedepth($token);
432: if ($Apache::lonxml::usestyle &&
433: exists($$style_for_target{$token->[1]})) {
434: $Apache::lonxml::usestyle=0;
435: my $string=$$style_for_target{$token->[1]}.
436: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
437: &Apache::lonxml::newparser($pars,\$string);
438: $Apache::lonxml::style_values=$$parstack[-1];
439: $Apache::lonxml::style_end_values=$$parstack[-1];
440: } else {
441: $result = &callsub("start_$token->[1]", $target, $token, $stack,
442: $parstack, $pars, $safeeval, $style_for_target);
443: }
444: } elsif ($token->[0] eq 'E') {
445: if ($Apache::lonxml::usestyle &&
446: exists($$style_for_target{'/'."$token->[1]"})) {
447: $Apache::lonxml::usestyle=0;
448: my $string=$$style_for_target{'/'.$token->[1]}.
449: '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
450: &Apache::lonxml::newparser($pars,\$string);
451: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
452: $Apache::lonxml::style_end_values='';
453: $dontpop=1;
454: } else {
455: #clear out any tags that didn't end
456: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
457: my $lasttag=$$stack[-1];
458: if ($token->[1] =~ /^\Q$lasttag\E$/i) {
459: &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>');
460: last;
461: } else {
462: &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file');
463: &end_tag($stack,$parstack,$token);
464: }
465: }
466: $result = &callsub("end_$token->[1]", $target, $token, $stack,
467: $parstack, $pars,$safeeval, $style_for_target);
468: }
469: } else {
470: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
471: }
472: #evaluate variable refs in result
473: if ($Apache::lonxml::post_evaluate &&$result ne "") {
474: my $extras;
475: if (!$Apache::lonxml::usestyle) {
476: $extras=$Apache::lonxml::style_values;
477: }
478: if ( $#$parstack > -1 ) {
479: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
480: } else {
481: $result= &Apache::run::evaluate($result,$safeeval,$extras);
482: }
483: }
484: $Apache::lonxml::post_evaluate=1;
485:
486: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
487: #Style file definitions should be correct
488: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
489: $result=&latex_special_symbols($result);
490: }
491: }
492:
493: if ($Apache::lonxml::redirection) {
494: $Apache::lonxml::outputstack['-1'] .= $result;
495: } else {
496: $finaloutput.=$result;
497: }
498: $result = '';
499:
500: if ($token->[0] eq 'E' && !$dontpop) {
501: &end_tag($stack,$parstack,$token);
502: }
503: $dontpop=0;
504: }
505: if ($#$pars > -1) {
506: pop @$pars;
507: pop @Apache::lonxml::pwd;
508: }
509: }
510:
511: # if ($target eq 'meta') {
512: # $finaloutput.=&endredirection;
513: # }
514:
515: if ( $start && $target eq 'grade') { &endredirection(); }
516: if ( $Apache::lonxml::redirection > $startredirection) {
517: while ($Apache::lonxml::redirection > $startredirection) {
518: $finaloutput .= &endredirection();
519: }
520: }
521: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
522: $finaloutput=&afterburn($finaloutput);
523: }
524: return $finaloutput;
525: }
526:
527: ##
528: ## Looks to see if there is a subroutine defined for this tag. If so, call it,
529: ## otherwise do not call it as we do not know what it is.
530: ##
531: sub callsub {
532: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
533: my $currentstring='';
534: my $nodefault;
535: {
536: my $sub1;
537: no strict 'refs';
538: my $tag=$token->[1];
539: # get utterly rid of extended html tags
540: if ($tag=~/^x\-/i) { return ''; }
541: my $space=$Apache::lonxml::alltags{$tag}[-1];
542: if (!$space) {
543: $tag=~tr/A-Z/a-z/;
544: $sub=~tr/A-Z/a-z/;
545: $space=$Apache::lonxml::alltags{$tag}[-1]
546: }
547:
548: my $deleted=0;
549: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
550: if (($token->[0] eq 'S') && ($target eq 'modified')) {
551: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
552: $parstack,$parser,$safeeval,
553: $style);
554: }
555: if (!$deleted) {
556: if ($space) {
557: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
558: $sub1="$space\:\:$sub";
559: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
560: $parstack,$parser,$safeeval,
561: $style);
562: } else {
563: if ($target eq 'tex') {
564: # throw away tag name
565: return '';
566: }
567: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
568: if ($metamode <1) {
569: if (defined($token->[4]) && ($metamode < 1)) {
570: $currentstring = $token->[4];
571: } else {
572: $currentstring = $token->[2];
573: }
574: }
575: }
576: # &Apache::lonxml::debug("nodefalt:$nodefault:");
577: if ($currentstring eq '' && $nodefault eq '') {
578: if ($target eq 'edit') {
579: #&Apache::lonxml::debug("doing default edit for $token->[1]");
580: if ($token->[0] eq 'S') {
581: $currentstring = &Apache::edit::tag_start($target,$token);
582: } elsif ($token->[0] eq 'E') {
583: $currentstring = &Apache::edit::tag_end($target,$token);
584: }
585: } elsif ($target eq 'modified') {
586: if ($token->[0] eq 'S') {
587: $currentstring = $token->[4];
588: $currentstring.=&Apache::edit::handle_insert();
589: } elsif ($token->[0] eq 'E') {
590: $currentstring = $token->[2];
591: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
592: } else {
593: $currentstring = $token->[2];
594: }
595: }
596: }
597: }
598: use strict 'refs';
599: }
600: return $currentstring;
601: }
602:
603: sub setup_globals {
604: my ($request,$target)=@_;
605: $Apache::lonxml::request=$request;
606: $Apache::lonxml::registered = 0;
607: $errorcount=0;
608: $warningcount=0;
609: $Apache::lonxml::default_homework_loaded=0;
610: $Apache::lonxml::usestyle=1;
611: &init_counter();
612: @Apache::lonxml::pwd=();
613: @Apache::lonxml::extlinks=();
614: @Apache::lonxml::ssi_info=();
615: $Apache::lonxml::post_evaluate=1;
616: $Apache::lonxml::warnings_error_header='';
617: $Apache::lonxml::substitute_LaTeX_symbols = 1;
618: if ($target eq 'meta') {
619: $Apache::lonxml::redirection = 0;
620: $Apache::lonxml::metamode = 1;
621: $Apache::lonxml::evaluate = 1;
622: $Apache::lonxml::import = 0;
623: } elsif ($target eq 'answer') {
624: $Apache::lonxml::redirection = 0;
625: $Apache::lonxml::metamode = 1;
626: $Apache::lonxml::evaluate = 1;
627: $Apache::lonxml::import = 1;
628: } elsif ($target eq 'grade') {
629: &startredirection(); #ended in inner_xmlparse on exit
630: $Apache::lonxml::metamode = 0;
631: $Apache::lonxml::evaluate = 1;
632: $Apache::lonxml::import = 1;
633: } elsif ($target eq 'modified') {
634: $Apache::lonxml::redirection = 0;
635: $Apache::lonxml::metamode = 0;
636: $Apache::lonxml::evaluate = 0;
637: $Apache::lonxml::import = 0;
638: } elsif ($target eq 'edit') {
639: $Apache::lonxml::redirection = 0;
640: $Apache::lonxml::metamode = 0;
641: $Apache::lonxml::evaluate = 0;
642: $Apache::lonxml::import = 0;
643: } elsif ($target eq 'analyze') {
644: $Apache::lonxml::redirection = 0;
645: $Apache::lonxml::metamode = 0;
646: $Apache::lonxml::evaluate = 1;
647: $Apache::lonxml::import = 1;
648: } else {
649: $Apache::lonxml::redirection = 0;
650: $Apache::lonxml::metamode = 0;
651: $Apache::lonxml::evaluate = 1;
652: $Apache::lonxml::import = 1;
653: }
654: }
655:
656: sub init_safespace {
657: my ($target,$safeeval,$safehole,$safeinit) = @_;
658: $safeeval->deny_only(':dangerous');
659: $safeeval->reval('use Math::Complex;');
660: $safeeval->permit_only(":default");
661: $safeeval->permit("entereval");
662: $safeeval->permit(":base_math");
663: $safeeval->permit("sort");
664: $safeeval->permit("time");
665: $safeeval->deny("rand");
666: $safeeval->deny("srand");
667: $safeeval->deny(":base_io");
668: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
669: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
670: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
671: $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
672: '&chem_standard_order');
673: $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
674:
675: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
676: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
677: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
678: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
679: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
680: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
681: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
682: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
683: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
684: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
685: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
686: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
687: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
688: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
689: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
690: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
691: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
692: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
693: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
694:
695: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
696: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
697: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
698: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
699: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
700: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
701: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
702: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
703: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
704: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
705: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
706: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
707: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
708: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
709: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
710: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
711: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
712: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
713: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
714: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
715: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
716: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
717:
718: $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
719: $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
720: '&Math::Cephes::Matrix::new');
721: $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
722: '&Math::Cephes::Matrix::coef');
723: $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
724: '&Math::Cephes::Matrix::clr');
725: $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
726: '&Math::Cephes::Matrix::add');
727: $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
728: '&Math::Cephes::Matrix::sub');
729: $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
730: '&Math::Cephes::Matrix::mul');
731: $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
732: '&Math::Cephes::Matrix::div');
733: $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
734: '&Math::Cephes::Matrix::inv');
735: $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
736: '&Math::Cephes::Matrix::transp');
737: $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
738: '&Math::Cephes::Matrix::simq');
739: $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
740: '&Math::Cephes::Matrix::mat_to_vec');
741: $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
742: '&Math::Cephes::Matrix::vec_to_mat');
743: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
744: '&Math::Cephes::Matrix::check');
745: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
746: '&Math::Cephes::Matrix::check');
747:
748: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
749: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
750: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
751: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
752: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
753: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
754:
755: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
756: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
757: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
758: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
759: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
760: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
761: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
762: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
763: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
764: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
765: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
766: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
767: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
768: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
769: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
770: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
771: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
772: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
773: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
774: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
775: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
776: $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
777: $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
778: $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
779:
780: #need to inspect this class of ops
781: # $safeeval->deny(":base_orig");
782: $safeeval->permit("require");
783: $safeinit .= ';$external::target="'.$target.'";';
784: &Apache::run::run($safeinit,$safeeval);
785: &initialize_rndseed($safeeval);
786: }
787:
788: sub clean_safespace {
789: my ($safeeval) = @_;
790: delete_package_recurse($safeeval->{Root});
791: }
792:
793: sub delete_package_recurse {
794: my ($package) = @_;
795: my @subp;
796: {
797: no strict 'refs';
798: while (my ($key,$val) = each(%{*{"$package\::"}})) {
799: if (!defined($val)) { next; }
800: local (*ENTRY) = $val;
801: if (defined *ENTRY{HASH} && $key =~ /::$/ &&
802: $key ne "main::" && $key ne "<none>::")
803: {
804: my ($p) = $package ne "main" ? "$package\::" : "";
805: ($p .= $key) =~ s/::$//;
806: push(@subp,$p);
807: }
808: }
809: }
810: foreach my $p (@subp) {
811: delete_package_recurse($p);
812: }
813: Symbol::delete_package($package);
814: }
815:
816: sub initialize_rndseed {
817: my ($safeeval)=@_;
818: my $rndseed;
819: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
820: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
821: my $safeinit = '$external::randomseed="'.$rndseed.'";';
822: &Apache::lonxml::debug("Setting rndseed to $rndseed");
823: &Apache::run::run($safeinit,$safeeval);
824: }
825:
826: sub default_homework_load {
827: my ($safeeval)=@_;
828: &Apache::lonxml::debug('Loading default_homework');
829: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
830: if ($default eq -1) {
831: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
832: } else {
833: &Apache::run::run($default,$safeeval);
834: $Apache::lonxml::default_homework_loaded=1;
835: }
836: }
837:
838: {
839: my $alarm_depth;
840: sub init_alarm {
841: alarm(0);
842: $alarm_depth=0;
843: }
844:
845: sub start_alarm {
846: if ($alarm_depth<1) {
847: my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
848: if ($old) {
849: &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
850: }
851: }
852: $alarm_depth++;
853: }
854:
855: sub end_alarm {
856: $alarm_depth--;
857: if ($alarm_depth<1) { alarm(0); }
858: }
859: }
860: my $metamode_was;
861: sub startredirection {
862: if (!$Apache::lonxml::redirection) {
863: $metamode_was=$Apache::lonxml::metamode;
864: }
865: $Apache::lonxml::metamode=0;
866: $Apache::lonxml::redirection++;
867: push (@Apache::lonxml::outputstack, '');
868: }
869:
870: sub endredirection {
871: if (!$Apache::lonxml::redirection) {
872: &Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
873: return '';
874: }
875: $Apache::lonxml::redirection--;
876: if (!$Apache::lonxml::redirection) {
877: $Apache::lonxml::metamode=$metamode_was;
878: }
879: pop @Apache::lonxml::outputstack;
880: }
881:
882: sub end_tag {
883: my ($tagstack,$parstack,$token)=@_;
884: pop(@$tagstack);
885: pop(@$parstack);
886: &decreasedepth($token);
887: }
888:
889: sub initdepth {
890: @Apache::lonxml::depthcounter=();
891: $Apache::lonxml::depth=-1;
892: $Apache::lonxml::olddepth=-1;
893: }
894:
895: my @timers;
896: my $lasttime;
897: sub increasedepth {
898: my ($token) = @_;
899: $Apache::lonxml::depth++;
900: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
901: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
902: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
903: }
904: my $time;
905: if ($Apache::lonxml::debug eq "1") {
906: push(@timers,[&gettimeofday()]);
907: $time=&tv_interval($lasttime);
908: $lasttime=[&gettimeofday()];
909: }
910: my $spacing=' 'x($Apache::lonxml::depth-1);
911: my $curdepth=join('_',@Apache::lonxml::depthcounter);
912: &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n");
913: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
914: }
915:
916: sub decreasedepth {
917: my ($token) = @_;
918: $Apache::lonxml::depth--;
919: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
920: $#Apache::lonxml::depthcounter--;
921: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
922: }
923: if ( $Apache::lonxml::depth < -1) {
924: &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
925: $Apache::lonxml::depth='-1';
926: }
927: my ($timer,$time);
928: if ($Apache::lonxml::debug eq "1") {
929: $timer=pop(@timers);
930: $time=&tv_interval($lasttime);
931: $lasttime=[&gettimeofday()];
932: }
933: my $spacing=' 'x$Apache::lonxml::depth;
934: my $curdepth=join('_',@Apache::lonxml::depthcounter);
935: &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n");
936: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
937: }
938:
939: sub get_id {
940: my ($parstack,$safeeval)=@_;
941: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
942: if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
943: &error(&mt("IDs are not allowed to contain "<tt>_</tt>" or "<tt>.</tt>""));
944: }
945: if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
946: return $id;
947: }
948:
949: sub get_all_text_unbalanced {
950: #there is a copy of this in lonpublisher.pm
951: my($tag,$pars)= @_;
952: my $token;
953: my $result='';
954: $tag='<'.$tag.'>';
955: while ($token = $$pars[-1]->get_token) {
956: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
957: if ($token->[0] eq 'T' && $token->[2]) {
958: $result.='<![CDATA['.$token->[1].']]>';
959: } else {
960: $result.=$token->[1];
961: }
962: } elsif ($token->[0] eq 'PI') {
963: $result.=$token->[2];
964: } elsif ($token->[0] eq 'S') {
965: $result.=$token->[4];
966: } elsif ($token->[0] eq 'E') {
967: $result.=$token->[2];
968: }
969: if ($result =~ /\Q$tag\E/is) {
970: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
971: #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
972: #&Apache::lonxml::debug('Result is :'.$1);
973: $redo=$tag.$redo;
974: &Apache::lonxml::newparser($pars,\$redo);
975: last;
976: }
977: }
978: return $result
979: }
980:
981: sub increment_counter {
982: my ($increment) = @_;
983: if (defined($increment) && $increment gt 0) {
984: $Apache::lonxml::counter+=$increment;
985: } else {
986: $Apache::lonxml::counter++;
987: }
988: $Apache::lonxml::counter_changed=1;
989: }
990:
991: sub init_counter {
992: if ($env{'request.state'} eq 'construct') {
993: $Apache::lonxml::counter=1;
994: $Apache::lonxml::counter_changed=1;
995: } elsif (defined($env{'form.counter'})) {
996: $Apache::lonxml::counter=$env{'form.counter'};
997: $Apache::lonxml::counter_changed=0;
998: } else {
999: $Apache::lonxml::counter=1;
1000: $Apache::lonxml::counter_changed=1;
1001: }
1002: }
1003:
1004: sub store_counter {
1005: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1006: $Apache::lonxml::counter_changed=0;
1007: return '';
1008: }
1009:
1010: {
1011: my $state;
1012: sub clear_problem_counter {
1013: undef($state);
1014: &Apache::lonnet::delenv('form.counter');
1015: &Apache::lonxml::init_counter();
1016: &Apache::lonxml::store_counter();
1017: }
1018:
1019: sub remember_problem_counter {
1020: &Apache::lonnet::transfer_profile_to_env();
1021: $state = $env{'form.counter'};
1022: }
1023:
1024: sub restore_problem_counter {
1025: if (defined($state)) {
1026: &Apache::lonnet::appenv(('form.counter' => $state));
1027: }
1028: }
1029: sub get_problem_counter {
1030: if ($Apache::lonxml::counter_changed) { &store_counter() }
1031: &Apache::lonnet::transfer_profile_to_env();
1032: return $env{'form.counter'};
1033: }
1034: }
1035:
1036: sub get_all_text {
1037: my($tag,$pars,$style)= @_;
1038: my $gotfullstack=1;
1039: if (ref($pars) ne 'ARRAY') {
1040: $gotfullstack=0;
1041: $pars=[$pars];
1042: }
1043: if (ref($style) ne 'HASH') {
1044: $style={};
1045: }
1046: my $depth=0;
1047: my $token;
1048: my $result='';
1049: if ( $tag =~ m:^/: ) {
1050: my $tag=substr($tag,1);
1051: #&Apache::lonxml::debug("have:$tag:");
1052: my $top_empty=0;
1053: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1054: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1055: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1056: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1057: if ($token->[2]) {
1058: $result.='<![CDATA['.$token->[1].']]>';
1059: } else {
1060: $result.=$token->[1];
1061: }
1062: } elsif ($token->[0] eq 'PI') {
1063: $result.=$token->[2];
1064: } elsif ($token->[0] eq 'S') {
1065: if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
1066: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1067: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1068: $result.=$token->[4];
1069: } elsif ($token->[0] eq 'E') {
1070: if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1071: #skip sending back the last end tag
1072: if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1073: my $string=
1074: '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
1075: $$style{'/'.$token->[1]}.
1076: $token->[2].
1077: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
1078: &Apache::lonxml::newparser($pars,\$string);
1079: #&Apache::lonxml::debug("reParsing $string");
1080: next;
1081: }
1082: if ($depth > -1) {
1083: $result.=$token->[2];
1084: } else {
1085: $$pars[-1]->unget_token($token);
1086: }
1087: }
1088: }
1089: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1090: if (($depth >=0) && ($#$pars > 0) ) {
1091: pop(@$pars);
1092: pop(@Apache::lonxml::pwd);
1093: }
1094: }
1095: if ($top_empty && $depth >= 0) {
1096: #never found the end tag ran out of text, throw error send back blank
1097: &error('Never found end tag for <'.$tag.
1098: '> current string <pre>'.
1099: &HTML::Entities::encode($result,'<>&"').
1100: '</pre>');
1101: if ($gotfullstack) {
1102: my $newstring='</'.$tag.'>'.$result;
1103: &Apache::lonxml::newparser($pars,\$newstring);
1104: }
1105: $result='';
1106: }
1107: } else {
1108: while ($#$pars > -1) {
1109: while ($token = $$pars[-1]->get_token) {
1110: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1111: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1112: ($token->[0] eq 'D')) {
1113: if ($token->[2]) {
1114: $result.='<![CDATA['.$token->[1].']]>';
1115: } else {
1116: $result.=$token->[1];
1117: }
1118: } elsif ($token->[0] eq 'PI') {
1119: $result.=$token->[2];
1120: } elsif ($token->[0] eq 'S') {
1121: if ( $token->[1] =~ /^\Q$tag\E$/i) {
1122: $$pars[-1]->unget_token($token); last;
1123: } else {
1124: $result.=$token->[4];
1125: }
1126: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1127: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1128: } elsif ($token->[0] eq 'E') {
1129: $result.=$token->[2];
1130: }
1131: }
1132: if (($#$pars > 0) ) {
1133: pop(@$pars);
1134: pop(@Apache::lonxml::pwd);
1135: } else { last; }
1136: }
1137: }
1138: #&Apache::lonxml::debug("Exit:$result:");
1139: return $result
1140: }
1141:
1142: sub newparser {
1143: my ($parser,$contentref,$dir) = @_;
1144: push (@$parser,HTML::LCParser->new($contentref));
1145: $$parser[-1]->xml_mode(1);
1146: $$parser[-1]->marked_sections(1);
1147: if ( $dir eq '' ) {
1148: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1149: } else {
1150: push (@Apache::lonxml::pwd, $dir);
1151: }
1152: }
1153:
1154: sub parstring {
1155: my ($token) = @_;
1156: my $temp='';
1157: foreach (@{$token->[3]}) {
1158: unless ($_=~/\W/) {
1159: my $val=$token->[2]->{$_};
1160: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1161: $val =~ s/(\$[^{a-zA-Z_])/\\$1/g;
1162: $val =~ s/(\$)$/\\$1/;
1163: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1164: $temp .= "my \$$_=\"$val\";";
1165: }
1166: }
1167: return $temp;
1168: }
1169:
1170: sub extlink {
1171: my ($res,$exact)=@_;
1172: if (!$exact) {
1173: $res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
1174: }
1175: push(@Apache::lonxml::extlinks,$res)
1176: }
1177:
1178: sub writeallows {
1179: unless ($#extlinks>=0) { return; }
1180: my $thisurl = &Apache::lonnet::clutter(shift);
1181: if ($env{'httpref.'.$thisurl}) {
1182: $thisurl=$env{'httpref.'.$thisurl};
1183: }
1184: my $thisdir=$thisurl;
1185: $thisdir=~s/\/[^\/]+$//;
1186: my %httpref=();
1187: foreach (@extlinks) {
1188: $httpref{'httpref.'.
1189: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1190: }
1191: @extlinks=();
1192: &Apache::lonnet::appenv(%httpref);
1193: }
1194:
1195: sub register_ssi {
1196: my ($url,%form)=@_;
1197: push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
1198: return '';
1199: }
1200:
1201: sub do_registered_ssi {
1202: foreach my $info (@Apache::lonxml::ssi_info) {
1203: my %form=%{ $info->{'form'}};
1204: my $url=$info->{'url'};
1205: &Apache::lonnet::ssi($url,%form);
1206: }
1207: }
1208: #
1209: # Afterburner handles anchors, highlights and links
1210: #
1211: sub afterburn {
1212: my $result=shift;
1213: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1214: ['highlight','anchor','link']);
1215: if ($env{'form.highlight'}) {
1216: foreach (split(/\,/,$env{'form.highlight'})) {
1217: my $anchorname=$_;
1218: my $matchthis=$anchorname;
1219: $matchthis=~s/\_+/\\s\+/g;
1220: $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1221: }
1222: }
1223: if ($env{'form.link'}) {
1224: foreach (split(/\,/,$env{'form.link'})) {
1225: my ($anchorname,$linkurl)=split(/\>/,$_);
1226: my $matchthis=$anchorname;
1227: $matchthis=~s/\_+/\\s\+/g;
1228: $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1229: }
1230: }
1231: if ($env{'form.anchor'}) {
1232: my $anchorname=$env{'form.anchor'};
1233: my $matchthis=$anchorname;
1234: $matchthis=~s/\_+/\\s\+/g;
1235: $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1236: $result.=(<<"ENDSCRIPT");
1237: <script type="text/javascript">
1238: document.location.hash='$anchorname';
1239: </script>
1240: ENDSCRIPT
1241: }
1242: return $result;
1243: }
1244:
1245: sub storefile {
1246: my ($file,$contents)=@_;
1247: &Apache::lonnet::correct_line_ends(\$contents);
1248: if (my $fh=Apache::File->new('>'.$file)) {
1249: print $fh $contents;
1250: $fh->close();
1251: return 1;
1252: } else {
1253: &warning("Unable to save file $file");
1254: return 0;
1255: }
1256: }
1257:
1258: sub createnewhtml {
1259: my $title=&mt('Title of document goes here');
1260: my $body=&mt('Body of document goes here');
1261: my $filecontents=(<<SIMPLECONTENT);
1262: <html>
1263: <head>
1264: <title>$title</title>
1265: </head>
1266: <body bgcolor="#FFFFFF">
1267: $body
1268: </body>
1269: </html>
1270: SIMPLECONTENT
1271: return $filecontents;
1272: }
1273:
1274: sub createnewsty {
1275: my $filecontents=(<<SIMPLECONTENT);
1276: <definetag name="">
1277: <render>
1278: <web></web>
1279: <tex></tex>
1280: </render>
1281: </definetag>
1282: SIMPLECONTENT
1283: return $filecontents;
1284: }
1285:
1286:
1287: sub inserteditinfo {
1288: my ($result,$filecontents,$filetype)=@_;
1289: $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1290: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1291: my $xml_help = '';
1292: my $initialize='';
1293: if ($filetype eq 'html') {
1294: my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1295: $initialize=&Apache::lonhtmlcommon::spellheader();
1296: if (!&Apache::lonhtmlcommon::htmlareablocked() &&
1297: &Apache::lonhtmlcommon::htmlareabrowser()) {
1298: $initialize.=(<<FULLPAGE);
1299: <script type="text/javascript">
1300: $addbuttons
1301:
1302: HTMLArea.loadPlugin("FullPage");
1303:
1304: function initDocument() {
1305: var editor=new HTMLArea("filecont",config);
1306: editor.registerPlugin(FullPage);
1307: editor.generate();
1308: }
1309: </script>
1310: FULLPAGE
1311: } else {
1312: $initialize.=(<<FULLPAGE);
1313: <script type="text/javascript">
1314: $addbuttons
1315: function initDocument() {
1316: }
1317: </script>
1318: FULLPAGE
1319: }
1320: $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
1321: $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1322: }
1323: my $cleanbut = '';
1324:
1325: my $titledisplay=&display_title();
1326: my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
1327: 'vi' => 'View',
1328: 'ed' => 'Edit');
1329: my $buttons=(<<BUTTONS);
1330: $cleanbut
1331: <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
1332: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1333: BUTTONS
1334: $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1335: $buttons.=&Apache::lonhtmlcommon::htmlareaselectactive('filecont');
1336: my $editfooter=(<<ENDFOOTER);
1337: $initialize
1338: <hr />
1339: <a name="editsection" />
1340: <form method="post" name="xmledit">
1341: $xml_help
1342: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1343: $buttons<br />
1344: <textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1345: <br />$buttons
1346: <br />
1347: </form>
1348: $titledisplay
1349: </body>
1350: ENDFOOTER
1351: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1352: $result=~s/(\<\/body\>)/$editfooter/is;
1353: return $result;
1354: }
1355:
1356: sub get_target {
1357: my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1358: if ( $env{'request.state'} eq 'published') {
1359: if ( defined($env{'form.grade_target'})
1360: && ($viewgrades == 'F' )) {
1361: return ($env{'form.grade_target'});
1362: } elsif (defined($env{'form.grade_target'})) {
1363: if (($env{'form.grade_target'} eq 'web') ||
1364: ($env{'form.grade_target'} eq 'tex') ) {
1365: return $env{'form.grade_target'}
1366: } else {
1367: return 'web';
1368: }
1369: } else {
1370: return 'web';
1371: }
1372: } elsif ($env{'request.state'} eq 'construct') {
1373: if ( defined($env{'form.grade_target'})) {
1374: return ($env{'form.grade_target'});
1375: } else {
1376: return 'web';
1377: }
1378: } else {
1379: return 'web';
1380: }
1381: }
1382:
1383: sub handler {
1384: my $request=shift;
1385:
1386: my $target=&get_target();
1387:
1388: $Apache::lonxml::debug=$env{'user.debug'};
1389:
1390: &Apache::loncommon::content_type($request,'text/html');
1391: &Apache::loncommon::no_cache($request);
1392: if ($env{'request.state'} eq 'published') {
1393: $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
1394: 'lastrevisiondate'));
1395: }
1396: $request->send_http_header;
1397:
1398: return OK if $request->header_only;
1399:
1400:
1401: my $file=&Apache::lonnet::filelocation("",$request->uri);
1402: my $filetype;
1403: if ($file =~ /\.sty$/) {
1404: $filetype='sty';
1405: } else {
1406: $filetype='html';
1407: }
1408: #
1409: # Edit action? Save file.
1410: #
1411: unless ($env{'request.state'} eq 'published') {
1412: if ($env{'form.savethisfile'}) {
1413: if (&storefile($file,$env{'form.filecont'})) {
1414: &Apache::lonxml::info("<font COLOR=\"#0000FF\">".
1415: &mt('Updated').": ".
1416: &Apache::lonlocal::locallocaltime(time).
1417: " </font>");
1418: }
1419: }
1420: }
1421: my %mystyle;
1422: my $result = '';
1423: my $filecontents=&Apache::lonnet::getfile($file);
1424: if ($filecontents eq -1) {
1425: my $start_page=&Apache::loncommon::start_page('File Error');
1426: my $end_page=&Apache::loncommon::end_page('File Error');
1427: my $fnf=&mt('File not found');
1428: $result=(<<ENDNOTFOUND);
1429: $start_page
1430: <b>$fnf: $file</b>
1431: $end_page
1432: ENDNOTFOUND
1433: $filecontents='';
1434: if ($env{'request.state'} ne 'published') {
1435: if ($filetype eq 'sty') {
1436: $filecontents=&createnewsty();
1437: } else {
1438: $filecontents=&createnewhtml();
1439: }
1440: $env{'form.editmode'}='Edit'; #force edit mode
1441: }
1442: } else {
1443: unless ($env{'request.state'} eq 'published') {
1444: if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1445: &Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
1446: }
1447: #
1448: # we are in construction space, see if edit mode forced
1449: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1450: ['editmode']);
1451: }
1452: if (!$env{'form.editmode'} || $env{'form.viewmode'}) {
1453: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1454: '',%mystyle);
1455: undef($Apache::lonhomework::parsing_a_task);
1456: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1457: ['rawmode']);
1458: if ($env{'form.rawmode'}) { $result = $filecontents; }
1459: }
1460: }
1461:
1462: #
1463: # Edit action? Insert editing commands
1464: #
1465: unless ($env{'request.state'} eq 'published') {
1466: if ($env{'form.editmode'} && (!($env{'form.viewmode'}))) {
1467: my $displayfile=$request->uri;
1468: $displayfile=~s/^\/[^\/]*//;
1469: my %options = ();
1470: if ($env{'environment.remote'} ne 'off') {
1471: $options{'bgcolor'} = '#FFFFFF';
1472: }
1473: my $start_page = &Apache::loncommon::start_page(undef,undef,
1474: \%options);
1475: $result=$start_page.
1476: &Apache::lonxml::message_location().'<h3>'.
1477: $displayfile.
1478: '</h3>'.&Apache::loncommon::end_page();
1479: $result=&inserteditinfo($result,$filecontents,$filetype);
1480: }
1481: }
1482: if ($filetype eq 'html') { &writeallows($request->uri); }
1483:
1484:
1485: &Apache::lonxml::add_messages(\$result);
1486: $request->print($result);
1487:
1488: return OK;
1489: }
1490:
1491: sub display_title {
1492: my $result;
1493: if ($env{'request.state'} eq 'construct') {
1494: my $title=&Apache::lonnet::gettitle();
1495: if (!defined($title) || $title eq '') {
1496: $title = $env{'request.filename'};
1497: $title = substr($title, rindex($title, '/') + 1);
1498: }
1499: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
1500: }
1501: return $result;
1502: }
1503:
1504: sub debug {
1505: if ($Apache::lonxml::debug eq "1") {
1506: $|=1;
1507: my $request=$Apache::lonxml::request;
1508: if (!$request) {
1509: eval { $request=Apache->request; };
1510: }
1511: if (!$request) {
1512: eval { $request=Apache2::RequestUtil->request; };
1513: }
1514: $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1515: #&Apache::lonnet::logthis($_[0]);
1516: }
1517: }
1518:
1519: sub show_error_warn_msg {
1520: if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
1521: &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1522: return 1;
1523: }
1524: return (($Apache::lonxml::debug eq 1) ||
1525: ($env{'request.state'} eq 'construct') ||
1526: ($Apache::lonhomework::browse eq 'F'
1527: &&
1528: $env{'form.show_errors'} eq 'on'));
1529: }
1530:
1531: sub error {
1532: $errorcount++;
1533: if ( &show_error_warn_msg() ) {
1534: # If printing in construction space, put the error inside <pre></pre>
1535: push(@Apache::lonxml::error_messages,
1536: $Apache::lonxml::warnings_error_header.
1537: "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
1538: $Apache::lonxml::warnings_error_header='';
1539: } else {
1540: my $errormsg;
1541: my ($symb)=&Apache::lonnet::symbread();
1542: if ( !$symb ) {
1543: #public or browsers
1544: $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
1545: }
1546: my $msg = join('<br />',@_);
1547: #notify author
1548: &Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1549: #notify course
1550: if ( $symb && $env{'request.course.id'} ) {
1551: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1552: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1553: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1554: my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1555: my @userlist;
1556: foreach (keys %users) {
1557: my ($user,$domain) = split(/:/, $_);
1558: push(@userlist,"$user\@$domain");
1559: my $key=$declutter.'_'.$user.'_'.$domain;
1560: my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
1561: [$key],
1562: $cdom,$cnum);
1563: my $now=time;
1564: if ($now-$lastnotified{$key}>86400) {
1565: &Apache::lonmsg::user_normal_msg($user,$domain,
1566: "Error [$declutter]",$msg);
1567: &Apache::lonnet::put('nohist_xmlerrornotifications',
1568: {$key => $now},
1569: $cdom,$cnum);
1570: }
1571: }
1572: if ($env{'request.role.adv'}) {
1573: $errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1574: } else {
1575: $errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
1576: }
1577: }
1578: push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1579: }
1580: }
1581:
1582: sub warning {
1583: $warningcount++;
1584:
1585: if ($env{'form.grade_target'} ne 'tex') {
1586: if ( &show_error_warn_msg() ) {
1587: push(@Apache::lonxml::warning_messages,
1588: $Apache::lonxml::warnings_error_header.
1589: "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1590: $Apache::lonxml::warnings_error_header='';
1591: }
1592: }
1593: }
1594:
1595: sub info {
1596: if ($env{'form.grade_target'} ne 'tex'
1597: && $env{'request.state'} eq 'construct') {
1598: push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
1599: }
1600: }
1601:
1602: sub message_location {
1603: return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
1604: }
1605:
1606: sub add_messages {
1607: my ($msg)=@_;
1608: my $result=join(' ',
1609: @Apache::lonxml::info_messages,
1610: @Apache::lonxml::error_messages,
1611: @Apache::lonxml::warning_messages);
1612: undef(@Apache::lonxml::info_messages);
1613: undef(@Apache::lonxml::error_messages);
1614: undef(@Apache::lonxml::warning_messages);
1615: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
1616: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1617: }
1618:
1619: sub get_param {
1620: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1621: if ( ! $context ) { $context = -1; }
1622: my $args ='';
1623: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1624: if ( ! $Apache::lonxml::usestyle ) {
1625: $args=$Apache::lonxml::style_values.$args;
1626: }
1627: if ( ! $args ) { return undef; }
1628: if ( $case_insensitive ) {
1629: if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
1630: return &Apache::run::run("{$args;".'return $'.$param.'}',
1631: $safeeval); #'
1632: } else {
1633: return undef;
1634: }
1635: } else {
1636: if ( $args =~ /my \$\Q$param\E=\"/ ) {
1637: return &Apache::run::run("{$args;".'return $'.$param.'}',
1638: $safeeval); #'
1639: } else {
1640: return undef;
1641: }
1642: }
1643: }
1644:
1645: sub get_param_var {
1646: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1647: if ( ! $context ) { $context = -1; }
1648: my $args ='';
1649: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1650: if ( ! $Apache::lonxml::usestyle ) {
1651: $args=$Apache::lonxml::style_values.$args;
1652: }
1653: &Apache::lonxml::debug("Args are $args param is $param");
1654: if ($case_insensitive) {
1655: if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
1656: return undef;
1657: }
1658: } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1659: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1660: &Apache::lonxml::debug("first run is $value");
1661: if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1662: &Apache::lonxml::debug("doing second");
1663: my @result=&Apache::run::run("return $value",$safeeval,1);
1664: if (!defined($result[0])) {
1665: return $value
1666: } else {
1667: if (wantarray) { return @result; } else { return $result[0]; }
1668: }
1669: } else {
1670: return $value;
1671: }
1672: }
1673:
1674: sub register_insert {
1675: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1676: my $i;
1677: my $tagnum=0;
1678: my @order;
1679: for ($i=0;$i < $#data; $i++) {
1680: my $line = $data[$i];
1681: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1682: if ( $line =~ /TABLE/ ) { last; }
1683: my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
1684: if ($tag) {
1685: $insertlist{"$tagnum.tag"} = $tag;
1686: $insertlist{"$tagnum.description"} = $descrip;
1687: $insertlist{"$tagnum.color"} = $color;
1688: $insertlist{"$tagnum.function"} = $function;
1689: if (!defined($show)) { $show='yes'; }
1690: $insertlist{"$tagnum.show"}= $show;
1691: $insertlist{"$tagnum.helpfile"} = $helpfile;
1692: $insertlist{"$tagnum.helpdesc"} = $helpdesc;
1693: $insertlist{"$tag.num"}=$tagnum;
1694: $tagnum++;
1695: }
1696: }
1697: $i++; #skipping TABLE line
1698: $tagnum = 0;
1699: for (;$i < $#data;$i++) {
1700: my $line = $data[$i];
1701: my ($mnemonic,@which) = split(/ +/,$line);
1702: my $tag = $insertlist{"$tagnum.tag"};
1703: for (my $j=0;$j <=$#which;$j++) {
1704: if ( $which[$j] eq 'Y' ) {
1705: if ($insertlist{"$j.show"} ne 'no') {
1706: push(@{ $insertlist{"$tag.which"} },$j);
1707: }
1708: }
1709: }
1710: $tagnum++;
1711: }
1712: }
1713:
1714: sub description {
1715: my ($token)=@_;
1716: my $tagnum;
1717: my $tag=$token->[1];
1718: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1719: my $testtag=$namespace.'::'.$tag;
1720: $tagnum=$insertlist{"$testtag.num"};
1721: if (defined($tagnum)) { last; }
1722: }
1723: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1724: return $insertlist{$tagnum.'.description'};
1725: }
1726:
1727: # Returns a list containing the help file, and the description
1728: sub helpinfo {
1729: my ($token)=@_;
1730: my $tagnum;
1731: my $tag=$token->[1];
1732: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1733: my $testtag=$namespace.'::'.$tag;
1734: $tagnum=$insertlist{"$testtag.num"};
1735: if (defined($tagnum)) { last; }
1736: }
1737: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1738: return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
1739: }
1740:
1741: # ----------------------------------------------------------------- whichuser
1742: # returns a list of $symb, $courseid, $domain, $name that is correct for
1743: # calls to lonnet functions for this setup.
1744: # - looks for form.grade_ parameters
1745: sub whichuser {
1746: my ($passedsymb)=@_;
1747: my ($symb,$courseid,$domain,$name,$publicuser);
1748: if (defined($env{'form.grade_symb'})) {
1749: my ($tmp_courseid)=
1750: &Apache::loncommon::get_env_multiple('form.grade_courseid');
1751: my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
1752: if (!$allowed &&
1753: exists($env{'request.course.sec'}) &&
1754: $env{'request.course.sec'} !~ /^\s*$/) {
1755: $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid.
1756: '/'.$env{'request.course.sec'});
1757: }
1758: if ($allowed) {
1759: ($symb)=&Apache::loncommon::get_env_multiple('form.grade_symb');
1760: $courseid=$tmp_courseid;
1761: ($domain)=&Apache::loncommon::get_env_multiple('form.grade_domain');
1762: ($name)=&Apache::loncommon::get_env_multiple('form.grade_username');
1763: return ($symb,$courseid,$domain,$name,$publicuser);
1764: }
1765: }
1766: if (!$passedsymb) {
1767: $symb=&Apache::lonnet::symbread();
1768: } else {
1769: $symb=$passedsymb;
1770: }
1771: $courseid=$env{'request.course.id'};
1772: $domain=$env{'user.domain'};
1773: $name=$env{'user.name'};
1774: if ($name eq 'public' && $domain eq 'public') {
1775: if (!defined($env{'form.username'})) {
1776: $env{'form.username'}.=time.rand(10000000);
1777: }
1778: $name.=$env{'form.username'};
1779: }
1780: return ($symb,$courseid,$domain,$name,$publicuser);
1781: }
1782:
1783: 1;
1784: __END__
1785:
1786:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>