Annotation of rat/lonuserstate.pm, revision 1.101

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

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