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