File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.149.2.2.2.3: download - view: text, annotated - select for diffs
Sun Jul 19 15:24:11 2020 UTC (3 years, 9 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.158, 1.159, 1.160

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

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