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