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'));
14: }
15:
16: $Apache::scripttag::SCRIPT_RESULT='';
17: sub start_script {
18: my ($target,$token,$parstack,$parser,$safeeval)=@_;
19: my $args ='';
20: if ( $#$parstack > -1 ) {
21: $args=$$parstack[$#$parstack];
22: }
23: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
24: #&Apache::lonxml::debug("found type of $type");
25: $Apache::scripttag::SCRIPT_RESULT='';
26: if ($type eq "loncapa/perl") {
27: $safeeval->share('$SCRIPT_RESULT');
28: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
29:
30: if ( $target eq "modified" ) {
31: }
32: &Apache::run::run($bodytext,$safeeval);
33:
34: if ($target eq "edit" ) {
35: $Apache::scripttag::SCRIPT_RESULT =
36: "<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
37: $Apache::scripttag::SCRIPT_RESULT.=&editfield($token->[1],$bodytext);
38: }
39: } else {
40: $Apache::scripttag::SCRIPT_RESULT = $token->[4];
41: }
42: return $Apache::scripttag::SCRIPT_RESULT;
43: }
44:
45: sub end_script {
46: my ($target,$token,$parstack,$parser,$safeeval)=@_;
47: return $token->[2];
48: }
49:
50: sub start_scriptlib {
51: my ($target,$token,$parstack,$parser,$safeeval)=@_;
52: my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
53: my $result ="";
54:
55: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
56: $$parstack[$#$parstack]);
57: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
58: $bodytext);
59: my $script=&Apache::lonnet::getfile($location);
60: if ($script == -1) {
61: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
62: return "";
63: }
64: &Apache::run::run($script,$safeeval);
65: #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
66:
67: if ($target eq "edit" ) {
68: $result.=&editfield($token->[1],$bodytext);
69: }
70: return $result;
71: }
72:
73: sub end_scriptlib {}
74:
75: sub start_parserlib {
76: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
77: my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
78: my $result ="";
79:
80: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
81: $$parstack[$#$parstack]);
82: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
83: $bodytext);
84: my $styletext=&Apache::lonnet::getfile($location);
85: if ($styletext == -1) {
86: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
87: return "";
88: }
89: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
90:
91: if ($target eq "edit" ) {
92: $result=&editfield($token->[1],$bodytext);
93: }
94: return $result;
95: }
96:
97: sub end_parserlib {
98: }
99:
100: sub start_footnote {
101: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
102: my $result = '';
103: $Apache::lonxml::redirection = 0;
104: return $result;
105: }
106:
107: sub end_footnote {
108: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
109: $Apache::lonxml::outputstack =~ s/\"/\"\;/g;
110: 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>";
111: $Apache::lonxml::outputstack = "";
112: $Apache::lonxml::redirection = 1;
113: return $result;
114: }
115:
116: sub start_import {
117: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
118: my $bodytext=$$parser[$#$parser]->get_text("/import");
119: my $result ="";
120:
121: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
122: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
123: my $file=&Apache::lonnet::getfile($location);
124: if ($file == -1) {
125: &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
126: return "";
127: }
128:
129: my $dir=$location;
130: $dir=~s:/[^/]*$::;
131: &Apache::lonxml::newparser($parser,\$file,$dir);
132:
133: if ($target eq "edit" ) {
134: $result.=&editfield($token->[1],$bodytext);
135: $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
136: }
137: }
138:
139: sub end_import {
140: }
141:
142: sub editfield {
143: my ($tag,$data)=@_;
144:
145: my $count=0;
146: my $maxlength=-1;
147: map { $count++;
148: if (length($_) > $maxlength) { $maxlength = length ($_); }
149: } split ("\n", $data);
150:
151: 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";
152: }
153:
154: sub getfilenothere {
155: my ($filename) = @_;
156: my $a="";
157:
158: $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
159: $filename="/home/httpd/html/res".$filename;
160: if (! -e $filename ) {
161: &Apache::lonnet::subscribe($filename);
162: &Apache::lonnet::repcopy($filename);
163: }
164: if (! -e $filename ) { return -1; };
165: my $fh=Apache::File->new($filename);
166: while (<$fh>) {
167: $a .=$_;
168: }
169: return $a
170: }
171:
172: 1;
173: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>