File:  [LON-CAPA] / rat / lonratsrv.pm
Revision 1.36: download - view: text, annotated - select for diffs
Fri Jul 21 00:21:42 2006 UTC (17 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #4931: clear out IMPORT shopping basket. Maybe overkill, but it seems to
work.

    1: # The LearningOnline Network with CAPA
    2: # Server for RAT Maps
    3: #
    4: # $Id: lonratsrv.pm,v 1.36 2006/07/21 00:21:42 www 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: 
   29: package Apache::lonratsrv;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::File;
   34: use HTML::TokeParser;
   35: use Apache::lonnet;
   36: use Apache::groupsort();
   37: 
   38: # ------------------------------------------------------------- From RAT to XML
   39: 
   40: sub qtescape {
   41:     my $str=shift;
   42:     $str=~s/\:/\:/g;
   43:     $str=~s/\&\#58\;/\:/g;
   44:     $str=~s/\&\#39\;/\'/g;
   45:     $str=~s/\&\#44\;/\,/g;
   46:     $str=~s/\"/\&\#34\;/g;
   47:     return $str;
   48: }
   49: 
   50: # ------------------------------------------------------------- From XML to RAT
   51: 
   52: sub qtunescape {
   53:     my $str=shift;
   54:     $str=~s/\:/\&colon\;/g;
   55:     $str=~s/\'/\&\#39\;/g;
   56:     $str=~s/\,/\&\#44\;/g;
   57:     $str=~s/\"/\&\#34\;/g;
   58:     return $str;
   59: }
   60: 
   61: # --------------------------------------------------------- Loads map from disk
   62: 
   63: sub loadmap {
   64:     my ($fn,$errtext,$infotext)=@_;
   65:     if ($errtext) { return('',$errtext); }
   66:     my $outstr='';
   67:     my @obj=();
   68:     my @links=();
   69:     my $instr='';
   70:     if ($fn=~/^\/*uploaded\//) {
   71:         $instr=&Apache::lonnet::getfile($fn);
   72:     } elsif (-e $fn) {
   73:         my @content=();
   74:         {
   75: 	    my $fh=Apache::File->new($fn);
   76:             @content=<$fh>;
   77:         }
   78:         $instr=join('',@content);
   79:     }
   80:     if ($instr eq -2) {
   81:         $errtext.='Map not loaded: An error occured while trying to load the map.';
   82:     } elsif ($instr) {
   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') {
   95: # -------------------------------------------------------------------- Resource
   96:                     $outstr.='<&>objcont';
   97:                     if (defined($token->[2]->{'id'})) {
   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.='<:>';
  111:                     $outstr.=qtunescape($token->[2]->{'title'}).":";
  112:                     $outstr.=qtunescape($token->[2]->{'src'}).":";
  113:                     if ($token->[2]->{'external'} eq 'true') {
  114:                         $outstr.='true:';
  115:                     } else {
  116:                         $outstr.='false:';
  117:                     }
  118:                     if (defined($token->[2]->{'type'})) {
  119: 			$outstr.=$token->[2]->{'type'}.':';
  120:                     }  else {
  121:                         $outstr.='normal:';
  122:                     }
  123: 		    if ($token->[2]->{'type'} ne 'zombie') {
  124: 			$outstr.='res';
  125: 		    } else {
  126:                         $outstr.='zombie';
  127: 		    }
  128:                 } elsif ($token->[1] eq 'condition') {
  129: # ------------------------------------------------------------------- Condition
  130:                     $outstr.='<&>objcont';
  131:                     if (defined($token->[2]->{'id'})) {
  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.='<:>';
  145:                     $outstr.=qtunescape($token->[2]->{'value'}).':';
  146:                     if (defined($token->[2]->{'type'})) {
  147: 			$outstr.=$token->[2]->{'type'}.':';
  148:                     } else {
  149:                         $outstr.='normal:';
  150:                     }
  151:                     $outstr.='cond';
  152:                 } elsif ($token->[1] eq 'link') {
  153: # ----------------------------------------------------------------------- Links
  154:                     $outstr.='<&>objlinks';
  155: 
  156:                         if (defined($token->[2]->{'index'})) {
  157: 			   if ($links[$token->[2]->{'index'}]) {
  158:                                $errtext.='Error: multiple use of link index '.
  159: 			       $token->[2]->{'index'}.'. ';
  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: 		       }
  169: 		    
  170:                     $outstr.='<:>'.$token->[2]->{'from'}.
  171:                              ':'.$token->[2]->{'to'};
  172:                     if (defined($token->[2]->{'condition'})) {
  173: 			$outstr.=':'.$token->[2]->{'condition'};
  174:                     } else {
  175:  			$outstr.=':0';
  176:                     }
  177: # ------------------------------------------------------------------- Parameter
  178:                 } elsif ($token->[1] eq 'param') {
  179:                     $outstr.='<&>objparms<:>'.$token->[2]->{'to'}.'<:>'.
  180:                             $token->[2]->{'type'}.'___'.$token->[2]->{'name'}
  181:                                                  .'___'.$token->[2]->{'value'};
  182:                 } elsif ($graphmode) {
  183: # --------------------------------------------- All other tags (graphical only)
  184:                     $outstr.='<&>'.$token->[1];
  185:                     if (defined($token->[2]->{'index'})) {
  186: 			$outstr.='<:>'.$token->[2]->{'index'};
  187:                         if ($token->[1] eq 'obj') {
  188: 			    $obj[$token->[2]->{'index'}]=2;
  189:                         }
  190:                     }
  191:                     $outstr.='<:>'.$token->[2]->{'value'};
  192:                 }
  193:             }
  194:         }
  195: 
  196:     } else {
  197:         $errtext.='Map not loaded: The file does not exist. ';
  198:     }
  199:     return($outstr,$errtext,$infotext);
  200: }
  201: 
  202: 
  203: # ----------------------------------------------------------- Saves map to disk
  204: 
  205: sub savemap {
  206:     my ($fn,$errtext)=@_;
  207:     my $infotext='';
  208:     my %alltypes;
  209:     my %allvalues;
  210:     if (($fn=~/\.sequence(\.tmp)*$/) ||
  211:         ($fn=~/\.page(\.tmp)*$/)) {
  212: 
  213: # ------------------------------------------------------------- Deal with input
  214:         my @tags=split(/<&>/,$env{'form.output'});
  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:         }
  223:         foreach (@tags) {
  224: 	   my @parts=split(/<:>/,$_);
  225:            if ($parts[0] eq 'objcont') {
  226:                my @comp=split(/:/,$parts[$#parts]);
  227: # --------------------------------------------------------------- Logical input
  228: 	       if (($comp[$#comp] eq 'res') || ($comp[$#comp] eq 'zombie')) {
  229:                    $comp[0]=qtescape($comp[0]);
  230:                    $comp[1]=qtescape($comp[1]);
  231:                    if ($comp[2] eq 'true') {
  232: 		       if ($comp[1]!~/^http\:\/\//) {
  233: 			   $comp[1]='http://'.$comp[1];
  234:                        }
  235:                        $comp[1].='" external="true';
  236:                    } else {
  237: 		       if ($comp[1]=~/^http\:\/\//) {
  238: 			   $comp[1]=~s/^http\:\/\/[^\/]*\//\//;
  239:                        }
  240:                    }
  241: 		   $outstr.='<resource id="'.$parts[1].'" src="'
  242:                           .$comp[1].'"';
  243: 
  244:                    if (($comp[3] ne '') && ($comp[3] ne 'normal')) {
  245: 		       $outstr.=' type="'.$comp[3].'"';
  246:                    }
  247:                    if ($comp[0] ne '') {
  248: 		       $outstr.=' title="'.$comp[0].'"';
  249:                    }
  250:                    $outstr.=" />\n";
  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]).'"';
  257:                    $outstr.=" />\n";
  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].'"';
  268:                $outstr.=" />\n";
  269:            } elsif ($parts[0] eq 'objparms') {
  270:                undef %alltypes;
  271:                undef %allvalues;
  272:                foreach (split(/:/,$parts[$#parts])) {
  273:                    my ($type,$name,$value)=split(/\_\_\_/,$_);
  274:                    $alltypes{$name}=$type;
  275:                    $allvalues{$name}=$value;
  276:                }
  277:                foreach (keys %allvalues) {
  278:                   if ($allvalues{$_} ne '') {
  279:                    $outstr.='<param to="'.$parts[1].'" type="'
  280:                           .$alltypes{$_}.'" name="'.$_
  281:                           .'" value="'.$allvalues{$_}.'" />'
  282:                           ."\n";
  283: 	          }
  284:                }
  285:            } elsif (($parts[0] ne '') && ($graphdef)) {
  286: # ------------------------------------------------------------- Graphical input
  287:                $outstr.='<'.$parts[0];
  288:                if ($#parts==2) {
  289: 		   $outstr.=' index="'.$parts[1].'"';
  290:                }
  291:                $outstr.=' value="'.qtescape($parts[$#parts]).'" />'."\n";
  292:            }
  293:         }
  294:         $outstr.="</map>\n";
  295: 	if ($fn=~/^\/*uploaded\/(\w+)\/(\w+)\/(.*)$/) {
  296: 	    $env{'form.output'}=$outstr;
  297:             my $result=&Apache::lonnet::finishuserfileupload($2,$1,
  298: 							     'output',$3);
  299: 	    if ($result != m|^/uploaded/|) {
  300: 		$errtext.='Map not saved: A network error occured when trying to save the map. ';
  301: 	    }
  302:         } else {
  303:           my $fh;
  304:           if ($fh=Apache::File->new(">$fn")) {
  305:              print $fh $outstr;
  306:              $infotext.="Map saved as $fn. ";
  307: 	  } else {
  308:              $errtext.='Could not write file '.$fn.'.  Map not saved. ';
  309: 	  }
  310:         }
  311:     } else {
  312: # -------------------------------------------- Cannot write to that file, error
  313:         $errtext.='Map not saved: The specified path does not exist. ';
  314:     }
  315:     &Apache::groupsort::clear_basket();
  316:     return ($errtext,$infotext);
  317: }
  318: 
  319: # ================================================================ Main Handler
  320: 
  321: sub handler {
  322:   my $r=shift;
  323:   &Apache::loncommon::content_type($r,'text/html');
  324:   $r->send_http_header;
  325: 
  326:   return OK if $r->header_only;
  327: 
  328:   my $url=$r->uri;
  329:   $url=~/\/(\w+)\/ratserver$/;
  330:   my $mode=$1;
  331: 
  332:   $url=~s/\/loadonly\/ratserver$/\/save\/ratserver/;
  333:   
  334:   my $fn=$r->filename;
  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:   }
  342:   my $errtext='';
  343:   my $infotext='';
  344:   my $outtext='';
  345: 
  346:   if ($mode ne 'loadonly') {
  347:      ($errtext,$infotext)=&savemap($fn,$errtext);
  348:   }
  349:   ($outtext,$errtext,$infotext)=&loadmap($fn,$errtext,$infotext);
  350: 
  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: 
  358:   $r->print(<<ENDDOCUMENT);
  359: $start_page
  360: <form name="storage" method="post" action="$url">
  361: <input type="hidden" name="output" value="$outtext" />
  362: </form>
  363: <script type ="text/javascript">
  364:     parent.flag=1;
  365: </script>
  366: ENDDOCUMENT
  367:     if (($errtext ne '') || ($infotext ne '')) {
  368: 	$r->print(<<ENDSCRIPT);
  369: <script type="text/javascript">
  370:     alert("$infotext $errtext");
  371: </script>
  372: ENDSCRIPT
  373:     }
  374:     $r->print($end_page);
  375: 
  376:   return OK;
  377: }
  378: 
  379: 1;
  380: __END__

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