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