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