1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.71 2002/04/25 17:25:36 matthew 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: $result.=$listing;
67: $result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
68: }
69: } elsif ($target eq "edit" ) {
70: #&Apache::run::run($bodytext,$safeeval);
71: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
72: $result=&Apache::edit::tag_start($target,$token,'Script');
73: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
74: }
75: } else {
76: if ($target ne "meta") {
77: $result = $token->[4];
78: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
79: $result.=$bodytext;
80: }
81: }
82: return $result;
83: }
84:
85: sub end_script {
86: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
87: if ( $target eq "meta" ) { return ''; }
88: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
89: my $result='';
90: #other script blocks need to survive
91: if ($type ne "loncapa/perl") {
92: return $token->[2];
93: } elsif ($target eq 'edit' ) {
94: return &Apache::edit::end_table();
95: } elsif (($ENV{'request.state'} eq 'construct') && ($target eq 'answer')) {
96: $Apache::lonxml::evaluate++;
97: }
98: return '';
99: }
100:
101: sub start_display {
102: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
103:
104: my $result;
105: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
106:
107: if ( $target eq "modified" ) {
108: $result=$token->[4].&Apache::edit::modifiedfield();
109: } elsif ( $target eq 'web' || $target eq 'tex' ||
110: $target eq 'grade' || $target eq 'answer') {
111: $result=&Apache::run::run($bodytext,$safeeval);
112: if ($target eq 'grade' || $target eq 'answer' ) {
113: $result=''; # grade should produce no output
114: }
115: } elsif ($target eq "edit" ) {
116: #$result =
117: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
118: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
119: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
120: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1)
121: }
122: return $result;
123: }
124:
125: sub end_display {
126: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
127: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
128: return '';
129: }
130:
131: sub start_scriptlib {
132: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
133: my $bodytext;
134: my $result ='';
135: my $error='';
136:
137: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
138: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
139: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
140: $$parstack[$#$parstack]);
141: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
142: $bodytext);
143: my $script=&Apache::lonnet::getfile($location);
144: if ($script == -1) {
145: if ($target eq 'edit') {
146: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
147: } else {
148: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
149: return "";
150: }
151: }
152: &Apache::run::run($script,$safeeval);
153: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
154: }
155: if ($target eq "edit" ) {
156: $result=
157: &Apache::edit::tag_start($target,$token,'New Script Functions').
158: &Apache::edit::editline($token->[1],$bodytext,'',40).
159: $error.'</td></tr>'.
160: &Apache::edit::end_table();
161: }
162: if ($target eq "modified" ) {
163: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
164: $result=$token->[4].&Apache::edit::modifiedfield($token);
165: &Apache::lonxml::debug($result);
166: }
167: return $result;
168: }
169:
170: sub end_scriptlib {
171: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
172: my @result;
173: if ($target eq "edit" ) { $result[1]='no'; }
174: return @result;
175: }
176:
177: sub start_parserlib {
178: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
179: my $bodytext;
180: my $result ="";
181: my $error='';
182: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
183: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
184: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
185: $$parstack[$#$parstack]);
186: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
187: $bodytext);
188: my $styletext=&Apache::lonnet::getfile($location);
189: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
190: if ($styletext == -1) {
191: if ($target eq 'edit') {
192: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
193: } else {
194: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
195: return "";
196: }
197: }
198: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
199: }
200: if ($target eq "edit" ) {
201: $result=
202: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
203: &Apache::edit::editline($token->[1],$bodytext,'',40).
204: $error.'</td></tr>'.
205: &Apache::edit::end_table();
206: }
207: if ($target eq "modified" ) {
208: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
209: $result=$token->[4].&Apache::edit::modifiedfield($token);
210: &Apache::lonxml::debug($result);
211: }
212: return $result;
213: }
214:
215: sub end_parserlib {
216: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
217: my @result;
218: if ($target eq "edit" ) { $result[1]='no'; }
219: return @result;
220: }
221:
222: sub start_window {
223: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
224: my $result = '';
225: if ($target eq 'web') {
226: &Apache::lonxml::startredirection;
227: } elsif ($target eq 'tex') {
228: $result = '\unskip\footnote{';
229: }
230: return $result;
231: }
232:
233: sub end_window {
234: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
235: my $result;
236: if ($target eq 'web') {
237: my $output=&Apache::lonxml::endredirection;
238: $output =~ s/\"/\"\;/g;
239: $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>";
240: } elsif ($target eq 'tex') {
241: $result = '}';
242: } else {
243: $result = '';
244: }
245: return $result;
246: }
247:
248: sub start_import {
249: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
250: my $bodytext=$$parser[$#$parser]->get_text("/import");
251: my $result ="";
252:
253: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
254:
255: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
256: # FIXME this probably needs to be smart about construction vs.
257: # non construction space.
258: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
259: &Apache::lonnet::logthis("$location,$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>