Annotation of rat/lonuserstate.pm, revision 1.96

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

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