File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.24: download - view: text, annotated - select for diffs
Mon Feb 3 18:03:53 2003 UTC (21 years, 3 months ago) by harris41
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
best wishes to all.

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

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