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