1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.72 2002/04/25 17:37:23 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: my $file=&Apache::lonnet::getfile($location);
260: if ($file == -1) {
261: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
262: return "";
263: }
264:
265: my $dir=$location;
266: $dir=~s:/[^/]*$::;
267: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
268: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
269: if (!$id) { $id=$Apache::lonxml::curdepth; }
270: push(@Apache::inputtags::import,$id);
271: &Apache::lonxml::newparser($parser,\$file,$dir);
272: } elsif ($target eq "edit" ) {
273: $result.=&Apache::edit::tag_start($target,$token);
274: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
275: #FIXME this need to convert $bodytext to be a contruction space reference
276: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
277: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
278: } elsif ($target eq 'modified') {
279: $bodytext=$$parser[$#$parser]->get_text("/import");
280: $result=$token->[4].&Apache::edit::modifiedfield($token);
281: &Apache::lonxml::debug($result);
282: } elsif ($target eq 'meta') {
283: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
284: $result.='<import part="'.$Apache::inputtags::part;
285: if ($id) {
286: $result.='" id="'.$id;
287: }
288: $result.='">';
289: $result.=$bodytext;
290: $result.='</import>';
291: }
292: return $result;
293: }
294:
295: sub end_import {
296: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
297: pop(@Apache::inputtags::import);
298: my $result;
299: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
300: return $result;
301: }
302:
303: sub start_storetc {
304: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
305: my $result = '';
306: &Apache::lonxml::startredirection;
307: return $result;
308: }
309:
310: sub end_storetc {
311: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
312: my $result;
313: my $output=&Apache::lonxml::endredirection;
314: $output =~ s/\"/\"\;/g;
315: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
316: return $result;
317: }
318:
319:
320: sub start_physnet {
321: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
322: my $bodytext = '/adm/includes/physnet.sty';
323: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
324: my $cbistyletext=&Apache::lonnet::getfile($location);
325:
326: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
327: $$parser['-1']->unget_token($token);
328: # if ( defined($$style{'physnet'}) ) {
329: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
330: # }
331: return "";
332: }
333:
334: sub end_physnet {
335: return '';
336: }
337:
338: sub start_standalone {
339: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
340: if ($target eq 'web' && $ENV{'request.course.id'} ) {
341: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
342: }
343: return '';
344: }
345:
346: sub end_standalone {
347: return '';
348: }
349:
350:
351: sub xmlparse {
352: my ($string) = @_;
353: &Apache::lonxml::debug("Got $string");
354: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
355: @Apache::scripttag::parser_env;
356: my @parser;
357: &Apache::lonxml::newparser(\@parser,\$string);
358: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
359: $parstack,\@parser,
360: $safeeval,$style);
361: return $result;
362: }
363:
364: 1;
365: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>