File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.87: download - view: text, annotated - select for diffs
Tue Mar 15 19:25:30 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#3995, could get 'Could not initlize this course' errors when really we were trying to init the course in several different apache children.

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

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