Annotation of rat/lonuserstate.pm, revision 1.98

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

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