File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.63: download - view: text, annotated - select for diffs
Thu Oct 23 21:01:54 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- takes care of more of BUG#55
   - added new parameter to packages .tab and away to associate parameters with only part 0
   - map level parameters work again

    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.63 2003/10/23 21:01:54 albertel 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: # YEAR=2000
   34: # 05/29/00,05/30 Gerd Kortemeyer)
   35: # 7/1 Gerd Kortemeyer)
   36: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
   37: #
   38: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
   39: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
   40: # 11/1,11/2,11/14,11/16,11/22,12/28,
   41: # YEAR=2001
   42: # 07/05/01,08/30,08/31 Gerd Kortemeyer
   43: #
   44: ###
   45: 
   46: package Apache::lonuserstate;
   47: 
   48: # ------------------------------------------------- modules used by this module
   49: use strict;
   50: use Apache::Constants qw(:common :http);
   51: use Apache::File;
   52: use HTML::TokeParser;
   53: use Apache::lonnet();
   54: use Apache::loncommon();
   55: use GDBM_File;
   56: use Apache::lonmsg;
   57: use Safe;
   58: use Safe::Hole;
   59: use Opcode;
   60: 
   61: # ---------------------------------------------------- Globals for this package
   62: 
   63: my $pc;      # Package counter
   64: my %hash;    # The big tied hash
   65: my %parmhash;# The hash with the parameters
   66: my @cond;    # Array with all of the conditions
   67: my $errtext; # variable with all errors
   68: my $retfurl; # variable with the very first URL in the course
   69: my %randompick; # randomly picked resources
   70: my %randompickseed; # optional seed for randomly picking resources
   71: 
   72: # ----------------------------------- Remove version from URL and store in hash
   73: 
   74: sub versiontrack {
   75:     my $uri=shift;
   76:     if ($uri=~/\.(\d+)\.\w+$/) {
   77: 	my $version=$1;
   78: 	$uri=~s/\.\d+\.(\w+)$/\.$1/;
   79:         unless ($hash{'version_'.$uri}) {
   80: 	    $hash{'version_'.$uri}=$version;
   81: 	}
   82:     }
   83:     return $uri;
   84: }
   85: 
   86: # -------------------------------------------------------------- Put in version
   87: 
   88: sub putinversion {
   89:     my $uri=shift;
   90:     if ($hash{'version_'.$uri}) {
   91: 	my $version=$hash{'version_'.$uri};
   92: 	if ($version eq 'current') { return $uri; }
   93: 	$uri=~s/\.(\w+)$/\.$version\.$1/;
   94:     }
   95:     return $uri;
   96: }
   97: 
   98: # ----------------------------------------- Processing versions file for course
   99: 
  100: sub processversionfile {
  101:     my %cenv=shift;
  102:     my %versions=&Apache::lonnet::dump('resourceversions',
  103: 				       $cenv{'domain'},
  104: 				       $cenv{'num'});
  105:     foreach (keys %versions) {
  106: 	if ($_=~/^error\:/) { return; }
  107: 	$hash{'version_'.$_}=$versions{$_};
  108:     }
  109: }
  110: 
  111: # --------------------------------------------------------- Loads map from disk
  112: 
  113: sub loadmap { 
  114:     my $uri=shift;
  115:     if ($hash{'map_pc_'.$uri}) { return OK; }
  116: 
  117:     $pc++;
  118:     my $lpc=$pc;
  119:     $hash{'map_pc_'.$uri}=$lpc;
  120:     $hash{'map_id_'.$lpc}=$uri;
  121: 
  122: # Determine and check filename
  123:     my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
  124: 
  125:     my $ispage=($fn=~/\.page$/);
  126: 
  127:     unless (($fn=~/\.sequence$/) ||
  128:             ($fn=~/\.page$/)) { 
  129:        $errtext.="Invalid map: $fn\n";
  130:        return OK; 
  131:     }
  132: 
  133:     my $instr=&Apache::lonnet::getfile($fn);
  134: 
  135:     unless ($instr eq -1) {
  136: 
  137: # Successfully got file, parse it
  138: 
  139:         my $parser = HTML::TokeParser->new(\$instr);
  140:         my $token;
  141: 
  142:         my $linkpc=0;
  143: 
  144:         $fn=~/\.(\w+)$/;
  145: 
  146:         $hash{'map_type_'.$lpc}=$1;
  147: 
  148:         while ($token = $parser->get_token) {
  149: 	    if ($token->[0] eq 'S') {
  150:                 if ($token->[1] eq 'resource') {
  151: # -------------------------------------------------------------------- Resource
  152: 
  153:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  154: 
  155:                     $hash{'kind_'.$rid}='res';
  156:                     $hash{'title_'.$rid}=$token->[2]->{'title'};
  157:                     my $turi=&versiontrack($token->[2]->{'src'});
  158:                     if ($token->[2]->{'version'}) {
  159: 			unless ($hash{'version_'.$turi}) {
  160: 			    $hash{'version_'.$turi}=$1;
  161: 			}
  162: 		    }
  163: 		    &Apache::lonnet::do_cache(\%Apache::lonnet::titlecache,
  164: 		       &Apache::lonnet::encode_symb($uri,$token->[2]->{'id'},
  165: 						    $turi),
  166: 					      $token->[2]->{'title'},'title');
  167:                     unless ($ispage) {
  168:                         $turi=~/\.(\w+)$/;
  169:                         my $embstyle=&Apache::loncommon::fileembstyle($1);
  170:                         if ($token->[2]->{'external'} eq 'true') { # external
  171:                             $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
  172:                         } elsif ($turi=~/^\/*uploaded\//) { # uploaded
  173: 			    if (($embstyle eq 'img') || ($embstyle eq 'emb')
  174:                              || ($embstyle eq 'ssi')) {
  175:                                 $turi='/adm/wrapper'.$turi;
  176:                             } elsif ($turi!~/\.(sequence|page)$/) {
  177: 				$turi='/adm/coursedocs/showdoc'.$turi;
  178:                             }
  179:                         } else { # normal internal resource
  180:                            if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
  181: 			       $turi='/adm/wrapper'.$turi;
  182:                            }
  183:                         }
  184: 		    }
  185: 
  186:                     if (defined($hash{'ids_'.$turi})) {
  187:                         $hash{'ids_'.$turi}.=','.$rid;
  188:                     } else {
  189:                         $hash{'ids_'.$turi}=''.$rid;
  190:                     }
  191:                
  192:                     if
  193: 	        ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/) {
  194: 			$turi.='?register=1';
  195: 		    }
  196: 
  197:                     $hash{'src_'.$rid}=$turi;
  198: 
  199:                     if ($token->[2]->{'external'} eq 'true') {
  200:                         $hash{'ext_'.$rid}='true:';
  201:                     } else {
  202:                         $hash{'ext_'.$rid}='false:';
  203:                     }
  204:                     if ($token->[2]->{'type'}) {
  205: 			$hash{'type_'.$rid}=$token->[2]->{'type'};
  206:                         if ($token->[2]->{'type'} eq 'start') {
  207: 			    $hash{'map_start_'.$uri}="$rid";
  208:                         }
  209:                         if ($token->[2]->{'type'} eq 'finish') {
  210: 			    $hash{'map_finish_'.$uri}="$rid";
  211:                         }
  212:                     }  else {
  213:                         $hash{'type_'.$rid}='normal';
  214:                     }
  215: 
  216:                     if (($turi=~/\.sequence$/) ||
  217:                         ($turi=~/\.page$/)) {
  218:                         $hash{'is_map_'.$rid}=1;
  219:                         &loadmap($turi);
  220:                     } 
  221:                     
  222:                 } elsif ($token->[1] eq 'condition') {
  223: # ------------------------------------------------------------------- Condition
  224: 
  225:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  226: 
  227:                     $hash{'kind_'.$rid}='cond';
  228:                     $cond[$#cond+1]=$token->[2]->{'value'};
  229:                     $hash{'condid_'.$rid}=$#cond;
  230:                     if ($token->[2]->{'type'}) {
  231:                         $cond[$#cond].=':'.$token->[2]->{'type'};
  232:                     }  else {
  233:                         $cond[$#cond].=':normal';
  234:                     }
  235: 
  236:                 } elsif ($token->[1] eq 'link') {
  237: # ----------------------------------------------------------------------- Links
  238: 
  239:                     $linkpc++;
  240:                     my $linkid=$lpc.'.'.$linkpc;
  241: 
  242:                     my $goesto=$lpc.'.'.$token->[2]->{'to'};
  243:                     my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
  244:                     my $undercond=0;
  245: 
  246:                     if ($token->[2]->{'condition'}) {
  247: 			$undercond=$lpc.'.'.$token->[2]->{'condition'};
  248:                     }
  249: 
  250:                     $hash{'goesto_'.$linkid}=$goesto;
  251:                     $hash{'comesfrom_'.$linkid}=$comesfrom;
  252:                     $hash{'undercond_'.$linkid}=$undercond;
  253: 
  254:                     if (defined($hash{'to_'.$comesfrom})) {
  255:                         $hash{'to_'.$comesfrom}.=','.$linkid;
  256:                     } else {
  257:                         $hash{'to_'.$comesfrom}=''.$linkid;
  258:                     }
  259:                     if (defined($hash{'from_'.$goesto})) {
  260:                         $hash{'from_'.$goesto}.=','.$linkid;
  261:                     } else {
  262:                         $hash{'from_'.$goesto}=''.$linkid;
  263:                     }
  264:                 } elsif ($token->[1] eq 'param') {
  265: # ------------------------------------------------------------------- Parameter
  266: 
  267:                     my $referid=$lpc.'.'.$token->[2]->{'to'};
  268: 		    my $name=$token->[2]->{'name'};
  269: 		    my $part;
  270: 		    if ($name=~/^parameter_(.*)_/) {
  271: 			$part=$1;
  272: 		    } else {
  273: 			$part=0;
  274: 		    }
  275: 		    $name=~s/^.*_([^_]*)$/$1/;
  276:                     my $newparam=
  277: 			&Apache::lonnet::escape($token->[2]->{'type'}).':'.
  278: 			&Apache::lonnet::escape($part.'.'.$name).'='.
  279: 			&Apache::lonnet::escape($token->[2]->{'value'});
  280:                     if (defined($hash{'param_'.$referid})) {
  281:                         $hash{'param_'.$referid}.='&'.$newparam;
  282:                     } else {
  283:                         $hash{'param_'.$referid}=''.$newparam;
  284:                     }
  285:                     if ($token->[2]->{'name'} eq 'parameter_mapalias') {
  286: 			$hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
  287:                     }
  288:                     if ($token->[2]->{'name'} eq 'parameter_randompick') {
  289: 			$randompick{$referid}=$token->[2]->{'value'};
  290:                     }
  291:                     if ($token->[2]->{'name'} eq 'parameter_randompickseed') {
  292: 			$randompick{$referid}=$token->[2]->{'value'};
  293:                     }
  294:                 } 
  295: 
  296:             }
  297:         }
  298: 
  299:     } else {
  300:         $errtext.='Map not loaded: The file does not exist. ';
  301:     }
  302: }
  303: 
  304: # --------------------------------------------------------- Simplify expression
  305: 
  306: sub simplify {
  307:    my $expression=shift;
  308: # (8)=8
  309:    $expression=~s/\((\d+)\)/$1/g;
  310: # 8&8=8
  311:    $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
  312: # 8|8=8
  313:    $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
  314: # (5&3)&4=5&3&4
  315:    $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
  316: # (((5&3)|(4&6)))=((5&3)|(4&6))
  317:    $expression=~
  318:        s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
  319: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  320:    $expression=~
  321:        s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
  322:    return $expression;
  323: }
  324: 
  325: # -------------------------------------------------------- Build condition hash
  326: 
  327: sub traceroute {
  328:     my ($sofar,$rid,$beenhere)=@_;
  329:     $sofar=simplify($sofar);
  330:     unless ($beenhere=~/\&$rid\&/) {
  331:        $beenhere.=$rid.'&';  
  332:        if (($retfurl eq '') && ($hash{'src_'.$rid})
  333:         && ($hash{'src_'.$rid}!~/\.sequence$/)) {
  334:            my ($mapid,$resid)=split(/\./,$rid);
  335:            $retfurl=$hash{'src_'.$rid}.
  336:            (($hash{'src_'.$rid}=~/\?/)?'&':'?').'symb='.
  337:            &Apache::lonnet::symbclean(
  338:                            &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  339:                            '___'.$resid.'___'.
  340:                            &Apache::lonnet::declutter($hash{'src_'.$rid}));
  341:        }
  342:        if (defined($hash{'conditions_'.$rid})) {
  343: 	   $hash{'conditions_'.$rid}=simplify(
  344:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  345:        } else {
  346:            $hash{'conditions_'.$rid}=$sofar;
  347:        }
  348:        if (defined($hash{'is_map_'.$rid})) {
  349:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  350: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
  351:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
  352: 		   $sofar=
  353:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
  354:                }
  355:            }
  356:        }
  357:        if (defined($hash{'to_'.$rid})) {
  358:           foreach (split(/\,/,$hash{'to_'.$rid})) {
  359: 		my $further=$sofar;
  360:                 if ($hash{'undercond_'.$_}) {
  361: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  362:   		       $further=simplify('('.$further.')&('.
  363:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  364: 		   } else {
  365:                        $errtext.='Undefined condition ID: '
  366:                                  .$hash{'undercond_'.$_}.'. ';
  367:                    }
  368:                 }
  369:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere);
  370:           }
  371:        }
  372:     }
  373: }
  374: 
  375: # ------------------------------ Cascading conditions, quick access, parameters
  376: 
  377: sub accinit {
  378:     my ($uri,$short,$fn)=@_;
  379:     my %acchash=();
  380:     my %captured=();
  381:     my $condcounter=0;
  382:     $acchash{'acc.cond.'.$short.'.0'}=0;
  383:     foreach (keys %hash) {
  384:        if ($_=~/^conditions/) {
  385: 	  my $expr=$hash{$_};
  386:          foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
  387:              my $sub=$_;
  388:              my $orig=$_;
  389:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  390:              my $factor=$1;
  391:              $sub=~s/$factor//g;
  392:              $sub=~s/^\(/\($factor\(/;
  393: 	     $sub.=')';
  394:              $sub=simplify($sub);
  395:              $orig=~s/(\W)/\\$1/g;
  396:  	     $expr=~s/$orig/$sub/;
  397: 	  }
  398:           $hash{$_}=$expr;
  399:           unless (defined($captured{$expr})) {
  400: 	      $condcounter++;
  401:               $captured{$expr}=$condcounter;
  402:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  403:           } 
  404:        } elsif ($_=~/^param_(\d+)\.(\d+)/) {
  405:           my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
  406:       '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
  407:           foreach (split(/\&/,$hash{$_})) {
  408: 	     my ($typename,$value)=split(/\=/,$_);
  409:              my ($type,$name)=split(/\:/,$typename);
  410:              $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
  411:                                    &Apache::lonnet::unescape($value);
  412: 	     $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
  413:                                    &Apache::lonnet::unescape($type);
  414:           }
  415:        }
  416:     }
  417:     foreach (keys %hash) {
  418: 	if ($_=~/^ids/) {
  419: 	  foreach (split(/\,/,$hash{$_})) {
  420: 	    my $resid=$_;
  421:             my $uri=$hash{'src_'.$resid};
  422:             $uri=~s/^\/adm\/wrapper//;
  423:             $uri=&Apache::lonnet::declutter($uri);
  424:             my @uriparts=split(/\//,$uri);
  425:             my $urifile=$uriparts[$#uriparts];
  426:             $#uriparts--;
  427:             my $uripath=join('/',@uriparts);
  428:            if ($uripath) {
  429:             my $uricond='0';
  430:             if (defined($hash{'conditions_'.$resid})) {
  431:  		$uricond=$captured{$hash{'conditions_'.$resid}};
  432:             }
  433:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  434:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  435:                    /(\&\Q$urifile\E\:[^\&]*)/) {
  436: 		    my $replace=$1;
  437:                     my $regexp=$replace;
  438:                     $regexp=~s/\|/\\\|/g;
  439:                     $acchash{'acc.res.'.$short.'.'.$uripath}
  440:                      =~s/$regexp/$replace\|$uricond/;
  441:                 } else {
  442: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
  443:                      $urifile.':'.$uricond.'&';
  444: 	        }
  445:             } else {
  446:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
  447:                  '&'.$urifile.':'.$uricond.'&';
  448:             }
  449:            } 
  450:          }
  451:       }
  452:     }
  453:     $acchash{'acc.res.'.$short.'.'}='&:0&';
  454:     my $courseuri=$uri;
  455:     $courseuri=~s/^\/res\///;
  456:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  457:     &Apache::lonnet::appenv(%acchash,
  458:                             "request.course.id"  => $short,
  459:                             "request.course.fn"  => $fn,
  460:                             "request.course.uri" => $courseuri); 
  461: }
  462: 
  463: # ------------------------------------- Selectively delete from randompick maps
  464: 
  465: sub pickrandom {
  466:     my $randomoutentry='';
  467:     foreach my $rid (keys %randompick) {
  468:         my $rndpick=$randompick{$rid};
  469:         my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
  470: # ------------------------------------------- put existing resources into array
  471:         my @currentrids=();
  472:         foreach (sort(keys(%hash))) {
  473: 	    if ($_=~/^src_($mpc\.\d+)/) {
  474: 		if ($hash{'src_'.$1}) { push @currentrids, $1; }
  475:             }
  476:         }
  477: 	# rids are number.number and we want to numercially sort on 
  478:         # the second number
  479: 	@currentrids=sort {
  480: 	    my (undef,$aid)=split(/\./,$a);
  481: 	    my (undef,$bid)=split(/\./,$b);
  482: 	    $aid <=> $bid;
  483: 	} @currentrids;
  484:         next if ($#currentrids<$rndpick);
  485: # -------------------------------- randomly eliminate the ones that should stay
  486: 	my (undef,$id)=split(/\./,$rid);
  487:         if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
  488: 	my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
  489: 	&Apache::lonnet::setup_random_from_rndseed($rndseed);
  490: 	my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
  491:         for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
  492: 	#&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
  493: # -------------------------------------------------------- delete the leftovers
  494:         for (my $k=0; $k<=$#currentrids; $k++) {
  495:             if ($currentrids[$k]) {
  496: 		$hash{'randomout_'.$currentrids[$k]}=1;
  497:                 my ($mapid,$resid)=split(/\./,$currentrids[$k]);
  498:                 $randomoutentry.='&'.
  499:                  &Apache::lonnet::symbclean(
  500: 		    &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  501:                     '___'.$resid.'___'.
  502: 		    &Apache::lonnet::declutter($hash{'src_'.$currentrids[$k]})
  503:                  ).'&';
  504:             }
  505:         }
  506:     }
  507:     if ($randomoutentry) {
  508: 	&Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
  509:     }
  510: }
  511: 
  512: # ---------------------------------------------------- Read map and all submaps
  513: 
  514: sub readmap {
  515:    my $short=shift;
  516:    $short=~s/^\///;
  517:    my %cenv=&Apache::lonnet::coursedescription($short);
  518:    my $fn=$cenv{'fn'};
  519:    my $uri;
  520:    $short=~s/\//\_/g;
  521:    unless ($uri=$cenv{'url'}) { 
  522:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  523:                        "Could not load course $short.</font>"); 
  524:       return 'No course data available.';
  525:    }
  526:    @cond=('true:normal');
  527:    unlink($fn.'.db');
  528:    unlink($fn.'_symb.db');
  529:    unlink($fn.'.state');
  530:    unlink($fn.'parms.db');
  531:    undef %randompick;
  532:    $retfurl='';
  533:    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
  534:        (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
  535:     %hash=();
  536:     %parmhash=();
  537:     $errtext='';
  538:     $pc=0;
  539:     &processversionfile(%cenv);
  540:     my $furi=&Apache::lonnet::clutter($uri);
  541:     $hash{'src_0.0'}=&versiontrack($furi);
  542:     $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
  543:     $hash{'ids_'.$furi}='0.0';
  544:     $hash{'is_map_0.0'}=1;
  545:     loadmap($uri);
  546:     if (defined($hash{'map_start_'.$uri})) {
  547:         &traceroute('0',$hash{'map_start_'.$uri},'&');
  548:         &accinit($uri,$short,$fn);
  549:         &pickrandom();
  550:     }
  551: # ------------------------------------------------------- Put versions into src
  552:     foreach (keys %hash) {
  553: 	if ($_=~/^src\_/) {
  554: 	    $hash{$_}=&putinversion($hash{$_});
  555: 	}
  556:     }
  557:     unless ((untie(%hash)) && (untie(%parmhash))) {
  558:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  559:                        "Could not untie coursemap $fn for $uri.</font>"); 
  560:     }
  561:     {
  562:      my $cfh;
  563:      if ($cfh=Apache::File->new(">$fn.state")) {
  564:         print $cfh join("\n",@cond);
  565:      } else {
  566:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  567:                        "Could not write statemap $fn for $uri.</font>"); 
  568:      }
  569:     }  
  570:    } else {
  571:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  572:                        "Could not tie coursemap $fn for $uri.</font>"); 
  573:    }
  574:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
  575: # ------------------------------------------------- Check for critical messages
  576: 
  577:     my @what=&Apache::lonnet::dump('critical',$ENV{'user.domain'},
  578:                                               $ENV{'user.name'});
  579:     if ($what[0]) {
  580: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  581: 	    $retfurl='/adm/email?critical=display';
  582:         }
  583:     }
  584:    return ($retfurl,$errtext);
  585: }
  586: 
  587: # ------------------------------------------------------- Evaluate state string
  588: 
  589: sub evalstate {
  590: 
  591:     my $fn=$ENV{'request.course.fn'}.'.state';
  592:     my $state='2';
  593:     if (-e $fn) {
  594:        my @conditions=();
  595:        {
  596:         my $fh=Apache::File->new($fn);
  597:         @conditions=<$fh>;
  598:        }  
  599:        my $safeeval = new Safe;
  600:        my $safehole = new Safe::Hole;
  601:        $safeeval->permit("entereval");
  602:        $safeeval->permit(":base_math");
  603:        $safeeval->deny(":base_io");
  604:        $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  605:        foreach (@conditions) {
  606: 	   my $line=$_;
  607:            chomp($line);
  608: 	   my ($condition,$weight)=split(/\:/,$_);
  609:            if ($safeeval->reval($condition)) {
  610: 	       if ($weight eq 'force') {
  611: 		   $state.='3';
  612:                } else {
  613:                    $state.='2';
  614:                }
  615:            } else {
  616:                if ($weight eq 'stop') {
  617: 		   $state.='0';
  618:                } else {
  619:                    $state.='1';
  620:                }
  621:            }
  622:        }
  623:     }
  624:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
  625:     return $state;
  626: }
  627: 
  628: 1;
  629: __END__
  630: 
  631: =head1 NAME
  632: 
  633: Apache::lonuserstate - Construct and maintain state and binary representation
  634: of course for user
  635: 
  636: =head1 SYNOPSIS
  637: 
  638: Invoked by lonroles.pm.
  639: 
  640: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  641: 
  642: =head1 INTRODUCTION
  643: 
  644: This module constructs and maintains state and binary representation
  645: of course for user.
  646: 
  647: This is part of the LearningOnline Network with CAPA project
  648: described at http://www.lon-capa.org.
  649: 
  650: =head1 HANDLER SUBROUTINE
  651: 
  652: There is no handler subroutine.
  653: 
  654: =head1 OTHER SUBROUTINES
  655: 
  656: =over 4
  657: 
  658: =item *
  659: 
  660: loadmap() : Loads map from disk
  661: 
  662: =item *
  663: 
  664: simplify() : Simplify expression
  665: 
  666: =item *
  667: 
  668: traceroute() : Build condition hash
  669: 
  670: =item *
  671: 
  672: accinit() : Cascading conditions, quick access, parameters
  673: 
  674: =item *
  675: 
  676: readmap() : Read map and all submaps
  677: 
  678: =item *
  679: 
  680: evalstate() : Evaluate state string
  681: 
  682: =back
  683: 
  684: =cut

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