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