Annotation of rat/lonuserstate.pm, revision 1.118.2.1

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

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