Annotation of rat/lonratsrv.pm, revision 1.21
1.1 www 1: # The LearningOnline Network with CAPA
2: # Server for RAT Maps
3: #
1.21 ! www 4: # $Id: lonratsrv.pm,v 1.20 2002/06/27 17:00:12 albertel Exp $
1.16 www 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: #
1.1 www 28: # (Edit Handler for RAT Maps
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30 Gerd Kortemeyer)
32: # 7/1 Gerd Kortemeyer)
1.7 www 33: # 7/1,7/3,7/4,7/7,7/8,7/10,7/26,10/2 Gerd Kortemeyer
1.8 harris41 34: # 4/30/2001 Scott Harrison
1.15 www 35: # 5/3,06/25,07/03,07/04,07/05 Gerd Kortemeyer
1.1 www 36:
37: package Apache::lonratsrv;
38:
39: use strict;
40: use Apache::Constants qw(:common);
1.2 www 41: use Apache::File;
42: use HTML::TokeParser;
43:
44:
1.4 www 45: # ------------------------------------------------------------- From RAT to XML
1.2 www 46:
47: sub qtescape {
48: my $str=shift;
1.4 www 49: $str=~s/\&\#58\;/\:/g;
50: $str=~s/\&\#39\;/\'/g;
51: $str=~s/\&\#44\;/\,/g;
1.15 www 52: $str=~s/\"/\&\#34\;/g;
1.2 www 53: return $str;
54: }
55:
1.4 www 56: # ------------------------------------------------------------- From XML to RAT
1.2 www 57:
1.4 www 58: sub qtunescape {
1.2 www 59: my $str=shift;
1.14 www 60: $str=~s/\:/\&colon\;/g;
1.4 www 61: $str=~s/\'/\&\#39\;/g;
62: $str=~s/\,/\&\#44\;/g;
63: $str=~s/\"/\&\#34\;/g;
1.2 www 64: return $str;
65: }
66:
67: # --------------------------------------------------------- Loads map from disk
68:
69: sub loadmap {
70: my ($fn,$errtext)=@_;
71: my $outstr='';
72: my @obj=();
73: my @links=();
1.21 ! www 74: my $instr='';
! 75: if ($fn=~/^\/*uploaded\//) {
! 76: $instr=&Apache::lonnet::getfile($fn);
! 77: } elsif (-e $fn) {
! 78: my @content=();
1.2 www 79: {
80: my $fh=Apache::File->new($fn);
81: @content=<$fh>;
82: }
1.21 ! www 83: $instr=join('',@content);
! 84: }
! 85: if ($instr) {
1.2 www 86: my $parser = HTML::TokeParser->new(\$instr);
87: my $token;
88: my $graphmode=0;
89:
90: $fn=~/\.(\w+)$/;
91: $outstr="mode<:>$1";
92:
93: while ($token = $parser->get_token) {
94: if ($token->[0] eq 'S') {
95: if ($token->[1] eq 'map') {
96: $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
97: } elsif ($token->[1] eq 'resource') {
1.3 www 98: # -------------------------------------------------------------------- Resource
99: $outstr.='<&>objcont';
100: if ($token->[2]->{'id'}) {
101: $outstr.='<:>'.$token->[2]->{'id'};
102: if ($obj[$token->[2]->{'id'}]==1) {
103: $errtext.='Error: multiple use of ID '.
104: $token->[2]->{'id'}.'. ';
105: }
106: $obj[$token->[2]->{'id'}]=1;
107: } else {
108: my $i=1;
109: while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
110: $outstr.='<:>'.$i;
111: $obj[$i]=1;
112: }
113: $outstr.='<:>';
1.4 www 114: $outstr.=qtunescape($token->[2]->{'title'}).":";
115: $outstr.=qtunescape($token->[2]->{'src'}).":";
1.14 www 116: if ($token->[2]->{'external'} eq 'true') {
1.4 www 117: $outstr.='true:';
118: } else {
119: $outstr.='false:';
120: }
121: if ($token->[2]->{'type'}) {
122: $outstr.=$token->[2]->{'type'}.':';
123: } else {
124: $outstr.='normal:';
125: }
126: $outstr.='res';
1.2 www 127: } elsif ($token->[1] eq 'condition') {
1.3 www 128: # ------------------------------------------------------------------- Condition
129: $outstr.='<&>objcont';
130: if ($token->[2]->{'id'}) {
131: $outstr.='<:>'.$token->[2]->{'id'};
132: if ($obj[$token->[2]->{'id'}]==1) {
133: $errtext.='Error: multiple use of ID '.
134: $token->[2]->{'id'}.'. ';
135: }
136: $obj[$token->[2]->{'id'}]=1;
137: } else {
138: my $i=1;
139: while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
140: $outstr.='<:>'.$i;
141: $obj[$i]=1;
142: }
143: $outstr.='<:>';
1.4 www 144: $outstr.=qtunescape($token->[2]->{'value'}).':';
145: if ($token->[2]->{'type'}) {
146: $outstr.=$token->[2]->{'type'}.':';
147: } else {
148: $outstr.='normal:';
149: }
150: $outstr.='cond';
1.2 www 151: } elsif ($token->[1] eq 'link') {
1.3 www 152: # ----------------------------------------------------------------------- Links
1.2 www 153: $outstr.='<&>objlinks';
1.7 www 154:
1.3 www 155: if ($token->[2]->{'index'}) {
1.4 www 156: if ($links[$token->[2]->{'index'}]) {
157: $errtext.='Error: multiple use of link index '.
1.3 www 158: $token->[2]->{'index'}.'. ';
1.4 www 159: }
160: $outstr.='<:>'.$token->[2]->{'index'};
161: $links[$token->[2]->{'index'}]=1;
162: } else {
163: my $i=1;
164: while (($i<=$#links) && ($links[$i]==1)) { $i++; }
165: $outstr.='<:>'.$i;
166: $links[$i]=1;
167: }
1.7 www 168:
1.2 www 169: $outstr.='<:>'.$token->[2]->{'from'}.
1.5 www 170: ':'.$token->[2]->{'to'};
1.2 www 171: if ($token->[2]->{'condition'}) {
1.5 www 172: $outstr.=':'.$token->[2]->{'condition'};
1.2 www 173: } else {
1.5 www 174: $outstr.=':0';
1.4 www 175: }
1.11 www 176: # ------------------------------------------------------------------- Parameter
177: } elsif ($token->[1] eq 'param') {
178: $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
1.13 www 179: $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
1.11 www 180: .'___'.$token->[2]->{'value'};
1.2 www 181: } elsif ($graphmode) {
1.3 www 182: # --------------------------------------------- All other tags (graphical only)
183: $outstr.='<&>'.$token->[1];
1.4 www 184: if (defined($token->[2]->{'index'})) {
1.3 www 185: $outstr.='<:>'.$token->[2]->{'index'};
186: if ($token->[1] eq 'obj') {
187: $obj[$token->[2]->{'index'}]=2;
188: }
189: }
190: $outstr.='<:>'.$token->[2]->{'value'};
1.2 www 191: }
192: }
193: }
194:
195: } else {
1.3 www 196: $errtext.='Map not loaded: The file does not exist. ';
1.2 www 197: }
198: return($outstr,$errtext);
199: }
200:
201:
202: # ----------------------------------------------------------- Saves map to disk
203:
204: sub savemap {
1.20 albertel 205: my ($fn,$errtext)=@_;
1.13 www 206: my %alltypes;
207: my %allvalues;
1.7 www 208: if (($fn=~/\.sequence$/) ||
1.2 www 209: ($fn=~/\.page$/)) {
1.4 www 210:
1.2 www 211: # ------------------------------------------------------------- Deal with input
212: my @tags=split(/<&>/,$ENV{'form.output'});
213: my $outstr='';
214: my $graphdef=0;
215: if ($tags[0] eq 'graphdef<:>yes') {
216: $outstr='<map mode="rat/graphical">'."\n";
217: $graphdef=1;
218: } else {
219: $outstr="<map>\n";
220: }
221: map {
222: my @parts=split(/<:>/,$_);
223: if ($parts[0] eq 'objcont') {
224: my @comp=split(/:/,$parts[$#parts]);
225: # --------------------------------------------------------------- Logical input
226: if ($comp[$#comp] eq 'res') {
1.4 www 227: $comp[0]=qtescape($comp[0]);
228: $comp[1]=qtescape($comp[1]);
1.2 www 229: if ($comp[2] eq 'true') {
230: if ($comp[1]!~/^http\:\/\//) {
231: $comp[1]='http://'.$comp[1];
232: }
1.14 www 233: $comp[1].='" external="true';
1.2 www 234: } else {
235: if ($comp[1]=~/^http\:\/\//) {
236: $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
237: }
238: }
239: $outstr.='<resource id="'.$parts[1].'" src="'
1.4 www 240: .$comp[1].'"';
1.2 www 241:
242: if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
243: $outstr.=' type="'.$comp[3].'"';
244: }
245: if ($comp[0] ne '') {
1.4 www 246: $outstr.=' title="'.$comp[0].'"';
1.2 www 247: }
248: $outstr.="></resource>\n";
249: } elsif ($comp[$#comp] eq 'cond') {
250: $outstr.='<condition id="'.$parts[1].'"';
251: if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
252: $outstr.=' type="'.$comp[1].'"';
253: }
254: $outstr.=' value="'.qtescape($comp[0]).'"';
255: $outstr.="></condition>\n";
256: }
257: } elsif ($parts[0] eq 'objlinks') {
258: my @comp=split(/:/,$parts[$#parts]);
259: $outstr.='<link';
260: $outstr.=' from="'.$comp[0].'"';
261: $outstr.=' to="'.$comp[1].'"';
262: if (($comp[2] ne '') && ($comp[2]!=0)) {
263: $outstr.=' condition="'.$comp[2].'"';
264: }
265: $outstr.=' index="'.$parts[1].'"';
266: $outstr.="></link>\n";
1.11 www 267: } elsif ($parts[0] eq 'objparms') {
1.13 www 268: undef %alltypes;
269: undef %allvalues;
1.20 albertel 270: foreach (split(/:/,$parts[$#parts])) {
1.11 www 271: my ($type,$name,$value)=split(/\_\_\_/,$_);
1.13 www 272: $alltypes{$name}=$type;
273: $allvalues{$name}=$value;
1.20 albertel 274: }
275: foreach (keys %allvalues) {
276: if ($allvalues{$_} ne '') {
1.13 www 277: $outstr.='<param to="'.$parts[1].'" type="'
278: .$alltypes{$_}.'" name="'.$_
279: .'" value="'.$allvalues{$_}.'">'
1.12 www 280: ."</param>\n";
1.20 albertel 281: }
282: }
1.2 www 283: } elsif (($parts[0] ne '') && ($graphdef)) {
284: # ------------------------------------------------------------- Graphical input
285: $outstr.='<'.$parts[0];
286: if ($#parts==2) {
287: $outstr.=' index="'.$parts[1].'"';
288: }
289: $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
290: $parts[0].">\n";
291: }
292: } @tags;
293: $outstr.="</map>\n";
1.21 ! www 294: if ($fn=~/^\/*uploaded\//) {
! 295: $ENV{'form.output'}=$outstr;
! 296: &Apache::lonnet::finishuserfileupload(
! 297: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
! 298: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
! 299: $ENV{'course.'.$ENV{'request.course.id'}.'.home'},
! 300: 'output',(split(/\//,$fn))[-1]);
! 301: } else {
1.2 www 302: my $fh;
303: if ($fh=Apache::File->new(">$fn")) {
304: print $fh $outstr;
1.3 www 305: $errtext.="Map saved as $fn. ";
1.2 www 306: } else {
1.17 matthew 307: $errtext.='Could not write file '.$fn.'. Map not saved. ';
1.2 www 308: }
309: }
310: } else {
311: # -------------------------------------------- Cannot write to that file, error
1.20 albertel 312: $errtext.='Map not saved: The specified path does not exist. ';
1.2 www 313: }
314: return $errtext;
315: }
1.1 www 316:
317: # ================================================================ Main Handler
318:
319: sub handler {
320: my $r=shift;
321: $r->content_type('text/html');
322: $r->send_http_header;
323:
324: return OK if $r->header_only;
325:
326: my $url=$r->uri;
1.2 www 327: $url=~/\/(\w+)\/ratserver$/;
328: my $mode=$1;
329:
330: $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
331:
332: my $fn=$r->filename;
1.19 albertel 333: my $lonDocRoot=$r->dir_config('lonDocRoot');
334: if ( $fn =~ /$lonDocRoot/ ) {
335: #internal authentication, needs fixup.
336: $fn = $url;
337: $fn=~s|^/~(\w+)|/home/$1/public_html|;
338: $fn=~s|/[^/]*/ratserver$||;
339: }
1.2 www 340: my $errtext='';
341: my $outtext='';
342:
343: if ($mode ne 'loadonly') {
1.20 albertel 344: $errtext=&savemap($fn,$errtext);
1.2 www 345: }
346: ($outtext,$errtext)=&loadmap($fn,$errtext);
1.1 www 347:
348: $r->print(<<ENDDOCUMENT);
349: <html>
1.8 harris41 350: <body bgcolor="#FFFFFF">
1.2 www 351: <form name=storage method=post action="$url">
352: <input type=hidden name=output value="$outtext">
1.1 www 353: </form>
1.8 harris41 354: <script>
1.9 harris41 355: parent.flag=1;
1.8 harris41 356: </script>
1.2 www 357: ENDDOCUMENT
358: if ($errtext ne '') {
359: $r->print(<<ENDSCRIPT);
360: <script>
361: alert("$errtext");
362: </script>
363: ENDSCRIPT
364: }
365: $r->print("</body>\n</html>\n");
1.1 www 366:
367: return OK;
368: }
369:
370: 1;
371: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>