File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.5: download - view: text, annotated - select for diffs
Wed Jul 12 23:55:33 2000 UTC (23 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug fix, : instead of <:> in links

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # (Edit Handler for RAT Maps
    5: # (TeX Content Handler
    6: #
    7: # 05/29/00,05/30 Gerd Kortemeyer)
    8: # 7/1 Gerd Kortemeyer)
    9: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer
   10: 
   11: package Apache::lonratsrv;
   12: 
   13: use strict;
   14: use Apache::Constants qw(:common);
   15: use Apache::File;
   16: use HTML::TokeParser;
   17: 
   18: 
   19: # ------------------------------------------------------------- From RAT to XML
   20: 
   21: sub qtescape {
   22:     my $str=shift;
   23:     $str=~s/\&\#58\;/\:/g;
   24:     $str=~s/\&\#39\;/\'/g;
   25:     $str=~s/\&\#44\;/\,/g;
   26:     $str=~s/\"/\&\#34\;/g;
   27:     return $str;
   28: }
   29: 
   30: # ------------------------------------------------------------- From XML to RAT
   31: 
   32: sub qtunescape {
   33:     my $str=shift;
   34:     $str=~s/\:/\&\#58\;/g;
   35:     $str=~s/\'/\&\#39\;/g;
   36:     $str=~s/\,/\&\#44\;/g;
   37:     $str=~s/\"/\&\#34\;/g;
   38:     return $str;
   39: }
   40: 
   41: # --------------------------------------------------------- Loads map from disk
   42: 
   43: sub loadmap {
   44:     my ($fn,$errtext)=@_;
   45:     my $outstr='';
   46:     my @content=();
   47:     my @obj=();
   48:     my @links=();
   49:     if (-e $fn) {
   50:         {
   51: 	    my $fh=Apache::File->new($fn);
   52:             @content=<$fh>;
   53:         }
   54:         my $instr=join('',@content);
   55:         my $parser = HTML::TokeParser->new(\$instr);
   56:         my $token;
   57:         my $graphmode=0;
   58: 
   59:         $fn=~/\.(\w+)$/;
   60:         $outstr="mode<:>$1";
   61: 
   62:         while ($token = $parser->get_token) {
   63: 	    if ($token->[0] eq 'S') {
   64:                 if ($token->[1] eq 'map') {
   65: 		    $graphmode=($token->[2]->{'mode'} eq 'rat/graphical');
   66:                 } elsif ($token->[1] eq 'resource') {
   67: # -------------------------------------------------------------------- Resource
   68:                     $outstr.='<&>objcont';
   69:                     if ($token->[2]->{'id'}) {
   70: 			$outstr.='<:>'.$token->[2]->{'id'};
   71:                         if ($obj[$token->[2]->{'id'}]==1) {
   72:                            $errtext.='Error: multiple use of ID '.
   73:                                      $token->[2]->{'id'}.'. ';
   74:                         }
   75:                         $obj[$token->[2]->{'id'}]=1; 
   76:                     } else {
   77:                         my $i=1;
   78:                         while (($i<=$#obj) && ($obj[$i]!=0)) { $i++; }
   79:                         $outstr.='<:>'.$i;
   80:                         $obj[$i]=1;
   81:                     }
   82:                     $outstr.='<:>';
   83:                     $outstr.=qtunescape($token->[2]->{'title'}).":";
   84:                     $outstr.=qtunescape($token->[2]->{'src'}).":";
   85:                     if ($token->[2]->{'src'}=~/\/\//) {
   86:                         $outstr.='true:';
   87:                     } else {
   88:                         $outstr.='false:';
   89:                     }
   90:                     if ($token->[2]->{'type'}) {
   91: 			$outstr.=$token->[2]->{'type'}.':';
   92:                     }  else {
   93:                         $outstr.='normal:';
   94:                     }
   95:                     $outstr.='res';
   96:                 } elsif ($token->[1] eq 'condition') {
   97: # ------------------------------------------------------------------- Condition
   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]->{'value'}).':';
  114:                     if ($token->[2]->{'type'}) {
  115: 			$outstr.=$token->[2]->{'type'}.':';
  116:                     } else {
  117:                         $outstr.='normal:';
  118:                     }
  119:                     $outstr.='cond';
  120:                 } elsif ($token->[1] eq 'link') {
  121: # ----------------------------------------------------------------------- Links
  122:                     $outstr.='<&>objlinks';
  123:                     if ($graphmode) {
  124:                         if ($token->[2]->{'index'}) {
  125: 			   if ($links[$token->[2]->{'index'}]) {
  126:                                $errtext.='Error: multiple use of link index '.
  127: 			       $token->[2]->{'index'}.'. ';
  128:                            }
  129: 			   $outstr.='<:>'.$token->[2]->{'index'};
  130:                            $links[$token->[2]->{'index'}]=1;
  131:                         } else {
  132:                            my $i=1;
  133:                            while (($i<=$#links) && ($links[$i]==1)) { $i++; }
  134:                            $outstr.='<:>'.$i;
  135:                            $links[$i]=1;
  136: 		       }
  137: 		    }
  138:                     $outstr.='<:>'.$token->[2]->{'from'}.
  139:                              ':'.$token->[2]->{'to'};
  140:                     if ($token->[2]->{'condition'}) {
  141: 			$outstr.=':'.$token->[2]->{'condition'};
  142:                     } else {
  143:  			$outstr.=':0';
  144:                     }
  145:                 } elsif ($graphmode) {
  146: # --------------------------------------------- All other tags (graphical only)
  147:                     $outstr.='<&>'.$token->[1];
  148:                     if (defined($token->[2]->{'index'})) {
  149: 			$outstr.='<:>'.$token->[2]->{'index'};
  150:                         if ($token->[1] eq 'obj') {
  151: 			    $obj[$token->[2]->{'index'}]=2;
  152:                         }
  153:                     }
  154:                     $outstr.='<:>'.$token->[2]->{'value'};
  155:                 }
  156:             }
  157:         }
  158: 
  159:     } else {
  160:         $errtext.='Map not loaded: The file does not exist. ';
  161:     }
  162:     return($outstr,$errtext);
  163: }
  164: 
  165: 
  166: # ----------------------------------------------------------- Saves map to disk
  167: 
  168: sub savemap {
  169:     my ($fn,$errtext)=@_;
  170:     if (($fn=~/\.course$/) ||
  171:         ($fn=~/\.sequence$/) ||
  172:         ($fn=~/\.page$/)) {
  173: 
  174: # ------------------------------------------------------------- Deal with input
  175:         my @tags=split(/<&>/,$ENV{'form.output'});
  176:         my $outstr='';
  177:         my $graphdef=0;
  178:         if ($tags[0] eq 'graphdef<:>yes') {
  179: 	    $outstr='<map mode="rat/graphical">'."\n";
  180:             $graphdef=1;
  181:         } else {
  182:             $outstr="<map>\n";
  183:         }
  184:         map {
  185: 	   my @parts=split(/<:>/,$_);
  186:            if ($parts[0] eq 'objcont') {
  187:                my @comp=split(/:/,$parts[$#parts]);
  188: # --------------------------------------------------------------- Logical input
  189: 	       if ($comp[$#comp] eq 'res') {
  190:                    $comp[0]=qtescape($comp[0]);
  191:                    $comp[1]=qtescape($comp[1]);
  192:                    if ($comp[2] eq 'true') {
  193: 		       if ($comp[1]!~/^http\:\/\//) {
  194: 			   $comp[1]='http://'.$comp[1];
  195:                        }
  196:                    } else {
  197: 		       if ($comp[1]=~/^http\:\/\//) {
  198: 			   $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
  199:                        }
  200:                    }
  201: 		   $outstr.='<resource id="'.$parts[1].'" src="'
  202:                           .$comp[1].'"';
  203: 
  204:                    if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
  205: 		       $outstr.=' type="'.$comp[3].'"';
  206:                    }
  207:                    if ($comp[0] ne '') {
  208: 		       $outstr.=' title="'.$comp[0].'"';
  209:                    }
  210:                    $outstr.="></resource>\n";
  211:                } elsif ($comp[$#comp] eq 'cond') {
  212:                    $outstr.='<condition id="'.$parts[1].'"';
  213:                    if (($comp[1] ne '') && ($comp[1] ne 'normal')) {
  214: 		       $outstr.=' type="'.$comp[1].'"';
  215:                    }
  216:                    $outstr.=' value="'.qtescape($comp[0]).'"';
  217:                    $outstr.="></condition>\n";
  218:                }
  219:            } elsif ($parts[0] eq 'objlinks') {
  220:                my @comp=split(/:/,$parts[$#parts]);
  221:                $outstr.='<link';
  222:                $outstr.=' from="'.$comp[0].'"';
  223:                $outstr.=' to="'.$comp[1].'"';
  224:                if (($comp[2] ne '') && ($comp[2]!=0)) {
  225:                   $outstr.=' condition="'.$comp[2].'"';
  226:                }
  227:                $outstr.=' index="'.$parts[1].'"';
  228:                $outstr.="></link>\n";
  229:            } elsif (($parts[0] ne '') && ($graphdef)) {
  230: # ------------------------------------------------------------- Graphical input
  231:                $outstr.='<'.$parts[0];
  232:                if ($#parts==2) {
  233: 		   $outstr.=' index="'.$parts[1].'"';
  234:                }
  235:                $outstr.=' value="'.qtescape($parts[$#parts]).'"></'.
  236:                         $parts[0].">\n";
  237:            }
  238:         } @tags;
  239:         $outstr.="</map>\n";
  240:         {
  241:           my $fh;
  242:           if ($fh=Apache::File->new(">$fn")) {
  243:              print $fh $outstr;
  244:              $errtext.="Map saved as $fn. ";
  245: 	  } else {
  246:              $errtext.='Could not write file $fn. Map not saved. ';
  247: 	  }
  248:         }
  249:     } else {
  250: # -------------------------------------------- Cannot write to that file, error
  251:         $errtext.='Map not saved: The specified path does not exist. ';
  252:     }
  253:     return $errtext;
  254: }
  255: 
  256: # ================================================================ Main Handler
  257: 
  258: sub handler {
  259:   my $r=shift;
  260:   $r->content_type('text/html');
  261:   $r->send_http_header;
  262: 
  263:   return OK if $r->header_only;
  264: 
  265:   my $url=$r->uri;
  266:   $url=~/\/(\w+)\/ratserver$/;
  267:   my $mode=$1;
  268: 
  269:   $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
  270:   
  271:   my $fn=$r->filename;
  272:   my $errtext='';
  273:   my $outtext='';
  274: 
  275:   if ($mode ne 'loadonly') {
  276:      $errtext=&savemap($fn,$errtext);
  277:   }
  278:   ($outtext,$errtext)=&loadmap($fn,$errtext);
  279: 
  280:   $r->print(<<ENDDOCUMENT);
  281: <html>
  282: <body bgcolor="#FFFFFF">
  283: <form name=storage method=post action="$url">
  284: <input type=hidden name=output value="$outtext">
  285: </form>
  286: ENDDOCUMENT
  287:     if ($errtext ne '') {
  288: 	$r->print(<<ENDSCRIPT);
  289: <script>
  290:     alert("$errtext");
  291: </script>
  292: ENDSCRIPT
  293:     }
  294:     $r->print("</body>\n</html>\n");
  295: 
  296:   return OK;
  297: }
  298: 
  299: 1;
  300: __END__
  301: 
  302: 
  303: 
  304: 
  305: 
  306: 
  307: 

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