1: # The LearningOnline Network with CAPA
2: # <script> definiton
3:
4:
5: package Apache::scripttag;
6:
7: use strict;
8: use Apache::lonnet;
9:
10: sub BEGIN {
11: &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
12: 'parserlib','import',
13: 'footnote','display'));
14: }
15:
16: sub start_script {
17: my ($target,$token,$parstack,$parser,$safeeval)=@_;
18: my $args ='';
19: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
20: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
21: my $result='';
22: #&Apache::lonxml::debug("found type of $type");
23: if ($type eq "loncapa/perl") {
24: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
25:
26: if ( $target eq "modified" ) {
27:
28: } elsif ( $target eq "web" || $target eq "grade" ) {
29: &Apache::run::run($bodytext,$safeeval);
30: } elsif ($target eq "edit" ) {
31: &Apache::run::run($bodytext,$safeeval);
32: $result="<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
33: $result.=&editfield($token->[1],$bodytext);
34: }
35: } else {
36: if ($target ne "meta") { $result = $token->[4]; }
37: }
38: return $result;
39: }
40:
41: sub end_script {
42: my ($target,$token,$parstack,$parser,$safeeval)=@_;
43: if ( $target eq "meta" ) { return ''; }
44: my $args ='';
45: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
46: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
47: my $result='';
48: #other script blocks need to survive
49: if ($type ne "loncapa/perl") { return $token->[2]; }
50: return '';
51: }
52:
53: sub start_display {
54: my ($target,$token,$parstack,$parser,$safeeval)=@_;
55: my $args ='';
56: if ( $#$parstack > -1 ) {
57: $args=$$parstack[$#$parstack];
58: }
59: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
60:
61: if ( $target eq "modified" ) {
62: }
63: my $result=&Apache::run::run($bodytext,$safeeval);
64: if ($target eq 'grade' ) {
65: # grade should produce no output
66: $result='';
67: }
68: if ($target eq "edit" ) {
69: $result =
70: "<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
71: $result.=&editfield($token->[1],$bodytext);
72: }
73: return $result;
74: }
75:
76: sub end_display {
77: }
78:
79: sub start_scriptlib {
80: my ($target,$token,$parstack,$parser,$safeeval)=@_;
81: my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
82: my $result ="";
83:
84: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
85: $$parstack[$#$parstack]);
86: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
87: $bodytext);
88: my $script=&Apache::lonnet::getfile($location);
89: if ($script == -1) {
90: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
91: return "";
92: }
93: &Apache::run::run($script,$safeeval);
94: #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
95:
96: if ($target eq "edit" ) {
97: $result.=&editfield($token->[1],$bodytext);
98: }
99: return $result;
100: }
101:
102: sub end_scriptlib {}
103:
104: sub start_parserlib {
105: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
106: my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
107: my $result ="";
108:
109: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
110: $$parstack[$#$parstack]);
111: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
112: $bodytext);
113: my $styletext=&Apache::lonnet::getfile($location);
114: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
115: if ($styletext == -1) {
116: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
117: return "";
118: }
119: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
120:
121: if ($target eq "edit" ) {
122: $result=&editfield($token->[1],$bodytext);
123: }
124: return $result;
125: }
126:
127: sub end_parserlib {
128: }
129:
130: sub start_footnote {
131: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
132: my $result = '';
133: $Apache::lonxml::redirection = 0;
134: return $result;
135: }
136:
137: sub end_footnote {
138: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
139: $Apache::lonxml::outputstack =~ s/\"/\"\;/g;
140: my $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor="#FFFFFF"> $Apache::lonxml::outputstack </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
141: $Apache::lonxml::outputstack = "";
142: $Apache::lonxml::redirection = 1;
143: return $result;
144: }
145:
146: sub start_import {
147: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
148: my $bodytext=$$parser[$#$parser]->get_text("/import");
149: my $result ="";
150:
151: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
152: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
153: my $file=&Apache::lonnet::getfile($location);
154: if ($file == -1) {
155: &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
156: return "";
157: }
158:
159: my $dir=$location;
160: $dir=~s:/[^/]*$::;
161: &Apache::lonxml::newparser($parser,\$file,$dir);
162:
163: if ($target eq "edit" ) {
164: $result.=&editfield($token->[1],$bodytext);
165: $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
166: }
167: return '';
168: }
169:
170: sub end_import {
171: }
172:
173: sub editfield {
174: my ($tag,$data)=@_;
175:
176: my $count=0;
177: my $maxlength=-1;
178: map { $count++;
179: if (length($_) > $maxlength) { $maxlength = length ($_); }
180: } split ("\n", $data);
181:
182: return "<br></br>\n<$tag><br></br>\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br></br>\n</$tag><br></br>\n";
183: }
184:
185: sub getfilenothere {
186: my ($filename) = @_;
187: my $a="";
188:
189: $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
190: $filename="/home/httpd/html/res".$filename;
191: if (! -e $filename ) {
192: &Apache::lonnet::subscribe($filename);
193: &Apache::lonnet::repcopy($filename);
194: }
195: if (! -e $filename ) { return -1; };
196: my $fh=Apache::File->new($filename);
197: while (<$fh>) {
198: $a .=$_;
199: }
200: return $a
201: }
202:
203: 1;
204: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>