File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.25: download - view: text, annotated - select for diffs
Thu Nov 29 19:23:49 2001 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
GPL

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

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