Annotation of loncom/xml/lonxml.pm, revision 1.493
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.493 ! raeburn 4: # $Id: lonxml.pm,v 1.492 2009/04/13 20:15:44 raeburn 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.489 jms 40: =pod
41:
42: =head1 NAME
43:
44: Apache::lonxml
45:
46: =head1 SYNOPSIS
47:
48: XML Parsing Module
49:
50: This is part of the LearningOnline Network with CAPA project
51: described at http://www.lon-capa.org.
52:
53:
54: =head1 SUBROUTINES
55:
56: =cut
57:
58:
1.2 sakharuk 59:
1.4 albertel 60: package Apache::lonxml;
1.33 www 61: use vars
1.410 albertel 62: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
1.1 sakharuk 63: use strict;
1.444 albertel 64: use LONCAPA;
1.167 albertel 65: use HTML::LCParser();
1.161 albertel 66: use HTML::TreeBuilder();
67: use HTML::Entities();
68: use Safe();
69: use Safe::Hole();
70: use Math::Cephes();
71: use Math::Random();
72: use Opcode();
1.271 www 73: use POSIX qw(strftime);
1.339 albertel 74: use Time::HiRes qw( gettimeofday tv_interval );
1.393 albertel 75: use Symbol();
1.266 bowersj2 76:
1.72 albertel 77: sub register {
1.141 albertel 78: my ($space,@taglist) = @_;
79: foreach my $temptag (@taglist) {
80: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72 albertel 81: }
82: }
83:
1.141 albertel 84: sub deregister {
85: my ($space,@taglist) = @_;
86: foreach my $temptag (@taglist) {
87: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
88: if ($tempspace eq $space) {
89: pop(@{ $Apache::lonxml::alltags{$temptag} });
90: }
91: }
1.142 albertel 92: #&printalltags();
1.141 albertel 93: }
94:
1.46 www 95: use Apache::Constants qw(:common);
1.161 albertel 96: use Apache::lontexconvert();
97: use Apache::style();
98: use Apache::run();
99: use Apache::londefdef();
100: use Apache::scripttag();
1.285 www 101: use Apache::languagetags();
1.161 albertel 102: use Apache::edit();
1.266 bowersj2 103: use Apache::inputtags();
104: use Apache::outputtags();
1.372 albertel 105: use Apache::lonnet;
1.161 albertel 106: use Apache::File();
107: use Apache::loncommon();
1.198 www 108: use Apache::lonfeedback();
1.200 www 109: use Apache::lonmsg();
1.217 matthew 110: use Apache::loncacc();
1.431 www 111: use Apache::lonmaxima();
1.280 www 112: use Apache::lonlocal;
1.79 www 113:
1.462 foxr 114: #==================================== Main subroutine: xmlparse
115:
1.72 albertel 116: #debugging control, to turn on debugging modify the correct handler
1.462 foxr 117:
1.72 albertel 118: $Apache::lonxml::debug=0;
1.206 albertel 119:
120: # keeps count of the number of warnings and errors generated in a parse
121: $warningcount=0;
122: $errorcount=0;
1.72 albertel 123:
124: #path to the directory containing the file currently being processed
125: @pwd=();
126:
127: #these two are used for capturing a subset of the output for later processing,
128: #don't touch them directly use &startredirection and &endredirection
129: @outputstack = ();
130: $redirection = 0;
131:
132: #controls wheter the <import> tag actually does
133: $import = 1;
134: @extlinks=();
135:
136: # meta mode is a bit weird only some output is to be turned off
137: #<output> tag turns metamode off (defined in londefdef.pm)
138: $metamode = 0;
139:
140: # turns on and of run::evaluate actually derefencing var refs
141: $evaluate = 1;
1.7 albertel 142:
1.74 albertel 143: # data structure for eidt mode, determines what tags can go into what other tags
144: %insertlist=();
1.68 www 145:
1.99 albertel 146: # stores the list of active tag namespaces
1.76 albertel 147: @namespace=();
148:
1.448 albertel 149: # stores all Scrit Vars displays for later showing
150: my @script_var_displays=();
151:
1.172 albertel 152: # a pointer the the Apache request object
153: $Apache::lonxml::request='';
154:
1.216 sakharuk 155: # a problem number counter, and check on ether it is used
1.237 sakharuk 156: $Apache::lonxml::counter=1;
1.204 albertel 157: $Apache::lonxml::counter_changed=0;
158:
1.462 foxr 159: # Part counter hash. In analysis mode, the
160: # problems can use this to record which parts increment the counter
161: # by how much. The counter subs will maintain this hash via
162: # their optional part parameters. Note that the assumption is that
163: # analysis is done in one request and therefore it is not necessary to
164: # save this information request-to-request.
165:
166:
167: %Apache::lonxml::counters_per_part = ();
168:
1.212 albertel 169: #internal check on whether to look at style defs
170: $Apache::lonxml::usestyle=1;
1.260 albertel 171:
172: #locations used to store the parameter string for style substitutions
173: $Apache::lonxml::style_values='';
174: $Apache::lonxml::style_end_values='';
1.212 albertel 175:
1.281 albertel 176: #array of ssi calls that need to occur after we are done parsing
177: @Apache::lonxml::ssi_info=();
178:
1.282 albertel 179: #should we do the postag variable interpolation
180: $Apache::lonxml::post_evaluate=1;
181:
1.295 albertel 182: #a header message to emit in the case of any generated warning or errors
183: $Apache::lonxml::warnings_error_header='';
184:
1.396 foxr 185: # Control whether or not LaTeX symbols should be substituted for their
186: # \ style equivalents...this may be turned off e.g. in an verbatim
187: # environment.
188:
189: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
190:
191: sub enable_LaTeX_substitutions {
192: $Apache::lonxml::substitute_LaTeX_symbols = 1;
193: }
194: sub disable_LaTeX_substitutions {
195: $Apache::lonxml::substitute_LaTeX_symbols = 0;
196: }
197:
1.68 www 198: sub xmlend {
1.335 sakharuk 199: my ($target,$parser)=@_;
1.278 www 200: my $mode='xml';
201: my $status='OPEN';
1.368 albertel 202: if ($Apache::lonhomework::parsing_a_problem ||
203: $Apache::lonhomework::parsing_a_task ) {
1.278 www 204: $mode='problem';
205: $status=$Apache::inputtags::status[-1];
206: }
1.362 matthew 207: my $discussion;
1.379 albertel 208: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
209: ['LONCAPA_INTERNAL_no_discussion']);
1.372 albertel 210: if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
211: $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
1.362 matthew 212: $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
213: }
1.334 sakharuk 214: if ($target eq 'tex') {
1.335 sakharuk 215: $discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334 sakharuk 216: &Apache::lonxml::newparser($parser,\$discussion,'');
217: return '';
218: }
1.405 albertel 219:
1.407 albertel 220: return $discussion;
1.119 www 221: }
222:
223: sub tokeninputfield {
1.120 www 224: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
225: $defhost=~tr/a-z/A-Z/;
1.119 www 226: return (<<ENDINPUTFIELD)
1.226 albertel 227: <script type="text/javascript">
1.120 www 228: function updatetoken() {
229: var comp=new Array;
230: var barcode=unescape(document.tokeninput.barcode.value);
231: comp=barcode.split('*');
232: if (typeof(comp[0])!="undefined") {
233: document.tokeninput.codeone.value=comp[0];
234: }
235: if (typeof(comp[1])!="undefined") {
236: document.tokeninput.codetwo.value=comp[1];
237: }
238: if (typeof(comp[2])!="undefined") {
239: comp[2]=comp[2].toUpperCase();
240: document.tokeninput.codethree.value=comp[2];
241: }
242: document.tokeninput.barcode.value='';
243: }
244: </script>
245: <form method="post" name="tokeninput">
1.119 www 246: <table border="2" bgcolor="#FFFFBB">
247: <tr><th>DocID Checkin</th></tr>
248: <tr><td>
249: <table>
250: <tr>
251: <td>Scan in Barcode</td>
1.120 www 252: <td><input type="text" size="22" name="barcode"
253: onChange="updatetoken()"/></td>
1.119 www 254: </tr>
255: <tr><td><i>or</i> Type in DocID</td>
256: <td>
257: <input type="text" size="5" name="codeone" />
1.120 www 258: <b><font size="+2">*</font></b>
1.119 www 259: <input type="text" size="5" name="codetwo" />
1.120 www 260: <b><font size="+2">*</font></b>
261: <input type="text" size="10" name="codethree" value="$defhost"
262: onChange="this.value=this.value.toUpperCase()" />
1.119 www 263: </td></tr>
264: </table>
265: </td></tr>
266: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
267: </table>
268: </form>
269: ENDINPUTFIELD
1.112 www 270: }
271:
1.116 www 272: sub maketoken {
1.118 www 273: my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112 www 274: unless ($symb) {
275: $symb=&Apache::lonnet::symbread();
276: }
277: unless ($tuname) {
1.372 albertel 278: $tuname=$env{'user.name'};
279: $tudom=$env{'user.domain'};
280: $tcrsid=$env{'request.course.id'};
1.112 www 281: }
1.116 www 282:
1.118 www 283: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
284: }
285:
286: sub printtokenheader {
1.133 albertel 287: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116 www 288: unless ($token) { return ''; }
1.118 www 289:
1.423 albertel 290: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.133 albertel 291: unless ($tsymb) {
292: $tsymb=$symb;
1.118 www 293: }
294: unless ($tuname) {
1.133 albertel 295: $tuname=$name;
296: $tudom=$domain;
297: $tcrsid=$courseid;
1.118 www 298: }
1.114 www 299:
1.390 albertel 300: my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
1.114 www 301:
1.112 www 302: if ($target eq 'web') {
1.145 www 303: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115 www 304: return
1.221 albertel 305: '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284 www 306: &mt('Checked out for').' '.$plainname.
307: '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
308: '<br />'.&mt('ID').': '.$idhash{$tuname}.
309: '<br />'.&mt('CourseID').': '.$tcrsid.
1.372 albertel 310: '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
1.284 www 311: '<br />'.&mt('DocID').': '.$token.
312: '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112 www 313: } else {
1.121 albertel 314: return $token;
1.112 www 315: }
1.68 www 316: }
317:
1.48 albertel 318: sub printalltags {
319: my $temp;
320: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141 albertel 321: &Apache::lonxml::debug("$temp -- ".
322: join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48 albertel 323: }
324: }
1.31 sakharuk 325:
1.3 sakharuk 326: sub xmlparse {
1.172 albertel 327: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96 albertel 328:
1.172 albertel 329: &setup_globals($request,$target);
1.232 albertel 330: &Apache::inputtags::initialize_inputtags();
1.370 albertel 331: &Apache::bridgetask::initialize_bridgetask();
1.232 albertel 332: &Apache::outputtags::initialize_outputtags();
333: &Apache::edit::initialize_edit();
1.287 albertel 334: &Apache::londefdef::initialize_londefdef();
1.244 albertel 335:
1.178 www 336: #
337: # do we have a course style file?
338: #
339:
1.372 albertel 340: if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
1.178 www 341: my $bodytext=
1.372 albertel 342: $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
1.178 www 343: if ($bodytext) {
1.337 albertel 344: foreach my $file (split(',',$bodytext)) {
345: my $location=&Apache::lonnet::filelocation('',$file);
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: }
351: }
352: }
1.449 albertel 353: } elsif ($env{'construct.style'}
354: && ($env{'request.state'} eq 'construct')) {
1.372 albertel 355: my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
1.291 sakharuk 356: my $styletext=&Apache::lonnet::getfile($location);
1.449 albertel 357: if ($styletext ne '-1') {
358: %style_for_target = (%style_for_target,
359: &Apache::style::styleparser($target,$styletext));
360: }
1.178 www 361: }
1.255 sakharuk 362: #&printalltags();
1.16 albertel 363: my @pars = ();
1.372 albertel 364: my $pwd=$env{'request.filename'};
1.23 albertel 365: $pwd =~ s:/[^/]*$::;
366: &newparser(\@pars,\$content_file_string,$pwd);
1.24 sakharuk 367:
1.3 sakharuk 368: my $safeeval = new Safe;
1.40 albertel 369: my $safehole = new Safe::Hole;
1.82 ng 370: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 371: #-------------------- Redefinition of the target in the case of compound target
372:
373: ($target, my @tenta) = split('&&',$target);
374:
1.150 albertel 375: my @stack = ();
1.3 sakharuk 376: my @parstack = ();
1.358 albertel 377: &initdepth();
378: &init_alarm();
1.101 albertel 379: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
1.394 albertel 380: $safeeval,\%style_for_target,1);
1.255 sakharuk 381:
1.425 albertel 382: if (@stack) {
1.482 bisitz 383: &warning(&mt('At end of file some tags were still left unclosed:').
384: ' <tt><'.join('></tt>, <tt><',reverse(@stack)).
1.425 albertel 385: '></tt>');
386: }
1.372 albertel 387: if ($env{'request.uri'}) {
388: &writeallows($env{'request.uri'});
1.125 www 389: }
1.281 albertel 390: &do_registered_ssi();
1.204 albertel 391: if ($Apache::lonxml::counter_changed) { &store_counter() }
1.393 albertel 392:
393: &clean_safespace($safeeval);
394:
1.448 albertel 395: if (@script_var_displays) {
396: $finaloutput .= join('',@script_var_displays);
397: undef(@script_var_displays);
398: }
1.469 albertel 399: &init_state();
1.372 albertel 400: if ($env{'form.return_only_error_and_warning_counts'}) {
1.473 www 401: if ($env{'request.filename'}=~/\.(html|htm|xml)$/i) {
402: my $error=&verify_html($content_file_string);
403: if ($error) { $errorcount++; }
404: }
1.361 www 405: return "$errorcount:$warningcount";
406: }
1.3 sakharuk 407: return $finaloutput;
1.106 www 408: }
409:
1.191 albertel 410: sub latex_special_symbols {
1.272 albertel 411: my ($string,$where)=@_;
1.396 foxr 412: #
413: # If e.g. in verbatim mode, then don't substitute.
414: # but return original string.
415: #
416: if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
417: return $string;
418: }
1.235 sakharuk 419: if ($where eq 'header') {
1.414 foxr 420: $string =~ s/\\/\$\\backslash\$/g; # \ -> $\backslash$ per LaTex line by line pg 10.
1.311 albertel 421: $string =~ s/(\$|%|\{|\})/\\$1/g;
422: $string=&Apache::lonprintout::character_chart($string);
423: # any & or # leftover should be safe to just escape
424: $string=~s/([^\\])\&/$1\\\&/g;
425: $string=~s/([^\\])\#/$1\\\#/g;
1.415 foxr 426: $string =~ s/_/\\_/g; # _ -> \_
427: $string =~ s/\^/\\\^{}/g; # ^ -> \^{}
1.229 sakharuk 428: } else {
1.312 albertel 429: $string=~s/\\/\\ensuremath{\\backslash}/g;
1.367 albertel 430: $string=~s/\\\%|\%/\\\%/g;
431: $string=~s/\\{|{/\\{/g;
432: $string=~s/\\}|}/\\}/g;
1.378 albertel 433: $string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
1.367 albertel 434: $string=~s/\\\$|\$/\\\$/g;
435: $string=~s/\\\_|\_/\\\_/g;
1.313 albertel 436: $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310 sakharuk 437: $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311 albertel 438: $string=&Apache::lonprintout::character_chart($string);
439: # any & or # leftover should be safe to just escape
1.367 albertel 440: $string=~s/\\\&|\&/\\\&/g;
441: $string=~s/\\\#|\#/\\\#/g;
1.332 sakharuk 442: $string=~s/\|/\$\\mid\$/g;
1.310 sakharuk 443: #single { or } How to escape?
1.229 sakharuk 444: }
1.272 albertel 445: return $string;
1.188 sakharuk 446: }
447:
1.101 albertel 448: sub inner_xmlparse {
1.394 albertel 449: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
1.101 albertel 450: my $finaloutput = '';
451: my $result;
452: my $token;
1.258 albertel 453: my $dontpop=0;
1.389 albertel 454: my $startredirection = $Apache::lonxml::redirection;
1.101 albertel 455: while ( $#$pars > -1 ) {
456: while ($token = $$pars['-1']->get_token) {
1.261 albertel 457: if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101 albertel 458: if ($metamode<1) {
1.190 albertel 459: my $text=$token->[1];
1.193 albertel 460: if ($token->[0] eq 'C' && $target eq 'tex') {
1.239 sakharuk 461: $text = '';
462: # $text = '%'.$text."\n";
1.182 sakharuk 463: }
1.190 albertel 464: $result.=$text;
1.101 albertel 465: }
1.261 albertel 466: } elsif (($token->[0] eq 'D')) {
467: if ($metamode<1 && $target eq 'web') {
468: my $text=$token->[1];
469: $result.=$text;
470: }
1.101 albertel 471: } elsif ($token->[0] eq 'PI') {
1.261 albertel 472: if ($metamode<1 && $target eq 'web') {
1.101 albertel 473: $result=$token->[2];
474: }
475: } elsif ($token->[0] eq 'S') {
1.140 albertel 476: # add tag to stack
1.101 albertel 477: push (@$stack,$token->[1]);
478: # add parameters list to another stack
479: push (@$parstack,&parstring($token));
1.140 albertel 480: &increasedepth($token);
1.212 albertel 481: if ($Apache::lonxml::usestyle &&
482: exists($$style_for_target{$token->[1]})) {
483: $Apache::lonxml::usestyle=0;
484: my $string=$$style_for_target{$token->[1]}.
485: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
486: &Apache::lonxml::newparser($pars,\$string);
1.257 albertel 487: $Apache::lonxml::style_values=$$parstack[-1];
1.259 albertel 488: $Apache::lonxml::style_end_values=$$parstack[-1];
1.101 albertel 489: } else {
490: $result = &callsub("start_$token->[1]", $target, $token, $stack,
491: $parstack, $pars, $safeeval, $style_for_target);
1.140 albertel 492: }
1.101 albertel 493: } elsif ($token->[0] eq 'E') {
1.212 albertel 494: if ($Apache::lonxml::usestyle &&
495: exists($$style_for_target{'/'."$token->[1]"})) {
496: $Apache::lonxml::usestyle=0;
497: my $string=$$style_for_target{'/'.$token->[1]}.
1.258 albertel 498: '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212 albertel 499: &Apache::lonxml::newparser($pars,\$string);
1.259 albertel 500: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
501: $Apache::lonxml::style_end_values='';
1.258 albertel 502: $dontpop=1;
1.101 albertel 503: } else {
1.258 albertel 504: #clear out any tags that didn't end
505: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
506: my $lasttag=$$stack[-1];
1.317 albertel 507: if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.483 bisitz 508: &Apache::lonxml::warning(&mt('Using tag [_1] on line [_2] as end tag to [_3]','</'.$token->[1].'>','.$token->[3].','<'.$$stack[-1].'>'));
1.258 albertel 509: last;
510: } else {
1.483 bisitz 511: &Apache::lonxml::warning(&mt('Found tag [_1] on line [_2] when looking for [_3] in file.','</'.$token->[1].'>',$token->[3],'</'.$$stack[-1].'>'));
1.258 albertel 512: &end_tag($stack,$parstack,$token);
513: }
514: }
515: $result = &callsub("end_$token->[1]", $target, $token, $stack,
516: $parstack, $pars,$safeeval, $style_for_target);
1.101 albertel 517: }
518: } else {
519: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
520: }
521: #evaluate variable refs in result
1.282 albertel 522: if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257 albertel 523: my $extras;
524: if (!$Apache::lonxml::usestyle) {
525: $extras=$Apache::lonxml::style_values;
526: }
1.488 raeburn 527: if ( $#$parstack > -1 ) {
528: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
529: } else {
530: $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.487 raeburn 531: }
1.163 albertel 532: }
1.282 albertel 533: $Apache::lonxml::post_evaluate=1;
534:
1.190 albertel 535: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249 albertel 536: #Style file definitions should be correct
1.250 albertel 537: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311 albertel 538: $result=&latex_special_symbols($result);
1.249 albertel 539: }
1.190 albertel 540: }
541:
1.169 albertel 542: if ($Apache::lonxml::redirection) {
543: $Apache::lonxml::outputstack['-1'] .= $result;
544: } else {
545: $finaloutput.=$result;
546: }
547: $result = '';
548:
1.258 albertel 549: if ($token->[0] eq 'E' && !$dontpop) {
1.101 albertel 550: &end_tag($stack,$parstack,$token);
551: }
1.258 albertel 552: $dontpop=0;
1.224 albertel 553: }
1.212 albertel 554: if ($#$pars > -1) {
555: pop @$pars;
556: pop @Apache::lonxml::pwd;
557: }
1.101 albertel 558: }
559:
560: # if ($target eq 'meta') {
561: # $finaloutput.=&endredirection;
562: # }
563:
1.394 albertel 564: if ( $start && $target eq 'grade') { &endredirection(); }
1.389 albertel 565: if ( $Apache::lonxml::redirection > $startredirection) {
566: while ($Apache::lonxml::redirection > $startredirection) {
567: $finaloutput .= &endredirection();
1.387 albertel 568: }
569: }
1.101 albertel 570: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
571: $finaloutput=&afterburn($finaloutput);
1.216 sakharuk 572: }
1.101 albertel 573: return $finaloutput;
574: }
1.67 www 575:
1.318 matthew 576: ##
577: ## Looks to see if there is a subroutine defined for this tag. If so, call it,
578: ## otherwise do not call it as we do not know what it is.
579: ##
1.7 albertel 580: sub callsub {
1.84 albertel 581: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 582: my $currentstring='';
1.72 albertel 583: my $nodefault;
1.7 albertel 584: {
1.59 albertel 585: my $sub1;
1.7 albertel 586: no strict 'refs';
1.68 www 587: my $tag=$token->[1];
1.236 www 588: # get utterly rid of extended html tags
589: if ($tag=~/^x\-/i) { return ''; }
1.141 albertel 590: my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68 www 591: if (!$space) {
1.141 albertel 592: $tag=~tr/A-Z/a-z/;
1.68 www 593: $sub=~tr/A-Z/a-z/;
1.141 albertel 594: $space=$Apache::lonxml::alltags{$tag}[-1]
1.68 www 595: }
1.97 albertel 596:
597: my $deleted=0;
598: if (($token->[0] eq 'S') && ($target eq 'modified')) {
599: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
600: $parstack,$parser,$safeeval,
601: $style);
602: }
603: if (!$deleted) {
604: if ($space) {
1.220 albertel 605: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97 albertel 606: $sub1="$space\:\:$sub";
607: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
608: $parstack,$parser,$safeeval,
609: $style);
610: } else {
1.318 matthew 611: if ($target eq 'tex') {
612: # throw away tag name
613: return '';
614: }
1.220 albertel 615: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97 albertel 616: if ($metamode <1) {
617: if (defined($token->[4]) && ($metamode < 1)) {
618: $currentstring = $token->[4];
619: } else {
620: $currentstring = $token->[2];
621: }
1.62 sakharuk 622: }
1.7 albertel 623: }
1.97 albertel 624: # &Apache::lonxml::debug("nodefalt:$nodefault:");
625: if ($currentstring eq '' && $nodefault eq '') {
626: if ($target eq 'edit') {
1.220 albertel 627: #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97 albertel 628: if ($token->[0] eq 'S') {
629: $currentstring = &Apache::edit::tag_start($target,$token);
630: } elsif ($token->[0] eq 'E') {
631: $currentstring = &Apache::edit::tag_end($target,$token);
632: }
1.441 albertel 633: }
634: }
635: if ($target eq 'modified' && $nodefault eq '') {
636: if ($currentstring eq '') {
637: if ($token->[0] eq 'S') {
638: $currentstring = $token->[4];
639: } elsif ($token->[0] eq 'E') {
640: $currentstring = $token->[2];
641: } else {
642: $currentstring = $token->[2];
643: }
644: }
1.97 albertel 645: if ($token->[0] eq 'S') {
1.447 albertel 646: $currentstring.=&Apache::edit::handle_insert();
1.210 www 647: } elsif ($token->[0] eq 'E') {
1.447 albertel 648: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97 albertel 649: }
1.72 albertel 650: }
1.7 albertel 651: }
652: use strict 'refs';
653: }
654: return $currentstring;
1.82 ng 655: }
656:
1.465 albertel 657: {
658: my %state;
659:
660: sub init_state {
661: undef(%state);
662: }
663:
664: sub set_state {
665: my ($key,$value) = @_;
666: $state{$key} = $value;
667: return $value;
668: }
669: sub get_state {
670: my ($key) = @_;
671: return $state{$key};
672: }
673: }
674:
1.96 albertel 675: sub setup_globals {
1.172 albertel 676: my ($request,$target)=@_;
677: $Apache::lonxml::request=$request;
1.205 www 678: $errorcount=0;
679: $warningcount=0;
1.490 www 680: $Apache::lonxml::internal_error=0;
1.207 albertel 681: $Apache::lonxml::default_homework_loaded=0;
1.212 albertel 682: $Apache::lonxml::usestyle=1;
1.204 albertel 683: &init_counter();
1.462 foxr 684: &clear_bubble_lines_for_part();
1.465 albertel 685: &init_state();
1.469 albertel 686: &set_state('target',$target);
1.101 albertel 687: @Apache::lonxml::pwd=();
1.124 albertel 688: @Apache::lonxml::extlinks=();
1.448 albertel 689: @script_var_displays=();
1.281 albertel 690: @Apache::lonxml::ssi_info=();
1.282 albertel 691: $Apache::lonxml::post_evaluate=1;
1.295 albertel 692: $Apache::lonxml::warnings_error_header='';
1.397 albertel 693: $Apache::lonxml::substitute_LaTeX_symbols = 1;
1.96 albertel 694: if ($target eq 'meta') {
695: $Apache::lonxml::redirection = 0;
696: $Apache::lonxml::metamode = 1;
697: $Apache::lonxml::evaluate = 1;
698: $Apache::lonxml::import = 0;
1.129 albertel 699: } elsif ($target eq 'answer') {
700: $Apache::lonxml::redirection = 0;
701: $Apache::lonxml::metamode = 1;
702: $Apache::lonxml::evaluate = 1;
703: $Apache::lonxml::import = 1;
1.96 albertel 704: } elsif ($target eq 'grade') {
1.387 albertel 705: &startredirection(); #ended in inner_xmlparse on exit
1.96 albertel 706: $Apache::lonxml::metamode = 0;
707: $Apache::lonxml::evaluate = 1;
708: $Apache::lonxml::import = 1;
709: } elsif ($target eq 'modified') {
710: $Apache::lonxml::redirection = 0;
711: $Apache::lonxml::metamode = 0;
712: $Apache::lonxml::evaluate = 0;
713: $Apache::lonxml::import = 0;
714: } elsif ($target eq 'edit') {
715: $Apache::lonxml::redirection = 0;
716: $Apache::lonxml::metamode = 0;
717: $Apache::lonxml::evaluate = 0;
718: $Apache::lonxml::import = 0;
1.163 albertel 719: } elsif ($target eq 'analyze') {
720: $Apache::lonxml::redirection = 0;
721: $Apache::lonxml::metamode = 0;
722: $Apache::lonxml::evaluate = 1;
723: $Apache::lonxml::import = 1;
1.96 albertel 724: } else {
725: $Apache::lonxml::redirection = 0;
726: $Apache::lonxml::metamode = 0;
727: $Apache::lonxml::evaluate = 1;
728: $Apache::lonxml::import = 1;
729: }
730: }
731:
1.82 ng 732: sub init_safespace {
733: my ($target,$safeeval,$safehole,$safeinit) = @_;
1.393 albertel 734: $safeeval->deny_only(':dangerous');
735: $safeeval->reval('use Math::Complex;');
1.383 albertel 736: $safeeval->permit_only(":default");
1.82 ng 737: $safeeval->permit("entereval");
738: $safeeval->permit(":base_math");
739: $safeeval->permit("sort");
1.286 albertel 740: $safeeval->permit("time");
1.480 www 741: $safeeval->permit("caller");
1.371 albertel 742: $safeeval->deny("rand");
743: $safeeval->deny("srand");
1.82 ng 744: $safeeval->deny(":base_io");
1.102 albertel 745: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251 albertel 746: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82 ng 747: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324 albertel 748: $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
749: '&chem_standard_order');
1.369 albertel 750: $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
1.471 bisitz 751: $safehole->wrap(\&Apache::response::implicit_multiplication,$safeeval,'&implicit_multiplication');
1.324 albertel 752:
1.431 www 753: $safehole->wrap(\&Apache::lonmaxima::maxima_eval,$safeeval,'&maxima_eval');
1.432 www 754: $safehole->wrap(\&Apache::lonmaxima::maxima_check,$safeeval,'&maxima_check');
1.433 albertel 755: $safehole->wrap(\&Apache::lonmaxima::maxima_cas_formula_fix,$safeeval,
756: '&maxima_cas_formula_fix');
757:
758: $safehole->wrap(\&Apache::caparesponse::capa_formula_fix,$safeeval,
759: '&capa_formula_fix');
1.431 www 760:
1.480 www 761: $safehole->wrap(\&Apache::lonlocal::locallocaltime,$safeeval,
762: '&locallocaltime');
763:
1.82 ng 764: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
765: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
766: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
767: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
768: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
769: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
770: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
771: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
772: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
773: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
774: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
775: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
776: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
777: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
778: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
779: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
780: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
781: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
782: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215 albertel 783:
784: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
785: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
786: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
787: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
788: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
789: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
790: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
791: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
792: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
793: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
794: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
795: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
796: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
797: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
798: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
799: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
800: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
801: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
802: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
803: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
804: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
805: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
806:
1.383 albertel 807: $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
808: $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
809: '&Math::Cephes::Matrix::new');
810: $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
811: '&Math::Cephes::Matrix::coef');
812: $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
813: '&Math::Cephes::Matrix::clr');
814: $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
815: '&Math::Cephes::Matrix::add');
816: $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
817: '&Math::Cephes::Matrix::sub');
818: $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
819: '&Math::Cephes::Matrix::mul');
820: $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
821: '&Math::Cephes::Matrix::div');
822: $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
823: '&Math::Cephes::Matrix::inv');
824: $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
825: '&Math::Cephes::Matrix::transp');
826: $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
827: '&Math::Cephes::Matrix::simq');
828: $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
829: '&Math::Cephes::Matrix::mat_to_vec');
830: $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
831: '&Math::Cephes::Matrix::vec_to_mat');
832: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
833: '&Math::Cephes::Matrix::check');
834: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
835: '&Math::Cephes::Matrix::check');
836:
1.215 albertel 837: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
838: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
839: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
840: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
841: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
842: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
843:
1.91 ng 844: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
845: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
846: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
847: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
848: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
849: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
850: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
851: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
852: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
853: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
854: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 855: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 856: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
857: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
858: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
859: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
860: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
861: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
862: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
863: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
864: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.458 albertel 865: $safehole->wrap(\&Apache::loncommon::languages,$safeeval,'&languages');
1.305 albertel 866: $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311 albertel 867: $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.420 albertel 868: $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&LONCAPA_INTERNAL_LOGTHIS');
869: $safehole->wrap(\&Apache::inputtags::finalizeawards,$safeeval,'&LONCAPA_INTERNAL_FINALIZEAWARDS');
1.322 albertel 870: $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.430 albertel 871: # use Data::Dumper;
872: # $safehole->wrap(\&Data::Dumper::Dumper,$safeeval,'&LONCAPA_INTERNAL_Dumper');
1.82 ng 873: #need to inspect this class of ops
874: # $safeeval->deny(":base_orig");
1.331 albertel 875: $safeeval->permit("require");
1.91 ng 876: $safeinit .= ';$external::target="'.$target.'";';
1.82 ng 877: &Apache::run::run($safeinit,$safeeval);
1.373 albertel 878: &initialize_rndseed($safeeval);
879: }
1.303 albertel 880:
1.393 albertel 881: sub clean_safespace {
882: my ($safeeval) = @_;
883: delete_package_recurse($safeeval->{Root});
884: }
885:
886: sub delete_package_recurse {
887: my ($package) = @_;
888: my @subp;
889: {
890: no strict 'refs';
891: while (my ($key,$val) = each(%{*{"$package\::"}})) {
892: if (!defined($val)) { next; }
893: local (*ENTRY) = $val;
894: if (defined *ENTRY{HASH} && $key =~ /::$/ &&
895: $key ne "main::" && $key ne "<none>::")
896: {
897: my ($p) = $package ne "main" ? "$package\::" : "";
898: ($p .= $key) =~ s/::$//;
899: push(@subp,$p);
900: }
901: }
902: }
903: foreach my $p (@subp) {
904: delete_package_recurse($p);
905: }
906: Symbol::delete_package($package);
907: }
908:
1.373 albertel 909: sub initialize_rndseed {
910: my ($safeeval)=@_;
911: my $rndseed;
1.423 albertel 912: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.373 albertel 913: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
914: my $safeinit = '$external::randomseed="'.$rndseed.'";';
915: &Apache::lonxml::debug("Setting rndseed to $rndseed");
916: &Apache::run::run($safeinit,$safeeval);
1.207 albertel 917: }
918:
919: sub default_homework_load {
920: my ($safeeval)=@_;
921: &Apache::lonxml::debug('Loading default_homework');
922: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241 albertel 923: if ($default eq -1) {
1.207 albertel 924: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
925: } else {
926: &Apache::run::run($default,$safeeval);
927: $Apache::lonxml::default_homework_loaded=1;
928: }
1.17 albertel 929: }
930:
1.358 albertel 931: {
932: my $alarm_depth;
933: sub init_alarm {
934: alarm(0);
935: $alarm_depth=0;
936: }
937:
938: sub start_alarm {
939: if ($alarm_depth<1) {
940: my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
941: if ($old) {
942: &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
943: }
944: }
945: $alarm_depth++;
946: }
947:
948: sub end_alarm {
949: $alarm_depth--;
950: if ($alarm_depth<1) { alarm(0); }
951: }
952: }
1.328 albertel 953: my $metamode_was;
1.55 albertel 954: sub startredirection {
1.328 albertel 955: if (!$Apache::lonxml::redirection) {
956: $metamode_was=$Apache::lonxml::metamode;
957: }
958: $Apache::lonxml::metamode=0;
959: $Apache::lonxml::redirection++;
960: push (@Apache::lonxml::outputstack, '');
1.55 albertel 961: }
962:
963: sub endredirection {
1.328 albertel 964: if (!$Apache::lonxml::redirection) {
1.380 www 965: &Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
1.328 albertel 966: return '';
967: }
968: $Apache::lonxml::redirection--;
969: if (!$Apache::lonxml::redirection) {
970: $Apache::lonxml::metamode=$metamode_was;
971: }
972: pop @Apache::lonxml::outputstack;
1.97 albertel 973: }
1.459 albertel 974: sub in_redirection {
975: return ($Apache::lonxml::redirection > 0)
976: }
1.97 albertel 977:
978: sub end_tag {
979: my ($tagstack,$parstack,$token)=@_;
980: pop(@$tagstack);
981: pop(@$parstack);
982: &decreasedepth($token);
1.55 albertel 983: }
984:
1.17 albertel 985: sub initdepth {
986: @Apache::lonxml::depthcounter=();
1.439 albertel 987: undef($Apache::lonxml::last_depth_count);
1.17 albertel 988: }
989:
1.438 albertel 990:
1.339 albertel 991: my @timers;
992: my $lasttime;
1.438 albertel 993: # @Apache::lonxml::depthcounter -> count of tags that exist so
994: # far at each level
1.439 albertel 995: # $Apache::lonxml::last_depth_count -> when ascending, need to
996: # remember the count for the level below the current level (for
997: # example going from 1_2 -> 1 -> 1_3 need to remember the 2 )
1.438 albertel 998:
1.17 albertel 999: sub increasedepth {
1.19 albertel 1000: my ($token) = @_;
1.439 albertel 1001: push(@Apache::lonxml::depthcounter,$Apache::lonxml::last_depth_count+1);
1002: undef($Apache::lonxml::last_depth_count);
1.340 albertel 1003: my $time;
1004: if ($Apache::lonxml::debug eq "1") {
1005: push(@timers,[&gettimeofday()]);
1006: $time=&tv_interval($lasttime);
1007: $lasttime=[&gettimeofday()];
1008: }
1.439 albertel 1009: my $spacing=' 'x($#Apache::lonxml::depthcounter);
1.438 albertel 1010: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.439 albertel 1011: # &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time");
1.54 albertel 1012: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 1013: }
1014:
1015: sub decreasedepth {
1.19 albertel 1016: my ($token) = @_;
1.439 albertel 1017: if ( $#Apache::lonxml::depthcounter == -1) {
1018: &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43 albertel 1019: }
1.439 albertel 1020: $Apache::lonxml::last_depth_count = pop(@Apache::lonxml::depthcounter);
1021:
1.340 albertel 1022: my ($timer,$time);
1023: if ($Apache::lonxml::debug eq "1") {
1024: $timer=pop(@timers);
1025: $time=&tv_interval($lasttime);
1026: $lasttime=[&gettimeofday()];
1027: }
1.439 albertel 1028: my $spacing=' 'x($#Apache::lonxml::depthcounter);
1029: $Apache::lonxml::curdepth = join('_',@Apache::lonxml::depthcounter);
1030: # &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time : ".&tv_interval($timer));
1.54 albertel 1031: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 1032: }
1.19 albertel 1033:
1.399 albertel 1034: sub get_id {
1035: my ($parstack,$safeeval)=@_;
1036: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.449 albertel 1037: if ($env{'request.state'} eq 'construct' && $id =~ /([._]|[^\w\d\s[:punct:]])/) {
1038: &error(&mt("ID "[_1]" contains invalid characters, IDs are only allowed to contain letters, numbers, spaces and -",'<tt>'.$id.'</tt>'));
1.399 albertel 1039: }
1040: if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
1041: return $id;
1042: }
1043:
1.180 albertel 1044: sub get_all_text_unbalanced {
1.190 albertel 1045: #there is a copy of this in lonpublisher.pm
1.326 albertel 1046: my($tag,$pars)= @_;
1047: my $token;
1048: my $result='';
1049: $tag='<'.$tag.'>';
1050: while ($token = $$pars[-1]->get_token) {
1051: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.386 albertel 1052: if ($token->[0] eq 'T' && $token->[2]) {
1.382 albertel 1053: $result.='<![CDATA['.$token->[1].']]>';
1054: } else {
1055: $result.=$token->[1];
1056: }
1.326 albertel 1057: } elsif ($token->[0] eq 'PI') {
1058: $result.=$token->[2];
1059: } elsif ($token->[0] eq 'S') {
1060: $result.=$token->[4];
1061: } elsif ($token->[0] eq 'E') {
1062: $result.=$token->[2];
1063: }
1064: if ($result =~ /\Q$tag\E/is) {
1065: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
1066: #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
1067: #&Apache::lonxml::debug('Result is :'.$1);
1068: $redo=$tag.$redo;
1069: &Apache::lonxml::newparser($pars,\$redo);
1070: last;
1071: }
1072: }
1073: return $result
1.462 foxr 1074:
1.204 albertel 1075: }
1076:
1.462 foxr 1077: #########################################################################
1078: # #
1079: # bubble line counter management #
1080: # #
1081: #########################################################################
1082:
1.447 albertel 1083: =pod
1084:
1085: For bubble grading mode and exam bubble printing mode, the tracking of
1086: the current 'bubble line number' is stored in the %env element
1087: 'form.counter', and is modifed and handled by the following routines.
1088:
1089: The value of it is stored in $Apache:lonxml::counter when live and
1090: stored back to env after done.
1091:
1092: =item &increment_counter($increment);
1093:
1094: Increments the internal counter environment variable a specified amount
1095:
1096: Optional Arguments:
1097: $increment - amount to increment by (defaults to 1)
1.462 foxr 1098: Also 1 if the value is negative or zero.
1.467 foxr 1099: $part_response - A concatenation of the part and response id
1100: identifying exactly what is being 'answered'.
1101:
1.447 albertel 1102:
1103: =cut
1104:
1.204 albertel 1105: sub increment_counter {
1.467 foxr 1106: my ($increment, $part_response) = @_;
1.481 raeburn 1107: if ($env{'form.grade_noincrement'}) { return; }
1.462 foxr 1108: if (!defined($increment) || $increment le 0) {
1109: $increment = 1;
1110: }
1111: $Apache::lonxml::counter += $increment;
1112:
1.466 foxr 1113: # If the caller supplied the response_id parameter,
1.462 foxr 1114: # Maintain its counter.. creating if necessary.
1115:
1.470 albertel 1116: if (defined($part_response)) {
1.467 foxr 1117: if (!defined($Apache::lonxml::counters_per_part{$part_response})) {
1118: $Apache::lonxml::counters_per_part{$part_response} = 0;
1.462 foxr 1119: }
1.467 foxr 1120: $Apache::lonxml::counters_per_part{$part_response} += $increment;
1121: my $new_value = $Apache::lonxml::counters_per_part{$part_response};
1.247 albertel 1122: }
1.462 foxr 1123:
1.289 sakharuk 1124: $Apache::lonxml::counter_changed=1;
1.204 albertel 1125: }
1126:
1.447 albertel 1127: =pod
1128:
1.461 foxr 1129: =item &init_counter($increment);
1.447 albertel 1130:
1131: Initialize the internal counter environment variable
1132:
1133: =cut
1134:
1.204 albertel 1135: sub init_counter {
1.391 albertel 1136: if ($env{'request.state'} eq 'construct') {
1137: $Apache::lonxml::counter=1;
1138: $Apache::lonxml::counter_changed=1;
1139: } elsif (defined($env{'form.counter'})) {
1.372 albertel 1140: $Apache::lonxml::counter=$env{'form.counter'};
1.247 albertel 1141: $Apache::lonxml::counter_changed=0;
1.237 sakharuk 1142: } else {
1.204 albertel 1143: $Apache::lonxml::counter=1;
1.247 albertel 1144: $Apache::lonxml::counter_changed=1;
1.204 albertel 1145: }
1146: }
1147:
1148: sub store_counter {
1.474 raeburn 1149: &Apache::lonnet::appenv({'form.counter' => $Apache::lonxml::counter});
1.401 albertel 1150: $Apache::lonxml::counter_changed=0;
1.204 albertel 1151: return '';
1.180 albertel 1152: }
1153:
1.398 albertel 1154: {
1155: my $state;
1156: sub clear_problem_counter {
1157: undef($state);
1158: &Apache::lonnet::delenv('form.counter');
1159: &Apache::lonxml::init_counter();
1160: &Apache::lonxml::store_counter();
1161: }
1162:
1163: sub remember_problem_counter {
1.422 albertel 1164: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.398 albertel 1165: $state = $env{'form.counter'};
1166: }
1167:
1168: sub restore_problem_counter {
1169: if (defined($state)) {
1.474 raeburn 1170: &Apache::lonnet::appenv({'form.counter' => $state});
1.398 albertel 1171: }
1172: }
1.401 albertel 1173: sub get_problem_counter {
1174: if ($Apache::lonxml::counter_changed) { &store_counter() }
1.422 albertel 1175: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.401 albertel 1176: return $env{'form.counter'};
1177: }
1.398 albertel 1178: }
1179:
1.462 foxr 1180: =pod
1181:
1.468 albertel 1182: =item bubble_lines_for_part(part_response)
1.462 foxr 1183:
1184: Returns the number of lines required to get a response for
1.467 foxr 1185: $part_response (this is just $Apache::lonxml::counters_per_part{$part_response}
1.462 foxr 1186:
1187: =cut
1188:
1189: sub bubble_lines_for_part {
1.467 foxr 1190: my ($part_response) = @_;
1.462 foxr 1191:
1.467 foxr 1192: if (!defined($Apache::lonxml::counters_per_part{$part_response})) {
1.462 foxr 1193: return 0;
1194: } else {
1.467 foxr 1195: return $Apache::lonxml::counters_per_part{$part_response};
1.462 foxr 1196: }
1197: }
1198:
1199: =pod
1200:
1201: =item clear_bubble_lines_for_part
1202:
1203: Clears the hash of bubble lines per part. If a caller
1204: needs to analyze several resources this should be called between
1205: resources to reset the hash for each problem being analyzed.
1206:
1207: =cut
1208:
1209: sub clear_bubble_lines_for_part {
1210: undef(%Apache::lonxml::counters_per_part);
1211: }
1212:
1213: =pod
1214:
1.468 albertel 1215: =item set_bubble_lines(part_response, value)
1.462 foxr 1216:
1217: If there is a problem part, that for whatever reason
1218: requires bubble lines that are not
1219: the same as the counter increment, it can call this sub during
1220: analysis to set its hash value explicitly.
1221:
1222: =cut
1223:
1224: sub set_bubble_lines {
1.467 foxr 1225: my ($part_response, $value) = @_;
1.462 foxr 1226:
1.467 foxr 1227: $Apache::lonxml::counters_per_part{$part_response} = $value;
1.462 foxr 1228: }
1229:
1230: =pod
1231:
1232: =item get_bubble_line_hash
1233:
1234: Returns the current bubble line hash. This is assumed to
1235: be small so we return a copy
1236:
1237:
1238: =cut
1239:
1240: sub get_bubble_line_hash {
1241: return %Apache::lonxml::counters_per_part;
1242: }
1243:
1244:
1245: #--------------------------------------------------
1246:
1.19 albertel 1247: sub get_all_text {
1.270 albertel 1248: my($tag,$pars,$style)= @_;
1249: my $gotfullstack=1;
1250: if (ref($pars) ne 'ARRAY') {
1251: $gotfullstack=0;
1252: $pars=[$pars];
1253: }
1254: if (ref($style) ne 'HASH') {
1255: $style={};
1256: }
1257: my $depth=0;
1258: my $token;
1259: my $result='';
1260: if ( $tag =~ m:^/: ) {
1261: my $tag=substr($tag,1);
1262: #&Apache::lonxml::debug("have:$tag:");
1263: my $top_empty=0;
1264: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1265: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1266: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1267: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.382 albertel 1268: if ($token->[2]) {
1269: $result.='<![CDATA['.$token->[1].']]>';
1270: } else {
1271: $result.=$token->[1];
1272: }
1.270 albertel 1273: } elsif ($token->[0] eq 'PI') {
1274: $result.=$token->[2];
1275: } elsif ($token->[0] eq 'S') {
1.316 albertel 1276: if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
1277: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1278: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1279: $result.=$token->[4];
1280: } elsif ($token->[0] eq 'E') {
1.316 albertel 1281: if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270 albertel 1282: #skip sending back the last end tag
1.283 albertel 1283: if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270 albertel 1284: my $string=
1285: '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
1286: $$style{'/'.$token->[1]}.
1287: $token->[2].
1288: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
1289: &Apache::lonxml::newparser($pars,\$string);
1290: #&Apache::lonxml::debug("reParsing $string");
1291: next;
1292: }
1293: if ($depth > -1) {
1294: $result.=$token->[2];
1295: } else {
1296: $$pars[-1]->unget_token($token);
1297: }
1298: }
1299: }
1300: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1301: if (($depth >=0) && ($#$pars > 0) ) {
1302: pop(@$pars);
1303: pop(@Apache::lonxml::pwd);
1304: }
1305: }
1306: if ($top_empty && $depth >= 0) {
1307: #never found the end tag ran out of text, throw error send back blank
1308: &error('Never found end tag for <'.$tag.
1309: '> current string <pre>'.
1.314 albertel 1310: &HTML::Entities::encode($result,'<>&"').
1.270 albertel 1311: '</pre>');
1312: if ($gotfullstack) {
1313: my $newstring='</'.$tag.'>'.$result;
1314: &Apache::lonxml::newparser($pars,\$newstring);
1315: }
1316: $result='';
1317: }
1318: } else {
1319: while ($#$pars > -1) {
1320: while ($token = $$pars[-1]->get_token) {
1321: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1322: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1323: ($token->[0] eq 'D')) {
1.382 albertel 1324: if ($token->[2]) {
1325: $result.='<![CDATA['.$token->[1].']]>';
1326: } else {
1327: $result.=$token->[1];
1328: }
1.270 albertel 1329: } elsif ($token->[0] eq 'PI') {
1330: $result.=$token->[2];
1331: } elsif ($token->[0] eq 'S') {
1.316 albertel 1332: if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270 albertel 1333: $$pars[-1]->unget_token($token); last;
1334: } else {
1335: $result.=$token->[4];
1336: }
1.316 albertel 1337: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1338: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1339: } elsif ($token->[0] eq 'E') {
1340: $result.=$token->[2];
1341: }
1342: }
1343: if (($#$pars > 0) ) {
1344: pop(@$pars);
1345: pop(@Apache::lonxml::pwd);
1346: } else { last; }
1347: }
1348: }
1349: #&Apache::lonxml::debug("Exit:$result:");
1350: return $result
1.19 albertel 1351: }
1352:
1.23 albertel 1353: sub newparser {
1354: my ($parser,$contentref,$dir) = @_;
1.167 albertel 1355: push (@$parser,HTML::LCParser->new($contentref));
1.365 albertel 1356: $$parser[-1]->xml_mode(1);
1357: $$parser[-1]->marked_sections(1);
1.23 albertel 1358: if ( $dir eq '' ) {
1359: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1360: } else {
1361: push (@Apache::lonxml::pwd, $dir);
1362: }
1363: }
1.1 sakharuk 1364:
1.8 albertel 1365: sub parstring {
1.417 albertel 1366: my ($token) = @_;
1367: my (@vars,@values);
1368: foreach my $attr (@{$token->[3]}) {
1369: if ($attr!~/\W/) {
1370: my $val=$token->[2]->{$attr};
1371: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1372: $val =~ s/(\$[^\{a-zA-Z_])/\\$1/g;
1373: $val =~ s/(\$)$/\\$1/;
1374: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1375: push(@vars,"\$$attr");
1376: push(@values,"\"$val\"");
1377: }
1378: }
1379: my $var_init =
1380: (@vars) ? 'my ('.join(',',@vars).') = ('.join(',',@values).');'
1381: : '';
1382: return $var_init;
1.8 albertel 1383: }
1.22 albertel 1384:
1.384 albertel 1385: sub extlink {
1386: my ($res,$exact)=@_;
1387: if (!$exact) {
1388: $res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
1389: }
1390: push(@Apache::lonxml::extlinks,$res)
1391: }
1392:
1.34 www 1393: sub writeallows {
1.126 www 1394: unless ($#extlinks>=0) { return; }
1.377 albertel 1395: my $thisurl = &Apache::lonnet::clutter(shift);
1.372 albertel 1396: if ($env{'httpref.'.$thisurl}) {
1397: $thisurl=$env{'httpref.'.$thisurl};
1.111 www 1398: }
1.34 www 1399: my $thisdir=$thisurl;
1400: $thisdir=~s/\/[^\/]+$//;
1401: my %httpref=();
1.142 albertel 1402: foreach (@extlinks) {
1.34 www 1403: $httpref{'httpref.'.
1.444 albertel 1404: &Apache::lonnet::hreflocation($thisdir,&unescape($_))}=$thisurl;
1.142 albertel 1405: }
1.126 www 1406: @extlinks=();
1.474 raeburn 1407: &Apache::lonnet::appenv(\%httpref);
1.34 www 1408: }
1409:
1.281 albertel 1410: sub register_ssi {
1411: my ($url,%form)=@_;
1412: push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
1413: return '';
1414: }
1415:
1416: sub do_registered_ssi {
1417: foreach my $info (@Apache::lonxml::ssi_info) {
1418: my %form=%{ $info->{'form'}};
1419: my $url=$info->{'url'};
1420: &Apache::lonnet::ssi($url,%form);
1421: }
1422: }
1.448 albertel 1423:
1424: sub add_script_result {
1425: my ($display) = @_;
1426: push(@script_var_displays, $display);
1427: }
1428:
1.66 www 1429: #
1430: # Afterburner handles anchors, highlights and links
1431: #
1432: sub afterburn {
1433: my $result=shift;
1.154 albertel 1434: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1435: ['highlight','anchor','link']);
1.372 albertel 1436: if ($env{'form.highlight'}) {
1437: foreach (split(/\,/,$env{'form.highlight'})) {
1.66 www 1438: my $anchorname=$_;
1439: my $matchthis=$anchorname;
1440: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1441: $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142 albertel 1442: }
1.66 www 1443: }
1.372 albertel 1444: if ($env{'form.link'}) {
1445: foreach (split(/\,/,$env{'form.link'})) {
1.66 www 1446: my ($anchorname,$linkurl)=split(/\>/,$_);
1447: my $matchthis=$anchorname;
1448: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1449: $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142 albertel 1450: }
1.66 www 1451: }
1.372 albertel 1452: if ($env{'form.anchor'}) {
1453: my $anchorname=$env{'form.anchor'};
1.66 www 1454: my $matchthis=$anchorname;
1455: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1456: $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66 www 1457: $result.=(<<"ENDSCRIPT");
1.226 albertel 1458: <script type="text/javascript">
1.66 www 1459: document.location.hash='$anchorname';
1460: </script>
1461: ENDSCRIPT
1462: }
1463: return $result;
1464: }
1465:
1.79 www 1466: sub storefile {
1467: my ($file,$contents)=@_;
1.290 albertel 1468: &Apache::lonnet::correct_line_ends(\$contents);
1.79 www 1469: if (my $fh=Apache::File->new('>'.$file)) {
1470: print $fh $contents;
1471: $fh->close();
1.271 www 1472: return 1;
1.147 albertel 1473: } else {
1.482 bisitz 1474: &warning(&mt('Unable to save file [_1]','<tt>'.$file.'</tt>'));
1.271 www 1475: return 0;
1.79 www 1476: }
1477: }
1478:
1.151 albertel 1479: sub createnewhtml {
1.321 www 1480: my $title=&mt('Title of document goes here');
1481: my $body=&mt('Body of document goes here');
1482: my $filecontents=(<<SIMPLECONTENT);
1.78 www 1483: <html>
1484: <head>
1.321 www 1485: <title>$title</title>
1.78 www 1486: </head>
1487: <body bgcolor="#FFFFFF">
1.321 www 1488: $body
1.78 www 1489: </body>
1490: </html>
1491: SIMPLECONTENT
1.321 www 1492: return $filecontents;
1.151 albertel 1493: }
1494:
1.274 albertel 1495: sub createnewsty {
1496: my $filecontents=(<<SIMPLECONTENT);
1497: <definetag name="">
1498: <render>
1499: <web></web>
1500: <tex></tex>
1501: </render>
1502: </definetag>
1503: SIMPLECONTENT
1504: return $filecontents;
1505: }
1506:
1.493 ! raeburn 1507: sub createnewjs {
! 1508: my $filecontents=(<<SIMPLECONTENT);
! 1509: <script type="text/javascript" language="Javascript">
! 1510:
! 1511: </script>
! 1512: SIMPLECONTENT
! 1513: return $filecontents;
! 1514: }
! 1515:
1.472 www 1516: sub verify_html {
1517: my ($filecontents)=@_;
1518: if ($filecontents!~/(?:\<|\<\;)(?:html|xml)[^\<]*(?:\>|\>\;)/is) {
1519: return &mt('File does not have [_1] or [_2] starting tag','<html>','<xml>');
1520: }
1521: if ($filecontents!~/(?:\<|\<\;)\/(?:html|xml)(?:\>|\>\;)/is) {
1522: return &mt('File does not have [_1] or [_2] ending tag','<html>','<xml>');
1523: }
1524: if ($filecontents!~/(?:\<|\<\;)(?:body|frameset)[^\<]*(?:\>|\>\;)/is) {
1525: return &mt('File does not have [_1] or [_2] starting tag','<body>','<frameset>');
1526: }
1527: if ($filecontents!~/(?:\<|\<\;)\/(?:body|frameset)[^\<]*(?:\>|\>\;)/is) {
1528: return &mt('File does not have [_1] or [_2] ending tag','<body>','<frameset>');
1529: }
1530: return '';
1531: }
1.147 albertel 1532:
1.478 www 1533: sub renderingoptions {
1534: my %langchoices=('' => '');
1535: foreach (&Apache::loncommon::languageids()) {
1536: if (&Apache::loncommon::supportedlanguagecode($_)) {
1537: $langchoices{&Apache::loncommon::supportedlanguagecode($_)}
1538: = &Apache::loncommon::plainlanguagedescription($_);
1539: }
1540: }
1541: return
1542: '<span class="LC_nobreak">'.
1.479 bisitz 1543: &mt('Language:').' '.
1.478 www 1544: &Apache::loncommon::select_form($env{'form.languages'},'languages',
1545: %langchoices).'
1546: </span>
1547: <span class="LC_nobreak">'.
1.479 bisitz 1548: &mt('Math Rendering:').' '.
1.478 www 1549: &Apache::loncommon::select_form($env{'form.texengine'},'texengine',
1550: ('' => '',
1.483 bisitz 1551: 'tth' => 'tth (TeX to HTML)',
1.478 www 1552: 'jsMath' => 'jsMath',
1.479 bisitz 1553: 'mimetex' => 'mimetex (Convert to Images)')).'
1.478 www 1554: </span>';
1555: }
1556:
1.151 albertel 1557: sub inserteditinfo {
1.470 albertel 1558: my ($filecontents, $filetype, $filename)=@_;
1.314 albertel 1559: $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.274 albertel 1560: my $xml_help = '';
1.321 www 1561: my $initialize='';
1.452 albertel 1562: my $textarea_id = 'filecont';
1.492 raeburn 1563: my $dragmath_button;
1.452 albertel 1564: my ($add_to_onload, $add_to_onresize);
1.456 albertel 1565: $initialize=&Apache::lonhtmlcommon::spellheader();
1566: if ($filetype eq 'html'
1567: && (!&Apache::lonhtmlcommon::htmlareablocked() &&
1568: &Apache::lonhtmlcommon::htmlareabrowser())) {
1.463 albertel 1569: $textarea_id .= '___Frame';
1570: my $lang = &Apache::lonhtmlcommon::htmlarea_lang();
1.456 albertel 1571: $initialize.=(<<FULLPAGE);
1.321 www 1572: <script type="text/javascript">
1.463 albertel 1573: lonca
1.321 www 1574: function initDocument() {
1.463 albertel 1575: var oFCKeditor = new FCKeditor('filecont');
1576: oFCKeditor.Config['CustomConfigurationsPath'] = '/fckeditor/loncapaconfig.js' ;
1577: oFCKeditor.Config['FullPage'] = true
1578: oFCKeditor.Config['AutoDetectLanguage'] = false;
1579: oFCKeditor.Config['DefaultLanguage'] = "$lang";
1580: oFCKeditor.ReplaceTextarea();
1581: }
1582: function check_if_dirty(editor) {
1583: if (editor.IsDirty()) {
1584: unClean();
1585: }
1586: }
1587: function FCKeditor_OnComplete(editor) {
1588: editor.Events.AttachEvent("OnSelectionChange",check_if_dirty);
1589: resize_textarea('$textarea_id','LC_aftertextarea');
1.321 www 1590: }
1591: </script>
1592: FULLPAGE
1.456 albertel 1593: } else {
1594: $initialize.=(<<FULLPAGE);
1.347 albertel 1595: <script type="text/javascript">
1596: function initDocument() {
1.453 albertel 1597: resize_textarea('$textarea_id','LC_aftertextarea');
1.347 albertel 1598: }
1599: </script>
1600: FULLPAGE
1.492 raeburn 1601: if ($filetype eq 'html') {
1602: $initialize .= "\n".&Apache::lonhtmlcommon::dragmath_js('EditMathPopup');
1603: $dragmath_button = &Apache::lonhtmlcommon::dragmath_button('filecont',1);
1604: }
1.456 albertel 1605: }
1606:
1607: $add_to_onload = 'initDocument();';
1608: $add_to_onresize = "resize_textarea('$textarea_id','LC_aftertextarea');";
1609:
1610: if ($filetype eq 'html') {
1.321 www 1611: $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274 albertel 1612: }
1.452 albertel 1613:
1.254 albertel 1614: my $titledisplay=&display_title();
1.426 banghart 1615: my %lt=&Apache::lonlocal::texthash('st' => 'Save and Edit',
1616: 'vi' => 'Save and View',
1.427 banghart 1617: 'dv' => 'Discard Edits and View',
1.428 banghart 1618: 'un' => 'undo',
1.280 www 1619: 'ed' => 'Edit');
1.470 albertel 1620: my $spelllink .=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.451 albertel 1621: my $textarea_events = &Apache::edit::element_change_detection();
1622: my $form_events = &Apache::edit::form_change_detection();
1.491 raeburn 1623: my $htmlerror;
1624: if ($filetype eq 'html') {
1625: $htmlerror=&verify_html($filecontents);
1626: if ($htmlerror) {
1627: $htmlerror='<span class="LC_error">'.$htmlerror.'</span>';
1628: }
1.472 www 1629: }
1.78 www 1630: my $editfooter=(<<ENDFOOTER);
1.321 www 1631: $initialize
1.78 www 1632: <a name="editsection" />
1.451 albertel 1633: <form $form_events method="post" name="xmledit">
1.470 albertel 1634: <div class="LC_edit_problem_editxml_header">
1635: <table class="LC_edit_problem_header_title"><tr><td>
1636: $filename
1637: </td><td align="right">
1638: $xml_help
1639: </td></tr>
1640: </table>
1641: <div class="LC_edit_problem_discards">
1642: <input type="submit" name="discardview" accesskey="d" value="$lt{'dv'}" />
1643: <input type="submit" name="Undo" accesskey="u" value="$lt{'un'}" />
1.492 raeburn 1644: $dragmath_button $spelllink $htmlerror
1.470 albertel 1645: </div>
1646: <div class="LC_edit_problem_saves">
1647: <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
1648: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1649: </div>
1650: </div>
1651: <textarea $textarea_events style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1652: <div id="LC_aftertextarea">
1653: <br />
1654: $titledisplay
1655: </div>
1.78 www 1656: </form>
1.321 www 1657: </body>
1.78 www 1658: ENDFOOTER
1.452 albertel 1659: return ($editfooter,$add_to_onload,$add_to_onresize);;
1.78 www 1660: }
1661:
1.152 albertel 1662: sub get_target {
1.372 albertel 1663: my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1664: if ( $env{'request.state'} eq 'published') {
1665: if ( defined($env{'form.grade_target'})
1.152 albertel 1666: && ($viewgrades == 'F' )) {
1.372 albertel 1667: return ($env{'form.grade_target'});
1668: } elsif (defined($env{'form.grade_target'})) {
1669: if (($env{'form.grade_target'} eq 'web') ||
1670: ($env{'form.grade_target'} eq 'tex') ) {
1671: return $env{'form.grade_target'}
1.153 albertel 1672: } else {
1673: return 'web';
1674: }
1.152 albertel 1675: } else {
1676: return 'web';
1677: }
1.372 albertel 1678: } elsif ($env{'request.state'} eq 'construct') {
1679: if ( defined($env{'form.grade_target'})) {
1680: return ($env{'form.grade_target'});
1.152 albertel 1681: } else {
1682: return 'web';
1683: }
1684: } else {
1685: return 'web';
1686: }
1687: }
1688:
1.24 sakharuk 1689: sub handler {
1.255 sakharuk 1690: my $request=shift;
1.493 ! raeburn 1691:
1.255 sakharuk 1692: my $target=&get_target();
1.372 albertel 1693: $Apache::lonxml::debug=$env{'user.debug'};
1.255 sakharuk 1694:
1.364 albertel 1695: &Apache::loncommon::content_type($request,'text/html');
1.255 sakharuk 1696: &Apache::loncommon::no_cache($request);
1.372 albertel 1697: if ($env{'request.state'} eq 'published') {
1.363 albertel 1698: $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
1699: 'lastrevisiondate'));
1700: }
1.255 sakharuk 1701: $request->send_http_header;
1702:
1703: return OK if $request->header_only;
1.68 www 1704:
1705:
1.255 sakharuk 1706: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274 albertel 1707: my $filetype;
1.493 ! raeburn 1708: if ($file =~ /\.(sty|css|js|txt)$/) {
! 1709: $filetype=$1;
1.274 albertel 1710: } else {
1711: $filetype='html';
1712: }
1.493 ! raeburn 1713:
1.78 www 1714: #
1715: # Edit action? Save file.
1716: #
1.428 banghart 1717: if (!($env{'request.state'} eq 'published')) {
1718: if ($env{'form.savethisfile'} || $env{'form.viewmode'} || $env{'form.Undo'}) {
1.429 albertel 1719: my $html_file=&Apache::lonnet::getfile($file);
1720: my $error = &Apache::lonhomework::handle_save_or_undo($request, \$html_file, \$env{'form.filecont'});
1.472 www 1721: if ($env{'form.savethisfile'}) {
1722: $env{'form.editmode'}='Edit'; #force edit mode
1723: }
1.255 sakharuk 1724: }
1725: }
1726: my %mystyle;
1727: my $result = '';
1728: my $filecontents=&Apache::lonnet::getfile($file);
1729: if ($filecontents eq -1) {
1.402 albertel 1730: my $start_page=&Apache::loncommon::start_page('File Error');
1.412 albertel 1731: my $end_page=&Apache::loncommon::end_page();
1.284 www 1732: my $fnf=&mt('File not found');
1.255 sakharuk 1733: $result=(<<ENDNOTFOUND);
1.402 albertel 1734: $start_page
1.284 www 1735: <b>$fnf: $file</b>
1.402 albertel 1736: $end_page
1.78 www 1737: ENDNOTFOUND
1.343 albertel 1738: $filecontents='';
1.372 albertel 1739: if ($env{'request.state'} ne 'published') {
1.274 albertel 1740: if ($filetype eq 'sty') {
1741: $filecontents=&createnewsty();
1.493 ! raeburn 1742: } elsif ($filetype eq 'js') {
! 1743: $filecontents=&createnewjs();
! 1744: } elsif (($filetype ne 'css') && ($filetype ne 'txt')) {
1.274 albertel 1745: $filecontents=&createnewhtml();
1746: }
1.372 albertel 1747: $env{'form.editmode'}='Edit'; #force edit mode
1.255 sakharuk 1748: }
1749: } else {
1.372 albertel 1750: unless ($env{'request.state'} eq 'published') {
1.343 albertel 1751: if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1.381 www 1752: &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.'));
1.343 albertel 1753: }
1.264 www 1754: #
1755: # we are in construction space, see if edit mode forced
1.385 albertel 1756: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1757: ['editmode']);
1.255 sakharuk 1758: }
1.427 banghart 1759: if (!$env{'form.editmode'} || $env{'form.viewmode'} || $env{'form.discardview'}) {
1.493 ! raeburn 1760: if ($filetype eq 'html' || $filetype eq 'sty') {
! 1761: &Apache::structuretags::reset_problem_globals();
! 1762: $result = &Apache::lonxml::xmlparse($request,$target,
! 1763: $filecontents,'',%mystyle);
1.455 albertel 1764: # .html files may contain <problem> or <Task> need to clean
1765: # up if it did
1.493 ! raeburn 1766: &Apache::structuretags::reset_problem_globals();
! 1767: &Apache::lonhomework::finished_parsing();
! 1768: } else {
! 1769: $result = $filecontents;
! 1770: }
1.385 albertel 1771: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1772: ['rawmode']);
1.395 albertel 1773: if ($env{'form.rawmode'}) { $result = $filecontents; }
1.493 ! raeburn 1774: if ($filetype ne 'html') {
! 1775: my $nochgview = 1;
1.457 albertel 1776: my $controls =
1.493 ! raeburn 1777: ($env{'request.state'} eq 'construct') ? &Apache::londefdef::edit_controls($nochgview)
1.457 albertel 1778: : '';
1.493 ! raeburn 1779: if ($filetype ne 'sty') {
! 1780: $result =~ s/</</g;
! 1781: $result =~ s/>/>/g;
! 1782: $result = '<table class="LC_sty_begin">'.
! 1783: '<tr><td><b><pre>'.$result.
! 1784: '</pre></b></td></tr></table>';
! 1785: }
! 1786: if ($env{'environment.remote'} eq 'off') {
! 1787: my %options = ('bgcolor' => '#FFFFFF');
! 1788: $result =
! 1789: &Apache::loncommon::start_page(undef,undef,\%options).
! 1790: $controls.
! 1791: $result.
! 1792: &Apache::loncommon::end_page();
! 1793: } else {
! 1794: $result = $controls.$result;
! 1795: }
! 1796: }
1.255 sakharuk 1797: }
1.147 albertel 1798: }
1.456 albertel 1799:
1.78 www 1800: #
1801: # Edit action? Insert editing commands
1802: #
1.372 albertel 1803: unless ($env{'request.state'} eq 'published') {
1.427 banghart 1804: if ($env{'form.editmode'} && (!($env{'form.viewmode'})) && (!($env{'form.discardview'})))
1.450 albertel 1805: {
1.470 albertel 1806: my $displayfile=$request->uri;
1807: $displayfile=~s/^\/[^\/]*//;
1808:
1.452 albertel 1809: my ($edit_info, $add_to_onload, $add_to_onresize)=
1.470 albertel 1810: &inserteditinfo($filecontents,$filetype,$displayfile);
1.450 albertel 1811:
1812: my %options =
1813: ('add_entries' =>
1.452 albertel 1814: {'onresize' => $add_to_onresize,
1815: 'onload' => $add_to_onload, });
1816:
1.402 albertel 1817: if ($env{'environment.remote'} ne 'off') {
1818: $options{'bgcolor'} = '#FFFFFF';
1.450 albertel 1819: $options{'only_body'} = 1;
1.349 albertel 1820: }
1.452 albertel 1821: my $js =
1822: &Apache::edit::js_change_detection().
1823: &Apache::loncommon::resize_textarea_js();
1.451 albertel 1824: my $start_page = &Apache::loncommon::start_page(undef,$js,
1.402 albertel 1825: \%options);
1826: $result=$start_page.
1.470 albertel 1827: &Apache::lonxml::message_location().
1.450 albertel 1828: $edit_info.
1829: &Apache::loncommon::end_page();
1.493 ! raeburn 1830: }
1.147 albertel 1831: }
1.402 albertel 1832: if ($filetype eq 'html') { &writeallows($request->uri); }
1.255 sakharuk 1833:
1.309 albertel 1834: &Apache::lonxml::add_messages(\$result);
1.255 sakharuk 1835: $request->print($result);
1836:
1837: return OK;
1.253 albertel 1838: }
1839:
1840: sub display_title {
1841: my $result;
1.372 albertel 1842: if ($env{'request.state'} eq 'construct') {
1.253 albertel 1843: my $title=&Apache::lonnet::gettitle();
1844: if (!defined($title) || $title eq '') {
1.372 albertel 1845: $title = $env{'request.filename'};
1.253 albertel 1846: $title = substr($title, rindex($title, '/') + 1);
1847: }
1.476 bisitz 1848: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA "
1849: .&mt('Construction Space')."';</script>";
1.253 albertel 1850: }
1851: return $result;
1.24 sakharuk 1852: }
1.147 albertel 1853:
1.22 albertel 1854: sub debug {
1.298 albertel 1855: if ($Apache::lonxml::debug eq "1") {
1856: $|=1;
1.300 albertel 1857: my $request=$Apache::lonxml::request;
1.388 albertel 1858: if (!$request) {
1859: eval { $request=Apache->request; };
1860: }
1861: if (!$request) {
1862: eval { $request=Apache2::RequestUtil->request; };
1863: }
1.314 albertel 1864: $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.346 albertel 1865: #&Apache::lonnet::logthis($_[0]);
1.298 albertel 1866: }
1.22 albertel 1867: }
1.49 albertel 1868:
1.348 albertel 1869: sub show_error_warn_msg {
1.372 albertel 1870: if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
1871: &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.351 albertel 1872: return 1;
1873: }
1.348 albertel 1874: return (($Apache::lonxml::debug eq 1) ||
1.372 albertel 1875: ($env{'request.state'} eq 'construct') ||
1.348 albertel 1876: ($Apache::lonhomework::browse eq 'F'
1877: &&
1.372 albertel 1878: $env{'form.show_errors'} eq 'on'));
1.348 albertel 1879: }
1880:
1.22 albertel 1881: sub error {
1.454 albertel 1882: my @errors = @_;
1883:
1.336 albertel 1884: $errorcount++;
1.454 albertel 1885:
1.490 www 1886: $Apache::lonxml::internal_error=1;
1887:
1.454 albertel 1888: if (defined($Apache::inputtags::part)) {
1889: if ( @Apache::inputtags::response ) {
1890: push(@errors,
1891: &mt("This error occurred while processing response [_1] in part [_2]",
1892: $Apache::inputtags::response[-1],
1893: $Apache::inputtags::part));
1894: } else {
1895: push(@errors,
1896: &mt("This error occurred while processing part [_1]",
1897: $Apache::inputtags::part));
1898: }
1899: }
1900:
1.348 albertel 1901: if ( &show_error_warn_msg() ) {
1.336 albertel 1902: # If printing in construction space, put the error inside <pre></pre>
1903: push(@Apache::lonxml::error_messages,
1.484 bisitz 1904: $Apache::lonxml::warnings_error_header
1905: .'<div class="LC_error">'
1906: .'<b>'.&mt('ERROR:').' </b>'.join("<br />\n",@errors)
1907: ."</div>\n");
1.336 albertel 1908: $Apache::lonxml::warnings_error_header='';
1909: } else {
1910: my $errormsg;
1911: my ($symb)=&Apache::lonnet::symbread();
1912: if ( !$symb ) {
1913: #public or browsers
1.486 bisitz 1914: $errormsg=&mt("An error occurred while processing this resource. The author has been notified.");
1.403 albertel 1915: }
1.413 albertel 1916: my $host=$Apache::lonnet::perlvar{'lonHostID'};
1.476 bisitz 1917: push(@errors,
1918: &mt("The error occurred on host [_1]",
1919: "<tt>$host</tt>"));
1.454 albertel 1920:
1921: my $msg = join('<br />', @errors);
1922:
1.336 albertel 1923: #notify author
1.403 albertel 1924: &Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1.336 albertel 1925: #notify course
1.372 albertel 1926: if ( $symb && $env{'request.course.id'} ) {
1.380 www 1927: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1928: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.440 albertel 1929: my (undef,%users)=&Apache::lonmsg::decide_receiver(undef,0,1,1,1);
1.372 albertel 1930: my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1.435 raeburn 1931: my $baseurl = &Apache::lonnet::clutter($declutter);
1.336 albertel 1932: my @userlist;
1933: foreach (keys %users) {
1934: my ($user,$domain) = split(/:/, $_);
1935: push(@userlist,"$user\@$domain");
1.380 www 1936: my $key=$declutter.'_'.$user.'_'.$domain;
1937: my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
1938: [$key],
1939: $cdom,$cnum);
1940: my $now=time;
1941: if ($now-$lastnotified{$key}>86400) {
1.434 raeburn 1942: my $title = &Apache::lonnet::gettitle($symb);
1943: my $sentmessage;
1.380 www 1944: &Apache::lonmsg::user_normal_msg($user,$domain,
1.435 raeburn 1945: "Error [$title]",$msg,'',$baseurl,'','',
1.434 raeburn 1946: \$sentmessage,$symb,$title,1);
1.380 www 1947: &Apache::lonnet::put('nohist_xmlerrornotifications',
1948: {$key => $now},
1949: $cdom,$cnum);
1950: }
1.336 albertel 1951: }
1.372 albertel 1952: if ($env{'request.role.adv'}) {
1.486 bisitz 1953: $errormsg=&mt("An error occurred while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1.336 albertel 1954: } else {
1.486 bisitz 1955: $errormsg=&mt("An error occurred while processing this resource. The instructor has been notified.");
1.336 albertel 1956: }
1957: }
1958: push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52 albertel 1959: }
1.22 albertel 1960: }
1.49 albertel 1961:
1.22 albertel 1962: sub warning {
1.295 albertel 1963: $warningcount++;
1.261 albertel 1964:
1.372 albertel 1965: if ($env{'form.grade_target'} ne 'tex') {
1.348 albertel 1966: if ( &show_error_warn_msg() ) {
1.309 albertel 1967: push(@Apache::lonxml::warning_messages,
1.484 bisitz 1968: $Apache::lonxml::warnings_error_header
1969: .'<div class="LC_warning">'
1970: .&mt('[_1]W[_2]ARNING','<b>','</b>')."<b>:</b> ".join('<br />',@_)
1971: ."</div>\n"
1.482 bisitz 1972: );
1.295 albertel 1973: $Apache::lonxml::warnings_error_header='';
1974: }
1975: }
1.309 albertel 1976: }
1977:
1978: sub info {
1.372 albertel 1979: if ($env{'form.grade_target'} ne 'tex'
1980: && $env{'request.state'} eq 'construct') {
1.309 albertel 1981: push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
1982: }
1983: }
1984:
1985: sub message_location {
1986: return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
1987: }
1988:
1989: sub add_messages {
1990: my ($msg)=@_;
1991: my $result=join(' ',
1992: @Apache::lonxml::info_messages,
1993: @Apache::lonxml::error_messages,
1994: @Apache::lonxml::warning_messages);
1995: undef(@Apache::lonxml::info_messages);
1996: undef(@Apache::lonxml::error_messages);
1997: undef(@Apache::lonxml::warning_messages);
1998: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
1999: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83 albertel 2000: }
2001:
2002: sub get_param {
1.213 albertel 2003: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
2004: if ( ! $context ) { $context = -1; }
2005: my $args ='';
2006: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 2007: if ( ! $Apache::lonxml::usestyle ) {
2008: $args=$Apache::lonxml::style_values.$args;
2009: }
1.213 albertel 2010: if ( ! $args ) { return undef; }
2011: if ( $case_insensitive ) {
1.417 albertel 2012: if ($args =~ s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei) {
1.213 albertel 2013: return &Apache::run::run("{$args;".'return $'.$param.'}',
2014: $safeeval); #'
2015: } else {
2016: return undef;
2017: }
2018: } else {
1.417 albertel 2019: if ( $args =~ /my .*\$\Q$param\E[,\)]/ ) {
1.213 albertel 2020: return &Apache::run::run("{$args;".'return $'.$param.'}',
2021: $safeeval); #'
2022: } else {
2023: return undef;
2024: }
2025: }
1.22 albertel 2026: }
2027:
1.132 albertel 2028: sub get_param_var {
1.213 albertel 2029: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132 albertel 2030: if ( ! $context ) { $context = -1; }
2031: my $args ='';
2032: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 2033: if ( ! $Apache::lonxml::usestyle ) {
2034: $args=$Apache::lonxml::style_values.$args;
2035: }
1.230 albertel 2036: &Apache::lonxml::debug("Args are $args param is $param");
1.213 albertel 2037: if ($case_insensitive) {
1.419 albertel 2038: if (! ($args=~s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei)) {
1.213 albertel 2039: return undef;
2040: }
1.419 albertel 2041: } elsif ( $args !~ /my .*\$\Q$param\E[,\)]/ ) { return undef; }
1.132 albertel 2042: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230 albertel 2043: &Apache::lonxml::debug("first run is $value");
1.341 albertel 2044: if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230 albertel 2045: &Apache::lonxml::debug("doing second");
2046: my @result=&Apache::run::run("return $value",$safeeval,1);
2047: if (!defined($result[0])) {
2048: return $value
2049: } else {
2050: if (wantarray) { return @result; } else { return $result[0]; }
2051: }
1.132 albertel 2052: } else {
2053: return $value;
2054: }
2055: }
2056:
1.438 albertel 2057: sub register_insert_xml {
2058: my $parser = HTML::LCParser->new($Apache::lonnet::perlvar{'lonTabDir'}
2059: .'/insertlist.xml');
2060: my ($tagnum,$in_help)=(0,0);
1.442 albertel 2061: my @alltags;
1.438 albertel 2062: my $tag;
2063: while (my $token = $parser->get_token()) {
2064: if ($token->[0] eq 'S') {
2065: my $key;
2066: if ($token->[1] eq 'tag') {
2067: $tag = $token->[2]{'name'};
2068: $insertlist{"$tagnum.tag"} = $tag;
2069: $insertlist{"$tag.num"} = $tagnum;
1.442 albertel 2070: push(@alltags,$tag);
1.438 albertel 2071: } elsif ($in_help && $token->[1] eq 'file') {
2072: $key = $tag.'.helpfile';
2073: } elsif ($in_help && $token->[1] eq 'description') {
2074: $key = $tag.'.helpdesc';
2075: } elsif ($token->[1] eq 'description' ||
2076: $token->[1] eq 'color' ||
2077: $token->[1] eq 'show' ) {
2078: $key = $tag.'.'.$token->[1];
2079: } elsif ($token->[1] eq 'insert_sub') {
2080: $key = $tag.'.function';
2081: } elsif ($token->[1] eq 'help') {
2082: $in_help=1;
2083: } elsif ($token->[1] eq 'allow') {
1.442 albertel 2084: $key = $tag.'.allow';
1.438 albertel 2085: }
2086: if (defined($key)) {
2087: $insertlist{$key} = $parser->get_text();
2088: $insertlist{$key} =~ s/(^\s*|\s*$ )//gx;
2089: }
2090: } elsif ($token->[0] eq 'E') {
2091: if ($token->[1] eq 'tag') {
2092: undef($tag);
2093: $tagnum++;
2094: } elsif ($token->[1] eq 'help') {
2095: undef($in_help);
2096: }
2097: }
2098: }
1.442 albertel 2099:
2100: # parse the allows and ignore tags set to <show>no</show>
2101: foreach my $tag (@alltags) {
1.445 albertel 2102: next if (!exists($insertlist{"$tag.allow"}));
1.442 albertel 2103: my $allow = $insertlist{"$tag.allow"};
2104: foreach my $element (split(',',$allow)) {
2105: $element =~ s/(^\s*|\s*$ )//gx;
1.446 albertel 2106: if (!exists($insertlist{"$element.show"})
2107: || $insertlist{"$element.show"} ne 'no') {
1.442 albertel 2108: push(@{ $insertlist{$tag.'.which'} },$element);
2109: }
2110: }
2111: }
1.438 albertel 2112: }
2113:
2114: sub register_insert {
2115: return ®ister_insert_xml(@_);
2116: # &dump_insertlist('2');
2117: }
2118:
2119: sub dump_insertlist {
2120: my ($ext) = @_;
2121: open(XML,">/tmp/insertlist.xml.$ext");
2122: print XML ("<insertlist>");
2123: my $i=0;
2124:
2125: while (exists($insertlist{"$i.tag"})) {
2126: my $tag = $insertlist{"$i.tag"};
2127: print XML ("
2128: \t<tag name=\"$tag\">");
2129: if (defined($insertlist{"$tag.description"})) {
2130: print XML ("
2131: \t\t<description>".$insertlist{"$tag.description"}."</description>");
2132: }
2133: if (defined($insertlist{"$tag.color"})) {
2134: print XML ("
2135: \t\t<color>".$insertlist{"$tag.color"}."</color>");
2136: }
2137: if (defined($insertlist{"$tag.function"})) {
2138: print XML ("
2139: \t\t<insert_sub>".$insertlist{"$tag.function"}."</insert_sub>");
2140: }
2141: if (defined($insertlist{"$tag.show"})
2142: && $insertlist{"$tag.show"} ne 'yes') {
2143: print XML ("
2144: \t\t<show>".$insertlist{"$tag.show"}."</show>");
2145: }
2146: if (defined($insertlist{"$tag.helpfile"})) {
2147: print XML ("
2148: \t\t<help>
2149: \t\t\t<file>".$insertlist{"$tag.helpfile"}."</file>");
2150: if ($insertlist{"$tag.helpdesc"} ne '') {
2151: print XML ("
2152: \t\t\t<description>".$insertlist{"$tag.helpdesc"}."</description>");
2153: }
2154: print XML ("
2155: \t\t</help>");
2156: }
2157: if (defined($insertlist{"$tag.which"})) {
2158: print XML ("
2159: \t\t<allow>".join(',',sort(@{ $insertlist{"$tag.which"} }))."</allow>");
2160: }
2161: print XML ("
2162: \t</tag>");
2163: $i++;
2164: }
2165: print XML ("\n</insertlist>\n");
2166: close(XML);
2167: }
2168:
1.98 albertel 2169: sub description {
1.437 albertel 2170: my ($token)=@_;
2171: my $tag = &get_tag($token);
2172: return $insertlist{$tag.'.description'};
1.268 bowersj2 2173: }
2174:
2175: # Returns a list containing the help file, and the description
2176: sub helpinfo {
1.437 albertel 2177: my ($token)=@_;
2178: my $tag = &get_tag($token);
2179: return ($insertlist{$tag.'.helpfile'}, $insertlist{$tag.'.helpdesc'});
2180: }
2181:
2182: sub get_tag {
2183: my ($token)=@_;
2184: my $tagnum;
2185: my $tag=$token->[1];
2186: foreach my $namespace (reverse(@Apache::lonxml::namespace)) {
2187: my $testtag = $namespace.'::'.$tag;
2188: $tagnum = $insertlist{"$testtag.num"};
2189: last if (defined($tagnum));
2190: }
2191: if (!defined($tagnum)) {
2192: $tagnum = $Apache::lonxml::insertlist{"$tag.num"};
2193: }
2194: return $insertlist{"$tagnum.tag"};
1.98 albertel 2195: }
1.123 albertel 2196:
1.485 onken 2197: ############################################################
2198: # PDF-FORM-METHODS
2199:
2200: =pod
2201:
2202: =item &print_pdf_radiobutton(fieldname, value, text)
2203:
2204: Returns a latexline to generate a PDF-Form-Radiobutton with Text.
2205:
2206: $fieldname: PDF internalname of the radiobutton
2207: $value: Value of radiobutton (read when dumping the PDF data)
2208: $text: Text on the rightside of the radiobutton
2209:
2210: =cut
2211: sub print_pdf_radiobutton {
2212: my $result = '';
2213: my ($fieldName, $value, $text) = @_;
2214: $result .= '\begin{tabularx}{\textwidth}{p{0cm}X}'."\n";
2215: $result .= '\radioButton[\symbolchoice{circle}]{'.
2216: $fieldName.'}{10bp}{10bp}{'.$value.'}&'.$text."\n";
2217: $result .= '\end{tabularx}' . "\n";
2218: $result .= '\hspace{2mm}' . "\n";
2219: return $result;
2220: }
2221:
2222:
2223: =pod
2224:
2225: =item &print_pdf_start_combobox(fieldname)
2226:
2227: Starts a latexline to generate a PDF-Form-Combobox with text.
2228:
2229: $fieldname: PDF internal name of the Combobox
2230:
2231: =cut
2232: sub print_pdf_start_combobox {
2233: my $result;
2234: my ($fieldName) = @_;
2235: $result .= '\begin{tabularx}{\textwidth}{p{2.5cm}X}'."\n";
2236: $result .= '\comboBox[]{'.$fieldName.'}{2.3cm}{14bp}{'; #
2237:
2238: return $result;
2239: }
2240:
2241:
2242: =pod
2243:
2244: =item &print_pdf_add_combobox_option(options)
2245:
2246: Generates a latexline to add Options to a PDF-Form-ComboBox.
2247:
2248: $option: PDF internal name of the Combobox-Option
2249:
2250: =cut
2251: sub print_pdf_add_combobox_option {
2252:
2253: my $result;
2254: my ($option) = @_;
2255:
2256: $result .= '('.$option.')';
2257:
2258: return $result;
2259: }
2260:
2261:
2262: =pod
2263:
2264: =item &print_pdf_end_combobox(text) {
2265:
2266: Returns latexcode to end a PDF-Form-Combobox with text.
2267:
2268: =cut
2269: sub print_pdf_end_combobox {
2270: my $result;
2271: my ($text) = @_;
2272:
2273: $result .= '}&'.$text."\\\\\n";
2274: $result .= '\end{tabularx}' . "\n";
2275: $result .= '\hspace{2mm}' . "\n";
2276: return $result;
2277: }
2278:
2279:
2280: =pod
2281:
2282: =item &print_pdf_hiddenField(fieldname, user, domain)
2283:
2284: Returns a latexline to generate a PDF-Form-hiddenField with userdata.
2285:
2286: $fieldname label for hiddentextfield
2287: $user: name of user
2288: $domain: domain of user
2289:
2290: =cut
2291: sub print_pdf_hiddenfield {
2292: my $result;
2293: my ($fieldname, $user, $domain) = @_;
2294:
2295: $result .= '\textField [\F{\FHidden}\F{-\FPrint}\V{'.$domain.'&'.$user.'}]{'.$fieldname.'}{0in}{0in}'."\n";
2296:
2297: return $result;
2298: }
2299:
1.1 sakharuk 2300: 1;
2301: __END__
1.68 www 2302:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>