1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.79 2002/10/21 20:31:06 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: }
48:
49: sub start_script {
50: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
51: @Apache::scripttag::parser_env = @_;
52: my $result='';
53: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
54: &Apache::lonxml::debug("found type of $type");
55: if ($type eq "loncapa/perl") {
56: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
57: if ( $target eq "modified" ) {
58: $result=$token->[4].&Apache::edit::modifiedfield();
59: } elsif ( $target eq 'web' || $target eq 'tex' ||
60: $target eq 'grade' || $target eq 'answer' ||
61: $target eq 'analyze' ) {
62: if (!$Apache::lonxml::default_homework_loaded) {
63: &Apache::lonxml::default_homework_load($safeeval);
64: }
65: &Apache::run::run($bodytext,$safeeval);
66: if (($target eq 'answer') && ($Apache::lonhomework::viewgrades == 'F')) {
67: $Apache::lonxml::evaluate--;
68: $result.="<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor="#FFFFFF"><pre>";
69: my $listing= &HTML::Entities::encode(&Apache::run::dump($target,$safeeval));
70:
71: $result.=$listing;
72: $result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
73: }
74: } elsif ($target eq "edit" ) {
75: #&Apache::run::run($bodytext,$safeeval);
76: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
77: $result=&Apache::edit::tag_start($target,$token,'Script');
78: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
79: }
80: } else {
81: if ($target ne "meta") {
82: $result = $token->[4];
83: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
84: $result.=$bodytext;
85: }
86: }
87: return $result;
88: }
89:
90: sub end_script {
91: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
92: if ( $target eq "meta" ) { return ''; }
93: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
94: my $result='';
95: #other script blocks need to survive
96: if ($type ne "loncapa/perl") {
97: return $token->[2];
98: } elsif ($target eq 'edit' ) {
99: return &Apache::edit::end_table();
100: } elsif ($target eq 'answer') {
101: $Apache::lonxml::evaluate++;
102: }
103: return '';
104: }
105:
106: sub start_display {
107: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
108:
109: my $result;
110: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
111:
112: if ( $target eq "modified" ) {
113: $result=$token->[4].&Apache::edit::modifiedfield();
114: } elsif ( $target eq 'web' || $target eq 'tex' ||
115: $target eq 'grade' || $target eq 'answer' ||
116: $target eq 'analyze') {
117: $result=&Apache::run::run($bodytext,$safeeval);
118: if ($target eq 'grade' || $target eq 'answer' ||
119: $target eq 'analyze') {
120: $result=''; # grade should produce no output
121: }
122: } elsif ($target eq "edit" ) {
123: #$result =
124: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
125: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
126: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
127: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
128: }
129: return $result;
130: }
131:
132: sub end_display {
133: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
134: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
135: return '';
136: }
137:
138: sub start_scriptlib {
139: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
140: my $bodytext;
141: my $result ='';
142: my $error='';
143:
144: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
145: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
146: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
147: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
148: $$parstack[$#$parstack]);
149: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
150: $bodytext);
151: my $script=&Apache::lonnet::getfile($location);
152: if ($script == -1) {
153: if ($target eq 'edit') {
154: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
155: } else {
156: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
157: return "";
158: }
159: }
160: &Apache::run::run($script,$safeeval);
161: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
162: }
163: if ($target eq "edit" ) {
164: $result=
165: &Apache::edit::tag_start($target,$token,'New Script Functions').
166: &Apache::edit::editline($token->[1],$bodytext,'',40).
167: $error.'</td></tr>'.
168: &Apache::edit::end_table();
169: }
170: if ($target eq "modified" ) {
171: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
172: $result=$token->[4].&Apache::edit::modifiedfield($token);
173: &Apache::lonxml::debug($result);
174: }
175: return $result;
176: }
177:
178: sub end_scriptlib {
179: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
180: my @result;
181: if ($target eq "edit" ) { $result[1]='no'; }
182: return @result;
183: }
184:
185: sub start_parserlib {
186: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
187: my $bodytext;
188: my $result ="";
189: my $error='';
190: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
191: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
192: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
193: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
194: $$parstack[$#$parstack]);
195: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
196: $bodytext);
197: my $styletext=&Apache::lonnet::getfile($location);
198: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
199: if ($styletext == -1) {
200: if ($target eq 'edit') {
201: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
202: } else {
203: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
204: return "";
205: }
206: }
207: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
208: }
209: if ($target eq "edit" ) {
210: $result=
211: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
212: &Apache::edit::editline($token->[1],$bodytext,'',40).
213: $error.'</td></tr>'.
214: &Apache::edit::end_table();
215: }
216: if ($target eq "modified" ) {
217: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
218: $result=$token->[4].&Apache::edit::modifiedfield($token);
219: &Apache::lonxml::debug($result);
220: }
221: return $result;
222: }
223:
224: sub end_parserlib {
225: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
226: my @result;
227: if ($target eq "edit" ) { $result[1]='no'; }
228: return @result;
229: }
230:
231: sub start_window {
232: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
233: my $result = '';
234: if ($target eq 'web') {
235: &Apache::lonxml::startredirection;
236: } elsif ($target eq 'tex') {
237: $result = '\unskip\footnote{';
238: }
239: return $result;
240: }
241:
242: sub end_window {
243: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
244: my $result;
245: if ($target eq 'web') {
246: my $output=&Apache::lonxml::endredirection;
247: $output =~ s/\"/\"\;/g;
248: $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,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();void(0);\"><sup>*</sup></a>";
249: } elsif ($target eq 'tex') {
250: $result = '}';
251: } else {
252: $result = '';
253: }
254: return $result;
255: }
256:
257: sub start_import {
258: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
259: my $bodytext=$$parser[$#$parser]->get_text("/import");
260: my $result ="";
261:
262: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
263:
264: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
265: $target eq 'tex' || $target eq 'analyze' ) {
266: # FIXME this probably needs to be smart about construction vs.
267: # non construction space.
268: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
269: my $file=&Apache::lonnet::getfile($location);
270: if ($file == -1) {
271: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
272: return "";
273: }
274:
275: my $dir=$location;
276: $dir=~s:/[^/]*$::;
277: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
278: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
279: if (!$id) { $id=$Apache::lonxml::curdepth; }
280: push(@Apache::inputtags::import,$id);
281: &Apache::lonxml::newparser($parser,\$file,$dir);
282: } elsif ($target eq "edit" ) {
283: $result.=&Apache::edit::tag_start($target,$token);
284: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
285: #FIXME this need to convert $bodytext to be a contruction space reference
286: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
287: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
288: } elsif ($target eq 'modified') {
289: $bodytext=$$parser[$#$parser]->get_text("/import");
290: $result=$token->[4].&Apache::edit::modifiedfield($token);
291: &Apache::lonxml::debug($result);
292: } elsif ($target eq 'meta') {
293: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
294: $result.='<import part="'.$Apache::inputtags::part;
295: if ($id) {
296: $result.='" id="'.$id;
297: }
298: $result.='">';
299: $result.=$bodytext;
300: $result.='</import>';
301: }
302: return $result;
303: }
304:
305: sub end_import {
306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
307: pop(@Apache::inputtags::import);
308: my $result;
309: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
310: return $result;
311: }
312:
313: sub start_storetc {
314: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
315: my $result = '';
316: &Apache::lonxml::startredirection;
317: return $result;
318: }
319:
320: sub end_storetc {
321: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
322: my $result;
323: my $output=&Apache::lonxml::endredirection;
324: $output =~ s/\"/\"\;/g;
325: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
326: return $result;
327: }
328:
329:
330: sub start_physnet {
331: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
332: my $bodytext = '/adm/includes/physnet.sty';
333: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
334: my $cbistyletext=&Apache::lonnet::getfile($location);
335:
336: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
337: $$parser['-1']->unget_token($token);
338: # if ( defined($$style{'physnet'}) ) {
339: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
340: # }
341: return "";
342: }
343:
344: sub end_physnet {
345: return '';
346: }
347:
348: sub start_standalone {
349: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
350: my $result='';
351: if ($target eq 'web' ) {
352: if ( $ENV{'request.course.id'} ) {
353: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
354: } else {
355: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
356: }
357: }
358: return $result;
359: }
360:
361: sub end_standalone {
362: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
363: my $result='';
364: if ($target eq 'web' ) {
365: if ( $ENV{'request.course.id'} ) {
366: } else {
367: $result='</td></tr></table>';
368: }
369: }
370: return $result;
371: }
372:
373: sub start_comment {
374: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
375: my $result='';
376: if ($target eq 'edit') {
377: $result=&Apache::edit::tag_start($target,$token);
378: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
379: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
380: } elsif ( $target eq 'modified') {
381: $result=$token->[4].&Apache::edit::modifiedfield($token);
382: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
383: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
384: #normally throw away comments
385: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
386: }
387: return $result;
388: }
389:
390: sub end_comment {
391: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
392: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
393: return '';
394: }
395:
396:
397: sub xmlparse {
398: my ($string) = @_;
399: &Apache::lonxml::debug("Got $string");
400: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
401: @Apache::scripttag::parser_env;
402: my @parser;
403: &Apache::lonxml::newparser(\@parser,\$string);
404: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
405: $parstack,\@parser,
406: $safeeval,$style);
407: return $result;
408: }
409:
410: 1;
411: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>