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