Annotation of rat/lonuserstate.pm, revision 1.121

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

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