Annotation of rat/lonratsrv.pm, revision 1.20

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>