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