1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.117 2004/09/28 20:40:05 www 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::style;
34:
35: #Globals
36: # this used to pass around the standard callsub arguments to a tag func
37: # so xmlparse can reenter the inner_xmlparse loop.
38:
39: @Apache::scripttag::parser_env = ();
40: BEGIN {
41: &Apache::lonxml::register('Apache::scripttag',
42: ('script','scriptlib','parserlib','import',
43: 'window','display','storetc','physnet',
44: 'standalone','comment',
45: 'LONCAPA_INTERNAL_TURN_STYLE_ON',
46: 'LONCAPA_INTERNAL_TURN_STYLE_OFF',
47: 'LONCAPA_INTERNAL_LONHTTPD_PORT'));
48: }
49:
50: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
51: $Apache::lonxml::usestyle=1;
52: $Apache::lonxml::style_values='';
53: return ('','no');
54: }
55:
56: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
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: }
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 {
80: return ('','no');
81: }
82:
83: sub start_LONCAPA_INTERNAL_LONHTTPD_PORT {
84: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
85: if ($target eq 'web') {
86: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
87: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
88: return '<script type="text/javascript">var lonhttpdport=\''.
89: $lonhttpdPort.'\';</script>';
90: }
91: return ('','no');
92: }
93:
94: sub end_LONCAPA_INTERNAL_LONHTTPD_PORT {
95: return ('','no');
96: }
97:
98: sub start_script {
99: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
100: @Apache::scripttag::parser_env = @_;
101: my $result='';
102: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
103: &Apache::lonxml::debug("found type of $type");
104: if ($type eq "loncapa/perl") {
105: if ( $target eq "modified" ) {
106: $result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
107: } elsif ( $target eq 'web' || $target eq 'tex' ||
108: $target eq 'grade' || $target eq 'answer' ||
109: $target eq 'analyze' ) {
110: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
111: if (!$Apache::lonxml::default_homework_loaded) {
112: &Apache::lonxml::default_homework_load($safeeval);
113: }
114: &Apache::run::run($bodytext,$safeeval);
115: if (($target eq 'answer') &&
116: ($ENV{'form.answer_output_mode'} ne 'tex') &&
117: ($Apache::lonhomework::viewgrades == 'F')) {
118: $Apache::lonxml::evaluate--;
119: my (undef,undef,$udom,$uname)=&Apache::lonxml::whichuser();
120: $result.="<script type=\"text/javascript\"> function LONCAPA_scriptvars_".$uname."_".$udom."_"."$Apache::lonxml::curdepth() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.document.open('text/html','replace');newWindow.document.writeln('<html><head><title>Script Vars</title></head><body bgcolor=\"#FFFFFF\"><pre>";
121: my $listing=&Apache::run::dump($target,$safeeval);
122: $listing=~s/\'/\\\'/g;
123: $listing=~s/\n/\\n/g;
124: $listing=~s/\r/\\r/g;
125: $result.=$listing;
126: $result.= "</pre></body></html>');newWindow.document.close();}</script><a href=\"javascript:LONCAPA_scriptvars_".$uname."_".$udom."_$Apache::lonxml::curdepth();void(0);\">Script Vars</a><br />";
127: }
128: } elsif ($target eq "edit" ) {
129: #&Apache::run::run($bodytext,$safeeval);
130: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
131: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
132: $result=&Apache::edit::tag_start($target,$token,'Script');
133: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
134: } elsif ($target eq 'meta') {
135: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
136: }
137: } else {
138: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
139: if ($target ne "meta" && $target ne 'tex') {
140: $result = $token->[4];
141: $result.=$bodytext;
142: }
143: }
144: return $result;
145: }
146:
147: sub end_script {
148: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
149: if ( $target eq "meta" ) { return ''; }
150: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
151: my $result='';
152: #other script blocks need to survive
153: if ($type ne "loncapa/perl" && $target ne 'tex') {
154: return $token->[2];
155: } elsif ($target eq 'edit' ) {
156: return &Apache::edit::end_table();
157: } elsif ($target eq 'answer') {
158: $Apache::lonxml::evaluate++;
159: }
160: return '';
161: }
162:
163: sub start_display {
164: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
165:
166: my $result;
167:
168: if ( $target eq "modified" ) {
169: $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
170: } elsif ( $target eq 'web' || $target eq 'tex' ||
171: $target eq 'grade' || $target eq 'answer' ||
172: $target eq 'analyze') {
173: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
174: if (!$Apache::lonxml::default_homework_loaded) {
175: &Apache::lonxml::default_homework_load($safeeval);
176: }
177: $result=&Apache::run::run($bodytext,$safeeval);
178: if ($target eq 'grade' || $target eq 'answer' ||
179: $target eq 'analyze') {
180: # grade/answer/analyxe should produce no output but if we
181: # are redirecting, the redirecter should know what to do
182: # with the output
183: if (!$Apache::lonxml::redirection) { $result=''; }
184: }
185: $Apache::lonxml::post_evaluate=0;
186: } elsif ($target eq "edit" ) {
187: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
188: #$result =
189: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
190: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
191: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
192: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
193: } elsif ($target eq 'meta') {
194: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
195: }
196: return $result;
197: }
198:
199: sub end_display {
200: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
201: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
202: return '';
203: }
204:
205: sub start_scriptlib {
206: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
207: my $bodytext;
208: my $result ='';
209: my $error='';
210:
211: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
212: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
213: $target eq 'analyze') {
214: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
215: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
216: $$parstack[$#$parstack]);
217: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
218: $bodytext);
219: my $script=&Apache::lonnet::getfile($location);
220: if ($script == -1) {
221: if ($target eq 'edit') {
222: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
223: } else {
224: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
225: return "";
226: }
227: }
228: &Apache::run::run($script,$safeeval);
229: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
230: }
231: if ($target eq "edit" ) {
232: $result=
233: &Apache::edit::tag_start($target,$token,'New Script Functions').
234: &Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
235: &Apache::edit::browse(undef,'textnode').
236: $error.'</td></tr>'.
237: &Apache::edit::end_table();
238: }
239: if ($target eq "modified" ) {
240: $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
241: }
242: return $result;
243: }
244:
245: sub end_scriptlib {
246: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
247: my @result;
248: if ($target eq "edit" ) { $result[1]='no'; }
249: return @result;
250: }
251:
252: sub start_parserlib {
253: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
254: my $bodytext;
255: my $result ="";
256: my $error='';
257: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
258: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
259: $target eq 'analyze') {
260: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
261: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
262: $$parstack[$#$parstack]);
263: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
264: $bodytext);
265: my $styletext=&Apache::lonnet::getfile($location);
266: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
267: if ($styletext == -1) {
268: if ($target eq 'edit') {
269: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
270: } else {
271: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
272: return "";
273: }
274: }
275: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
276: }
277: if ($target eq "edit" ) {
278: $result=
279: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
280: &Apache::edit::editline($token->[1],$bodytext,'',40).
281: $error.'</td></tr>'.
282: &Apache::edit::end_table();
283: }
284: if ($target eq "modified" ) {
285: $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
286: }
287: return $result;
288: }
289:
290: sub end_parserlib {
291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
292: my @result;
293: if ($target eq "edit" ) { $result[1]='no'; }
294: return @result;
295: }
296:
297: sub start_window {
298: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
299: my $result = '';
300: if ($target eq 'web') {
301: &Apache::lonxml::startredirection;
302: } elsif ($target eq 'tex') {
303: $result = '\unskip\footnote{';
304: } elsif ($target eq 'edit') {
305: $result.=&Apache::edit::tag_start($target,$token);
306: $result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
307: $result.=&Apache::edit::text_arg('Height:','width',$token,5);
308: $result.=&Apache::edit::text_arg('Width:','height',$token,5);
309: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
310: } elsif ($target eq 'modified') {
311: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
312: $safeeval,'linttext',
313: 'width','height');
314: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
315: }
316: return $result;
317: }
318:
319: sub end_window {
320: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
321: my $result;
322: if ($target eq 'web') {
323: my $output=&Apache::lonxml::endredirection;
324: $output =~ s/[\n\r]/ /g;
325: # $output = &HTML::Entities::encode($output,'<>&"\'');
326: $output =~ s/\'/\\\'/g;
327: my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
328: if (!$linktext) { $linktext='<sup>*</sup>'; }
329: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
330: if (!$width) { $width='500'; }
331: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
332: if (!$height) { $height='200'; }
333: $result = "<script type=\"text/javascript\"> function LONCAPA_newwindow_$Apache::lonxml::curdepth() {newWindow=open('','new_W','width=$width,height=$height,scrollbars=1');newWindow.document.open('text/html','replace');newWindow.document.writeln('<html><head><title>newwindow</title></head><body bgcolor=\"#FFFFFF\"> $output </body></html>');newWindow.document.close();}</script><a href=\"javascript:LONCAPA_newwindow_$Apache::lonxml::curdepth();void(0);\">$linktext</a>";
334: } elsif ($target eq 'tex') {
335: $result = '}';
336: } else {
337: $result = '';
338: }
339: return $result;
340: }
341:
342: sub start_import {
343: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
344: my $bodytext=$$parser[$#$parser]->get_text("/import");
345: my $result ="";
346:
347: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
348:
349: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
350: $target eq 'tex' || $target eq 'analyze' ) {
351: # FIXME this probably needs to be smart about construction vs.
352: # non construction space.
353: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
354: my $file=&Apache::lonnet::getfile($location);
355: if ($file == -1) {
356: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
357: return "";
358: }
359:
360: my $dir=$location;
361: $dir=~s:/[^/]*$::;
362: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
363: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
364: if (!$id) { $id=$Apache::lonxml::curdepth; }
365: push(@Apache::inputtags::import,$id);
366: push(@Apache::inputtags::importlist,$id);
367:
368: &Apache::lonxml::newparser($parser,\$file,$dir);
369:
370: } elsif ($target eq "edit" ) {
371: $result.=&Apache::edit::tag_start($target,$token);
372: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
373: $result.=&Apache::edit::browse(undef,'textnode');
374: #FIXME this need to convert $bodytext to be a contruction space reference
375: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
376: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
377: } elsif ($target eq 'modified') {
378: $result=$token->[4].&Apache::edit::modifiedfield("/import",$parser);
379: } elsif ($target eq 'meta') {
380: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
381: $result.='<import part="'.$Apache::inputtags::part;
382: if ($id) {
383: $result.='" id="'.$id;
384: }
385: $result.='">';
386: $result.=$bodytext;
387: $result.='</import>';
388: }
389: return $result;
390: }
391:
392: sub end_import {
393: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
394: pop(@Apache::inputtags::import);
395: my $result;
396: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
397: return $result;
398: }
399:
400: sub start_storetc {
401: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
402: my $result = '';
403: &Apache::lonxml::startredirection;
404: return $result;
405: }
406:
407: sub end_storetc {
408: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
409: my $result;
410: my $output=&Apache::lonxml::endredirection;
411: $output =~ s/\"/\"\;/g;
412: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
413: return $result;
414: }
415:
416:
417: sub start_physnet {
418: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
419: my $bodytext = '/adm/includes/physnet.sty';
420: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
421: my $cbistyletext=&Apache::lonnet::getfile($location);
422:
423: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
424: $$parser['-1']->unget_token($token);
425: # if ( defined($$style{'physnet'}) ) {
426: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
427: # }
428: return "";
429: }
430:
431: sub end_physnet {
432: return '';
433: }
434:
435: sub start_standalone {
436: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
437: my $result='';
438: if ($target eq 'web' ) {
439: if ( $ENV{'request.course.id'} ) {
440: my $inside = &Apache::lonxml::get_all_text("/standalone",$parser);
441: } else {
442: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
443: }
444: }
445: return $result;
446: }
447:
448: sub end_standalone {
449: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
450: my $result='';
451: if ($target eq 'web' ) {
452: if ( $ENV{'request.course.id'} ) {
453: } else {
454: $result='</td></tr></table>';
455: }
456: }
457: return $result;
458: }
459:
460: sub start_comment {
461: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
462: my $result='';
463: if ($target eq 'edit') {
464: $result=&Apache::edit::tag_start($target,$token);
465: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
466: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
467: } elsif ( $target eq 'modified') {
468: $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
469: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
470: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
471: #normally throw away comments
472: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
473: }
474: return $result;
475: }
476:
477: sub end_comment {
478: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
479: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
480: return '';
481: }
482:
483:
484: sub xmlparse {
485: my ($string) = @_;
486: &Apache::lonxml::debug("xmlparse recursion starting with $string");
487: # Apache::run::evaluate does an 'eval' on the name of the subroutine
488: # if it detects something that looks like a subroutine, this ends up calling
489: # things without any arguments and since perl is nice enough to pass
490: # along the default arguments when you don't explicitly say no arguments
491: # if you call &xmlparse, it gets &xmlparse passed as it argument.
492: # Same thing soccurs with &chemparse.
493: if ($string eq '&xmlparse') { return '&xmlparse'; }
494: if ($string eq '&chemparse') { return '&chemparse'; }
495: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
496: @Apache::scripttag::parser_env;
497: my @parser;
498: &Apache::lonxml::newparser(\@parser,\$string);
499: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
500: $parstack,\@parser,
501: $safeeval,$style);
502: &Apache::lonxml::debug("xmlparse recursion ending with $result");
503: return $result;
504: }
505:
506: 1;
507: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>