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