File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.62: download - view: text, annotated - select for diffs
Wed Oct 22 16:52:14 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
At least a few loose ends regarding versioning, unfortunately much more to
come.

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

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