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