1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.115 2004/08/18 20:48:45 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: # 2/21 Guy
29: # 8/20 Gerd Kortemeyer
30:
31: package Apache::scripttag;
32:
33: use strict;
34: use Apache::lonnet;
35: use Apache::style;
36:
37: #Globals
38: # this used to pass around the standard callsub arguments to a tag func
39: # so xmlparse can reenter the inner_xmlparse loop.
40:
41: @Apache::scripttag::parser_env = ();
42: BEGIN {
43: &Apache::lonxml::register('Apache::scripttag',
44: ('script','scriptlib','parserlib','import',
45: 'window','display','storetc','physnet',
46: 'standalone','comment',
47: 'LONCAPA_INTERNAL_TURN_STYLE_ON',
48: 'LONCAPA_INTERNAL_TURN_STYLE_OFF',
49: 'LONCAPA_INTERNAL_LONHTTPD_PORT'));
50: }
51:
52: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
53: $Apache::lonxml::usestyle=1;
54: $Apache::lonxml::style_values='';
55: return ('','no');
56: }
57:
58: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
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: }
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 {
82: return ('','no');
83: }
84:
85: sub start_LONCAPA_INTERNAL_LONHTTPD_PORT {
86: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
87: if ($target eq 'web') {
88: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
89: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
90: return '<script type="text/javascript">var lonhttpdport=\''.
91: $lonhttpdPort.'\';</script>';
92: }
93: return ('','no');
94: }
95:
96: sub end_LONCAPA_INTERNAL_LONHTTPD_PORT {
97: return ('','no');
98: }
99:
100: sub start_script {
101: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
102: @Apache::scripttag::parser_env = @_;
103: my $result='';
104: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
105: &Apache::lonxml::debug("found type of $type");
106: if ($type eq "loncapa/perl") {
107: if ( $target eq "modified" ) {
108: $result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
109: } elsif ( $target eq 'web' || $target eq 'tex' ||
110: $target eq 'grade' || $target eq 'answer' ||
111: $target eq 'analyze' ) {
112: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
113: if (!$Apache::lonxml::default_homework_loaded) {
114: &Apache::lonxml::default_homework_load($safeeval);
115: }
116: &Apache::run::run($bodytext,$safeeval);
117: if (($target eq 'answer') &&
118: ($ENV{'form.answer_output_mode'} ne 'tex') &&
119: ($Apache::lonhomework::viewgrades == 'F')) {
120: $Apache::lonxml::evaluate--;
121: my (undef,undef,$udom,$uname)=&Apache::lonxml::whichuser();
122: $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>";
123: my $listing=&Apache::run::dump($target,$safeeval);
124: $listing=~s/\'/\\\'/g;
125: $listing=~s/\n/\\n/g;
126: $listing=~s/\r/\\r/g;
127: $result.=$listing;
128: $result.= "</pre></body></html>');newWindow.document.close();}</script><a href=\"javascript:LONCAPA_scriptvars_".$uname."_".$udom."_$Apache::lonxml::curdepth();void(0);\">Script Vars</a><br />";
129: }
130: } elsif ($target eq "edit" ) {
131: #&Apache::run::run($bodytext,$safeeval);
132: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
133: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
134: $result=&Apache::edit::tag_start($target,$token,'Script');
135: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
136: } elsif ($target eq 'meta') {
137: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
138: }
139: } else {
140: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
141: if ($target ne "meta" && $target ne 'tex') {
142: $result = $token->[4];
143: $result.=$bodytext;
144: }
145: }
146: return $result;
147: }
148:
149: sub end_script {
150: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
151: if ( $target eq "meta" ) { return ''; }
152: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
153: my $result='';
154: #other script blocks need to survive
155: if ($type ne "loncapa/perl" && $target ne 'tex') {
156: return $token->[2];
157: } elsif ($target eq 'edit' ) {
158: return &Apache::edit::end_table();
159: } elsif ($target eq 'answer') {
160: $Apache::lonxml::evaluate++;
161: }
162: return '';
163: }
164:
165: sub start_display {
166: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
167:
168: my $result;
169:
170: if ( $target eq "modified" ) {
171: $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
172: } elsif ( $target eq 'web' || $target eq 'tex' ||
173: $target eq 'grade' || $target eq 'answer' ||
174: $target eq 'analyze') {
175: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
176: if (!$Apache::lonxml::default_homework_loaded) {
177: &Apache::lonxml::default_homework_load($safeeval);
178: }
179: $result=&Apache::run::run($bodytext,$safeeval);
180: if ($target eq 'grade' || $target eq 'answer' ||
181: $target eq 'analyze') {
182: # grade/answer/analyxe should produce no output but if we
183: # are redirecting, the redirecter should know what to do
184: # with the output
185: if (!$Apache::lonxml::redirection) { $result=''; }
186: }
187: $Apache::lonxml::post_evaluate=0;
188: } elsif ($target eq "edit" ) {
189: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
190: #$result =
191: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
192: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
193: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
194: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
195: } elsif ($target eq 'meta') {
196: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
197: }
198: return $result;
199: }
200:
201: sub end_display {
202: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
203: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
204: return '';
205: }
206:
207: sub start_scriptlib {
208: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
209: my $bodytext;
210: my $result ='';
211: my $error='';
212:
213: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
214: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
215: $target eq 'analyze') {
216: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
217: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
218: $$parstack[$#$parstack]);
219: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
220: $bodytext);
221: my $script=&Apache::lonnet::getfile($location);
222: if ($script == -1) {
223: if ($target eq 'edit') {
224: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
225: } else {
226: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
227: return "";
228: }
229: }
230: &Apache::run::run($script,$safeeval);
231: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
232: }
233: if ($target eq "edit" ) {
234: $result=
235: &Apache::edit::tag_start($target,$token,'New Script Functions').
236: &Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
237: &Apache::edit::browse(undef,'textnode').
238: $error.'</td></tr>'.
239: &Apache::edit::end_table();
240: }
241: if ($target eq "modified" ) {
242: $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
243: }
244: return $result;
245: }
246:
247: sub end_scriptlib {
248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
249: my @result;
250: if ($target eq "edit" ) { $result[1]='no'; }
251: return @result;
252: }
253:
254: sub start_parserlib {
255: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
256: my $bodytext;
257: my $result ="";
258: my $error='';
259: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
260: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
261: $target eq 'analyze') {
262: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
263: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
264: $$parstack[$#$parstack]);
265: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
266: $bodytext);
267: my $styletext=&Apache::lonnet::getfile($location);
268: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
269: if ($styletext == -1) {
270: if ($target eq 'edit') {
271: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
272: } else {
273: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
274: return "";
275: }
276: }
277: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
278: }
279: if ($target eq "edit" ) {
280: $result=
281: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
282: &Apache::edit::editline($token->[1],$bodytext,'',40).
283: $error.'</td></tr>'.
284: &Apache::edit::end_table();
285: }
286: if ($target eq "modified" ) {
287: $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
288: }
289: return $result;
290: }
291:
292: sub end_parserlib {
293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
294: my @result;
295: if ($target eq "edit" ) { $result[1]='no'; }
296: return @result;
297: }
298:
299: sub start_window {
300: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
301: my $result = '';
302: if ($target eq 'web') {
303: &Apache::lonxml::startredirection;
304: } elsif ($target eq 'tex') {
305: $result = '\unskip\footnote{';
306: } elsif ($target eq 'edit') {
307: $result.=&Apache::edit::tag_start($target,$token);
308: $result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
309: $result.=&Apache::edit::text_arg('Height:','width',$token,5);
310: $result.=&Apache::edit::text_arg('Width:','height',$token,5);
311: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
312: } elsif ($target eq 'modified') {
313: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
314: $safeeval,'linttext',
315: 'width','height');
316: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
317: }
318: return $result;
319: }
320:
321: sub end_window {
322: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
323: my $result;
324: if ($target eq 'web') {
325: my $output=&Apache::lonxml::endredirection;
326: $output =~ s/[\n\r]/ /g;
327: # $output = &HTML::Entities::encode($output,'<>&"\'');
328: $output =~ s/\'/\\\'/g;
329: my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
330: if (!$linktext) { $linktext='<sup>*</sup>'; }
331: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
332: if (!$width) { $width='500'; }
333: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
334: if (!$height) { $height='200'; }
335: $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>";
336: } elsif ($target eq 'tex') {
337: $result = '}';
338: } else {
339: $result = '';
340: }
341: return $result;
342: }
343:
344: sub start_import {
345: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
346: my $bodytext=$$parser[$#$parser]->get_text("/import");
347: my $result ="";
348:
349: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
350:
351: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
352: $target eq 'tex' || $target eq 'analyze' ) {
353: # FIXME this probably needs to be smart about construction vs.
354: # non construction space.
355: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
356: my $file=&Apache::lonnet::getfile($location);
357: if ($file == -1) {
358: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
359: return "";
360: }
361:
362: my $dir=$location;
363: $dir=~s:/[^/]*$::;
364: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
365: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
366: if (!$id) { $id=$Apache::lonxml::curdepth; }
367: push(@Apache::inputtags::import,$id);
368: push(@Apache::inputtags::importlist,$id);
369:
370: &Apache::lonxml::newparser($parser,\$file,$dir);
371:
372: } elsif ($target eq "edit" ) {
373: $result.=&Apache::edit::tag_start($target,$token);
374: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
375: $result.=&Apache::edit::browse(undef,'textnode');
376: #FIXME this need to convert $bodytext to be a contruction space reference
377: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
378: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
379: } elsif ($target eq 'modified') {
380: $result=$token->[4].&Apache::edit::modifiedfield("/import",$parser);
381: } elsif ($target eq 'meta') {
382: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
383: $result.='<import part="'.$Apache::inputtags::part;
384: if ($id) {
385: $result.='" id="'.$id;
386: }
387: $result.='">';
388: $result.=$bodytext;
389: $result.='</import>';
390: }
391: return $result;
392: }
393:
394: sub end_import {
395: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
396: pop(@Apache::inputtags::import);
397: my $result;
398: if ($target eq 'edit' ) { $result=&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>