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