File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.94: download - view: text, annotated - select for diffs
Thu Jul 14 21:30:24 2005 UTC (18 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- make lonwhats new faster (BUG#4240)

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

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