File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.107.2.3: download - view: text, annotated - select for diffs
Fri May 12 15:28:50 2006 UTC (17 years, 11 months ago) by matthew
Branches: version_2_1_X
Diff to branchpoint 1.107: preferred, unified
- keep both the old (unversioned) and new (version) keys

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

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