File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.19: download - view: text, annotated - select for diffs
Thu Nov 16 11:58:30 2000 UTC (23 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Parameter mechanism changes

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

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