File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.15: download - view: text, annotated - select for diffs
Wed Nov 1 22:21:36 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
New function to calculate state string in environment.

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

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