Annotation of rat/lonuserstate.pm, revision 1.88

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

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