File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.20: download - view: text, annotated - select for diffs
Tue Nov 21 20:46:42 2000 UTC (23 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Implementation of "part" in param

    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,11/22 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 $part=$token->[2]->{'part'};
  177:                     unless ($part) { $part=0; }
  178:                     my $newparam=
  179: 			&Apache::lonnet::escape($token->[2]->{'type'}).':'.
  180: 			&Apache::lonnet::escape($part.'.'.
  181:                          $token->[2]->{'name'}).'='.
  182: 			&Apache::lonnet::escape($token->[2]->{'value'});
  183:                     if (defined($hash{'param_'.$referid})) {
  184:                         $hash{'param_'.$referid}.='&'.$newparam;
  185:                     } else {
  186:                         $hash{'param_'.$referid}=''.$newparam;
  187:                     }
  188: 
  189:                 } 
  190: 
  191:             }
  192:         }
  193: 
  194:     } else {
  195:         $errtext.='Map not loaded: The file does not exist. ';
  196:     }
  197: }
  198: 
  199: # --------------------------------------------------------- Simplify expression
  200: 
  201: sub simplify {
  202:    my $expression=shift;
  203: # (8)=8
  204:    $expression=~s/\((\d+)\)/$1/g;
  205: # 8&8=8
  206:    $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
  207: # 8|8=8
  208:    $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
  209: # (5&3)&4=5&3&4
  210:    $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
  211: # (((5&3)|(4&6)))=((5&3)|(4&6))
  212:    $expression=~
  213:        s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
  214: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  215:    $expression=~
  216:        s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
  217:    return $expression;
  218: }
  219: 
  220: # -------------------------------------------------------- Build condition hash
  221: 
  222: sub traceroute {
  223:     my ($sofar,$rid,$beenhere)=@_;
  224:     $sofar=simplify($sofar);
  225:     unless ($beenhere=~/\&$rid\&/) {
  226:        $beenhere.=$rid.'&';  
  227:        if (defined($hash{'conditions_'.$rid})) {
  228: 	   $hash{'conditions_'.$rid}=simplify(
  229:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  230:        } else {
  231:            $hash{'conditions_'.$rid}=$sofar;
  232:        }
  233:        if (defined($hash{'is_map_'.$rid})) {
  234:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  235: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
  236:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
  237: 		   $sofar=
  238:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
  239:                }
  240:            }
  241:        }
  242:        if (defined($hash{'to_'.$rid})) {
  243:           map {
  244: 		my $further=$sofar;
  245:                 if ($hash{'undercond_'.$_}) {
  246: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  247:   		       $further=simplify('('.$further.')&('.
  248:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  249: 		   } else {
  250:                        $errtext.='Undefined condition ID: '
  251:                                  .$hash{'undercond_'.$_}.'. ';
  252:                    }
  253:                 }
  254:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere);
  255:           } split(/\,/,$hash{'to_'.$rid});
  256:        }
  257:     }
  258: }
  259: 
  260: # ------------------------------ Cascading conditions, quick access, parameters
  261: 
  262: sub accinit {
  263:     my ($uri,$short,$fn)=@_;
  264:     my %acchash=();
  265:     my %captured=();
  266:     my $condcounter=0;
  267:     $acchash{'acc.cond.'.$short.'.0'}=0;
  268:     map {
  269:        if ($_=~/^conditions/) {
  270: 	  my $expr=$hash{$_};
  271:           map {
  272:              my $sub=$_;
  273:              my $orig=$_;
  274:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  275:              my $factor=$1;
  276:              $sub=~s/$factor//g;
  277:              $sub=~s/^\(/\($factor\(/;
  278: 	     $sub.=')';
  279:              $sub=simplify($sub);
  280:              $orig=~s/(\W)/\\$1/g;
  281:  	     $expr=~s/$orig/$sub/;
  282: 	  } ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g);
  283:           $hash{$_}=$expr;
  284:           unless (defined($captured{$expr})) {
  285: 	      $condcounter++;
  286:               $captured{$expr}=$condcounter;
  287:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  288:           } 
  289:        } elsif ($_=~/^param_(\d+)\.(\d+)/) {
  290:           my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
  291:       '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
  292:           map {
  293: 	     my ($typename,$value)=split(/\=/,$_);
  294:              my ($type,$name)=split(/\:/,$typename);
  295:              $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
  296:                                    &Apache::lonnet::unescape($value);
  297: 	     $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
  298:                                    &Apache::lonnet::unescape($type);
  299:           } split(/\&/,$hash{$_});
  300:        }
  301:     } keys %hash;
  302:     map {
  303: 	if ($_=~/^ids/) {
  304: 	  map {
  305: 	    my $resid=$_;
  306:             my $uri=$hash{'src_'.$resid};
  307:             my @uriparts=split(/\//,$uri);
  308:             my $urifile=$uriparts[$#uriparts];
  309:             $#uriparts--;
  310:             my $uripath=join('/',@uriparts);
  311:             $uripath=~s/^\/res\///;
  312:             my $uricond='0';
  313:             if (defined($hash{'conditions_'.$resid})) {
  314:  		$uricond=$captured{$hash{'conditions_'.$resid}};
  315:             }
  316:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  317:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  318:                    /(\&$urifile\:[^\&]*)/) {
  319: 		    my $replace=$1;
  320:                     $acchash{'acc.res.'.$short.'.'.$uripath}
  321:                      =~s/$replace/$replace\|$uricond/;
  322:                 } else {
  323: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
  324:                      $urifile.':'.$uricond.'&';
  325: 	        }
  326:             } else {
  327:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
  328:                  '&'.$urifile.':'.$uricond.'&';
  329:             } 
  330:          } split(/\,/,$hash{$_});
  331:       }
  332:     } keys %hash;
  333:     my $courseuri=$uri;
  334:     $courseuri=~s/^\/res\///;
  335:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  336:     &Apache::lonnet::appenv(%acchash,
  337:                             "request.course.id"  => $short,
  338:                             "request.course.fn"  => $fn,
  339:                             "request.course.uri" => $courseuri); 
  340: }
  341: 
  342: # ---------------------------------------------------- Read map and all submaps
  343: 
  344: sub readmap {
  345:    my $short=shift;
  346:    $short=~s/^\///;
  347:    my %cenv=&Apache::lonnet::coursedescription($short);
  348:    my $fn=$cenv{'fn'};
  349:    my $uri;
  350:    $short=~s/\//\_/g;
  351:    unless ($uri=$cenv{'url'}) { 
  352:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  353:                        "Could not load course $short.</font>"); 
  354:       return 'No course data available.';
  355:    }
  356:    @cond=('true:normal');
  357:    unlink($fn.'.db');
  358:    unlink($fn.'_symb.db');
  359:    unlink($fn.'.state');
  360:    unlink($fn.'parms.db');
  361:    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) &&
  362:        (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT,0640))) {
  363:     %hash=();
  364:     %parmhash=();
  365:     $errtext='';
  366:     $pc=0;
  367:     loadmap($uri);
  368:     if (defined($hash{'map_start_'.$uri})) {
  369:         &traceroute('0',$hash{'map_start_'.$uri},'&');
  370:         &accinit($uri,$short,$fn);
  371:     }
  372:     unless ((untie(%hash)) && (untie(%parmhash))) {
  373:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  374:                        "Could not untie coursemap $fn for $uri.</font>"); 
  375:     }
  376:     {
  377:      my $cfh;
  378:      if ($cfh=Apache::File->new(">$fn.state")) {
  379:         print $cfh join("\n",@cond);
  380:      } else {
  381:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  382:                        "Could not write statemap $fn for $uri.</font>"); 
  383:      }
  384:     }  
  385:    } else {
  386:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  387:                        "Could not tie coursemap $fn for $uri.</font>"); 
  388:    }
  389:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
  390:    return $errtext;
  391: }
  392: 
  393: # ------------------------------------------------------- Evaluate state string
  394: 
  395: sub evalstate {
  396:     my $safeeval = new Safe;
  397:     my $fn=$ENV{'request.course.fn'}.'.state';
  398:     my $state='2';
  399:     if (-e $fn) {
  400:        my @conditions=();
  401:        {
  402:         my $fh=Apache::File->new($fn);
  403:         @conditions=<$fh>;
  404:        }  
  405:        $safeeval->permit("entereval");
  406:        $safeeval->permit(":base_math");
  407:        $safeeval->deny(":base_io");
  408:        $safeeval->share_from('Apache::lonnet',['&EXT']);
  409:        map {
  410: 	   my $line=$_;
  411:            chomp($line);
  412: 	   my ($condition,$weight)=split(/\:/,$_);
  413:            if ($safeeval->reval($condition)) {
  414: 	       if ($weight eq 'force') {
  415: 		   $state.='3';
  416:                } else {
  417:                    $state.='2';
  418:                }
  419:            } else {
  420:                if ($weight eq 'stop') {
  421: 		   $state.='0';
  422:                } else {
  423:                    $state.='1';
  424:                }
  425:            }
  426:        } @conditions;
  427:     }
  428:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
  429:     return $state;
  430: }
  431: 
  432: 1;
  433: __END__
  434: 
  435: 
  436: 
  437: 
  438: 
  439: 
  440: 

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