Annotation of rat/lonuserstate.pm, revision 1.149.2.3

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Construct and maintain state and binary representation of course for user
                      3: #
1.149.2.3! raeburn     4: # $Id: lonuserstate.pm,v 1.149.2.2 2019/02/07 01:51:34 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;
1.138     foxr       46: use File::Basename;
                     47: 
1.114     www        48:  
1.15      www        49: 
1.1       www        50: # ---------------------------------------------------- Globals for this package
                     51: 
1.138     foxr       52: my $pc;      # Package counter is this what 'Guts' calls the map counter?
1.1       www        53: my %hash;    # The big tied hash
1.19      www        54: my %parmhash;# The hash with the parameters
1.1       www        55: my @cond;    # Array with all of the conditions
                     56: my $errtext; # variable with all errors
1.116     www        57: my $retfrid; # variable with the very first RID in the course
                     58: my $retfurl; # first URL
1.29      www        59: my %randompick; # randomly picked resources
1.51      www        60: my %randompickseed; # optional seed for randomly picking resources
1.124     albertel   61: my %randomorder; # maps to order contents randomly
1.146     raeburn    62: my %randomizationcode; # code used to grade folder for bubblesheet exam 
1.73      www        63: my %encurl; # URLs in this folder are supposed to be encrypted
                     64: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
1.149.2.2  raeburn    65: my %rescount; # count of unhidden items in each map
                     66: my %mapcount; # count of unhidden maps in each map
1.61      www        67: 
                     68: # ----------------------------------- Remove version from URL and store in hash
                     69: 
1.134     www        70: sub versionerror {
                     71:     my ($uri,$usedversion,$unusedversion)=@_;
                     72:     return '<br />'.&mt('Version discrepancy: resource [_1] included in both version [_2] and version [_3]. Using version [_2].',
                     73:                     $uri,$usedversion,$unusedversion).'<br />';
                     74: }
                     75: 
1.139     foxr       76: #  Removes the version number from a URI and returns the resulting
                     77: #  URI (e.g. mumbly.version.stuff => mumbly.stuff).
                     78: #
                     79: #   If the URI has not been seen with a versio before the
                     80: #   hash{'version_'.resultingURI} is set to the  version number.
                     81: #   If the URI has been seen and the version does not match and error
                     82: #   is added to the error string.
                     83: #
                     84: # Parameters:
                     85: #   URI potentially with a version.
                     86: # Returns:
                     87: #   URI with the version cut out.
                     88: # See above for side effects.
                     89: #
                     90: 
1.61      www        91: sub versiontrack {
                     92:     my $uri=shift;
                     93:     if ($uri=~/\.(\d+)\.\w+$/) {
                     94: 	my $version=$1;
                     95: 	$uri=~s/\.\d+\.(\w+)$/\.$1/;
1.62      www        96:         unless ($hash{'version_'.$uri}) {
                     97: 	    $hash{'version_'.$uri}=$version;
1.134     www        98: 	} elsif ($version!=$hash{'version_'.$uri}) {
                     99:             $errtext.=&versionerror($uri,$hash{'version_'.$uri},$version);
                    100:         }
1.61      www       101:     }
                    102:     return $uri;
                    103: }
                    104: 
                    105: # -------------------------------------------------------------- Put in version
                    106: 
                    107: sub putinversion {
                    108:     my $uri=shift;
1.93      www       109:     my $key=$env{'request.course.id'}.'_'.&Apache::lonnet::clutter($uri);
1.61      www       110:     if ($hash{'version_'.$uri}) {
                    111: 	my $version=$hash{'version_'.$uri};
1.65      www       112: 	if ($version eq 'mostrecent') { return $uri; }
1.66      www       113: 	if ($version eq &Apache::lonnet::getversion(
                    114: 			&Apache::lonnet::filelocation('',$uri))) 
                    115: 	             { return $uri; }
1.61      www       116: 	$uri=~s/\.(\w+)$/\.$version\.$1/;
                    117:     }
1.93      www       118:     &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
1.61      www       119:     return $uri;
                    120: }
                    121: 
                    122: # ----------------------------------------- Processing versions file for course
                    123: 
                    124: sub processversionfile {
1.64      www       125:     my %cenv=@_;
1.61      www       126:     my %versions=&Apache::lonnet::dump('resourceversions',
                    127: 				       $cenv{'domain'},
                    128: 				       $cenv{'num'});
1.106     albertel  129:     foreach my $ver (keys(%versions)) {
                    130: 	if ($ver=~/^error\:/) { return; }
                    131: 	$hash{'version_'.$ver}=$versions{$ver};
1.61      www       132:     }
                    133: }
1.45      www       134: 
1.138     foxr      135: # --------------------------------------------------------- Loads from disk
1.1       www       136: 
1.140     foxr      137: 
                    138: #
                    139: #  Loads a map file.
                    140: #  Note that this may implicitly recurse via parse_resource if one of the resources
                    141: #  is itself composed.
                    142: #
                    143: # Parameters:
                    144: #    uri         - URI of the map file.
                    145: #    parent_rid  - Resource id in the map of the parent resource (0.0 for the top level map)
1.146     raeburn   146: #    courseid    - Course id for the course for which the map is being loaded
1.140     foxr      147: #
1.1       www       148: sub loadmap { 
1.146     raeburn   149:     my ($uri,$parent_rid,$courseid)=@_;
1.138     foxr      150: 
                    151:     # Is the map already included?
                    152: 
1.114     www       153:     if ($hash{'map_pc_'.$uri}) { 
1.120     albertel  154: 	$errtext.='<p class="LC_error">'.
                    155: 	    &mt('Multiple use of sequence/page [_1]! The course will not function properly.','<tt>'.$uri.'</tt>').
                    156: 	    '</p>';
1.114     www       157: 	return; 
                    158:     }
1.138     foxr      159:     # Register the resource in it's map_pc_ [for the URL]
                    160:     # map_id.nnn is the nesting level -> to the URI.
                    161: 
1.1       www       162:     $pc++;
                    163:     my $lpc=$pc;
                    164:     $hash{'map_pc_'.$uri}=$lpc;
                    165:     $hash{'map_id_'.$lpc}=$uri;
1.138     foxr      166: 
                    167:     # If the parent is of the form n.m hang this map underneath it in the
                    168:     # map hierarchy.
                    169: 
1.136     raeburn   170:     if ($parent_rid =~ /^(\d+)\.\d+$/) {
                    171:         my $parent_pc = $1;
                    172:         if (defined($hash{'map_hierarchy_'.$parent_pc})) {
                    173:             $hash{'map_hierarchy_'.$lpc}=$hash{'map_hierarchy_'.$parent_pc}.','.
                    174:                                          $parent_pc;
                    175:         } else {
                    176:             $hash{'map_hierarchy_'.$lpc}=$parent_pc;
                    177:         }
                    178:     }
1.1       www       179: 
1.138     foxr      180: # Determine and check filename of the sequence we need to read:
                    181: 
1.62      www       182:     my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
1.37      www       183: 
                    184:     my $ispage=($fn=~/\.page$/);
1.1       www       185: 
1.138     foxr      186:     # We can only nest sequences or pages.  Anything else is an illegal nest.
                    187: 
                    188:     unless (($fn=~/\.sequence$/) || $ispage) { 
1.147     raeburn   189: 	$errtext.='<br />'.&mt('Invalid map: [_1]',"<tt>$fn</tt>");
1.98      albertel  190: 	return; 
1.1       www       191:     }
                    192: 
1.138     foxr      193:     # Read the XML that constitutes the file.
                    194: 
1.37      www       195:     my $instr=&Apache::lonnet::getfile($fn);
                    196: 
1.124     albertel  197:     if ($instr eq -1) {
1.147     raeburn   198:         $errtext.= '<br />'
                    199:                   .&mt('Map not loaded: The file [_1] does not exist.',
                    200:                        "<tt>$fn</tt>");
1.124     albertel  201: 	return;
                    202:     }
1.22      www       203: 
1.138     foxr      204:     # Successfully got file, parse it
                    205: 
                    206:     # parse for parameter processing.
                    207:     # Note that these are <param... / > tags
                    208:     # so we only care about 'S' (tag start) nodes.
                    209: 
1.1       www       210: 
1.124     albertel  211:     my $parser = HTML::TokeParser->new(\$instr);
                    212:     $parser->attr_encoded(1);
1.138     foxr      213: 
1.124     albertel  214:     # first get all parameters
1.138     foxr      215: 
                    216: 
1.124     albertel  217:     while (my $token = $parser->get_token) {
                    218: 	next if ($token->[0] ne 'S');
                    219: 	if ($token->[1] eq 'param') {
                    220: 	    &parse_param($token,$lpc);
                    221: 	} 
                    222:     }
1.138     foxr      223: 
                    224:     # Get set to take another pass through the XML:
                    225:     # for resources and links.
                    226: 
1.124     albertel  227:     $parser = HTML::TokeParser->new(\$instr);
                    228:     $parser->attr_encoded(1);
1.1       www       229: 
1.124     albertel  230:     my $linkpc=0;
1.1       www       231: 
1.124     albertel  232:     $fn=~/\.(\w+)$/;
1.1       www       233: 
1.124     albertel  234:     $hash{'map_type_'.$lpc}=$1;
1.1       www       235: 
1.124     albertel  236:     my $randomize = ($randomorder{$parent_rid} =~ /^yes$/i);
1.1       www       237: 
1.138     foxr      238:     # Parse the resources, link and condition tags.
                    239:     # Note that if randomorder or random select is chosen the links and
                    240:     # conditions are meaningless but are determined by the randomization.
                    241:     # This is handled in the next chunk of code.
                    242: 
1.124     albertel  243:     my @map_ids;
1.146     raeburn   244:     my $codechecked;
1.149.2.2  raeburn   245:     $rescount{$lpc} = 0;
                    246:     $mapcount{$lpc} = 0;
1.124     albertel  247:     while (my $token = $parser->get_token) {
                    248: 	next if ($token->[0] ne 'S');
1.138     foxr      249: 
                    250: 	# Resource
                    251: 
1.124     albertel  252: 	if ($token->[1] eq 'resource') {
1.146     raeburn   253: 	    my $resource_id = &parse_resource($token,$lpc,$ispage,$uri,$courseid);
1.138     foxr      254: 	    if (defined $resource_id) {
1.146     raeburn   255: 		push(@map_ids, $resource_id);
1.149.2.2  raeburn   256:                 if ($hash{'src_'.$lpc.'.'.$resource_id}) {
                    257:                     $rescount{$lpc} ++;
                    258:                     if (($hash{'src_'.$lpc.'.'.$resource_id}=~/\.sequence$/) ||
                    259:                         ($hash{'src_'.$lpc.'.'.$resource_id}=~/\.page$/)) {
                    260:                         $mapcount{$lpc} ++;
                    261:                     }
                    262:                 }
1.146     raeburn   263:                 unless ($codechecked) {
                    264:                     my $startsymb =
                    265:                        &Apache::lonnet::encode_symb($hash{'map_id_'.$lpc},$resource_id,
                    266:                                                     $hash{'src_'."$lpc.$resource_id"});
                    267:                     my $code = 
                    268:                         &Apache::lonnet::EXT('resource.0.examcode',$startsymb,undef,undef,
                    269:                                              undef,undef,$courseid);
                    270:                     if ($code) {
                    271:                         $randomizationcode{$parent_rid} = $code;
                    272:                     }
                    273:                     $codechecked = 1; 
                    274:                 }
1.138     foxr      275: 	    }
                    276: 
                    277:        # Link
                    278: 
1.124     albertel  279: 	} elsif ($token->[1] eq 'link' && !$randomize) {
                    280: 	    &make_link(++$linkpc,$lpc,$token->[2]->{'to'},
                    281: 		       $token->[2]->{'from'},
1.139     foxr      282: 		       $token->[2]->{'condition'}); # note ..condition may be undefined.
1.138     foxr      283: 
                    284: 	# condition
                    285: 
1.124     albertel  286: 	} elsif ($token->[1] eq 'condition' && !$randomize) {
                    287: 	    &parse_condition($token,$lpc);
                    288: 	}
                    289:     }
1.146     raeburn   290:     undef($codechecked);
1.1       www       291: 
1.138     foxr      292:     # Handle randomization and random selection
                    293: 
1.124     albertel  294:     if ($randomize) {
1.148     raeburn   295:         my $advanced;
                    296:         if ($env{'request.course.id'}) {
                    297:             $advanced = (&Apache::lonnet::allowed('adv') eq 'F');
                    298:         } else {
                    299:             $env{'request.course.id'} = $courseid;
                    300:             $advanced = (&Apache::lonnet::allowed('adv') eq 'F');
                    301:             $env{'request.course.id'} = '';
                    302:         }
                    303:         unless ($advanced) {
                    304:             # Order of resources is not randomized if user has and advanced role in the course.
1.124     albertel  305: 	    my $seed;
1.139     foxr      306: 
1.148     raeburn   307:             # If the map's random seed parameter has been specified
                    308:             # it is used as the basis for computing the seed ...
1.139     foxr      309: 
1.124     albertel  310: 	    if (defined($randompickseed{$parent_rid})) {
                    311: 		$seed = $randompickseed{$parent_rid};
                    312: 	    } else {
1.139     foxr      313: 
                    314: 		# Otherwise the parent's fully encoded symb is used.
                    315: 
1.124     albertel  316: 		my ($mapid,$resid)=split(/\./,$parent_rid);
                    317: 		my $symb=
                    318: 		    &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
                    319: 						 $resid,$hash{'src_'.$parent_rid});
1.85      albertel  320: 		
1.124     albertel  321: 		$seed = $symb;
                    322: 	    }
1.138     foxr      323: 
1.139     foxr      324: 	    # TODO: Here for sure we need to pass along the username/domain
1.138     foxr      325: 	    # so that we can impersonate users in lonprintout e.g.
                    326: 
1.146     raeburn   327:             my $setcode;
                    328:             if (defined($randomizationcode{$parent_rid})) {
                    329:                 if ($env{'form.CODE'} eq '') {
                    330:                     $env{'form.CODE'} = $randomizationcode{$parent_rid};
                    331:                     $setcode = 1;
                    332:                 }
                    333:             }
                    334: 
1.124     albertel  335: 	    my $rndseed=&Apache::lonnet::rndseed($seed);
                    336: 	    &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.140     foxr      337: 
1.146     raeburn   338:             if ($setcode) {
                    339:                 undef($env{'form.CODE'});
                    340:                 undef($setcode);
                    341:             }
                    342: 
1.140     foxr      343: 	    # Take the set of map ids we have decoded and permute them to a
                    344: 	    # random order based on the seed set above. All of this is
                    345: 	    # processing the randomorder parameter if it is set, not
                    346: 	    # randompick.
                    347: 
1.148     raeburn   348: 	    @map_ids=&Math::Random::random_permutation(@map_ids);
1.124     albertel  349: 	}
1.139     foxr      350: 
1.124     albertel  351: 	my $from = shift(@map_ids);
                    352: 	my $from_rid = $lpc.'.'.$from;
                    353: 	$hash{'map_start_'.$uri} = $from_rid;
                    354: 	$hash{'type_'.$from_rid}='start';
                    355: 
1.140     foxr      356: 	# Create links to reflect the random re-ordering done above.
                    357: 	# In the code to process the map XML, we did not process links or conditions
                    358: 	# if randomorder was set.  This means that for an instructor to choose
1.139     foxr      359: 
1.124     albertel  360: 	while (my $to = shift(@map_ids)) {
                    361: 	    &make_link(++$linkpc,$lpc,$to,$from);
                    362: 	    my $to_rid =  $lpc.'.'.$to;
                    363: 	    $hash{'type_'.$to_rid}='normal';
                    364: 	    $from = $to;
                    365: 	    $from_rid = $to_rid;
                    366: 	}
1.1       www       367: 
1.124     albertel  368: 	$hash{'map_finish_'.$uri}= $from_rid;
                    369: 	$hash{'type_'.$from_rid}='finish';
1.1       www       370:     }
1.121     albertel  371: 
1.127     albertel  372:     $parser = HTML::TokeParser->new(\$instr);
1.121     albertel  373:     $parser->attr_encoded(1);
1.139     foxr      374: 
1.149.2.2  raeburn   375:     # last parse out the mapalias params.  These provide mnemonic
1.140     foxr      376:     # tags to resources that can be used in conditions
1.139     foxr      377: 
1.121     albertel  378:     while (my $token = $parser->get_token) {
                    379: 	next if ($token->[0] ne 'S');
                    380: 	if ($token->[1] eq 'param') {
                    381: 	    &parse_mapalias_param($token,$lpc);
                    382: 	} 
                    383:     }
                    384: }
                    385: 
1.124     albertel  386: 
                    387: # -------------------------------------------------------------------- Resource
1.138     foxr      388: #
                    389: #  Parses a resource tag to produce the value to push into the
                    390: #  map_ids array.
                    391: # 
                    392: #
                    393: #  Information about the actual type of resource is provided by the file extension
                    394: #  of the uri (e.g. .problem, .sequence etc. etc.).
                    395: #
                    396: #  Parameters:
                    397: #    $token   - A token from HTML::TokeParser
                    398: #               This is an array that describes the most recently parsed HTML item.
                    399: #    $lpc     - Map nesting level (?)
                    400: #    $ispage  - True if this resource is encapsulated in a .page (assembled resourcde).
                    401: #    $uri     - URI of the enclosing resource.
1.146     raeburn   402: #    $courseid - Course id of the course containing the resource being parsed. 
1.138     foxr      403: # Returns:
1.139     foxr      404: #   Value of the id attribute of the tag.
1.138     foxr      405: #
                    406: # Note:
                    407: #   The token is an array that contains the following elements:
                    408: #   [0]   => 'S' indicating this is a start token
                    409: #   [1]   => 'resource'  indicating this tag is a <resource> tag.
                    410: #   [2]   => Hash of attribute =>value pairs.
                    411: #   [3]   => @(keys [2]).
                    412: #   [4]   => unused.
                    413: #
                    414: #   The attributes of the resourcde tag include:
                    415: #
                    416: #   id     - The resource id.
                    417: #   src    - The URI of the resource.
                    418: #   type   - The resource type (e.g. start and finish).
                    419: #   title  - The resource title.
                    420: 
                    421: 
1.124     albertel  422: sub parse_resource {
1.146     raeburn   423:     my ($token,$lpc,$ispage,$uri,$courseid) = @_;
1.138     foxr      424:     
1.139     foxr      425:     # I refuse to countenance code like this that has 
1.138     foxr      426:     # such a dirty side effect (and forcing this sub to be called within a loop).
                    427:     #
                    428:     #  if ($token->[2]->{'type'} eq 'zombie') { next; }
1.139     foxr      429:     #
                    430:     #  The original code both returns _and_ skips to the next pass of the >caller's<
                    431:     #  loop, that's just dirty.
                    432:     #
1.138     foxr      433: 
                    434:     # Zombie resources don't produce anything useful.
                    435: 
                    436:     if ($token->[2]->{'type'} eq 'zombie') {
                    437: 	return undef;
                    438:     }
                    439: 
1.139     foxr      440:     my $rid=$lpc.'.'.$token->[2]->{'id'}; # Resource id in hash is levelcounter.id-in-xml.
                    441: 
                    442:     # Save the hash element type and title:
1.124     albertel  443: 	    
                    444:     $hash{'kind_'.$rid}='res';
                    445:     $hash{'title_'.$rid}=$token->[2]->{'title'};
1.139     foxr      446: 
                    447:     # Get the version free URI for the resource.
                    448:     # If a 'version' attribute was supplied, and this resource's version 
                    449:     # information has not yet been stored, store it.
                    450:     #
                    451: 
1.124     albertel  452:     my $turi=&versiontrack($token->[2]->{'src'});
                    453:     if ($token->[2]->{'version'}) {
                    454: 	unless ($hash{'version_'.$turi}) {
                    455: 	    $hash{'version_'.$turi}=$1;
                    456: 	}
                    457:     }
1.139     foxr      458:     # Pull out the title and do entity substitution on &colon
                    459:     # Q: Why no other entity substitutions?
                    460: 
1.124     albertel  461:     my $title=$token->[2]->{'title'};
                    462:     $title=~s/\&colon\;/\:/gs;
1.139     foxr      463: 
                    464: 
                    465: 
                    466:     # I think the point of all this code is to construct a final
                    467:     # URI that apache and its rewrite rules can use to
                    468:     # fetch the resource.   Thi s sonly necessary if the resource
                    469:     # is not a page.  If the resource is a page then it must be
                    470:     # assembled (at fetch time?).
                    471: 
1.149.2.3! raeburn   472:     if ($ispage) {
        !           473:         if ($token->[2]->{'external'} eq 'true') { # external
        !           474:             $turi=~s{^http\://}{/ext/};
        !           475:         }
        !           476:     } else {
1.124     albertel  477: 	$turi=~/\.(\w+)$/;
                    478: 	my $embstyle=&Apache::loncommon::fileembstyle($1);
                    479: 	if ($token->[2]->{'external'} eq 'true') { # external
1.130     raeburn   480: 	    $turi=~s/^https?\:\/\//\/adm\/wrapper\/ext\//;
1.124     albertel  481: 	} elsif ($turi=~/^\/*uploaded\//) { # uploaded
                    482: 	    if (($embstyle eq 'img') 
                    483: 		|| ($embstyle eq 'emb')
                    484: 		|| ($embstyle eq 'wrp')) {
                    485: 		$turi='/adm/wrapper'.$turi;
                    486: 	    } elsif ($embstyle eq 'ssi') {
                    487: 		#do nothing with these
                    488: 	    } elsif ($turi!~/\.(sequence|page)$/) {
                    489: 		$turi='/adm/coursedocs/showdoc'.$turi;
                    490: 	    }
                    491: 	} elsif ($turi=~/\S/) { # normal non-empty internal resource
                    492: 	    my $mapdir=$uri;
                    493: 	    $mapdir=~s/[^\/]+$//;
                    494: 	    $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
                    495: 	    if (($embstyle eq 'img') 
                    496: 		|| ($embstyle eq 'emb')
                    497: 		|| ($embstyle eq 'wrp')) {
                    498: 		$turi='/adm/wrapper'.$turi;
                    499: 	    }
                    500: 	}
                    501:     }
1.139     foxr      502:     # Store reverse lookup, remove query string resource 'ids'_uri => resource id.
                    503:     # If the URI appears more than one time in the sequence, it's resourcde
                    504:     # id's are constructed as a comma spearated list.
                    505: 
1.124     albertel  506:     my $idsuri=$turi;
                    507:     $idsuri=~s/\?.+$//;
                    508:     if (defined($hash{'ids_'.$idsuri})) {
                    509: 	$hash{'ids_'.$idsuri}.=','.$rid;
                    510:     } else {
                    511: 	$hash{'ids_'.$idsuri}=''.$rid;
                    512:     }
                    513:     
1.139     foxr      514: 
                    515: 
1.135     raeburn   516:     if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard|viewclasslist)$/) {
1.124     albertel  517: 	$turi.='?register=1';
                    518:     }
                    519:     
1.139     foxr      520: 
                    521:     # resource id lookup:  'src'_resourc-di  => URI decorated with a query
                    522:     # parameter as above if necessary due to the resource type.
                    523:     
1.124     albertel  524:     $hash{'src_'.$rid}=$turi;
1.139     foxr      525: 
                    526:     # Mark the external-ness of the resource:
1.124     albertel  527:     
                    528:     if ($token->[2]->{'external'} eq 'true') {
                    529: 	$hash{'ext_'.$rid}='true:';
                    530:     } else {
                    531: 	$hash{'ext_'.$rid}='false:';
                    532:     }
1.139     foxr      533: 
                    534:     # If the resource is a start/finish resource set those
                    535:     # entries in the has so that navigation knows where everything starts.
                    536:     # TODO?  If there is a malformed sequence that has no start or no finish
                    537:     # resource, should this be detected and errors thrown?  How would such a 
                    538:     # resource come into being other than being manually constructed by a person
                    539:     # and then uploaded?  Could that happen if an author decided a sequence was almost
                    540:     # right edited it by hand and then reuploaded it to 'fix it' but accidently cut the
                    541:     #  start or finish resources?
                    542:     #
                    543:     #  All resourcess also get a type_id => (start | finish | normal)    hash entr.
                    544:     #
1.124     albertel  545:     if ($token->[2]->{'type'}) {
                    546: 	$hash{'type_'.$rid}=$token->[2]->{'type'};
                    547: 	if ($token->[2]->{'type'} eq 'start') {
                    548: 	    $hash{'map_start_'.$uri}="$rid";
                    549: 	}
                    550: 	if ($token->[2]->{'type'} eq 'finish') {
                    551: 	    $hash{'map_finish_'.$uri}="$rid";
                    552: 	}
                    553:     }  else {
                    554: 	$hash{'type_'.$rid}='normal';
                    555:     }
1.139     foxr      556: 
                    557:     # Sequences end pages are constructed entities.  They require that the 
                    558:     # map that defines _them_ be loaded as well into the hash...with this resourcde
                    559:     # as the base of the nesting.
                    560:     # Resources like that are also marked with is_map_id => 1 entries.
                    561:     #
1.124     albertel  562:     
                    563:     if (($turi=~/\.sequence$/) ||
                    564: 	($turi=~/\.page$/)) {
                    565: 	$hash{'is_map_'.$rid}=1;
1.146     raeburn   566: 	&loadmap($turi,$rid,$courseid);
1.124     albertel  567:     } 
                    568:     return $token->[2]->{'id'};
                    569: }
                    570: 
1.139     foxr      571: #-------------------------------------------------------------------- link
                    572: #  Links define how you are allowed to move from one resource to another.
                    573: #  They are the transition edges in the directed graph that a map is.
                    574: #  This sub takes informatino from a <link> tag and constructs the
                    575: #  navigation bits and pieces of a map.  There is no requirement that the
                    576: #  resources that are linke are already defined, however clearly the map is 
                    577: #  badly broken if they are not _eventually_ defined.
                    578: #
                    579: #  Note that links can be unconditional or conditional.
                    580: #
                    581: #  Parameters:
                    582: #     linkpc   - The link counter for this level of map nesting (this is 
                    583: #                reset to zero by loadmap prior to starting to process
                    584: #                links for map).
                    585: #     lpc      - The map level ocounter (how deeply nested this map is in
                    586: #                the hierarchy of maps that are recursively read in.
                    587: #     to       - resource id (within the XML) of the target of the edge.
                    588: #     from     - resource id (within the XML) of the source of the edge.
                    589: #     condition- id of condition associated with the edge (also within the XML).
                    590: #
                    591: 
1.124     albertel  592: sub make_link {
                    593:     my ($linkpc,$lpc,$to,$from,$condition) = @_;
                    594:     
1.139     foxr      595:     #  Compute fully qualified ids for the link, the 
                    596:     # and from/to by prepending lpc.
                    597:     #
                    598: 
1.124     albertel  599:     my $linkid=$lpc.'.'.$linkpc;
                    600:     my $goesto=$lpc.'.'.$to;
                    601:     my $comesfrom=$lpc.'.'.$from;
                    602:     my $undercond=0;
                    603: 
1.139     foxr      604: 
                    605:     # If there is a condition, qualify it with the level counter.
                    606: 
1.124     albertel  607:     if ($condition) {
                    608: 	$undercond=$lpc.'.'.$condition;
                    609:     }
                    610: 
1.139     foxr      611:     # Links are represnted by:
                    612:     #  goesto_.fuullyqualifedlinkid => fully qualified to
                    613:     #  comesfrom.fullyqualifiedlinkid => fully qualified from
                    614:     #  undercond_.fullyqualifiedlinkid => fully qualified condition id.
                    615: 
1.124     albertel  616:     $hash{'goesto_'.$linkid}=$goesto;
                    617:     $hash{'comesfrom_'.$linkid}=$comesfrom;
                    618:     $hash{'undercond_'.$linkid}=$undercond;
                    619: 
1.139     foxr      620:     # In addition:
                    621:     #   to_.fully qualified from => comma separated list of 
                    622:     #   link ids with that from.
                    623:     # Similarly:
                    624:     #   from_.fully qualified to => comma separated list of link ids`
                    625:     #                               with that to.
                    626:     #  That allows us given a resource id to know all edges that go to it
                    627:     #  and leave from it.
                    628:     #
                    629: 
1.124     albertel  630:     if (defined($hash{'to_'.$comesfrom})) {
                    631: 	$hash{'to_'.$comesfrom}.=','.$linkid;
                    632:     } else {
                    633: 	$hash{'to_'.$comesfrom}=''.$linkid;
                    634:     }
                    635:     if (defined($hash{'from_'.$goesto})) {
                    636: 	$hash{'from_'.$goesto}.=','.$linkid;
                    637:     } else {
                    638: 	$hash{'from_'.$goesto}=''.$linkid;
                    639:     }
                    640: }
                    641: 
                    642: # ------------------------------------------------------------------- Condition
1.139     foxr      643: #
                    644: #  Processes <condition> tags, storing sufficient information about them
                    645: #  in the hash so that they can be evaluated and used to conditionalize
                    646: #  what is presented to the student.
                    647: #
                    648: #  these can have the following attributes 
                    649: #
                    650: #    id    = A unique identifier of the condition within the map.
                    651: #
                    652: #    value = Is a perl script-let that, when evaluated in safe space
                    653: #            determines whether or not the condition is true.
                    654: #            Normally this takes the form of a test on an  Apache::lonnet::EXT call
                    655: #            to find the value of variable associated with a resource in the
                    656: #            map identified by a mapalias.
                    657: #            Here's a fragment of XML code that illustrates this:
                    658: #
                    659: #           <param to="5" value="mainproblem" name="parameter_0_mapalias" type="string" />
                    660: #           <resource src="" id="1" type="start" title="Start" />
                    661: #           <resource src="/res/msu/albertel/b_and_c/p1.problem" id="5"  title="p1.problem" />
                    662: #           <condition value="&EXT('user.resource.resource.0.tries','mainproblem')
                    663: #           <2 " id="61" type="stop" />
                    664: #           <link to="5" index="1" from="1" condition="61" />    
                    665: #
                    666: #           In this fragment:
                    667: #             - The param tag establishes an alias to resource id 5 of 'mainproblem'.
                    668: #             - The resource that is the start of the map is identified.
                    669: #             - The resource tag identifies the resource associated with this tag
                    670: #               and gives it the id 5.
                    671: #             - The condition is true if the tries variable associated with mainproblem
                    672: #               is less than 2 (that is the user has had more than 2 tries).
                    673: #               The condition type is a stop condition which inhibits(?) the associated
                    674: #               link if the condition  is false. 
                    675: #             - The link to resource 5 from resource 1 is affected by this condition.    
                    676: #            
                    677: #    type  = Type of the condition. The type determines how the condition affects the
                    678: #            link associated with it and is one of
                    679: #            -  'force'
                    680: #            -  'stop'
                    681: #              anything else including not supplied..which treated as:
                    682: #            - 'normal'.
                    683: #            Presumably maps get created by the resource assembly tool and therefore
                    684: #            illegal type values won't squirm their way into the XML.
                    685: #
                    686: # Side effects:
                    687: #   -  The kind_level-qualified-condition-id hash element is set to 'cond'.
                    688: #   -  The condition text is pushed into the cond array and its element number is
                    689: #      set in the condid_level-qualified-condition-id element of the hash.
                    690: #   - The condition type is colon appneded to the cond array element for this condition.
1.124     albertel  691: sub parse_condition {
                    692:     my ($token,$lpc) = @_;
                    693:     my $rid=$lpc.'.'.$token->[2]->{'id'};
                    694:     
                    695:     $hash{'kind_'.$rid}='cond';
                    696: 
                    697:     my $condition = $token->[2]->{'value'};
                    698:     $condition =~ s/[\n\r]+/ /gs;
                    699:     push(@cond, $condition);
                    700:     $hash{'condid_'.$rid}=$#cond;
                    701:     if ($token->[2]->{'type'}) {
                    702: 	$cond[$#cond].=':'.$token->[2]->{'type'};
                    703:     }  else {
                    704: 	$cond[$#cond].=':normal';
                    705:     }
                    706: }
                    707: 
                    708: # ------------------------------------------------------------------- Parameter
1.138     foxr      709: # Parse a <parameter> tag in the map.
                    710: # Parmameters:
                    711: #    $token Token array for a start tag from HTML::TokeParser
                    712: #           [0] = 'S'
                    713: #           [1] = tagname ("param")
                    714: #           [2] = Hash of {attribute} = values.
                    715: #           [3] = Array of the keys in [2].
                    716: #           [4] = unused.
                    717: #    $lpc   Current map nesting level.a
                    718: #
                    719: #  Typical attributes:
                    720: #     to=n      - Number of the resource the parameter applies to.
                    721: #     type=xx   - Type of parameter value (e.g. string_yesno or int_pos).
1.149.2.2  raeburn   722: #     name=xxx  - Name of parameter (e.g. parameter_randompick or parameter_randomorder).
1.138     foxr      723: #     value=xxx - value of the parameter.
1.124     albertel  724: 
                    725: sub parse_param {
                    726:     my ($token,$lpc) = @_;
1.138     foxr      727:     my $referid=$lpc.'.'.$token->[2]->{'to'}; # Resource param applies to.
                    728:     my $name=$token->[2]->{'name'};	      # Name of parameter
1.124     albertel  729:     my $part;
1.138     foxr      730: 
                    731: 
                    732:     if ($name=~/^parameter_(.*)_/) { 
1.124     albertel  733: 	$part=$1;
                    734:     } else {
                    735: 	$part=0;
                    736:     }
1.138     foxr      737: 
                    738:     # Peel the parameter_ off the parameter name.
                    739: 
1.124     albertel  740:     $name=~s/^.*_([^_]*)$/$1/;
1.138     foxr      741: 
                    742:     # The value is:
                    743:     #   type.part.name.value
                    744: 
1.124     albertel  745:     my $newparam=
                    746: 	&escape($token->[2]->{'type'}).':'.
                    747: 	&escape($part.'.'.$name).'='.
                    748: 	&escape($token->[2]->{'value'});
1.138     foxr      749: 
                    750:     # The hash key is param_resourceid.
                    751:     # Multiple parameters for a single resource are & separated in the hash.
                    752: 
                    753: 
1.124     albertel  754:     if (defined($hash{'param_'.$referid})) {
                    755: 	$hash{'param_'.$referid}.='&'.$newparam;
                    756:     } else {
                    757: 	$hash{'param_'.$referid}=''.$newparam;
                    758:     }
1.138     foxr      759:     #
                    760:     #  These parameters have to do with randomly selecting
                    761:     # resources, therefore a separate hash is also created to 
                    762:     # make it easy to locate them when actually computing the resource set later on
                    763:     # See the code conditionalized by ($randomize) in loadmap().
                    764: 
                    765:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) { # Random selection turned on
1.124     albertel  766: 	$randompick{$referid}=$token->[2]->{'value'};
                    767:     }
1.138     foxr      768:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) { # Randomseed provided.
1.124     albertel  769: 	$randompickseed{$referid}=$token->[2]->{'value'};
                    770:     }
1.138     foxr      771:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randomorder$/) { # Random order turned on.
1.124     albertel  772: 	$randomorder{$referid}=$token->[2]->{'value'};
                    773:     }
1.138     foxr      774: 
                    775:     # These parameters have to do with how the URLs of resources are presented to
                    776:     # course members(?).  encrypturl presents encypted url's while
                    777:     # hiddenresource hides the URL.
                    778:     #
                    779: 
1.124     albertel  780:     if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
                    781: 	if ($token->[2]->{'value'}=~/^yes$/i) {
                    782: 	    $encurl{$referid}=1;
                    783: 	}
                    784:     }
                    785:     if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
                    786: 	if ($token->[2]->{'value'}=~/^yes$/i) {
                    787: 	    $hiddenurl{$referid}=1;
                    788: 	}
                    789:     }
                    790: }
1.140     foxr      791: #
                    792: #  Parse mapalias parameters.
                    793: #  these are tags of the form:
                    794: #  <param to="nn" 
                    795: #         value="some-alias-for-resourceid-nn" 
                    796: #         name="parameter_0_mapalias" 
                    797: #         type="string" />
                    798: #  A map alias is a textual name for a resource:
                    799: #    - The to  attribute identifies the resource (this gets level qualified below)
                    800: #    - The value attributes provides the alias string.
                    801: #    - name must be of the regexp form: /^parameter_(0_)*mapalias$/
                    802: #    - e.g. the string 'parameter_' followed by 0 or more "0_" strings
                    803: #      terminating with the string 'mapalias'.
                    804: #      Examples:
                    805: #         'parameter_mapalias', 'parameter_0_mapalias', parameter_0_0_mapalias'
                    806: #  Invalid to ids are silently ignored.
                    807: #
                    808: #  Parameters:
                    809: #     token - The token array fromthe HMTML::TokeParser
                    810: #     lpc   - The current map level counter.
                    811: #
1.121     albertel  812: sub parse_mapalias_param {
                    813:     my ($token,$lpc) = @_;
1.140     foxr      814: 
                    815:     # Fully qualify the to value and ignore the alias if there is no
                    816:     # corresponding resource.
                    817: 
1.121     albertel  818:     my $referid=$lpc.'.'.$token->[2]->{'to'};
                    819:     return if (!exists($hash{'src_'.$referid}));
                    820: 
1.140     foxr      821:     # If this is a valid mapalias parameter, 
                    822:     # Append the target id to the count_mapalias element for that
                    823:     # alias so that we can detect doubly defined aliases
                    824:     # e.g.:
                    825:     #  <param to="1" value="george" name="parameter_0_mapalias" type="string" />
                    826:     #  <param to="2" value="george" name="parameter_0_mapalias" type="string" />
                    827:     #
                    828:     #  The example above is trivial but the case that's important has to do with
                    829:     #  constructing a map that includes a nested map where the nested map may have
                    830:     #  aliases that conflict with aliases established in the enclosing map.
                    831:     #
                    832:     # ...and create/update the hash mapalias entry to actually store the alias.
                    833:     #
                    834: 
1.121     albertel  835:     if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
1.122     albertel  836: 	&count_mapalias($token->[2]->{'value'},$referid);
1.121     albertel  837: 	$hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
                    838:     }
1.1       www       839: }
                    840: 
1.3       www       841: # --------------------------------------------------------- Simplify expression
                    842: 
1.140     foxr      843: 
                    844: #
                    845: #  Someone should really comment this to describe what it does to what and why.
                    846: #
1.3       www       847: sub simplify {
1.85      albertel  848:     my $expression=shift;
1.101     albertel  849: # (0&1) = 1
1.105     albertel  850:     $expression=~s/\(0\&([_\.\d]+)\)/$1/g;
1.3       www       851: # (8)=8
1.105     albertel  852:     $expression=~s/\(([_\.\d]+)\)/$1/g;
1.3       www       853: # 8&8=8
1.105     albertel  854:     $expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
1.3       www       855: # 8|8=8
1.141     raeburn   856:     $expression=~s/([^_\.\d])([_\.\d]+)(?:\|\2)+([^_\.\d])/$1$2$3/g;
1.3       www       857: # (5&3)&4=5&3&4
1.105     albertel  858:     $expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
1.3       www       859: # (((5&3)|(4&6)))=((5&3)|(4&6))
1.105     albertel  860:     $expression=~
                    861: 	s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
1.3       www       862: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
1.85      albertel  863:     $expression=~
1.105     albertel  864: 	s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
1.85      albertel  865:     return $expression;
1.3       www       866: }
                    867: 
1.2       www       868: # -------------------------------------------------------- Build condition hash
                    869: 
1.140     foxr      870: #
                    871: #  Traces a route recursively through the map after it has been loaded
                    872: #  (I believe this really visits each resourcde that is reachable fromt he
                    873: #  start top node.
                    874: #
                    875: #  - Marks hidden resources as hidden.
                    876: #  - Marks which resource URL's must be encrypted.
                    877: #  - Figures out (if necessary) the first resource in the map.
                    878: #  - Further builds the chunks of the big hash that define how 
                    879: #    conditions work
                    880: #
                    881: #  Note that the tracing strategy won't visit resources that are not linked to
                    882: #  anything or islands in the map (groups of resources that form a path but are not
                    883: #  linked in to the path that can be traced from the start resource...but that's ok
                    884: #  because by definition, those resources are not reachable by users of the course.
                    885: #
                    886: # Parameters:
                    887: #   sofar    - _URI of the prior entry or 0 if this is the top.
                    888: #   rid      - URI of the resource to visit.
                    889: #   beenhere - list of resources (each resource enclosed by &'s) that have
                    890: #              already been visited.
                    891: #   encflag  - If true the resource that resulted in a recursive call to us
                    892: #              has an encoded URL (which means contained resources should too). 
                    893: #   hdnflag  - If true,the resource that resulted in a recursive call to us
                    894: #              was hidden (which means contained resources should be hidden too).
                    895: # Returns
                    896: #    new value indicating how far the map has been traversed (the sofar).
                    897: #
1.2       www       898: sub traceroute {
1.77      www       899:     my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
1.81      albertel  900:     my $newsofar=$sofar=simplify($sofar);
1.140     foxr      901: 
1.120     albertel  902:     unless ($beenhere=~/\&\Q$rid\E\&/) {
1.85      albertel  903: 	$beenhere.=$rid.'&';  
                    904: 	my ($mapid,$resid)=split(/\./,$rid);
                    905: 	my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
                    906: 	my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
1.91      albertel  907: 
1.90      albertel  908: 	if ($hdnflag || lc($hidden) eq 'yes') {
                    909: 	    $hiddenurl{$rid}=1;
1.91      albertel  910: 	}
                    911: 	if (!$hdnflag && lc($hidden) eq 'no') {
1.90      albertel  912: 	    delete($hiddenurl{$rid});
                    913: 	}
1.91      albertel  914: 
1.85      albertel  915: 	my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
                    916: 	if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
1.140     foxr      917: 
1.116     www       918: 	if (($retfrid eq '') && ($hash{'src_'.$rid})
1.85      albertel  919: 	    && ($hash{'src_'.$rid}!~/\.sequence$/)) {
1.116     www       920: 	    $retfrid=$rid;
1.85      albertel  921: 	}
1.140     foxr      922: 
1.85      albertel  923: 	if (defined($hash{'conditions_'.$rid})) {
                    924: 	    $hash{'conditions_'.$rid}=simplify(
1.103     albertel  925:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.85      albertel  926: 	} else {
                    927: 	    $hash{'conditions_'.$rid}=$sofar;
                    928: 	}
1.107     albertel  929: 
                    930: 	# if the expression is just the 0th condition keep it
                    931: 	# otherwise leave a pointer to this condition expression
1.140     foxr      932: 
1.107     albertel  933: 	$newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
                    934: 
1.140     foxr      935: 	# Recurse if the resource is a map:
                    936: 
1.85      albertel  937: 	if (defined($hash{'is_map_'.$rid})) {
                    938: 	    if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
                    939: 		$sofar=$newsofar=
                    940: 		    &traceroute($sofar,
1.126     albertel  941: 				$hash{'map_start_'.$hash{'src_'.$rid}},
                    942: 				$beenhere,
1.85      albertel  943: 				$encflag || $encurl{$rid},
                    944: 				$hdnflag || $hiddenurl{$rid});
                    945: 	    }
                    946: 	}
1.140     foxr      947: 
                    948: 	# Processes  links to this resource:
                    949: 	#  - verify the existence of any conditionals on the link to here.
                    950: 	#  - Recurse to any resources linked to us.
                    951: 	#
1.85      albertel  952: 	if (defined($hash{'to_'.$rid})) {
1.106     albertel  953: 	    foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
1.2       www       954: 		my $further=$sofar;
1.140     foxr      955: 		#
                    956: 		# If there's a condition associated with this link be sure
                    957: 		# it's been defined else that's an error:
                    958: 		#
1.106     albertel  959:                 if ($hash{'undercond_'.$id}) {
                    960: 		    if (defined($hash{'condid_'.$hash{'undercond_'.$id}})) {
1.105     albertel  961: 			$further=simplify('('.'_'.$rid.')&('.
1.106     albertel  962: 					  $hash{'condid_'.$hash{'undercond_'.$id}}.')');
1.85      albertel  963: 		    } else {
1.147     raeburn   964: 			$errtext.= '<br />'.
                    965:                                    &mt('Undefined condition ID: [_1]',
                    966:                                        $hash{'undercond_'.$id});
1.85      albertel  967: 		    }
1.2       www       968:                 }
1.140     foxr      969: 		#  Recurse to resoruces that have to's to us.
1.106     albertel  970:                 $newsofar=&traceroute($further,$hash{'goesto_'.$id},$beenhere,
1.81      albertel  971: 				      $encflag,$hdnflag);
1.85      albertel  972: 	    }
                    973: 	}
1.2       www       974:     }
1.81      albertel  975:     return $newsofar;
1.2       www       976: }
1.1       www       977: 
1.19      www       978: # ------------------------------ Cascading conditions, quick access, parameters
1.4       www       979: 
1.140     foxr      980: #
                    981: #  Seems a rather strangely named sub given what the comment above says it does.
                    982: 
                    983: 
1.4       www       984: sub accinit {
                    985:     my ($uri,$short,$fn)=@_;
                    986:     my %acchash=();
                    987:     my %captured=();
                    988:     my $condcounter=0;
1.5       www       989:     $acchash{'acc.cond.'.$short.'.0'}=0;
1.140     foxr      990: 
                    991:     # This loop is only interested in conditions and 
                    992:     # parameters in the big hash:
                    993: 
1.104     albertel  994:     foreach my $key (keys(%hash)) {
1.140     foxr      995: 
                    996: 	# conditions:
                    997: 
1.104     albertel  998: 	if ($key=~/^conditions/) {
                    999: 	    my $expr=$hash{$key};
1.140     foxr     1000: 
1.109     albertel 1001: 	    # try to find and factor out common sub-expressions
1.140     foxr     1002: 	    # Any subexpression that is found is simplified, removed from
                   1003: 	    # the original condition expression and the simplified sub-expression
                   1004: 	    # substituted back in to the epxression..I'm not actually convinced this
                   1005: 	    # factors anything out...but instead maybe simplifies common factors(?)
                   1006: 
1.105     albertel 1007: 	    foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
1.104     albertel 1008: 		my $orig=$sub;
1.109     albertel 1009: 
                   1010: 		my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
                   1011: 		next if (!defined($factor));
                   1012: 
                   1013: 		$sub=~s/\Q$factor\E//g;
1.85      albertel 1014: 		$sub=~s/^\(/\($factor\(/;
                   1015: 		$sub.=')';
                   1016: 		$sub=simplify($sub);
1.109     albertel 1017: 		$expr=~s/\Q$orig\E/$sub/;
1.85      albertel 1018: 	    }
1.104     albertel 1019: 	    $hash{$key}=$expr;
1.140     foxr     1020: 
                   1021:            # If not yet seen, record in acchash and that we've seen it.
                   1022: 
1.85      albertel 1023: 	    unless (defined($captured{$expr})) {
                   1024: 		$condcounter++;
                   1025: 		$captured{$expr}=$condcounter;
                   1026: 		$acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
                   1027: 	    } 
1.140     foxr     1028:         # Parameters:
                   1029: 
1.104     albertel 1030: 	} elsif ($key=~/^param_(\d+)\.(\d+)/) {
1.86      albertel 1031: 	    my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
                   1032: 						    $hash{'src_'.$1.'.'.$2});
1.104     albertel 1033: 	    foreach my $param (split(/\&/,$hash{$key})) {
                   1034: 		my ($typename,$value)=split(/\=/,$param);
1.85      albertel 1035: 		my ($type,$name)=split(/\:/,$typename);
1.114     www      1036: 		$parmhash{$prefix.'.'.&unescape($name)}=
                   1037: 		    &unescape($value);
                   1038: 		$parmhash{$prefix.'.'.&unescape($name).'.type'}=
                   1039: 		    &unescape($type);
1.85      albertel 1040: 	    }
                   1041: 	}
1.26      harris41 1042:     }
1.140     foxr     1043:     # This loop only processes id entries in the big hash.
                   1044: 
1.104     albertel 1045:     foreach my $key (keys(%hash)) {
                   1046: 	if ($key=~/^ids/) {
                   1047: 	    foreach my $resid (split(/\,/,$hash{$key})) {
1.85      albertel 1048: 		my $uri=$hash{'src_'.$resid};
1.100     albertel 1049: 		my ($uripath,$urifile) =
                   1050: 		    &Apache::lonnet::split_uri_for_cond($uri);
1.85      albertel 1051: 		if ($uripath) {
                   1052: 		    my $uricond='0';
                   1053: 		    if (defined($hash{'conditions_'.$resid})) {
                   1054: 			$uricond=$captured{$hash{'conditions_'.$resid}};
                   1055: 		    }
                   1056: 		    if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
                   1057: 			if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
                   1058: 			    /(\&\Q$urifile\E\:[^\&]*)/) {
                   1059: 			    my $replace=$1;
                   1060: 			    my $regexp=$replace;
                   1061: 			    #$regexp=~s/\|/\\\|/g;
1.105     albertel 1062: 			    $acchash{'acc.res.'.$short.'.'.$uripath} =~
1.104     albertel 1063: 				s/\Q$regexp\E/$replace\|$uricond/;
1.85      albertel 1064: 			} else {
                   1065: 			    $acchash{'acc.res.'.$short.'.'.$uripath}.=
                   1066: 				$urifile.':'.$uricond.'&';
                   1067: 			}
                   1068: 		    } else {
                   1069: 			$acchash{'acc.res.'.$short.'.'.$uripath}=
                   1070: 			    '&'.$urifile.':'.$uricond.'&';
                   1071: 		    }
                   1072: 		} 
                   1073: 	    }
                   1074: 	}
1.26      harris41 1075:     }
1.24      www      1076:     $acchash{'acc.res.'.$short.'.'}='&:0&';
1.8       www      1077:     my $courseuri=$uri;
                   1078:     $courseuri=~s/^\/res\///;
1.131     raeburn  1079:     my $regexp = 1;
                   1080:     &Apache::lonnet::delenv('(acc\.|httpref\.)',$regexp);
1.128     raeburn  1081:     &Apache::lonnet::appenv(\%acchash);
1.4       www      1082: }
                   1083: 
1.73      www      1084: # ---------------- Selectively delete from randompick maps and hidden url parms
1.29      www      1085: 
1.73      www      1086: sub hiddenurls {
1.31      www      1087:     my $randomoutentry='';
1.149     raeburn  1088:     foreach my $rid (keys(%randompick)) {
1.29      www      1089:         my $rndpick=$randompick{$rid};
                   1090:         my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
                   1091: # ------------------------------------------- put existing resources into array
                   1092:         my @currentrids=();
1.106     albertel 1093:         foreach my $key (sort(keys(%hash))) {
                   1094: 	    if ($key=~/^src_($mpc\.\d+)/) {
1.29      www      1095: 		if ($hash{'src_'.$1}) { push @currentrids, $1; }
                   1096:             }
                   1097:         }
1.50      albertel 1098: 	# rids are number.number and we want to numercially sort on 
                   1099:         # the second number
                   1100: 	@currentrids=sort {
                   1101: 	    my (undef,$aid)=split(/\./,$a);
                   1102: 	    my (undef,$bid)=split(/\./,$b);
                   1103: 	    $aid <=> $bid;
                   1104: 	} @currentrids;
1.29      www      1105:         next if ($#currentrids<$rndpick);
                   1106: # -------------------------------- randomly eliminate the ones that should stay
1.50      albertel 1107: 	my (undef,$id)=split(/\./,$rid);
1.51      www      1108:         if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1.146     raeburn  1109:         my $setcode;
                   1110:         if (defined($randomizationcode{$rid})) {
                   1111:             if ($env{'form.CODE'} eq '') {
                   1112:                 $env{'form.CODE'} = $randomizationcode{$rid};
                   1113:                 $setcode = 1;
                   1114:             }
                   1115:         }
1.50      albertel 1116: 	my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
1.146     raeburn  1117:         if ($setcode) {
                   1118:             undef($env{'form.CODE'});
                   1119:             undef($setcode);
                   1120:         }
1.58      albertel 1121: 	&Apache::lonnet::setup_random_from_rndseed($rndseed);
1.50      albertel 1122: 	my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
                   1123:         for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
                   1124: 	#&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1.29      www      1125: # -------------------------------------------------------- delete the leftovers
                   1126:         for (my $k=0; $k<=$#currentrids; $k++) {
                   1127:             if ($currentrids[$k]) {
                   1128: 		$hash{'randomout_'.$currentrids[$k]}=1;
1.32      www      1129:                 my ($mapid,$resid)=split(/\./,$currentrids[$k]);
1.149.2.2  raeburn  1130:                 if ($rescount{$mapid}) {
                   1131:                     $rescount{$mapid} --;
                   1132:                 }
                   1133:                 if ($hash{'is_map_'.$currentrids[$k]}) {
                   1134:                     if ($mapcount{$mapid}) {
                   1135:                         $mapcount{$mapid} --;
                   1136:                     }
                   1137:                 }
1.32      www      1138:                 $randomoutentry.='&'.
1.86      albertel 1139: 		    &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
                   1140: 						 $resid,
                   1141: 						 $hash{'src_'.$currentrids[$k]}
                   1142: 						 ).'&';
1.29      www      1143:             }
                   1144:         }
1.31      www      1145:     }
1.73      www      1146: # ------------------------------ take care of explicitly hidden urls or folders
1.149     raeburn  1147:     foreach my $rid (keys(%hiddenurl)) {
1.73      www      1148: 	$hash{'randomout_'.$rid}=1;
                   1149: 	my ($mapid,$resid)=split(/\./,$rid);
1.149.2.2  raeburn  1150:         if ($rescount{$mapid}) {
                   1151:             $rescount{$mapid} --;
                   1152:         }
                   1153:         if ($hash{'is_map_'.$rid}) {
                   1154:             if ($mapcount{$mapid}) {
                   1155:                 $mapcount{$mapid} --;
                   1156:             }
                   1157:         }
1.73      www      1158: 	$randomoutentry.='&'.
1.86      albertel 1159: 	    &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
                   1160: 					 $hash{'src_'.$rid}).'&';
1.73      www      1161:     }
                   1162: # --------------------------------------- append randomout entry to environment
1.31      www      1163:     if ($randomoutentry) {
1.128     raeburn  1164: 	&Apache::lonnet::appenv({'acc.randomout' => $randomoutentry});
1.29      www      1165:     }
                   1166: }
                   1167: 
1.149.2.2  raeburn  1168: # -------------------------------------- populate big hash with map breadcrumbs
                   1169: 
                   1170: # Create map_breadcrumbs_$pc from map_hierarchy_$pc by omitting intermediate
                   1171: # maps not shown in Course Contents table.
                   1172: 
                   1173: sub mapcrumbs {
                   1174:     foreach my $key (keys(%rescount)) {
                   1175:         if ($hash{'map_hierarchy_'.$key}) {
                   1176:             my $skipnext = 0;
                   1177:             foreach my $id (split(/,/,$hash{'map_hierarchy_'.$key}),$key) {
                   1178:                 unless ($skipnext) {
                   1179:                     $hash{'map_breadcrumbs_'.$key} .= "$id,";
                   1180:                 }
                   1181:                 unless (($id == 0) || ($id == 1)) {
                   1182:                     if ((!$rescount{$id}) || ($rescount{$id} == 1 && $mapcount{$id} == 1)) {
                   1183:                         $skipnext = 1;
                   1184:                     } else {
                   1185:                         $skipnext = 0;
                   1186:                     }
                   1187:                 }
                   1188:             }
                   1189:             $hash{'map_breadcrumbs_'.$key} =~ s/,$//;
                   1190:         }
                   1191:     }
                   1192: }
                   1193: 
1.1       www      1194: # ---------------------------------------------------- Read map and all submaps
                   1195: 
                   1196: sub readmap {
1.85      albertel 1197:     my $short=shift;
                   1198:     $short=~s/^\///;
1.138     foxr     1199: 
                   1200:     # TODO:  Hidden dependency on current user:
                   1201: 
                   1202:     my %cenv=&Apache::lonnet::coursedescription($short,{'freshen_cache'=>1}); 
                   1203: 
1.85      albertel 1204:     my $fn=$cenv{'fn'};
                   1205:     my $uri;
                   1206:     $short=~s/\//\_/g;
                   1207:     unless ($uri=$cenv{'url'}) { 
1.133     raeburn  1208: 	&Apache::lonnet::logthis('<font color="blue">WARNING: '.
1.85      albertel 1209: 				 "Could not load course $short.</font>"); 
1.114     www      1210: 	return ('',&mt('No course data available.'));;
1.85      albertel 1211:     }
                   1212:     @cond=('true:normal');
1.96      albertel 1213: 
1.149.2.2  raeburn  1214:     unless (open(LOCKFILE,">","$fn.db.lock")) {
1.138     foxr     1215: 	# 
                   1216: 	# Most likely a permissions problem on the lockfile or its directory.
                   1217: 	#
1.133     raeburn  1218:         $retfurl = '';
1.144     raeburn  1219:         return ($retfurl,'<br />'.&mt('Map not loaded - Lock file could not be opened when reading map:').' <tt>'.$fn.'</tt>.');
1.133     raeburn  1220:     }
1.96      albertel 1221:     my $lock=0;
1.132     raeburn  1222:     my $gotstate=0;
1.138     foxr     1223:     
                   1224:     # If we can get the lock without delay any files there are idle
                   1225:     # and from some prior request.  We'll kill them off and regenerate them:
                   1226: 
                   1227:     if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {	
                   1228: 	$lock=1;		# Remember that we hold the lock.
1.132     raeburn  1229:         &unlink_tmpfiles($fn);
1.96      albertel 1230:     }
1.85      albertel 1231:     undef %randompick;
1.149.2.1  raeburn  1232:     undef %randompickseed;
                   1233:     undef %randomorder;
1.149.2.2  raeburn  1234:     undef %randomizationcode;
1.85      albertel 1235:     undef %hiddenurl;
                   1236:     undef %encurl;
1.149.2.2  raeburn  1237:     undef %rescount;
                   1238:     undef %mapcount;
1.116     www      1239:     $retfrid='';
1.144     raeburn  1240:     $errtext='';
1.138     foxr     1241:     my ($untiedhash,$untiedparmhash,$tiedhash,$tiedparmhash); # More state flags.
                   1242: 
                   1243:     # if we got the lock, regenerate course regnerate empty files and tie them.
                   1244: 
1.132     raeburn  1245:     if ($lock) {
                   1246:         if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
                   1247:             $tiedhash = 1;
                   1248:             if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
                   1249:                 $tiedparmhash = 1;
1.138     foxr     1250:                 $gotstate = &build_tmp_hashes($uri,
                   1251: 					      $fn,
                   1252: 					      $short,
                   1253: 					      \%cenv); # TODO: Need to provide requested user@dom
1.132     raeburn  1254:                 unless ($gotstate) {
                   1255:                     &Apache::lonnet::logthis('Failed to write statemap at first attempt '.$fn.' for '.$uri.'.</font>');
                   1256:                 }
                   1257:                 $untiedparmhash = untie(%parmhash);
                   1258:                 unless ($untiedparmhash) {
                   1259:                     &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1260:                         'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
                   1261:                 }
                   1262:             }
                   1263:             $untiedhash = untie(%hash);
                   1264:             unless ($untiedhash) {
                   1265:                 &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1266:                     'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
                   1267:             }
                   1268:         }
1.138     foxr     1269: 	flock(LOCKFILE,LOCK_UN); # RF: this is what I don't get unless there are other
                   1270: 	                         # unlocked places the remainder happens..seems like if we
                   1271:                                  # just kept the lock here the rest of the code would have
                   1272:                                  # been much easier? 
1.132     raeburn  1273:     }
                   1274:     unless ($lock && $tiedhash && $tiedparmhash) { 
1.87      albertel 1275: 	# if we are here it is likely because we are already trying to 
                   1276: 	# initialize the course in another child, busy wait trying to 
                   1277: 	# tie the hashes for the next 90 seconds, if we succeed forward 
                   1278: 	# them on to navmaps, if we fail, throw up the Could not init 
                   1279: 	# course screen
1.138     foxr     1280: 	#
                   1281: 	# RF: I'm not seeing the case where the ties/unties can fail in a way
                   1282: 	#     that can be remedied by this.  Since we owned the lock seems
                   1283: 	#     Tie/untie failures are a result of something like a permissions problem instead?
                   1284: 	#
                   1285: 
                   1286: 	#  In any vent, undo what we did manage to do above first:
1.96      albertel 1287: 	if ($lock) {
                   1288: 	    # Got the lock but not the DB files
                   1289: 	    flock(LOCKFILE,LOCK_UN);
1.132     raeburn  1290:             $lock = 0;
1.96      albertel 1291: 	}
1.132     raeburn  1292:         if ($tiedhash) {
                   1293:             unless($untiedhash) {
                   1294: 	        untie(%hash);
                   1295:             }
                   1296:         }
                   1297:         if ($tiedparmhash) {
                   1298:             unless($untiedparmhash) {
                   1299:                 untie(%parmhash);
                   1300:             }
                   1301:         }
1.138     foxr     1302: 	# Log our failure:
                   1303: 
1.133     raeburn  1304: 	&Apache::lonnet::logthis('<font color="blue">WARNING: '.
1.132     raeburn  1305: 				 "Could not tie coursemap $fn for $uri.</font>");
                   1306:         $tiedhash = '';
                   1307:         $tiedparmhash = '';
1.87      albertel 1308: 	my $i=0;
1.138     foxr     1309: 
                   1310: 	# Keep on retrying the lock for 90 sec until we succeed.
                   1311: 
1.87      albertel 1312: 	while($i<90) {
                   1313: 	    $i++;
                   1314: 	    sleep(1);
1.132     raeburn  1315: 	    if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
1.138     foxr     1316: 
                   1317: 		# Got the lock, tie the hashes...the assumption in this code is
                   1318: 		# that some other worker thread has created the db files quite recently
                   1319: 		# so no load is needed:
                   1320: 
1.132     raeburn  1321:                 $lock = 1;
                   1322: 		if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
                   1323:                     $tiedhash = 1;
                   1324: 		    if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
                   1325:                         $tiedparmhash = 1;
                   1326:                         if (-e "$fn.state") {
                   1327: 		            $retfurl='/adm/navmaps';
1.138     foxr     1328: 
                   1329: 			    # BUG BUG: Side effect!
                   1330: 			    # Should conditionalize on something so that we can use this
                   1331: 			    # to load maps for courses that are not current?
                   1332: 			    #
1.132     raeburn  1333: 		            &Apache::lonnet::appenv({"request.course.id"  => $short,
                   1334: 		   			             "request.course.fn"  => $fn,
1.143     raeburn  1335: 					             "request.course.uri" => $uri,
                   1336:                                                      "request.course.tied" => time});
                   1337:                             
1.132     raeburn  1338: 		            $untiedhash = untie(%hash);
                   1339: 		            $untiedparmhash = untie(%parmhash);
                   1340:                             $gotstate = 1;
                   1341: 		            last;
                   1342: 		        }
                   1343:                         $untiedparmhash = untie(%parmhash);
                   1344: 	            }
                   1345: 	            $untiedhash = untie(%hash);
                   1346:                 }
                   1347:             }
1.87      albertel 1348: 	}
1.132     raeburn  1349:         if ($lock) {
                   1350:             flock(LOCKFILE,LOCK_UN);
1.133     raeburn  1351:             $lock = 0;
1.132     raeburn  1352:             if ($tiedparmhash) {
                   1353:                 unless ($untiedparmhash) {
                   1354:                     &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1355:                         'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
                   1356:                 }
                   1357:             }
                   1358:             if ($tiedparmhash) {
                   1359:                 unless ($untiedhash) {
                   1360:                     &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1361:                         'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
                   1362:                 }
                   1363:             }
                   1364:         }
                   1365:     }
1.138     foxr     1366:     # I think this branch of code is all about what happens if we just did the stuff above, 
                   1367:     # but found that the  state file did not exist...again if we'd just held the lock
                   1368:     # would that have made this logic simpler..as generating all the files would be
                   1369:     # an atomic operation with respect to the lock.
                   1370:     #
1.132     raeburn  1371:     unless ($gotstate) {
1.133     raeburn  1372:         $lock = 0;
1.132     raeburn  1373:         &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1374:                      'Could not read statemap '.$fn.' for '.$uri.'.</font>');
                   1375:         &unlink_tmpfiles($fn);
1.133     raeburn  1376:         if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
                   1377:             $lock=1;
                   1378:         }
                   1379:         undef %randompick;
1.149.2.1  raeburn  1380:         undef %randompickseed;
                   1381:         undef %randomorder;
1.149.2.2  raeburn  1382:         undef %randomizationcode;
1.133     raeburn  1383:         undef %hiddenurl;
                   1384:         undef %encurl;
1.149.2.2  raeburn  1385:         undef %rescount;
                   1386:         undef %mapcount;
1.144     raeburn  1387:         $errtext='';
1.133     raeburn  1388:         $retfrid='';
1.138     foxr     1389: 	#
                   1390: 	# Once more through the routine of tying and loading and so on.
                   1391: 	#
1.133     raeburn  1392:         if ($lock) {
                   1393:             if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
                   1394:                 if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
1.138     foxr     1395:                     $gotstate = &build_tmp_hashes($uri,$fn,$short,\%cenv); # TODO: User dependent?
1.133     raeburn  1396:                     unless ($gotstate) {
1.132     raeburn  1397:                         &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1.133     raeburn  1398:                             'Failed to write statemap at second attempt '.$fn.' for '.$uri.'.</font>');
1.132     raeburn  1399:                     }
1.133     raeburn  1400:                     unless (untie(%parmhash)) {
1.132     raeburn  1401:                         &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1.133     raeburn  1402:                             'Could not untie coursemap parmhash '.$fn.'.db for '.$uri.'.</font>');
1.132     raeburn  1403:                     }
1.133     raeburn  1404:                 } else {
                   1405:                     &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1406:                         'Could not tie coursemap '.$fn.'__parms.db for '.$uri.'.</font>');
                   1407:                 }
                   1408:                 unless (untie(%hash)) {
                   1409:                     &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1410:                         'Could not untie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
                   1411:                 }
1.132     raeburn  1412:             } else {
1.133     raeburn  1413:                &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1414:                    'Could not tie coursemap '.$fn.'.db for '.$uri.'.</font>');
1.132     raeburn  1415:             }
1.133     raeburn  1416:             flock(LOCKFILE,LOCK_UN);
                   1417:             $lock = 0;
                   1418:         } else {
1.138     foxr     1419: 	    # Failed to get the immediate lock.
                   1420: 
1.133     raeburn  1421:             &Apache::lonnet::logthis('<font color="blue">WARNING: '.
                   1422:             'Could not obtain lock to tie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
1.132     raeburn  1423:         }
                   1424:     }
1.133     raeburn  1425:     close(LOCKFILE);
1.132     raeburn  1426:     unless (($errtext eq '') || ($env{'request.course.uri'} =~ m{^/uploaded/})) {
                   1427:         &Apache::lonmsg::author_res_msg($env{'request.course.uri'},
1.138     foxr     1428:                                         $errtext); # TODO: User dependent?
1.1       www      1429:     }
1.46      www      1430: # ------------------------------------------------- Check for critical messages
                   1431: 
1.138     foxr     1432: #  Depends on user must parameterize this as well..or separate as this is:
                   1433: #  more part of determining what someone sees on entering a course?
                   1434: 
1.89      albertel 1435:     my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
                   1436: 				   $env{'user.name'});
1.46      www      1437:     if ($what[0]) {
                   1438: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
                   1439: 	    $retfurl='/adm/email?critical=display';
                   1440:         }
                   1441:     }
1.85      albertel 1442:     return ($retfurl,$errtext);
1.1       www      1443: }
1.15      www      1444: 
1.138     foxr     1445: #
                   1446: #  This sub is called when the course hash and the param hash have been tied and
                   1447: #  their lock file is held.
                   1448: #  Parameters:
                   1449: #     $uri      -  URI that identifies the course.
                   1450: #     $fn       -  The base path/filename of the files that make up the context
                   1451: #                  being built.
                   1452: #     $short    -  Short course name.
                   1453: #     $cenvref  -  Reference to the course environment hash returned by 
                   1454: #                  Apache::lonnet::coursedescription
                   1455: #
                   1456: #  Assumptions:
                   1457: #    The globals
                   1458: #    %hash, %paramhash are tied to their gdbm files and we hold the lock on them.
                   1459: #
1.132     raeburn  1460: sub build_tmp_hashes {
                   1461:     my ($uri,$fn,$short,$cenvref) = @_;
1.138     foxr     1462:     
1.132     raeburn  1463:     unless(ref($cenvref) eq 'HASH') {
                   1464:         return;
                   1465:     }
                   1466:     my %cenv = %{$cenvref};
                   1467:     my $gotstate = 0;
1.138     foxr     1468:     %hash=();			# empty the global course and  parameter hashes.
1.132     raeburn  1469:     %parmhash=();
1.138     foxr     1470:     $errtext='';		# No error messages yet.
1.132     raeburn  1471:     $pc=0;
                   1472:     &clear_mapalias_count();
                   1473:     &processversionfile(%cenv);
1.140     foxr     1474: 
                   1475:     # URI Of the map file.
                   1476: 
1.132     raeburn  1477:     my $furi=&Apache::lonnet::clutter($uri);
1.138     foxr     1478:     #
                   1479:     #  the map staring points.
                   1480:     #
1.132     raeburn  1481:     $hash{'src_0.0'}=&versiontrack($furi);
                   1482:     $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
                   1483:     $hash{'ids_'.$furi}='0.0';
                   1484:     $hash{'is_map_0.0'}=1;
1.140     foxr     1485: 
                   1486:     # Load the map.. note that loadmap may implicitly recurse if the map contains 
                   1487:     # sub-maps.
                   1488: 
                   1489: 
1.146     raeburn  1490:     &loadmap($uri,'0.0',$short);
1.140     foxr     1491: 
                   1492:     #  The code below only executes if there is a starting point for the map>
                   1493:     #  Q/BUG??? If there is no start resource for the map should that be an error?
                   1494:     #
                   1495: 
1.132     raeburn  1496:     if (defined($hash{'map_start_'.$uri})) {
                   1497:         &Apache::lonnet::appenv({"request.course.id"  => $short,
                   1498:                                  "request.course.fn"  => $fn,
1.143     raeburn  1499:                                  "request.course.uri" => $uri,
                   1500:                                  "request.course.tied" => time});
1.132     raeburn  1501:         $env{'request.course.id'}=$short;
                   1502:         &traceroute('0',$hash{'map_start_'.$uri},'&');
                   1503:         &accinit($uri,$short,$fn);
                   1504:         &hiddenurls();
1.149.2.2  raeburn  1505:         &mapcrumbs();
1.132     raeburn  1506:     }
                   1507:     $errtext .= &get_mapalias_errors();
                   1508: # ------------------------------------------------------- Put versions into src
                   1509:     foreach my $key (keys(%hash)) {
                   1510:         if ($key=~/^src_/) {
                   1511:             $hash{$key}=&putinversion($hash{$key});
                   1512:         } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
                   1513:             my ($type, $url) = ($1,$2);
                   1514:             my $value = $hash{$key};
                   1515:             $hash{$type.&putinversion($url)}=$value;
                   1516:         }
                   1517:     }
                   1518: # ---------------------------------------------------------------- Encrypt URLs
                   1519:     foreach my $id (keys(%encurl)) {
                   1520: #           $hash{'src_'.$id}=&Apache::lonenc::encrypted($hash{'src_'.$id});
                   1521:         $hash{'encrypted_'.$id}=1;
                   1522:     }
                   1523: # ----------------------------------------------- Close hashes to finally store
                   1524: # --------------------------------- Routine must pass this point, no early outs
                   1525:     $hash{'first_rid'}=$retfrid;
                   1526:     my ($mapid,$resid)=split(/\./,$retfrid);
                   1527:     $hash{'first_mapurl'}=$hash{'map_id_'.$mapid};
                   1528:     my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$retfrid});
                   1529:     $retfurl=&add_get_param($hash{'src_'.$retfrid},{ 'symb' => $symb });
                   1530:     if ($hash{'encrypted_'.$retfrid}) {
                   1531:         $retfurl=&Apache::lonenc::encrypted($retfurl,(&Apache::lonnet::allowed('adv') ne 'F'));
                   1532:     }
                   1533:     $hash{'first_url'}=$retfurl;
                   1534: # ---------------------------------------------------- Store away initial state
                   1535:     {
                   1536:         my $cfh;
1.149.2.2  raeburn  1537:         if (open($cfh,">","$fn.state")) {
1.132     raeburn  1538:             print $cfh join("\n",@cond);
                   1539:             $gotstate = 1;
                   1540:         } else {
                   1541:             &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                   1542:                                      "Could not write statemap $fn for $uri.</font>");
                   1543:         }
                   1544:     }
                   1545:     return $gotstate;
                   1546: }
                   1547: 
                   1548: sub unlink_tmpfiles {
                   1549:     my ($fn) = @_;
1.138     foxr     1550:     my $file_dir = dirname($fn);
                   1551: 
1.145     raeburn  1552:     if ("$file_dir/" eq LONCAPA::tempdir()) {
1.132     raeburn  1553:         my @files = qw (.db _symb.db .state _parms.db);
                   1554:         foreach my $file (@files) {
                   1555:             if (-e $fn.$file) {
                   1556:                 unless (unlink($fn.$file)) {
                   1557:                     &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                   1558:                                  "Could not unlink ".$fn.$file."</font>");
                   1559:                 }
                   1560:             }
                   1561:         }
                   1562:     }
                   1563:     return;
                   1564: }
                   1565: 
1.15      www      1566: # ------------------------------------------------------- Evaluate state string
                   1567: 
                   1568: sub evalstate {
1.89      albertel 1569:     my $fn=$env{'request.course.fn'}.'.state';
1.80      albertel 1570:     my $state='';
1.15      www      1571:     if (-e $fn) {
1.80      albertel 1572: 	my @conditions=();
                   1573: 	{
1.149.2.2  raeburn  1574: 	    open(my $fh,"<",$fn);
1.80      albertel 1575: 	    @conditions=<$fh>;
1.115     raeburn  1576:             close($fh);
1.80      albertel 1577: 	}  
                   1578: 	my $safeeval = new Safe;
                   1579: 	my $safehole = new Safe::Hole;
                   1580: 	$safeeval->permit("entereval");
                   1581: 	$safeeval->permit(":base_math");
                   1582: 	$safeeval->deny(":base_io");
                   1583: 	$safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                   1584: 	foreach my $line (@conditions) {
                   1585: 	    chomp($line);
                   1586: 	    my ($condition,$weight)=split(/\:/,$line);
                   1587: 	    if ($safeeval->reval($condition)) {
                   1588: 		if ($weight eq 'force') {
                   1589: 		    $state.='3';
                   1590: 		} else {
                   1591: 		    $state.='2';
                   1592: 		}
                   1593: 	    } else {
                   1594: 		if ($weight eq 'stop') {
                   1595: 		    $state.='0';
                   1596: 		} else {
                   1597: 		    $state.='1';
                   1598: 		}
                   1599: 	    }
                   1600: 	}
1.15      www      1601:     }
1.128     raeburn  1602:     &Apache::lonnet::appenv({'user.state.'.$env{'request.course.id'} => $state});
1.15      www      1603:     return $state;
                   1604: }
                   1605: 
1.138     foxr     1606: #  This block seems to have code to manage/detect doubly defined
                   1607: #  aliases in maps.
                   1608: 
1.122     albertel 1609: {
                   1610:     my %mapalias_cache;
                   1611:     sub count_mapalias {
                   1612: 	my ($value,$resid) = @_;
                   1613:  	push(@{ $mapalias_cache{$value} }, $resid);
                   1614:     }
                   1615: 
                   1616:     sub get_mapalias_errors {
                   1617: 	my $error_text;
                   1618: 	foreach my $mapalias (sort(keys(%mapalias_cache))) {
                   1619: 	    next if (scalar(@{ $mapalias_cache{$mapalias} } ) == 1);
                   1620: 	    my $count;
                   1621: 	    my $which =
                   1622: 		join('</li><li>', 
                   1623: 		     map {
                   1624: 			 my $id = $_;
                   1625: 			 if (exists($hash{'src_'.$id})) {
                   1626: 			     $count++;
                   1627: 			 }
                   1628: 			 my ($mapid) = split(/\./,$id);
1.147     raeburn  1629:                          &mt('Resource [_1][_2]in Map [_3]',
                   1630: 			     $hash{'title_'.$id},'<br />',
1.122     albertel 1631: 			     $hash{'title_'.$hash{'ids_'.$hash{'map_id_'.$mapid}}});
                   1632: 		     } (@{ $mapalias_cache{$mapalias} }));
                   1633: 	    next if ($count < 2);
                   1634: 	    $error_text .= '<div class="LC_error">'.
                   1635: 		&mt('Error: Found the mapalias "[_1]" defined multiple times.',
                   1636: 		    $mapalias).
                   1637: 		'</div><ul><li>'.$which.'</li></ul>';
                   1638: 	}
                   1639: 	&clear_mapalias_count();
                   1640: 	return $error_text;
                   1641:     }
                   1642:     sub clear_mapalias_count {
                   1643: 	undef(%mapalias_cache);
                   1644:     }
                   1645: }
1.1       www      1646: 1;
                   1647: __END__
                   1648: 
1.26      harris41 1649: =head1 NAME
                   1650: 
                   1651: Apache::lonuserstate - Construct and maintain state and binary representation
                   1652: of course for user
                   1653: 
                   1654: =head1 SYNOPSIS
                   1655: 
                   1656: Invoked by lonroles.pm.
                   1657: 
                   1658: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                   1659: 
                   1660: =head1 INTRODUCTION
                   1661: 
                   1662: This module constructs and maintains state and binary representation
                   1663: of course for user.
                   1664: 
                   1665: This is part of the LearningOnline Network with CAPA project
                   1666: described at http://www.lon-capa.org.
                   1667: 
1.129     jms      1668: =head1 SUBROUTINES
1.26      harris41 1669: 
1.129     jms      1670: =over
1.26      harris41 1671: 
1.129     jms      1672: =item loadmap()
1.26      harris41 1673: 
1.129     jms      1674: Loads map from disk
1.26      harris41 1675: 
1.129     jms      1676: =item simplify()
1.26      harris41 1677: 
1.129     jms      1678: Simplify expression
1.26      harris41 1679: 
1.129     jms      1680: =item traceroute()
1.26      harris41 1681: 
1.129     jms      1682: Build condition hash
1.26      harris41 1683: 
1.129     jms      1684: =item accinit()
1.26      harris41 1685: 
1.129     jms      1686: Cascading conditions, quick access, parameters
1.26      harris41 1687: 
1.129     jms      1688: =item readmap()
1.26      harris41 1689: 
1.129     jms      1690: Read map and all submaps
1.1       www      1691: 
1.129     jms      1692: =item evalstate()
1.1       www      1693: 
1.129     jms      1694: Evaluate state string
1.1       www      1695: 
1.26      harris41 1696: =back
1.1       www      1697: 
1.26      harris41 1698: =cut

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