File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Nov 2 17:46:43 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Now calls EXT instead of varval

    1: # The LearningOnline Network with CAPA
    2: # Construct and maintain state and binary representation of course for user
    3: #
    4: # (Server for RAT Maps
    5: #
    6: # (Edit Handler for RAT Maps
    7: # (TeX Content Handler
    8: #
    9: # 05/29/00,05/30 Gerd Kortemeyer)
   10: # 7/1 Gerd Kortemeyer)
   11: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
   12: #
   13: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
   14: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,11/1,11/2 Gerd Kortemeyer
   15: 
   16: package Apache::lonuserstate;
   17: 
   18: use strict;
   19: use Apache::Constants qw(:common :http);
   20: use Apache::File;
   21: use HTML::TokeParser;
   22: use Apache::lonnet();
   23: use GDBM_File;
   24: use Apache::lonmsg;
   25: use Safe;
   26: use Opcode;
   27: 
   28: # ---------------------------------------------------- Globals for this package
   29: 
   30: my $pc;      # Package counter
   31: my %hash;    # The big tied hash
   32: my @cond;    # Array with all of the conditions
   33: my $errtext; # variable with all errors
   34: 
   35: # --------------------------------------------------------- Loads map from disk
   36: 
   37: sub loadmap { 
   38:     my $uri=shift;
   39:     if ($hash{'map_pc_'.$uri}) { return OK; }
   40: 
   41:     $pc++;
   42:     my $lpc=$pc;
   43:     $hash{'map_pc_'.$uri}=$lpc;
   44:     $hash{'map_id_'.$lpc}=$uri;
   45: 
   46:     my $fn='/home/httpd/html'.$uri;
   47: 
   48:     unless (($fn=~/\.sequence$/) ||
   49:             ($fn=~/\.page$/)) { 
   50:        $errtext.="Invalid map: $fn\n";
   51:        return OK; 
   52:     }
   53: 
   54:     unless (-e $fn) {
   55: 	my $returned=Apache::lonnet::repcopy($fn);
   56:         unless ($returned eq OK) {
   57:            $errtext.="Could not import: $fn - ";
   58:            if ($returned eq HTTP_SERVICE_UNAVAILABLE) {
   59: 	      $errtext.="Server unavailable\n";
   60:            }
   61:            if ($returned eq HTTP_NOT_FOUND) {
   62: 	      $errtext.="File not found\n";
   63:            }
   64:            if ($returned eq FORBIDDEN) {
   65: 	      $errtext.="Access forbidden\n";
   66:            }
   67:            return OK;
   68:        }
   69:     }
   70: 
   71:     if (-e $fn) {
   72:         my @content;
   73:         {
   74: 	    my $fh=Apache::File->new($fn);
   75:             @content=<$fh>;
   76:         }
   77:         my $instr=join('',@content);
   78:         my $parser = HTML::TokeParser->new(\$instr);
   79:         my $token;
   80: 
   81:         my $linkpc=0;
   82: 
   83:         $fn=~/\.(\w+)$/;
   84: 
   85:         $hash{'map_type_'.$lpc}=$1;
   86: 
   87:         while ($token = $parser->get_token) {
   88: 	    if ($token->[0] eq 'S') {
   89:                 if ($token->[1] eq 'resource') {
   90: # -------------------------------------------------------------------- Resource
   91: 
   92:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
   93: 
   94:                     $hash{'kind_'.$rid}='res';
   95:                     $hash{'title_'.$rid}=$token->[2]->{'title'};
   96:                     my $turi=$token->[2]->{'src'};
   97:                     $hash{'src_'.$rid}=$turi;
   98: 
   99:                     if (defined($hash{'ids_'.$turi})) {
  100:                         $hash{'ids_'.$turi}.=','.$rid;
  101:                     } else {
  102:                         $hash{'ids_'.$turi}=''.$rid;
  103:                     }
  104: 
  105:                     if ($token->[2]->{'src'}=~/\/\//) {
  106:                         $hash{'ext_'.$rid}='true:';
  107:                     } else {
  108:                         $hash{'ext_'.$rid}='false:';
  109:                     }
  110:                     if ($token->[2]->{'type'}) {
  111: 			$hash{'type_'.$rid}=$token->[2]->{'type'};
  112:                         if ($token->[2]->{'type'} eq 'start') {
  113: 			    $hash{'map_start_'.$uri}="$rid";
  114:                         }
  115:                         if ($token->[2]->{'type'} eq 'finish') {
  116: 			    $hash{'map_finish_'.$uri}="$rid";
  117:                         }
  118:                     }  else {
  119:                         $hash{'type_'.$rid}='normal';
  120:                     }
  121: 
  122:                     if (($turi=~/\.sequence$/) ||
  123:                         ($turi=~/\.page$/)) {
  124:                         $hash{'is_map_'.$rid}=1;
  125:                         &loadmap($turi);
  126:                     } 
  127:                     
  128:                 } elsif ($token->[1] eq 'condition') {
  129: # ------------------------------------------------------------------- Condition
  130: 
  131:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  132: 
  133:                     $hash{'kind_'.$rid}='cond';
  134:                     $cond[$#cond+1]=$token->[2]->{'value'};
  135:                     $hash{'condid_'.$rid}=$#cond;
  136:                     if ($token->[2]->{'type'}) {
  137:                         $cond[$#cond].=':'.$token->[2]->{'type'};
  138:                     }  else {
  139:                         $cond[$#cond].=':normal';
  140:                     }
  141: 
  142:                 } elsif ($token->[1] eq 'link') {
  143: # ----------------------------------------------------------------------- Links
  144: 
  145:                     $linkpc++;
  146:                     my $linkid=$lpc.'.'.$linkpc;
  147: 
  148:                     my $goesto=$lpc.'.'.$token->[2]->{'to'};
  149:                     my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
  150:                     my $undercond=0;
  151: 
  152:                     if ($token->[2]->{'condition'}) {
  153: 			$undercond=$lpc.'.'.$token->[2]->{'condition'};
  154:                     }
  155: 
  156:                     $hash{'goesto_'.$linkid}=$goesto;
  157:                     $hash{'comesfrom_'.$linkid}=$comesfrom;
  158:                     $hash{'undercond_'.$linkid}=$undercond;
  159: 
  160:                     if (defined($hash{'to_'.$comesfrom})) {
  161:                         $hash{'to_'.$comesfrom}.=','.$linkid;
  162:                     } else {
  163:                         $hash{'to_'.$comesfrom}=''.$linkid;
  164:                     }
  165:                     if (defined($hash{'from_'.$goesto})) {
  166:                         $hash{'from_'.$goesto}.=','.$linkid;
  167:                     } else {
  168:                         $hash{'from_'.$goesto}=''.$linkid;
  169:                     }
  170:                 } 
  171: 
  172:             }
  173:         }
  174: 
  175:     } else {
  176:         $errtext.='Map not loaded: The file does not exist. ';
  177:     }
  178: }
  179: 
  180: # --------------------------------------------------------- Simplify expression
  181: 
  182: sub simplify {
  183:    my $expression=shift;
  184: # (8)=8
  185:    $expression=~s/\((\d+)\)/$1/g;
  186: # 8&8=8
  187:    $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
  188: # 8|8=8
  189:    $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
  190: # (5&3)&4=5&3&4
  191:    $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
  192: # (((5&3)|(4&6)))=((5&3)|(4&6))
  193:    $expression=~
  194:        s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
  195: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  196:    $expression=~
  197:        s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
  198:    return $expression;
  199: }
  200: 
  201: # -------------------------------------------------------- Build condition hash
  202: 
  203: sub traceroute {
  204:     my ($sofar,$rid,$beenhere)=@_;
  205:     $sofar=simplify($sofar);
  206:     unless ($beenhere=~/\&$rid\&/) {
  207:        $beenhere.=$rid.'&';  
  208:        if (defined($hash{'conditions_'.$rid})) {
  209: 	   $hash{'conditions_'.$rid}=simplify(
  210:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  211:        } else {
  212:            $hash{'conditions_'.$rid}=$sofar;
  213:        }
  214:        if (defined($hash{'is_map_'.$rid})) {
  215:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  216: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
  217:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
  218: 		   $sofar=
  219:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
  220:                }
  221:            }
  222:        }
  223:        if (defined($hash{'to_'.$rid})) {
  224:           map {
  225: 		my $further=$sofar;
  226:                 if ($hash{'undercond_'.$_}) {
  227: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  228:   		       $further=simplify('('.$further.')&('.
  229:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  230: 		   } else {
  231:                        $errtext.='Undefined condition ID: '
  232:                                  .$hash{'undercond_'.$_}.'. ';
  233:                    }
  234:                 }
  235:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere);
  236:           } split(/\,/,$hash{'to_'.$rid});
  237:        }
  238:     }
  239: }
  240: 
  241: # ------------------------------------------ Cascading conditions, quick access
  242: 
  243: sub accinit {
  244:     my ($uri,$short,$fn)=@_;
  245:     my %acchash=();
  246:     my %captured=();
  247:     my $condcounter=0;
  248:     $acchash{'acc.cond.'.$short.'.0'}=0;
  249:     map {
  250:        if ($_=~/^conditions/) {
  251: 	  my $expr=$hash{$_};
  252:           map {
  253:              my $sub=$_;
  254:              my $orig=$_;
  255:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  256:              my $factor=$1;
  257:              $sub=~s/$factor//g;
  258:              $sub=~s/^\(/\($factor\(/;
  259: 	     $sub.=')';
  260:              $sub=simplify($sub);
  261:              $orig=~s/(\W)/\\$1/g;
  262:  	     $expr=~s/$orig/$sub/;
  263: 	  } ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g);
  264:           $hash{$_}=$expr;
  265:           unless (defined($captured{$expr})) {
  266: 	      $condcounter++;
  267:               $captured{$expr}=$condcounter;
  268:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  269:           } 
  270:         }
  271:     } keys %hash;
  272:     map {
  273: 	if ($_=~/^ids/) {
  274: 	  map {
  275: 	    my $resid=$_;
  276:             my $uri=$hash{'src_'.$resid};
  277:             my @uriparts=split(/\//,$uri);
  278:             my $urifile=$uriparts[$#uriparts];
  279:             $#uriparts--;
  280:             my $uripath=join('/',@uriparts);
  281:             $uripath=~s/^\/res\///;
  282:             my $uricond='0';
  283:             if (defined($hash{'conditions_'.$resid})) {
  284:  		$uricond=$captured{$hash{'conditions_'.$resid}};
  285:             }
  286:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  287:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  288:                    /(\&$urifile\:[^\&]*)/) {
  289: 		    my $replace=$1;
  290:                     $acchash{'acc.res.'.$short.'.'.$uripath}
  291:                      =~s/$replace/$replace\|$uricond/;
  292:                 } else {
  293: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
  294:                      $urifile.':'.$uricond.'&';
  295: 	        }
  296:             } else {
  297:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
  298:                  '&'.$urifile.':'.$uricond.'&';
  299:             } 
  300:          } split(/\,/,$hash{$_});
  301:       }
  302:     } keys %hash;
  303:     my $courseuri=$uri;
  304:     $courseuri=~s/^\/res\///;
  305:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  306:     &Apache::lonnet::appenv(%acchash,
  307:                             "request.course.id"  => $short,
  308:                             "request.course.fn"  => $fn,
  309:                             "request.course.uri" => $courseuri); 
  310: }
  311: 
  312: # ---------------------------------------------------- Read map and all submaps
  313: 
  314: sub readmap {
  315:    my $short=shift;
  316:    $short=~s/^\///;
  317:    my %cenv=&Apache::lonnet::coursedescription($short);
  318:    my $fn=$cenv{'fn'};
  319:    my $uri;
  320:    $short=~s/\//\_/g;
  321:    unless ($uri=$cenv{'url'}) { 
  322:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  323:                        "Could not load course $short.</font>"); 
  324:       return 'No course data available.';
  325:    }
  326:    @cond=('true:normal');
  327:    unlink($fn.'.db');
  328:    unlink($fn.'_symb.db');
  329:    unlink($fn.'.state');
  330:    if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) {
  331:     %hash=();
  332:     $errtext='';
  333:     $pc=0;
  334:     loadmap($uri);
  335:     if (defined($hash{'map_start_'.$uri})) {
  336:         &traceroute('0',$hash{'map_start_'.$uri},'&');
  337:         &accinit($uri,$short,$fn);
  338:     }
  339:     unless (untie(%hash)) {
  340:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  341:                        "Could not untie coursemap $fn for $uri.</font>"); 
  342:     }
  343:     {
  344:      my $cfh;
  345:      if ($cfh=Apache::File->new(">$fn.state")) {
  346:         print $cfh join("\n",@cond);
  347:      } else {
  348:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  349:                        "Could not write statemap $fn for $uri.</font>"); 
  350:      }
  351:     }  
  352:    } else {
  353:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  354:                        "Could not tie coursemap $fn for $uri.</font>"); 
  355:    }
  356:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
  357:    return $errtext;
  358: }
  359: 
  360: # ------------------------------------------------------- Evaluate state string
  361: 
  362: sub evalstate {
  363:     my $safeeval = new Safe;
  364:     my $fn=$ENV{'request.course.fn'}.'.state';
  365:     my $state='2';
  366:     if (-e $fn) {
  367:        my @conditions=();
  368:        {
  369:         my $fh=Apache::File->new($fn);
  370:         @conditions=<$fh>;
  371:        }  
  372:        $safeeval->permit("entereval");
  373:        $safeeval->permit(":base_math");
  374:        $safeeval->deny(":base_io");
  375:        $safeeval->share_from('Apache::lonnet',['&EXT']);
  376:        map {
  377: 	   my $line=$_;
  378:            chomp($line);
  379: 	   my ($condition,$weight)=split(/\:/,$_);
  380:            if ($safeeval->reval($condition)) {
  381: 	       if ($weight eq 'force') {
  382: 		   $state.='3';
  383:                } else {
  384:                    $state.='2';
  385:                }
  386:            } else {
  387:                if ($weight eq 'stop') {
  388: 		   $state.='0';
  389:                } else {
  390:                    $state.='1';
  391:                }
  392:            }
  393:        } @conditions;
  394:     }
  395:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
  396:     return $state;
  397: }
  398: 
  399: 1;
  400: __END__
  401: 
  402: 
  403: 
  404: 
  405: 
  406: 
  407: 

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