1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.172.2.2.2.1 2024/02/28 15:52:39 raeburn Exp $
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:
29: package Apache::scripttag;
30:
31: use strict;
32: use Apache::lonnet;
33: use Apache::loncommon;
34: use Apache::lonlocal;
35: use Apache::lonxml();
36: use Apache::londefdef();
37: use Apache::style();
38:
39: #Globals
40: # this used to pass around the standard callsub arguments to a tag func
41: # so xmlparse can reenter the inner_xmlparse loop.
42:
43: @Apache::scripttag::parser_env = ();
44: BEGIN {
45: &Apache::lonxml::register('Apache::scripttag',
46: ('script','scriptlib','parserlib','import',
47: 'window','windowlink','togglebox','display','storetc','physnet',
48: 'standalone','comment','num','parse','algebra',
49: 'LONCAPA_INTERNAL_TURN_STYLE_ON',
50: 'LONCAPA_INTERNAL_TURN_STYLE_OFF'));
51: }
52:
53: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
54: $Apache::lonxml::usestyle=1;
55: $Apache::lonxml::style_values='';
56: return ('','no');
57: }
58:
59: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
60: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
61: my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
62: if (defined($end)) {
63: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
64: }
65: return ('','no');
66: }
67:
68: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
69: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
70: $Apache::lonxml::usestyle=0;
71: my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
72: if (!$end) {
73: $Apache::lonxml::style_values=$$parstack[-1];
74: $Apache::lonxml::style_end_values=$$parstack[-1];
75: } else {
76: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
77: $Apache::lonxml::style_end_values='';
78: }
79: return ('','no');
80: }
81:
82: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
83: return ('','no');
84: }
85:
86: sub start_script {
87: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
88: @Apache::scripttag::parser_env = @_;
89: my $result='';
90: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
91: &Apache::lonxml::debug("found type of $type");
92: if ($type eq "loncapa/perl") {
93: if ( $target eq "modified" ) {
94: $result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
95: } elsif ( $target eq 'web' || $target eq 'tex' ||
96: $target eq 'grade' || $target eq 'webgrade' ||
97: $target eq 'answer' || $target eq 'analyze' ) {
98: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
99: if (!$Apache::lonxml::default_homework_loaded) {
100: &Apache::lonxml::default_homework_load($safeeval);
101: }
102: &Apache::run::run($bodytext,$safeeval);
103: if (($target eq 'answer') &&
104: ($env{'form.answer_output_mode'} ne 'tex') &&
105: ($Apache::lonhomework::viewgrades == 'F')) {
106: $Apache::lonxml::evaluate--;
107: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
108: $uname =~s/\W//g;
109: $udom =~s/\W//g;
110: my $function_name =
111: join('_','LONCAPA_scriptvars',$uname,$udom,
112: $env{'form.counter'},$Apache::lonxml::curdepth);
113: &Apache::lonxml::add_script_result(
114: &Apache::loncommon::modal_adhoc_window($function_name,500,500,
115: '<pre style="background-color:#ffffff;">'.
116: &Apache::run::dump($target,$safeeval).'</pre>',
117: &mt('Script Vars'))."<br />");
118: }
119: } elsif ($target eq "edit" ) {
120: #&Apache::run::run($bodytext,$safeeval);
121: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
122: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
123: $result=&Apache::edit::tag_start($target,$token,'Script');
124:
125: my $depth = $Apache::lonxml::curdepth;
126: $result.='<span id="LC_edit_problem_codemirror">';
127: my $nocodemirror = &Apache::loncommon::nocodemirror();
128: unless ($nocodemirror) {
129: # only show button if codemirror activated
130: $result.='<input type="button" id="fitsize'.$depth.'" value="'.&mt("Dynamic size").
131: '" onclick="autosize(\''.$depth.'\')" />';
132: }
133: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4).'</span>';
134:
135: unless ($nocodemirror) {
136: $result.='<script type="text/javascript">
137: var cm'.$depth.' = CodeMirror.fromTextArea(document.getElementById("homework_edit_'.$depth.'"),
138: {
139: mode: "perl",
140: lineWrapping: true,
141: lineNumbers: true,
142: tabSize: 4,
143: indentUnit: 4,
144: autoCloseBrackets: true,
145: styleActiveLine: true,
146:
147: extraKeys: {
148: "Tab": "indentMore",
149: "Shift-Tab": "indentLess"
150: }
151: });
152: if(sessionStorage.getItem("autosized_'.$depth.'") != null) {
153: document.getElementById("fitsize'.$depth.'").value = "'.&mt("Fixed size").'";
154: cm'.$depth.'.setSize("","auto");
155: }
156: </script>';
157: }
158:
159: } elsif ($target eq 'meta') {
160: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
161: }
162: } else {
163: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
164: if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
165: $result = $token->[4];
166: $result.=$bodytext;
167: my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
168: my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
169: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
170: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
171: if ($src ne '') {
172: if ($src !~ m{^(/|https?://)}) {
173: my $cleanhref = &Apache::londefdef::clean_docs_httpref($src,$url,$cdom,$cnum);
174: if ($cleanhref) {
175: &Apache::lonxml::extlink($cleanhref);
176: }
177: }
178: } elsif (($type eq 'text/javascript') && ($bodytext ne '')) {
179: if ($url =~ m{^\Q/uploaded/$cdom/$cnum/\E(docs|supplemental)/}) {
180: if ($bodytext =~ m{\.addMediaSrc\((["'])((?!\1).)+\1\);}) {
181: my $quote = $1;
182: if ($bodytext =~ m{\Q.addMediaSrc($quote\E([^$quote]+)\Q$quote)\E}) {
183: my $fname = $1;
184: my $cleanhref =
185: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
186: if ($cleanhref) {
187: &Apache::lonxml::extlink($cleanhref);
188: }
189: }
190: }
191: if ($bodytext =~ m{\.set\w+(Src|Swf)\(["']}i) {
192: my @srcs = split(/\.set/,$bodytext);
193: if (scalar(@srcs) > 1) {
194: foreach my $item (@srcs) {
195: if ($item =~ m{^(FlashPlayerSwf|MediaSrc|XMPSrc|ConfigurationSrc|PosterImageSrc)\((['"])(?:(?!\2).)+\2\)}is) {
196: my $srctype = $1;
197: my $quote = $2;
198: my ($fname) = ($item =~ m{^\Q$srctype($quote\E([^$quote]+)\Q$quote)\E});
199: my $cleanhref =
200: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
201: if ($cleanhref) {
202: &Apache::lonxml::extlink($cleanhref);
203: if ($srctype eq 'ConfigurationSrc') {
204: if ($cleanhref =~ m{^(.+/)configuration_express\.xml$}) {
205: #
206: # Camtasia 8.1: express_show/spritesheet.png needed, and included in zip archive.
207: # Not referenced directly in <main>.html or <main>_player.html files,
208: # so call lonxml::extlink() here to include httpref for the uploaded file.
209: # (where <main> is name user gave to file/archive).
210: #
211:
212: my $spritesheet = $1.'express_show/spritesheet.png';
213: if (&Apache::lonnet::repcopy_userfile($spritesheet) eq 'ok') {
214: &Apache::lonxml::extlink($spritesheet);
215: }
216: }
217: #
218: # Camtasia 8.4: express_show/spritesheet.min.css needed, and included in zip archive.
219: # Not referenced directly in <main>.html or <main>_player.html files,
220: # so call lonxml::extlink() here to include httpref for the uploaded file.
221: # (where <main> is name user gave to file/archive).
222: #
223: my $spritesheet_css = $1.'express_show/spritesheet.min.css';
224: if (&Apache::lonnet::repcopy_userfile($spritesheet_css) eq 'ok') {
225: &Apache::lonxml::extlink($spritesheet_css);
226: }
227: } elsif ($srctype eq 'PosterImageSrc') {
228: if ($fname =~ m{^(.+)_First_Frame\.png$}) {
229: my $prefix = $1;
230: my ($path) = ($cleanhref =~ m{^(.+/)\Q$fname\E});
231: #
232: # Camtasia 8.1: <main>_Thumbnails.png needed, and included in zip archive.
233: # Not referenced directly in <main>.html or <main>_player.html files,
234: # so call lonxml::extlink() here to include httpref for the uploaded file
235: # (where <main> is name user gave to file/archive).
236: #
237: my $thumbnail = $path.$prefix.'_Thumbnails.png';
238: if (&Apache::lonnet::repcopy_userfile($thumbnail) eq 'ok') {
239: &Apache::lonxml::extlink($thumbnail);
240: }
241: }
242: }
243: }
244: }
245: }
246: }
247: }
248: if ($bodytext =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
249: my $scriptslist = $2;
250: my @srcs = split(/\s*,\s*/,$scriptslist);
251: foreach my $src (@srcs) {
252: if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
253: my $quote = $1;
254: my ($fname) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
255: my $cleanhref =
256: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
257: if ($cleanhref) {
258: &Apache::lonxml::extlink($cleanhref);
259: }
260: }
261: }
262: }
263: if ($bodytext =~ m{loadScript\(\s*(['"])((?:(?!\1).)+\.js)\1,\s*function}is) {
264: my $fname = $2;
265: if ($fname) {
266: my $cleanhref =
267: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
268: if ($cleanhref) {
269: &Apache::lonxml::extlink($cleanhref);
270: }
271: }
272: }
273: }
274: }
275: }
276: }
277: return $result;
278: }
279:
280: sub end_script {
281: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
282: if ( $target eq "meta" ) { return ''; }
283: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
284: my $result='';
285: #other script blocks need to survive
286: if ($type ne "loncapa/perl" && $target ne 'tex') {
287: return $token->[2];
288: } elsif ($target eq 'edit' ) {
289: return &Apache::edit::end_table();
290: } elsif ($target eq 'answer') {
291: $Apache::lonxml::evaluate++;
292: }
293: return '';
294: }
295:
296: sub start_display {
297: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
298: @Apache::scripttag::parser_env = @_;
299: my $result;
300:
301: if ( $target eq "modified" ) {
302: $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
303: } elsif ( $target eq 'web' || $target eq 'tex' ||
304: $target eq 'grade' || $target eq 'webgrade' ||
305: $target eq 'answer' || $target eq 'analyze') {
306: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
307: if (!$Apache::lonxml::default_homework_loaded) {
308: &Apache::lonxml::default_homework_load($safeeval);
309: }
310: $result=&Apache::run::run($bodytext,$safeeval);
311: if ($target eq 'grade' || $target eq 'answer' ||
312: $target eq 'analyze') {
313: # grade/answer/analyxe should produce no output but if we
314: # are redirecting, the redirecter should know what to do
315: # with the output
316: if (!$Apache::lonxml::redirection) { $result=''; }
317: }
318: $Apache::lonxml::post_evaluate=0;
319: } elsif ($target eq "edit" ) {
320: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
321: #$result =
322: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
323: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
324: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
325: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
326: } elsif ($target eq 'meta') {
327: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
328: }
329: return $result;
330: }
331:
332: sub end_display {
333: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
334: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
335: return '';
336: }
337:
338: sub start_scriptlib {
339: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
340: my $bodytext;
341: my $result ='';
342: my $error='';
343:
344: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
345: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
346: $target eq 'analyze' || $target eq 'webgrade') {
347: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
348: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
349: $$parstack[$#$parstack]);
350: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
351: $bodytext);
352: my $script=&Apache::lonnet::getfile($location);
353: if ($script == -1) {
354: if ($target eq 'edit') {
355: $error='</tr><tr><td>'.&mt('Errors').'</td><td colspan="2"><b>'.&mt(' Unable to find [_1]','<span class="LC_filename">'.$location.'</span>').'</b></td>'."\n";
356: } else {
357: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
358: return "";
359: }
360: }
361: &Apache::run::run($script,$safeeval);
362: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
363: }
364: if ($target eq "edit" ) {
365: $result=
366: &Apache::edit::tag_start($target,$token,'New Script Functions').
367: &Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
368: &Apache::edit::browse(undef,'textnode').
369: $error.'</td></tr>'.
370: &Apache::edit::end_table();
371: }
372: if ($target eq "modified" ) {
373: $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
374: }
375: return $result;
376: }
377:
378: sub end_scriptlib {
379: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
380: my @result;
381: if ($target eq "edit" ) { $result[1]='no'; }
382: return @result;
383: }
384:
385: sub start_parserlib {
386: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
387: my $bodytext;
388: my $result ="";
389: my $error='';
390: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
391: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
392: $target eq 'analyze' || $target eq 'webgrade') {
393: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
394: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
395: $$parstack[$#$parstack]);
396: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
397: $bodytext);
398: my $styletext=&Apache::lonnet::getfile($location);
399: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
400: if ($styletext == -1) {
401: if ($target eq 'edit') {
402: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
403: } else {
404: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
405: return "";
406: }
407: }
408: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
409: }
410: if ($target eq "edit" ) {
411: $result=
412: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
413: &Apache::edit::editline($token->[1],$bodytext,'',40).
414: $error.'</td></tr>'.
415: &Apache::edit::end_table();
416: }
417: if ($target eq "modified" ) {
418: $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
419: }
420: return $result;
421: }
422:
423: sub end_parserlib {
424: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
425: my @result;
426: if ($target eq "edit" ) { $result[1]='no'; }
427: return @result;
428: }
429:
430: sub start_window {
431: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
432: my $result = '';
433: if ($target eq 'web' || $target eq 'webgrade') {
434: &Apache::lonxml::startredirection;
435: } elsif ($target eq 'tex') {
436: my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
437: if ($printtext=~/\w/) {
438: # If printtext is given, do not output any intervening information
439: &Apache::lonxml::startredirection;
440: } else {
441: $result = '\unskip\footnote{';
442: }
443: } elsif ($target eq 'edit') {
444: $result.=&Apache::edit::tag_start($target,$token);
445: $result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
446: $result.=&Apache::edit::text_arg('Height:','height',$token,5);
447: $result.=&Apache::edit::text_arg('Width:','width',$token,5);
448: $result.=&Apache::edit::text_arg('Printed text (optional):','printtext',$token,20);
449: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
450: } elsif ($target eq 'modified') {
451: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
452: $safeeval,'linktext',
453: 'width','height');
454: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
455: }
456: return $result;
457: }
458:
459: sub end_window {
460: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
461: my $result;
462: if ($target eq 'web' || $target eq 'webgrade') {
463: my $output=&Apache::lonxml::endredirection;
464: my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
465: if (!$linktext) { $linktext='<sup>*</sup>'; }
466: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
467: if (!$width) { $width='500'; }
468: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
469: if (!$height) { $height='200'; }
470: $result=&Apache::loncommon::modal_adhoc_window
471: ("LONCAPA_newwindow_$Apache::lonxml::curdepth",$width,$height,$output,$linktext);
472: } elsif ($target eq 'tex') {
473: my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
474: if ($printtext=~/\w/) {
475: # If a "printtext" is given, proceed to retrieve all intervening information and trash it
476: my $output=&Apache::lonxml::endredirection;
477: # Use printtext instead
478: $result=$printtext;
479: } else {
480: $result='}';
481: }
482: } else {
483: $result = '';
484: }
485: return $result;
486: }
487:
488:
489: sub start_windowlink {
490: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
491: my $result = '';
492: if ($target eq 'web' || $target eq 'webgrade') {
493: &Apache::lonxml::startredirection;
494: } elsif ($target eq 'edit') {
495: $result.=&Apache::edit::tag_start($target,$token);
496: $result.=&Apache::edit::text_arg('Link:','href',$token,70);
497: $result.=&Apache::edit::text_arg('Height:','height',$token,5);
498: $result.=&Apache::edit::text_arg('Width:','width',$token,5);
499: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
500: } elsif ($target eq 'modified') {
501: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
502: $safeeval,'href',
503: 'width','height');
504: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
505: }
506: return $result;
507: }
508:
509: sub end_windowlink {
510: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
511: my $result;
512: if ($target eq 'web' || $target eq 'webgrade') {
513: my $output=&Apache::lonxml::endredirection;
514: my $href= &Apache::lonxml::get_param('href',$parstack,$safeeval);
515: if (!$href) { $href='/adm/rat/empty.html'; }
516: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
517: if (!$width) { $width='500'; }
518: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
519: if (!$height) { $height='200'; }
520: $result=&Apache::loncommon::modal_link($href,$output,$width,$height);
521: } else {
522: $result = '';
523: }
524: return $result;
525: }
526:
527:
528: sub start_togglebox {
529: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
530: my $result = '';
531: if ($target eq 'web' || $target eq 'webgrade') {
532: my $id="LONCAPA_togglebox_$Apache::lonxml::curdepth";
533: my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
534: unless ($heading) { $heading=''; } else { $heading.=' '; }
535: my $showtext=&Apache::lonxml::get_param('showtext',$parstack,$safeeval);
536: my $hidetext=&Apache::lonxml::get_param('hidetext',$parstack,$safeeval);
537: my $headerbg=&Apache::lonxml::get_param('headerbg',$parstack,$safeeval);
538: $result=&Apache::loncommon::start_togglebox($id,$heading,$headerbg,$hidetext,$showtext);
539: } elsif ($target eq 'tex') {
540: my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
541: unless ($heading) { $heading=''; } else { $heading.=' '; }
542: $result = "\n\n".'\fbox{{\bf '.$heading.'} \qquad '."\n";
543: } elsif ($target eq 'edit') {
544: $result.=&Apache::edit::tag_start($target,$token);
545: $result.=&Apache::edit::text_arg('Heading:','heading',$token,70);
546: $result.=&Apache::edit::text_arg('Header Background:','headerbg',$token,7);
547: $result.=&Apache::edit::text_arg('Show text:','showtext',$token,10);
548: $result.=&Apache::edit::text_arg('Hide text:','hidetext',$token,10);
549: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
550: } elsif ($target eq 'modified') {
551: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
552: $safeeval,'heading',
553: 'showtext','hidetext',
554: 'headerbg','textbg');
555: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
556: }
557: return $result;
558: }
559:
560: sub end_togglebox {
561: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
562: my $result;
563: if ($target eq 'web' || $target eq 'webgrade') {
564: $result=&Apache::loncommon::end_togglebox();
565: } elsif ($target eq 'tex') {
566: $result = "}\n\n";
567: } else {
568: $result = '';
569: }
570: return $result;
571: }
572:
573:
574:
575: sub start_import {
576: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
577: my $bodytext=$$parser[$#$parser]->get_text("/import");
578: my $result ="";
579:
580: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
581:
582: if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade'
583: || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
584: # FIXME this probably needs to be smart about construction vs.
585: # non construction space.
586: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
587: my $file=&Apache::lonnet::getfile($location);
588: if ($file == -1) {
589: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
590: return "";
591: }
592: my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
593: if (($importmode eq 'problem') || ($importmode eq 'part')) {
594: # We are using import to import published problems
595: if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
596: # We explicitly don't want this to be a separate part or the problem already has parts
597: $file=~s/^\s*<problem>/<library>/s;
598: $file=~s/<\/problem>\s*$/<\/library>/s;
599: } else {
600: # We want this to be a separate part, but it currently is not
601: $file=~s/^\s*<problem>/<library><part>/s;
602: $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
603: }
604: }
605: my $dir=$location;
606: $dir=~s:/[^/]*$::;
607: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
608: my $id= &Apache::lonxml::get_id($parstack,$safeeval);
609: if (!$id) { $id=$Apache::lonxml::curdepth; }
610: push(@Apache::inputtags::import,$id);
611: push(@Apache::inputtags::importlist,$id);
612:
613: &Apache::lonxml::newparser($parser,\$file,$dir);
614:
615: } elsif ($target eq "edit" ) {
616: $result.=&Apache::edit::tag_start($target,$token);
617: my $location=$token->[1];
618: $location=~s/^\s*//s;
619: $location=~s/\s*$//s;
620: $result.=&Apache::edit::editline($location,$bodytext,'',40);
621: $result.=&Apache::edit::browse(undef,'textnode');
622: $result.= ' <label>'.&mt('Import as:').
623: '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
624: my %options=&Apache::lonlocal::texthash('' => 'as standard library',
625: 'problem' => 'as problem',
626: 'part' => 'as problem part(s)');
627: foreach my $option (sort(keys(%options))) {
628: $result.='<option value="'.$option.'"';
629: if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
630: $result.=' selected="selected"';
631: }
632: $result.='>'.$options{$option}.'</option>';
633: }
634: $result.='</select></label>';
635: #FIXME this need to convert $bodytext to be a contruction space reference
636: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
637: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
638: } elsif ($target eq 'modified') {
639: &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
640: $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
641: $result.=&Apache::edit::modifiedfield("/import",$parser);
642: } elsif ($target eq 'meta') {
643: my $id= &Apache::lonxml::get_id($parstack,$safeeval);
644: $result.='<import part="'.$Apache::inputtags::part;
645: if ($id) {
646: $result.='" id="'.$id;
647: }
648: $result.='" importmode="'.$token->[2]{'importmode'}.'">';
649: $result.=$bodytext;
650: $result.='</import>';
651: }
652: return $result;
653: }
654:
655: sub end_import {
656: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
657: pop(@Apache::inputtags::import);
658: my $result;
659: if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
660: &Apache::edit::end_table(); }
661: return $result;
662: }
663:
664: sub start_storetc {
665: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
666: my $result = '';
667: &Apache::lonxml::startredirection;
668: return $result;
669: }
670:
671: sub end_storetc {
672: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
673: my $result;
674: my $output=&Apache::lonxml::endredirection;
675: $output =~ s/\"/\"\;/g;
676: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
677: return $result;
678: }
679:
680:
681: sub start_physnet {
682: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
683: my $bodytext = '/res/adm/includes/physnet.sty';
684: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
685: my $cbistyletext=&Apache::lonnet::getfile($location);
686:
687: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
688: if (keys(%$style) && (($target eq 'web') || ($target eq 'tex'))) {
689: $$parser['-1']->unget_token($token);
690: }
691: # if ( defined($$style{'physnet'}) ) {
692: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
693: # }
694: return "";
695: }
696:
697: sub end_physnet {
698: return '';
699: }
700:
701: sub start_standalone {
702: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
703: my $result='';
704: if ($target eq 'web' || $target eq 'webgrade') {
705: if ( $env{'request.course.id'} ) {
706: my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
707: } else {
708: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
709: }
710: }
711: return $result;
712: }
713:
714: sub end_standalone {
715: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
716: my $result='';
717: if ($target eq 'web' || $target eq 'webgrade' ) {
718: if ( $env{'request.course.id'} ) {
719: } else {
720: $result='</td></tr></table>';
721: }
722: }
723: return $result;
724: }
725:
726: sub start_comment {
727: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
728: my $result='';
729: if ($target eq 'edit') {
730: $result=&Apache::edit::tag_start($target,$token);
731: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
732: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
733: } elsif ( $target eq 'modified') {
734: $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
735: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
736: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
737: $target eq 'webgrade') {
738: #normally throw away comments
739: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
740: }
741: return $result;
742: }
743:
744: sub end_comment {
745: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
746: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
747: return '';
748: }
749:
750:
751: sub xmlparse {
752: my ($string) = @_;
753: &Apache::lonxml::debug("xmlparse recursion starting with $string");
754: # Apache::run::evaluate does an 'eval' on the name of the subroutine
755: # if it detects something that looks like a subroutine, this ends up calling
756: # things without any arguments and since perl is nice enough to pass
757: # along the default arguments when you don't explicitly say no arguments
758: # if you call &xmlparse, it gets &xmlparse passed as it argument.
759: # Same thing soccurs with &chemparse.
760: if ($string eq '&xmlparse') { return '&xmlparse'; }
761: if ($string eq '&chemparse') { return '&chemparse'; }
762: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
763: @Apache::scripttag::parser_env;
764: my @parser;
765: &Apache::lonxml::newparser(\@parser,\$string);
766: &Apache::lonxml::startredirection();
767: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
768: $parstack,\@parser,
769: $safeeval,$style);
770: $result.=&Apache::lonxml::endredirection();
771: &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
772: return $result;
773: }
774:
775: sub start_num {
776: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
777: my $result = '';
778: my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
779: if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
780: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
781: if (!$Apache::lonxml::default_homework_loaded) {
782: &Apache::lonxml::default_homework_load($safeeval);
783: }
784: @Apache::scripttag::parser_env = @_;
785: my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
786: $result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
787: }
788: return $result;
789: }
790:
791: sub end_num {
792: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
793: my $result = '';
794: return $result;
795: }
796:
797: sub start_parse {
798: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
799: my $result = '';
800: if ( $target eq 'web' || $target eq 'tex' ||
801: $target eq 'grade' || $target eq 'answer' ||
802: $target eq 'analyze'|| $target eq 'webgrade') {
803: my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
804: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
805: if (!$Apache::lonxml::default_homework_loaded) {
806: &Apache::lonxml::default_homework_load($safeeval);
807: }
808: @Apache::scripttag::parser_env = @_;
809: $result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
810: if ($target eq 'grade' || $target eq 'answer' ||
811: $target eq 'analyze') {
812: # grade/answer/analyxe should produce no output but if we
813: # are redirecting, the redirecter should know what to do
814: # with the output
815: if (!$Apache::lonxml::redirection) { $result=''; }
816: }
817: }
818: return $result;
819: }
820:
821: sub end_parse {
822: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
823: my $result = '';
824: return $result;
825: }
826:
827: sub start_algebra {
828: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
829: my $result = '';
830: if ( $target eq 'web' || $target eq 'tex' ||
831: $target eq 'grade' || $target eq 'answer' ||
832: $target eq 'analyze' || $target eq 'webgrade') {
833: my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
834: $inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
835: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
836: my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
837: $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
838: }
839: $Apache::lonxml::post_evaluate=0;
840: }
841: return $result;
842: }
843:
844: sub end_algebra {
845: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
846: my $result = '';
847: return $result;
848: }
849:
850: 1;
851: __END__
852:
853: =pod
854:
855: =head1 NAME
856:
857: Apache::scripttag.pm
858:
859: =head1 SYNOPSIS
860:
861: implements <script>, <scriptlib>, <parserlib>,
862: and <import>
863:
864: This is part of the LearningOnline Network with CAPA project
865: described at http://www.lon-capa.org.
866:
867: =cut
868:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>