File:  [LON-CAPA] / loncom / lonmap.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Oct 6 10:56:49 2011 UTC (12 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Works correctly for simple maps with random out/randompick.
(need to take out the log spew of course).

    1: # The LearningOnline Network
    2: #
    3: #  Read maps into a 'big hash'.
    4: #
    5: # $Id: lonmap.pm,v 1.3 2011/10/06 10:56:49 foxr Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: package Apache::lonmap;
   32: use strict;
   33: 
   34: #------------- Required external modules.
   35: 
   36: use Error qw(:try);
   37: 
   38: use HTML::TokeParser;
   39: 
   40: 
   41: use LONCAPA;
   42: use Apache::lonnet;
   43: use Data::Dumper;
   44: 
   45: 
   46: #------------- File scoped variables:
   47: 
   48: my $map_number = 0;		# keep track of maps within the course.
   49: my $course_id;     		# Will be the id of the course being read in.
   50: 
   51: #
   52: # The variables below are auxiliary hashes.  They will be tacked onto the
   53: # big hash though currently not used by clients.. you never know about later.
   54: #
   55: 
   56: my %randompick;
   57: my %randompickseed;
   58: my %randomorder;
   59: my %encurl;
   60: my %hiddenurl;
   61: my %parmhash;
   62: my @cond;			# Array of conditions.
   63: my $retfrid;
   64: #
   65: #  Other stuff we make global (sigh) so that it does not need
   66: #  to be passed around all the time:
   67: #
   68: 
   69: my $username;			# User for whom the map is being read.
   70: my $userdomain;  		# Domain the user lives in.
   71: my %mapalias_cache;		# Keeps track of map aliases -> resources detects duplicates.
   72: my %cenv;			# Course environment.
   73: 
   74: #------------- Executable code: 
   75: 
   76: 
   77: #----------------------------------------------------------------
   78: #
   79: #  General utilities:
   80: 
   81: 
   82: #
   83: #  I _think_ this does common sub-expression simplification and 
   84: #  optimization for condition strings...based on common pattern matching.
   85: # Parameters:
   86: #    expression - the condition expression string.
   87: # Returns:
   88: #    The optimized expression if an optimization could be found.
   89: #
   90: # NOTE:
   91: #   Added repetetive optimization..it's possible that an
   92: #   optimization results in an expression that can be recognized further in
   93: #   a subsequent optimization pass:
   94: #
   95: 
   96: sub simplify {
   97:     my $expression=shift;
   98:     my $prior = '';		# This is safe as a null expression is pretty optimal.
   99:     
  100:     while ($prior ne $expression) {
  101: 	$prior = $expression;	# Stop when the substitutions below do nothing.
  102: # (0&1) = 1
  103: 	$expression=~s/\(0\&([_\.\d]+)\)/$1/g;
  104: # (8)=8
  105: 	$expression=~s/\(([_\.\d]+)\)/$1/g;
  106: # 8&8=8
  107: 	$expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
  108: # 8|8=8
  109: 	$expression=~s/([^_\.\d])([_\.\d]+)(?:\|\2)+([^_\.\d])/$1$2$3/g;
  110: # (5&3)&4=5&3&4
  111: 	$expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
  112: # (((5&3)|(4&6)))=((5&3)|(4&6))
  113: 	$expression=~
  114: 	    s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
  115: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  116: 	$expression=~
  117: 	    s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
  118:     }
  119:     return $expression;
  120: }
  121:     
  122: 
  123: #
  124: #  Merge the conditions into the big hash
  125: #  these will result in hash entries of the form:
  126: #   'condition.n'  where 'n' is the array index of the condition in the
  127: #   @cond array above.
  128: #
  129: #  Parameters:
  130: #    $hash - big hashthat's being built up.
  131: #
  132: sub merge_conditions {
  133:     my $hash = shift;
  134: 
  135:     for (my $i = 0; $i < scalar(@cond); $i++) {
  136: 	$hash->{'condition' . '.' . $i} = $cond[$i];
  137:     }
  138: }
  139: 
  140: # Merge the contents of a 'child hash' into a parent hash hanging it off another key.
  141: # This is _not_ done by hanging a reference to the child hash as the parent may be
  142: # bound to a GDBM file e.g. and shared by more than one process ..and references are
  143: # pretty clearly not going to work across process boundaries.
  144: #
  145: # Parameters:
  146: #   $parent  - The hash to which the child will be merged (reference)
  147: #   $key     - The key in the parent hash on which the child elements will be hung.
  148: #              given a key named $childkey the final parent hash entry will be
  149: #              $parent . '.' $childkey
  150: #  $child    - The hash whose contents we merge into the parent (reference)
  151: #
  152: sub merge_hash {
  153:     my ($parent, $key, $child) = @_;
  154: 
  155:     if ($key ne '') {
  156: 	$key .= '.';		# If we are prefixing, prefix then .
  157:     }
  158: 
  159:     foreach my $childkey (keys (%$child)) {
  160: 	$parent->{$key . $childkey} = $child->{$childkey};
  161:     }
  162: }
  163: 
  164: #----------------------------------------------------------------------------------
  165: #
  166: #   Code to keep track of map aliases and to determine if there are doubly 
  167: #   defined aliases.
  168: #
  169: 
  170: #
  171: #  Maintains the mapalias hash.  This is a hash of arrays.  Each array
  172: #  is indexed by the alias and contains the set of resource ids pointed to by that
  173: #  alias.  In an ideal world, there will only be one element in each array.
  174: #  The point of this, however is to determine which aliases might be doubley defined
  175: #  due to map nesting e.g.
  176: #
  177: #  Parameters:
  178: #    $value   - Alias name.
  179: #    $resid   - Resource id pointed to by the alias.
  180: #
  181: #    
  182: sub count_mapalias {
  183:     my ($value,$resid) = @_;
  184:     push(@{ $mapalias_cache{$value} }, $resid);
  185: }
  186: #
  187: #  Looks at each key in the mapalias hash and, for each case where an
  188: #  alias points to more than one value adds an error text to the
  189: #  result string.'
  190: #
  191: #  Parameters:
  192: #     hash - Reference to the hash we are trying t build up.
  193: #  Implicit inputs
  194: #     %mapalias - a hash that is indexed by map aliases and contains for each key
  195: #                 an array of the resource id's the alias 'points to'.
  196: # Returns:
  197: #    A hopefully empty string of messages that descsribe the aliases that have more
  198: #    than one value.  This string is formatted like an html list.
  199: #
  200: #
  201: sub get_mapalias_errors {
  202:     my $hash = shift;
  203:     my $error_text;
  204:     foreach my $mapalias (sort(keys(%mapalias_cache))) {
  205: 	next if (scalar(@{ $mapalias_cache{$mapalias} } ) == 1);
  206: 	my $count;
  207: 	my $which =
  208: 	    join('</li><li>', 
  209: 		 map {
  210: 		     my $id = $_;
  211: 		     if (exists($hash->{'src_'.$id})) {
  212: 			 $count++;
  213: 		     }
  214: 		     my ($mapid) = split(/\./,$id);
  215: 		     &mt('Resource "[_1]" <br /> in Map "[_2]"',
  216: 			 $hash->{'title_'.$id},
  217: 			 $hash->{'title_'.$hash->{'ids_'.$hash->{'map_id_'.$mapid}}});
  218: 		 } (@{ $mapalias_cache{$mapalias} }));
  219: 	next if ($count < 2);
  220: 	$error_text .= '<div class="LC_error">'.
  221: 	    &mt('Error: Found the mapalias "[_1]" defined multiple times.',
  222: 		$mapalias).
  223: 		'</div><ul><li>'.$which.'</li></ul>';
  224:     }
  225:     &clear_mapalias_count();
  226:     return $error_text;
  227: }
  228: #
  229: #   Clears the map aliase hash.
  230: #
  231: sub clear_mapalias_count {
  232:     undef(%mapalias_cache);
  233: }
  234: 
  235: #----------------------------------------------------------------
  236: #
  237: #  Code dealing with resource versions.
  238: #
  239: 
  240: #
  241: #  Put a version into a src element of a hash or url:
  242: #
  243: #  Parameters:
  244: #     uri - URI into which the version must be added.
  245: #    hash - Reference to the hash being built up.
  246: #    short- Short coursename.
  247: #
  248: 
  249: sub putinversion {
  250:     my ($uri, $hash, $short) = @_;
  251:     my $key=$short.'_'.&Apache::lonnet::clutter($uri);
  252:     if ($hash->{'version_'.$uri}) {
  253: 	my $version=$hash->{'version_'.$uri};
  254: 	if ($version eq 'mostrecent') { return $uri; }
  255: 	if ($version eq &Apache::lonnet::getversion(
  256: 			&Apache::lonnet::filelocation('',$uri))) 
  257: 	             { return $uri; }
  258: 	$uri=~s/\.(\w+)$/\.$version\.$1/;
  259:     }
  260:     &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
  261:     return $uri;
  262: }
  263: 
  264: 
  265: #
  266: #  Create hash entries for each version of the course.
  267: # Parameters:
  268: #   $cenv    - Reference to a course environment from lonnet::coursedescription.
  269: #   $hash    - Reference to a hash that will be populated.
  270: 
  271: #
  272: sub process_versions {
  273:     my ($cenv, $hash) = @_;
  274: 
  275:     
  276:     my %versions = &Apache::lonnet::dump('resourceversions',
  277: 					 $cenv->{'domain'},
  278: 					 $cenv->{'num'});
  279: 
  280:     foreach my $ver (keys (%versions)) {
  281: 	if ($ver =~/^error\:/) { # lonc/lond transaction failed.
  282: 	    throw Error::Simple('lonc/lond returned error: ' . $ver);
  283: 	}
  284: 	$hash->{'version_'.$ver} = $versions{$ver};
  285:     }
  286: }
  287: 
  288: #
  289: #  Generate text for a version discrepancy error:
  290: # Parameters:
  291: #  $uri   - URI of the resource.
  292: #  $used  - Version used.
  293: #  $unused - Veresion of duplicate.
  294: #
  295: sub versionerror {
  296:     my ($uri, $used, $unused) = @_;
  297:     my ($uri,$usedversion,$unusedversion)=@_;
  298:     return '<br />'.
  299: 	&mt('Version discrepancy: resource [_1] included in both version [_2] and version [_3]. Using version [_2].',
  300: 	    $uri,$used,$unused).'<br />';
  301: 
  302: }
  303: 
  304: #  Removes the version number from a URI and returns the resulting
  305: #  URI (e.g. mumbly.version.stuff => mumbly.stuff).
  306: #
  307: #   If the URI has not been seen with a version before the
  308: #   hash{'version_'.resultingURI} is set to the  version number.
  309: #   If the hash has already been seen, but differs then
  310: #   an error is raised.
  311: #
  312: # Parameters:
  313: #   $uri  -  potentially with a version.
  314: #   $hash -  reference to a hash to fill in. 
  315: # Returns:
  316: #   URI with the version cut out.
  317: #
  318: sub versiontrack {
  319:     my ($uri, $hash) = @_;
  320: 
  321: 
  322:     if ($uri=~/\.(\d+)\.\w+$/) { # URI like *.n.text it's version 'n'
  323: 	my $version=$1;
  324: 	$uri=~s/\.\d+\.(\w+)$/\.$1/; # elide the version.
  325:         unless ($hash->{'version_'.$uri}) {
  326: 	    $hash->{'version_'.$uri}=$version;
  327: 	} elsif ($version!=$hash->{'version_'.$uri}) {
  328: 	    throw Error::Simple(&versionerror($uri, $hash->{'version_'.$uri}, $version));
  329:         }
  330:     }
  331:     return $uri;
  332: }
  333: #
  334: #  Appends the version of a resource to its uri and also caches the 
  335: #  URI (contents?) on the local server
  336: #
  337: #  Parameters:
  338: #     $uri   - URI of the course (without version informatino.
  339: #     $hash  - What we have of  the big hash.
  340: #
  341: # Side-Effects:
  342: #   The URI is cached by memcached.
  343: #
  344: # Returns:
  345: #    The version appended URI.
  346: #
  347: sub append_version {
  348:     my ($uri, $hash) = @_;
  349: 
  350:     # Create the key for the cache entry.
  351: 
  352:     my $key = $course_id . '_' . &Apache::lonnet::clutter($uri);
  353: 
  354:     # If there is a version it will already be  in the hash:
  355: 
  356:     if ($hash->{'version_' . $uri}) {
  357: 	my $version = $hash->{'version_' . $uri};
  358: 	if ($version eq 'mostrecent') {
  359: 	    return $uri;     # Most recent version does not require decoration (or caching?).
  360: 	}
  361: 	if ($version eq 
  362: 	    &Apache::lonnet::getversion(&Apache::lonnet::filelocation('', $uri))) {
  363: 	    return $uri;	# version matches the most recent file version?
  364: 	}
  365: 	$uri =~ s/\.(\w+)$/\.$version\.$1/; # insert the versino prior to the last .word.
  366:     }
  367:  
  368:    # cache the version:
  369: 
  370:    &Apache::lonnet::do_cache_new('courseresversion', $key, 
  371: 				 &Apache::lonnet::declutter($uri), 600);
  372: 
  373:     return $uri;
  374: 
  375: }
  376: #--------------------------------------------------------------------------------
  377: # Post processing subs:
  378: sub hiddenurls {
  379:     my $hash = shift;
  380: 
  381:     my $uname    = $hash->{'context.username'};
  382:     my $udom     = $hash->{'context.userdom'};
  383:     my $courseid = $hash->{'context.courseid'};
  384: 
  385:     my $randomoutentry='';
  386:     foreach my $rid (keys %randompick) {
  387:         my $rndpick=$randompick{$rid};
  388:         my $mpc=$hash->{'map_pc_'.$hash->{'src_'.$rid}};
  389: # ------------------------------------------- put existing resources into array
  390:         my @currentrids=();
  391:         foreach my $key (sort(keys(%$hash))) {
  392: 	    if ($key=~/^src_($mpc\.\d+)/) {
  393: 		if ($hash->{'src_'.$1}) { push @currentrids, $1; }
  394:             }
  395:         }
  396: 	# rids are number.number and we want to numercially sort on 
  397:         # the second number
  398: 	@currentrids=sort {
  399: 	    my (undef,$aid)=split(/\./,$a);
  400: 	    my (undef,$bid)=split(/\./,$b);
  401: 	    $aid <=> $bid;
  402: 	} @currentrids;
  403:         next if ($#currentrids<$rndpick);
  404: # -------------------------------- randomly eliminate the ones that should stay
  405: 	my (undef,$id)=split(/\./,$rid);
  406:         if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
  407: 	my $rndseed=&Apache::lonnet::rndseed($id, $courseid, $udom, $uname, \%cenv); # use id instead of symb
  408: 	&Apache::lonnet::logthis("lonmap random seed: $rndseed");
  409: 	&Apache::lonnet::setup_random_from_rndseed($rndseed);
  410: 	my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
  411:         for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
  412: 
  413: # -------------------------------------------------------- delete the leftovers
  414:         for (my $k=0; $k<=$#currentrids; $k++) {
  415:             if ($currentrids[$k]) {
  416: 		$hash->{'randomout_'.$currentrids[$k]}='1';
  417:                 my ($mapid,$resid)=split(/\./,$currentrids[$k]);
  418:                 $randomoutentry.='&'.
  419: 		    &Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
  420: 						 $resid,
  421: 						 $hash->{'src_'.$currentrids[$k]}
  422: 						 ).'&';
  423:             }
  424:         }
  425:     }
  426: # ------------------------------ take care of explicitly hidden urls or folders
  427:     foreach my $rid (keys %hiddenurl) {
  428: 	$hash->{'randomout_'.$rid}='1';
  429: 	my ($mapid,$resid)=split(/\./,$rid);
  430: 	$randomoutentry.='&'.
  431: 	    &Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},$resid,
  432: 					 $hash->{'src_'.$rid}).'&';
  433:     }
  434: # --------------------------------------- add randomout to the hash.
  435:     if ($randomoutentry) {
  436: 	$hash->{'acc.randomout'} = $randomoutentry;
  437: 
  438:     }
  439: }
  440: 
  441: #
  442: # It's not so clear to me what this sub does.
  443: #
  444: #  Parameters
  445: #     uri   - URI from the course description hash.
  446: #     short - Course short name.
  447: #     fn    - Course filename.
  448: #     hash  - Reference to the big hash as filled in so far
  449: #       
  450: 
  451: sub accinit {
  452:     my ($uri, $short, $hash)=@_;
  453:     my %acchash=();
  454:     my %captured=();
  455:     my $condcounter=0;
  456:     $acchash{'acc.cond.'.$short.'.0'}=0;
  457: 
  458:     # This loop is only interested in conditions and 
  459:     # parameters in the big hash:
  460: 
  461:     foreach my $key (keys(%$hash)) {
  462: 
  463: 	# conditions:
  464: 
  465: 	if ($key=~/^conditions/) {
  466: 	    my $expr=$hash->{$key};
  467: 
  468: 	    # try to find and factor out common sub-expressions
  469: 	    # Any subexpression that is found is simplified, removed from
  470: 	    # the original condition expression and the simplified sub-expression
  471: 	    # substituted back in to the epxression..I'm not actually convinced this
  472: 	    # factors anything out...but instead maybe simplifies common factors(?)
  473: 
  474: 	    foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
  475: 		my $orig=$sub;
  476: 
  477: 		my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
  478: 		next if (!defined($factor));
  479: 
  480: 		$sub=~s/\Q$factor\E//g;
  481: 		$sub=~s/^\(/\($factor\(/;
  482: 		$sub.=')';
  483: 		$sub=simplify($sub);
  484: 		$expr=~s/\Q$orig\E/$sub/;
  485: 	    }
  486: 	    $hash->{$key}=$expr;
  487: 
  488:            # If not yet seen, record in acchash and that we've seen it.
  489: 
  490: 	    unless (defined($captured{$expr})) {
  491: 		$condcounter++;
  492: 		$captured{$expr}=$condcounter;
  493: 		$acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  494: 	    } 
  495:         # Parameters:
  496: 
  497: 	} elsif ($key=~/^param_(\d+)\.(\d+)/) {
  498: 	    my $prefix=&Apache::lonnet::encode_symb($hash->{'map_id_'.$1},$2,
  499: 						    $hash->{'src_'.$1.'.'.$2});
  500: 	    foreach my $param (split(/\&/,$hash->{$key})) {
  501: 		my ($typename,$value)=split(/\=/,$param);
  502: 		my ($type,$name)=split(/\:/,$typename);
  503: 		$parmhash{$prefix.'.'.&unescape($name)}=
  504: 		    &unescape($value);
  505: 		$parmhash{$prefix.'.'.&unescape($name).'.type'}=
  506: 		    &unescape($type);
  507: 	    }
  508: 	}
  509:     }
  510:     # This loop only processes id entries in the big hash.
  511: 
  512:     foreach my $key (keys(%$hash)) {
  513: 	if ($key=~/^ids/) {
  514: 	    foreach my $resid (split(/\,/,$hash->{$key})) {
  515: 		my $uri=$hash->{'src_'.$resid};
  516: 		my ($uripath,$urifile) =
  517: 		    &Apache::lonnet::split_uri_for_cond($uri);
  518: 		if ($uripath) {
  519: 		    my $uricond='0';
  520: 		    if (defined($hash->{'conditions_'.$resid})) {
  521: 			$uricond=$captured{$hash->{'conditions_'.$resid}};
  522: 		    }
  523: 		    if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  524: 			if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  525: 			    /(\&\Q$urifile\E\:[^\&]*)/) {
  526: 			    my $replace=$1;
  527: 			    my $regexp=$replace;
  528: 			    #$regexp=~s/\|/\\\|/g;
  529: 			    $acchash{'acc.res.'.$short.'.'.$uripath} =~
  530: 				s/\Q$regexp\E/$replace\|$uricond/;
  531: 			} else {
  532: 			    $acchash{'acc.res.'.$short.'.'.$uripath}.=
  533: 				$urifile.':'.$uricond.'&';
  534: 			}
  535: 		    } else {
  536: 			$acchash{'acc.res.'.$short.'.'.$uripath}=
  537: 			    '&'.$urifile.':'.$uricond.'&';
  538: 		    }
  539: 		} 
  540: 	    }
  541:         }
  542:     }
  543:     $acchash{'acc.res.'.$short.'.'}='&:0&';
  544:     my $courseuri=$uri;
  545:     $courseuri=~s/^\/res\///;
  546:     my $regexp = 1;
  547:  
  548:     &merge_hash($hash, '', \%acchash); # there's already an acc prefix in the hash keys.
  549: 
  550: 
  551: }
  552: 
  553: 
  554: #
  555: #  Traces a route recursively through the map after it has been loaded
  556: #  (I believe this really visits each resource that is reachable fromt he
  557: #  start top node.
  558: #
  559: #  - Marks hidden resources as hidden.
  560: #  - Marks which resource URL's must be encrypted.
  561: #  - Figures out (if necessary) the first resource in the map.
  562: #  - Further builds the chunks of the big hash that define how 
  563: #    conditions work
  564: #
  565: #  Note that the tracing strategy won't visit resources that are not linked to
  566: #  anything or islands in the map (groups of resources that form a path but are not
  567: #  linked in to the path that can be traced from the start resource...but that's ok
  568: #  because by definition, those resources are not reachable by users of the course.
  569: #
  570: # Parameters:
  571: #   sofar    - _URI of the prior entry or 0 if this is the top.
  572: #   rid      - URI of the resource to visit.
  573: #   beenhere - list of resources (each resource enclosed by &'s) that have
  574: #              already been visited.
  575: #   encflag  - If true the resource that resulted in a recursive call to us
  576: #              has an encoded URL (which means contained resources should too). 
  577: #   hdnflag  - If true,the resource that resulted in a recursive call to us
  578: #              was hidden (which means contained resources should be hidden too).
  579: #   hash     - Reference to the hash we are traversing.
  580: # Returns
  581: #    new value indicating how far the map has been traversed (the sofar).
  582: #
  583: sub traceroute {
  584:     my ($sofar, $rid, $beenhere, $encflag, $hdnflag, $hash)=@_;
  585:     my $newsofar=$sofar=simplify($sofar);
  586: 
  587:     unless ($beenhere=~/\&\Q$rid\E\&/) {
  588: 	$beenhere.=$rid.'&';  
  589: 	my ($mapid,$resid)=split(/\./,$rid);
  590: 	my $symb=&Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},$resid,
  591: 					      $hash->{'src_'.$rid});
  592: 	my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
  593: 
  594: 	if ($hdnflag || lc($hidden) eq 'yes') {
  595: 	    $hiddenurl{$rid}=1;
  596: 	}
  597: 	if (!$hdnflag && lc($hidden) eq 'no') {
  598: 	    delete($hiddenurl{$rid});
  599: 	}
  600: 
  601: 	my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
  602: 	if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
  603: 
  604: 	if (($retfrid eq '') && ($hash->{'src_'.$rid})
  605: 	    && ($hash->{'src_'.$rid}!~/\.sequence$/)) {
  606: 	    $retfrid=$rid;
  607: 	}
  608: 
  609: 	if (defined($hash->{'conditions_'.$rid})) {
  610: 	    $hash->{'conditions_'.$rid}=simplify(
  611:            '('.$hash->{'conditions_'.$rid}.')|('.$sofar.')');
  612: 	} else {
  613: 	    $hash->{'conditions_'.$rid}=$sofar;
  614: 	}
  615: 
  616: 	# if the expression is just the 0th condition keep it
  617: 	# otherwise leave a pointer to this condition expression
  618: 
  619: 	$newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
  620: 
  621: 	# Recurse if the resource is a map:
  622: 
  623: 	if (defined($hash->{'is_map_'.$rid})) {
  624: 	    if (defined($hash->{'map_start_'.$hash->{'src_'.$rid}})) {
  625: 		$sofar=$newsofar=
  626: 		    &traceroute($sofar,
  627: 				$hash->{'map_start_'.$hash->{'src_'.$rid}},
  628: 				$beenhere,
  629: 				$encflag || $encurl{$rid},
  630: 				$hdnflag || $hiddenurl{$rid}, $hash);
  631: 	    }
  632: 	}
  633: 
  634: 	# Processes  links to this resource:
  635: 	#  - verify the existence of any conditionals on the link to here.
  636: 	#  - Recurse to any resources linked to us.
  637: 	#
  638: 	if (defined($hash->{'to_'.$rid})) {
  639: 	    foreach my $id (split(/\,/,$hash->{'to_'.$rid})) {
  640: 		my $further=$sofar;
  641: 		#
  642: 		# If there's a condition associated with this link be sure
  643: 		# it's been defined else that's an error:
  644: 		#
  645:                 if ($hash->{'undercond_'.$id}) {
  646: 		    if (defined($hash->{'condid_'.$hash->{'undercond_'.$id}})) {
  647: 			$further=simplify('('.'_'.$rid.')&('.
  648: 					  $hash->{'condid_'.$hash->{'undercond_'.$id}}.')');
  649: 		    } else {
  650: 			my $errtext.=&mt('<br />Undefined condition ID: [_1]',$hash->{'undercond_'.$id});
  651: 			throw Error::Simple($errtext);
  652: 		    }
  653:                 }
  654: 		#  Recurse to resoruces that have to's to us.
  655:                 $newsofar=&traceroute($further,$hash->{'goesto_'.$id},$beenhere,
  656: 				      $encflag,$hdnflag, $hash);
  657: 	    }
  658: 	}
  659:     }
  660:     return $newsofar;
  661: }
  662: 
  663: 
  664: #---------------------------------------------------------------------------------
  665: #
  666: #  Map parsing code:
  667: #
  668: 
  669: # 
  670: #  Parse the <param> tag.  for most parameters, the only action is to define/extend
  671: #  a has entry for {'param_{refid}'} where refid is the resource the parameter is
  672: #  attached to and the value built up is an & separated list of parameters of the form:
  673: #  type:part.name=value
  674: #
  675: #   In addition there is special case code for:
  676: #   - randompick
  677: #   - randompickseed
  678: #   - randomorder
  679: #
  680: #   - encrypturl
  681: #   - hiddenresource
  682: #
  683: # Parameters:
  684: #    token - The token array from HTML::TokeParse  we mostly care about element [2]
  685: #            which is a hash of attribute => values supplied in the tag
  686: #            (remember this sub is only processing start tag tokens).
  687: #    mno   - Map number.  This is used to qualify resource ids within a map
  688: #            to make them unique course wide (a process known as uniquifaction).
  689: #    hash  - Reference to the hash we are building.
  690: #
  691: sub parse_param {
  692:     my ($token, $mno, $hash)  = @_;
  693: 
  694:     # Qualify the reference and name by the map number and part number.
  695:     # if no explicit part number is supplied, 0 is the implicit part num.
  696: 
  697:     my $referid=$mno.'.'.$token->[2]->{'to'}; # Resource param applies to.
  698:     my $name=$token->[2]->{'name'};	      # Name of parameter
  699:     my $part;
  700: 
  701: 
  702:     if ($name=~/^parameter_(.*)_/) { 
  703: 	$part=$1;
  704:     } else {
  705: 	$part=0;
  706:     }
  707: 
  708:     # Peel the parameter_ off the parameter name.
  709: 
  710:     $name=~s/^.*_([^_]*)$/$1/;
  711: 
  712:     # The value is:
  713:     #   type.part.name.value
  714: 
  715:     my $newparam=
  716: 	&escape($token->[2]->{'type'}).':'.
  717: 	&escape($part.'.'.$name).'='.
  718: 	&escape($token->[2]->{'value'});
  719: 
  720:     # The hash key is param_resourceid.
  721:     # Multiple parameters for a single resource are & separated in the hash.
  722: 
  723: 
  724:     if (defined($hash->{'param_'.$referid})) {
  725: 	$hash->{'param_'.$referid}.='&'.$newparam;
  726:     } else {
  727: 	$hash->{'param_'.$referid}=''.$newparam;
  728:     }
  729:     #
  730:     #  These parameters have to do with randomly selecting
  731:     # resources, therefore a separate hash is also created to 
  732:     # make it easy to locate them when actually computing the resource set later on
  733:     # See the code conditionalized by ($randomize) in read_map().
  734: 
  735:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) { # Random selection turned on
  736: 	$randompick{$referid}=$token->[2]->{'value'};
  737:     }
  738:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) { # Randomseed provided.
  739: 	$randompickseed{$referid}=$token->[2]->{'value'};
  740:     }
  741:     if ($token->[2]->{'name'}=~/^parameter_(0_)*randomorder$/) { # Random order turned on.
  742: 	$randomorder{$referid}=$token->[2]->{'value'};
  743:     }
  744: 
  745:     # These parameters have to do with how the URLs of resources are presented to
  746:     # course members(?).  encrypturl presents encypted url's while
  747:     # hiddenresource hides the URL.
  748:     #
  749: 
  750:     if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
  751: 	if ($token->[2]->{'value'}=~/^yes$/i) {
  752: 	    $encurl{$referid}=1;
  753: 	}
  754:     }
  755:     if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
  756: 	if ($token->[2]->{'value'}=~/^yes$/i) {
  757: 	    $hiddenurl{$referid}=1;
  758: 	}
  759:     }
  760: 
  761: }
  762: 
  763: 
  764: #
  765: #  Parses a resource tag to produce the value to push into the
  766: #  map_ids array.
  767: # 
  768: #
  769: #  Information about the actual type of resource is provided by the file extension
  770: #  of the uri (e.g. .problem, .sequence etc. etc.).
  771: #
  772: #  Parameters:
  773: #    $token   - A token from HTML::TokeParser
  774: #               This is an array that describes the most recently parsed HTML item.
  775: #    $lpc     - Map nesting level (?)
  776: #    $ispage  - True if this resource is encapsulated in a .page (assembled resourcde).
  777: #    $uri     - URI of the enclosing resource.
  778: #    $hash    - Reference to the hash we are building.
  779: #
  780: # Returns:
  781: #   Value of the id attribute of the tag.
  782: #
  783: # Note:
  784: #   The token is an array that contains the following elements:
  785: #   [0]   => 'S' indicating this is a start token
  786: #   [1]   => 'resource'  indicating this tag is a <resource> tag.
  787: #   [2]   => Hash of attribute =>value pairs.
  788: #   [3]   => @(keys [2]).
  789: #   [4]   => unused.
  790: #
  791: #   The attributes of the resourcde tag include:
  792: #
  793: #   id     - The resource id.
  794: #   src    - The URI of the resource.
  795: #   type   - The resource type (e.g. start and finish).
  796: #   title  - The resource title.
  797: #
  798: 
  799: sub parse_resource {
  800:     my ($token,$lpc,$ispage,$uri, $hash) = @_;
  801:     
  802:     # I refuse to countenance code like this that has 
  803:     # such a dirty side effect (and forcing this sub to be called within a loop).
  804:     #
  805:     #  if ($token->[2]->{'type'} eq 'zombie') { next; }
  806:     #
  807:     #  The original code both returns _and_ skips to the next pass of the >caller's<
  808:     #  loop, that's just dirty.
  809:     #
  810: 
  811:     # Zombie resources don't produce anything useful.
  812: 
  813:     if ($token->[2]->{'type'} eq 'zombie') {
  814: 	return undef;
  815:     }
  816: 
  817:     my $rid=$lpc.'.'.$token->[2]->{'id'}; # Resource id in hash is levelcounter.id-in-xml.
  818: 
  819:     # Save the hash element type and title:
  820: 	    
  821:     $hash->{'kind_'.$rid}='res';
  822:     $hash->{'title_'.$rid}=$token->[2]->{'title'};
  823: 
  824:     # Get the version free URI for the resource.
  825:     # If a 'version' attribute was supplied, and this resource's version 
  826:     # information has not yet been stored, store it.
  827:     #
  828: 
  829: 
  830:     my $turi=&versiontrack($token->[2]->{'src'});
  831:     if ($token->[2]->{'version'}) {
  832: 	unless ($hash->{'version_'.$turi}) {
  833: 
  834: 	    #Where does the value of $1 below come from?
  835: 	    #$1 for the regexps in versiontrack should have gone out of scope.
  836: 	    #
  837: 	    # I think this may be dead code since versiontrack ought to set
  838: 	    # this hash element(?).
  839: 	    #
  840: 	    $hash->{'version_'.$turi}=$1;
  841: 	}
  842:     }
  843:     # Pull out the title and do entity substitution on &colon
  844:     # Q: Why no other entity substitutions?
  845: 
  846:     my $title=$token->[2]->{'title'};
  847:     $title=~s/\&colon\;/\:/gs;
  848: 
  849: 
  850: 
  851:     # I think the point of all this code is to construct a final
  852:     # URI that apache and its rewrite rules can use to
  853:     # fetch the resource.   Thi s sonly necessary if the resource
  854:     # is not a page.  If the resource is a page then it must be
  855:     # assembled (at fetch time?).
  856: 
  857:     unless ($ispage) {
  858: 	$turi=~/\.(\w+)$/;
  859: 	my $embstyle=&Apache::loncommon::fileembstyle($1);
  860: 	if ($token->[2]->{'external'} eq 'true') { # external
  861: 	    $turi=~s/^https?\:\/\//\/adm\/wrapper\/ext\//;
  862: 	} elsif ($turi=~/^\/*uploaded\//) { # uploaded
  863: 	    if (($embstyle eq 'img') 
  864: 		|| ($embstyle eq 'emb')
  865: 		|| ($embstyle eq 'wrp')) {
  866: 		$turi='/adm/wrapper'.$turi;
  867: 	    } elsif ($embstyle eq 'ssi') {
  868: 		#do nothing with these
  869: 	    } elsif ($turi!~/\.(sequence|page)$/) {
  870: 		$turi='/adm/coursedocs/showdoc'.$turi;
  871: 	    }
  872: 	} elsif ($turi=~/\S/) { # normal non-empty internal resource
  873: 	    my $mapdir=$uri;
  874: 	    $mapdir=~s/[^\/]+$//;
  875: 	    $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
  876: 	    if (($embstyle eq 'img') 
  877: 		|| ($embstyle eq 'emb')
  878: 		|| ($embstyle eq 'wrp')) {
  879: 		$turi='/adm/wrapper'.$turi;
  880: 	    }
  881: 	}
  882:     }
  883:     # Store reverse lookup, remove query string resource 'ids'_uri => resource id.
  884:     # If the URI appears more than one time in the sequence, it's resourcde
  885:     # id's are constructed as a comma spearated list.
  886: 
  887:     my $idsuri=$turi;
  888:     $idsuri=~s/\?.+$//;
  889:     if (defined($hash->{'ids_'.$idsuri})) {
  890: 	$hash->{'ids_'.$idsuri}.=','.$rid;
  891:     } else {
  892: 	$hash->{'ids_'.$idsuri}=''.$rid;
  893:     }
  894:     
  895: 
  896: 
  897:     if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard|viewclasslist)$/) {
  898: 	$turi.='?register=1';
  899:     }
  900:     
  901: 
  902:     # resource id lookup:  'src'_resourc-di  => URI decorated with a query
  903:     # parameter as above if necessary due to the resource type.
  904:     
  905:     $hash->{'src_'.$rid}=$turi;
  906: 
  907:     # Mark the external-ness of the resource:
  908:     
  909:     if ($token->[2]->{'external'} eq 'true') {
  910: 	$hash->{'ext_'.$rid}='true:';
  911:     } else {
  912: 	$hash->{'ext_'.$rid}='false:';
  913:     }
  914: 
  915:     # If the resource is a start/finish resource set those
  916:     # entries in the has so that navigation knows where everything starts.
  917:     #   If there is a malformed sequence that has no start or no finish
  918:     # resource, should this be detected and errors thrown?  How would such a 
  919:     # resource come into being other than being manually constructed by a person
  920:     # and then uploaded?  Could that happen if an author decided a sequence was almost
  921:     # right edited it by hand and then reuploaded it to 'fix it' but accidently cut the
  922:     #  start or finish resources?
  923:     #
  924:     #  All resourcess also get a type_id => (start | finish | normal)    hash entr.
  925:     #
  926:     if ($token->[2]->{'type'}) {
  927: 	$hash->{'type_'.$rid}=$token->[2]->{'type'};
  928: 	if ($token->[2]->{'type'} eq 'start') {
  929: 	    $hash->{'map_start_'.$uri}="$rid";
  930: 	}
  931: 	if ($token->[2]->{'type'} eq 'finish') {
  932: 	    $hash->{'map_finish_'.$uri}="$rid";
  933: 	}
  934:     }  else {
  935: 	$hash->{'type_'.$rid}='normal';
  936:     }
  937: 
  938:     # Sequences end pages are constructed entities.  They require that the 
  939:     # map that defines _them_ be loaded as well into the hash...with this resourcde
  940:     # as the base of the nesting.
  941:     # Resources like that are also marked with is_map_id => 1 entries.
  942:     #
  943:     
  944:     if (($turi=~/\.sequence$/) ||
  945: 	($turi=~/\.page$/)) {
  946: 	$hash->{'is_map_'.$rid}='1'; # String in lonuserstate.
  947: 	&read_map($turi,$rid, $hash);
  948:     } 
  949:     return $token->[2]->{'id'};
  950: }
  951: 
  952: #  Links define how you are allowed to move from one resource to another.
  953: #  They are the transition edges in the directed graph that a map is.
  954: #  This sub takes informatino from a <link> tag and constructs the
  955: #  navigation bits and pieces of a map.  There is no requirement that the
  956: #  resources that are linke are already defined, however clearly the map is 
  957: #  badly broken if they are not _eventually_ defined.
  958: #
  959: #  Note that links can be unconditional or conditional.
  960: #
  961: #  Parameters:
  962: #     linkpc   - The link counter for this level of map nesting (this is 
  963: #                reset to zero by read_map prior to starting to process
  964: #                links for map).
  965: #     lpc      - The map level ocounter (how deeply nested this map is in
  966: #                the hierarchy of maps that are recursively read in.
  967: #     to       - resource id (within the XML) of the target of the edge.
  968: #     from     - resource id (within the XML) of the source of the edge.
  969: #     condition- id of condition associated with the edge (also within the XML).
  970: #     hash     - reference to the hash we are building.
  971: 
  972: #
  973: 
  974: sub make_link {
  975:     my ($linkpc,$lpc,$to,$from,$condition, $hash) = @_;
  976:     
  977:     #  Compute fully qualified ids for the link, the 
  978:     # and from/to by prepending lpc.
  979:     #
  980: 
  981:     my $linkid=$lpc.'.'.$linkpc;
  982:     my $goesto=$lpc.'.'.$to;
  983:     my $comesfrom=$lpc.'.'.$from;
  984:     my $undercond='0';
  985: 
  986: 
  987:     # If there is a condition, qualify it with the level counter.
  988: 
  989:     if ($condition) {
  990: 	$undercond=$lpc.'.'.$condition;
  991:     }
  992: 
  993:     # Links are represnted by:
  994:     #  goesto_.fuullyqualifedlinkid => fully qualified to
  995:     #  comesfrom.fullyqualifiedlinkid => fully qualified from
  996:     #  undercond_.fullyqualifiedlinkid => fully qualified condition id.
  997: 
  998:     $hash->{'goesto_'.$linkid}=$goesto;
  999:     $hash->{'comesfrom_'.$linkid}=$comesfrom;
 1000:     $hash->{'undercond_'.$linkid}=$undercond;
 1001: 
 1002:     # In addition:
 1003:     #   to_.fully qualified from => comma separated list of 
 1004:     #   link ids with that from.
 1005:     # Similarly:
 1006:     #   from_.fully qualified to => comma separated list of link ids`
 1007:     #                               with that to.
 1008:     #  That allows us given a resource id to know all edges that go to it
 1009:     #  and leave from it.
 1010:     #
 1011: 
 1012:     if (defined($hash->{'to_'.$comesfrom})) {
 1013: 	$hash->{'to_'.$comesfrom}.=','.$linkid;
 1014:     } else {
 1015: 	$hash->{'to_'.$comesfrom}=''.$linkid;
 1016:     }
 1017:     if (defined($hash->{'from_'.$goesto})) {
 1018: 	$hash->{'from_'.$goesto}.=','.$linkid;
 1019:     } else {
 1020: 	$hash->{'from_'.$goesto}=''.$linkid;
 1021:     }
 1022: }
 1023: 
 1024: # ------------------------------------------------------------------- Condition
 1025: #
 1026: #  Processes <condition> tags, storing sufficient information about them
 1027: #  in the hash so that they can be evaluated and used to conditionalize
 1028: #  what is presented to the student.
 1029: #
 1030: #  these can have the following attributes 
 1031: #
 1032: #    id    = A unique identifier of the condition within the map.
 1033: #
 1034: #    value = Is a perl script-let that, when evaluated in safe space
 1035: #            determines whether or not the condition is true.
 1036: #            Normally this takes the form of a test on an  Apache::lonnet::EXT call
 1037: #            to find the value of variable associated with a resource in the
 1038: #            map identified by a mapalias.
 1039: #            Here's a fragment of XML code that illustrates this:
 1040: #
 1041: #           <param to="5" value="mainproblem" name="parameter_0_mapalias" type="string" />
 1042: #           <resource src="" id="1" type="start" title="Start" />
 1043: #           <resource src="/res/msu/albertel/b_and_c/p1.problem" id="5"  title="p1.problem" />
 1044: #           <condition value="&EXT('user.resource.resource.0.tries','mainproblem')
 1045: #           <2 " id="61" type="stop" />
 1046: #           <link to="5" index="1" from="1" condition="61" />    
 1047: #
 1048: #           In this fragment:
 1049: #             - The param tag establishes an alias to resource id 5 of 'mainproblem'.
 1050: #             - The resource that is the start of the map is identified.
 1051: #             - The resource tag identifies the resource associated with this tag
 1052: #               and gives it the id 5.
 1053: #             - The condition is true if the tries variable associated with mainproblem
 1054: #               is less than 2 (that is the user has had more than 2 tries).
 1055: #               The condition type is a stop condition which inhibits(?) the associated
 1056: #               link if the condition  is false. 
 1057: #             - The link to resource 5 from resource 1 is affected by this condition.    
 1058: #            
 1059: #    type  = Type of the condition. The type determines how the condition affects the
 1060: #            link associated with it and is one of
 1061: #            -  'force'
 1062: #            -  'stop'
 1063: #              anything else including not supplied..which treated as:
 1064: #            - 'normal'.
 1065: #            Presumably maps get created by the resource assembly tool and therefore
 1066: #            illegal type values won't squirm their way into the XML.
 1067: #   hash   - Reference to the hash we are trying to build up.
 1068: #
 1069: # Side effects:
 1070: #   -  The kind_level-qualified-condition-id hash element is set to 'cond'.
 1071: #   -  The condition text is pushed into the cond array and its element number is
 1072: #      set in the condid_level-qualified-condition-id element of the hash.
 1073: #   - The condition type is colon appneded to the cond array element for this condition.
 1074: sub parse_condition {
 1075:     my ($token, $lpc, $hash) = @_;
 1076:     my $rid=$lpc.'.'.$token->[2]->{'id'};
 1077:     
 1078:     $hash->{'kind_'.$rid}='cond';
 1079: 
 1080:     my $condition = $token->[2]->{'value'};
 1081:     $condition =~ s/[\n\r]+/ /gs;
 1082:     push(@cond, $condition);
 1083:     $hash->{'condid_'.$rid}=$#cond;
 1084:     if ($token->[2]->{'type'}) {
 1085: 	$cond[$#cond].=':'.$token->[2]->{'type'};
 1086:     }  else {
 1087: 	$cond[$#cond].=':normal';
 1088:     }
 1089: }
 1090: 
 1091: #
 1092: #  Parse mapalias parameters.
 1093: #  these are tags of the form:
 1094: #  <param to="nn" 
 1095: #         value="some-alias-for-resourceid-nn" 
 1096: #         name="parameter_0_mapalias" 
 1097: #         type="string" />
 1098: #  A map alias is a textual name for a resource:
 1099: #    - The to  attribute identifies the resource (this gets level qualified below)
 1100: #    - The value attributes provides the alias string.
 1101: #    - name must be of the regexp form: /^parameter_(0_)*mapalias$/
 1102: #    - e.g. the string 'parameter_' followed by 0 or more "0_" strings
 1103: #      terminating with the string 'mapalias'.
 1104: #      Examples:
 1105: #         'parameter_mapalias', 'parameter_0_mapalias', parameter_0_0_mapalias'
 1106: #  Invalid to ids are silently ignored.
 1107: #
 1108: #  Parameters:
 1109: #     token - The token array fromthe HMTML::TokeParser
 1110: #     lpc   - The current map level counter.
 1111: #     hash  - Reference to the hash that we are building.
 1112: #
 1113: sub parse_mapalias_param {
 1114:     my ($token, $lpc, $hash) = @_;
 1115: 
 1116:     # Fully qualify the to value and ignore the alias if there is no
 1117:     # corresponding resource.
 1118: 
 1119:     my $referid=$lpc.'.'.$token->[2]->{'to'};
 1120:     return if (!exists($hash->{'src_'.$referid}));
 1121: 
 1122:     # If this is a valid mapalias parameter, 
 1123:     # Append the target id to the count_mapalias element for that
 1124:     # alias so that we can detect doubly defined aliases
 1125:     # e.g.:
 1126:     #  <param to="1" value="george" name="parameter_0_mapalias" type="string" />
 1127:     #  <param to="2" value="george" name="parameter_0_mapalias" type="string" />
 1128:     #
 1129:     #  The example above is trivial but the case that's important has to do with
 1130:     #  constructing a map that includes a nested map where the nested map may have
 1131:     #  aliases that conflict with aliases established in the enclosing map.
 1132:     #
 1133:     # ...and create/update the hash mapalias entry to actually store the alias.
 1134:     #
 1135: 
 1136:     if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
 1137: 	&count_mapalias($token->[2]->{'value'},$referid);
 1138: 	$hash->{'mapalias_'.$token->[2]->{'value'}}=$referid;
 1139:     }
 1140: }
 1141: 
 1142: 
 1143: #---------------------------------------------------------------------------------
 1144: #
 1145: #  Code to process the map file.
 1146: 
 1147: #  read a map file and add it to the hash.  Since a course map can contain resources
 1148: #  that are themselves maps, read_map might be recursively called.
 1149: #
 1150: # Parameters:
 1151: #   $uri         - URI of the course itself (not the map file).
 1152: #   $parent_rid  - map number qualified id of the parent of the map being read.
 1153: #                  For the top level course map this is 0.0.  For the first nested
 1154: #                  map 1.n  where n is the id of the resource within the
 1155: #                  top level map and so on.  
 1156: #   $hash        - Reference to a hash that will become the big hash for the course
 1157: #                  This hash is modified as per the map description.
 1158: # Side-effects:
 1159: #   $map_number - Will be  incremented.   This keeps track of the number of the map
 1160: #                 we are currently working on (see parent_rid above, the number to the
 1161: #                 left of the . in $parent_rid is the map number).
 1162: #
 1163: #  
 1164: sub read_map {
 1165:     my ($uri, $parent_rid, $hash) = @_;
 1166: 
 1167: 
 1168:     # Check for duplication: A map may only be included once.
 1169: 
 1170:     if($hash->{'map_pc_' . $uri}) {
 1171: 	throw Error::Simple('Duplicate map: ', $uri);
 1172:     }
 1173:     # count the map number and save it locally so that we don't lose it
 1174:     # when we recurse.
 1175: 
 1176:     $map_number++;
 1177:     my $lmap_no = $map_number;
 1178: 
 1179:     # save the map_pc and map_id elements of the hash for this map:
 1180:     #  map_pc_uri is the map number of the map with that URI.
 1181:     #  map_id_$lmap_no is the URI for this map level.
 1182:     #
 1183:     $hash->{'map_pc_' . $uri}     = "$lmap_no"; # string form in lonuserstate.
 1184:     $hash->{'map_id_' . $lmap_no} = "$uri";
 1185: 
 1186:     # Create the path up to the top of the course.
 1187:     # this is in 'map_hierarchy_mapno'  that's a comma separated path down to us
 1188:     # in the hierarchy:
 1189: 
 1190:     if ($parent_rid =~/^(\d+).\d+$/) { 
 1191: 	my $parent_no = $1;	       # Parent's map number.
 1192: 	if (defined($hash->{'map_hierarchy_' . $parent_no})) {
 1193: 	    $hash->{'map_hierarchy_' . $lmap_no} =
 1194: 		$hash->{'map_hierarchy_' . $parent_no} . ',' . $parent_no;
 1195: 	} else {
 1196: 	    # Only 1 level deep ..nothing to append to:
 1197: 
 1198: 	    $hash->{'map_hierarchy_' . $lmap_no} = $parent_no;
 1199: 	}
 1200:     }
 1201: 
 1202:     # figure out the name of the map file we need to read.
 1203:     # ensure that it is a .page or a .sequence as those are the only 
 1204:     # sorts of files that make sense for this sub 
 1205: 
 1206:     my $filename = &Apache::lonnet::filelocation('', &append_version($uri, $hash));
 1207: 
 1208: 
 1209:     my $ispage = ($filename =~/\.page$/);
 1210:     unless ($ispage || ($filename =~ /\.sequence$/)) {
 1211: 	throw Error::Simple(&mt("<br />Invalid map: <tt>[_1]</tt>", $filename));
 1212:     }
 1213: 
 1214:     $filename =~ /\.(\w+)$/;
 1215: 
 1216:     $hash->{'map_type_'.$lmap_no}=$1;
 1217: 
 1218:     # Repcopy the file and get its contents...report errors if we can't
 1219:    
 1220:     my $contents = &Apache::lonnet::getfile($filename);
 1221:     if($contents eq -1) {
 1222:         throw Error::Simple(&mt('<br />Map not loaded: The file <tt>[_1]</tt> does not exist.',
 1223: 				$filename));
 1224:     }
 1225:     # Now that we succesfully retrieved the file we can make our parsing passes over it:
 1226:     # parsing is done in passes:
 1227:     # 1. Parameters are parsed.
 1228:     # 2. Resource, links and conditions are parsed.
 1229:     #
 1230:     # post processing takes care of the case where the sequence is random ordered
 1231:     # or randomselected.
 1232: 
 1233:     # Parse the parameters,  This pass only cares about start tags for <param>
 1234:     # tags.. this is because there is no body to a <param> tag.
 1235:     #
 1236: 
 1237:     my $parser  = HTML::TokeParser->new(\$contents);
 1238:     $parser->attr_encoded(1);	# Don't interpret entities in attributes (leave &xyz; alone).
 1239: 
 1240:     while (my $token = $parser->get_token()) {
 1241: 	if (($token->[0] eq 'S') && ($token->[1] eq 'param')) { 
 1242: 	    &parse_param($token, $map_number, $hash);
 1243: 	}
 1244:     }
 1245: 
 1246:     # ready for pass 2: Resource links and conditions.
 1247:     # Note that if the map is random-ordered link tags are computed by randomizing
 1248:     # resource order.  Furthermore, since conditions are set on links rather than
 1249:     # resources, they are also not processed if random order is turned on.
 1250:     #
 1251: 
 1252:     $parser = HTML::TokeParser->new(\$contents); # no way to reset the existing parser
 1253:     $parser->attr_encoded(1);
 1254: 
 1255:     my $linkpc=0;
 1256:     my $randomize = ($randomorder{$parent_rid} =~ /^yes$/i);
 1257: 
 1258:     my @map_ids;
 1259:     while (my $token = $parser->get_token) {
 1260: 	next if ($token->[0] ne 'S');
 1261: 
 1262: 	# Resource
 1263: 
 1264: 	if ($token->[1] eq 'resource') {
 1265: 	    my $resource_id = &parse_resource($token,$lmap_no,$ispage,$uri, $hash);
 1266: 	    if (defined $resource_id) {
 1267: 		push(@map_ids, $resource_id); 
 1268: 	    }
 1269: 
 1270:        # Link
 1271: 
 1272: 	} elsif ($token->[1] eq 'link' && !$randomize) {
 1273: 	    &make_link(++$linkpc,$lmap_no,$token->[2]->{'to'},
 1274: 		       $token->[2]->{'from'},
 1275: 		       $token->[2]->{'condition'}, $hash); # note ..condition may be undefined.
 1276: 
 1277: 	# condition
 1278: 
 1279: 	} elsif ($token->[1] eq 'condition' && !$randomize) {
 1280: 	    &parse_condition($token,$lmap_no, $hash);
 1281: 	}
 1282:     }
 1283: 
 1284:     #  This section handles random ordering by permuting the 
 1285:     # IDs of the map according to the user's random seed.
 1286:     # 
 1287: 
 1288:     if ($randomize) {
 1289: 	if (!$env{'request.role.adv'}) {
 1290: 	    my $seed;
 1291: 
 1292: 	    # In the advanced role, the map's random seed
 1293: 	    # parameter is used as the basis for computing the
 1294: 	    # seed ... if it has been specified:
 1295: 
 1296: 	    if (defined($randompickseed{$parent_rid})) {
 1297: 		$seed = $randompickseed{$parent_rid};
 1298: 	    } else {
 1299: 
 1300: 		# Otherwise the parent's fully encoded symb is used.
 1301: 
 1302: 		my ($mapid,$resid)=split(/\./,$parent_rid);
 1303: 		my $symb=
 1304: 		    &Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
 1305: 						 $resid,$hash->{'src_'.$parent_rid});
 1306: 		
 1307: 		$seed = $symb;
 1308: 	    }
 1309: 
 1310: 
 1311: 	    my $rndseed=&Apache::lonnet::rndseed($seed, $username, $userdomain);
 1312: 	    &Apache::lonnet::setup_random_from_rndseed($rndseed);
 1313: 
 1314: 	    # Take the set of map ids we have decoded and permute them to a
 1315: 	    # random order based on the seed set above. All of this is
 1316: 	    # processing the randomorder parameter if it is set, not
 1317: 	    # randompick.
 1318: 
 1319: 	    @map_ids=&math::Random::random_permutation(@map_ids); 
 1320: 	}
 1321: 
 1322: 
 1323: 	my $from = shift(@map_ids);
 1324: 	my $from_rid = $lmap_no.'.'.$from;
 1325: 	$hash->{'map_start_'.$uri} = $from_rid;
 1326: 	$hash->{'type_'.$from_rid}='start';
 1327: 
 1328: 	# Create links to reflect the random re-ordering done above.
 1329: 	# In the code to process the map XML, we did not process links or conditions
 1330: 	# if randomorder was set.  This means that for an instructor to choose
 1331: 
 1332: 	while (my $to = shift(@map_ids)) {
 1333: 	    &make_link(++$linkpc,$lmap_no,$to,$from, 0, $hash);
 1334: 	    my $to_rid =  $lmap_no.'.'.$to;
 1335: 	    $hash->{'type_'.$to_rid}='normal';
 1336: 	    $from = $to;
 1337: 	    $from_rid = $to_rid;
 1338: 	}
 1339: 
 1340: 	$hash->{'map_finish_'.$uri}= $from_rid;
 1341: 	$hash->{'type_'.$from_rid}='finish';
 1342:     }
 1343: 
 1344:     #  The last parsing pass parses the <mapalias> tags that associate a name
 1345:     #  with resource ids.
 1346: 
 1347:     $parser = HTML::TokeParser->new(\$contents);
 1348:     $parser->attr_encoded(1);
 1349: 
 1350:     while (my $token = $parser->get_token) {
 1351: 	next if ($token->[0] ne 'S');
 1352: 	if ($token->[1] eq 'param') {
 1353: 	    &parse_mapalias_param($token,$lmap_no, $hash);  
 1354: 	} 
 1355:     }
 1356: 
 1357: }
 1358: 
 1359: 
 1360: #
 1361: #  Load a map from file into a target hash.  This is done by first parsing the 
 1362: #  map file into local hashes and then unrolling those hashes into the big hash.
 1363: # 
 1364: # Parameters:
 1365: #
 1366: #    $cnum       - number of course being read.
 1367: #    $cdom       - Domain in which the course is evaluated.
 1368: #    $uname      - Name of the user for whom the course is being read
 1369: #    $udom       - Name of the domain of the user for whom the course is being read.
 1370: #    $target_hash- Reference to the target hash into which all of this is read.
 1371: #                  Note tht some of the hash entries we need to build require knowledge of the
 1372: #                  course URI.. these are expected to be filled in by the caller.
 1373: #
 1374: # Errors are logged to lonnet and are managed via the Perl structured exception package.
 1375: #
 1376: #  
 1377: sub loadmap {
 1378:     my ($cnum, $cdom, $uname, $udom,  $target_hash) = @_;
 1379: 
 1380: 
 1381: 
 1382:     # Clear the auxillary hashes and the cond array.
 1383: 
 1384: 
 1385:     %randompick     = ();
 1386:     %randompickseed = ();
 1387:     %encurl         = ();
 1388:     %hiddenurl      = ();
 1389:     %parmhash       = ();
 1390:     @cond           = ();
 1391:     $retfrid        = '';
 1392:     $username       = '';
 1393:     $userdomain     = '';
 1394:     %mapalias_cache = ();
 1395:     %cenv           = ();
 1396: 
 1397:     
 1398:     # 
 1399: 
 1400:     $username   = $uname;
 1401:     $userdomain = $udom;
 1402: 
 1403:     my $short_name = $cdom .'/' . $cnum;
 1404:     my $retfurl;
 1405: 
 1406:     try {
 1407: 
 1408: 	
 1409: 	# Get the information we need about the course.
 1410: 	# Return without filling in anything if we can't get any info:
 1411: 	
 1412: 	%cenv = &Apache::lonnet::coursedescription($short_name,
 1413: 						     {'freshen_cache' => 1,
 1414: 						      'user'          => $uname}); 
 1415: 
 1416: 	unless ($cenv{'url'}) { 
 1417: 	    &Apache::lonnet::logthis("lonmap::loadmap failed: $cnum/$cdom - did not get url");
 1418: 	    return; 
 1419: 	}
 1420: 	&Apache::lonnet::logthis("Course environment: \n" . Dumper(\%cenv));
 1421: 
 1422: 	$course_id = $cdom . '_' . $cnum; # Long course id.
 1423: 
 1424: 	# Load the version information into the hash
 1425: 
 1426: 	
 1427: 	&process_versions(\%cenv, $target_hash);
 1428: 	
 1429: 	
 1430: 	# Figure out the map filename's URI, and set up some starting points for the map.
 1431: 	
 1432: 	my $course_uri = $cenv{'url'};
 1433: 	my $map_uri    = &Apache::lonnet::clutter($course_uri);
 1434: 	
 1435: 	$target_hash->{'src_0.0'}            = &versiontrack($map_uri, $target_hash); 
 1436: 	$target_hash->{'title_0.0'}          = &Apache::lonnet::metadata($course_uri, 'title');
 1437: 	if(!defined $target_hash->{'title_0.0'}) {
 1438: 	    $target_hash->{'title_0.0'} = '';
 1439: 	}
 1440: 	$target_hash->{'ids_'.$map_uri} = '0.0';
 1441: 	$target_hash->{'is_map_0.0'}         = '1';
 1442: 
 1443: 	# In some places we need a username a domain and the courseid...store that
 1444: 	# in the target hash in the context.xxxx keys:
 1445: 
 1446: 	$target_hash->{'context.username'} = $username;
 1447: 	$target_hash->{'context.userdom'}  = $userdomain;
 1448: 	$target_hash->{'context.courseid'} = $course_id;
 1449: 
 1450:         &read_map($course_uri, '0.0', $target_hash);
 1451: 
 1452: 	# 
 1453: 
 1454: 	if (defined($target_hash->{'map_start_'.$map_uri})) {
 1455: 
 1456: 	    &traceroute('0',$target_hash->{'map_start_'.$course_uri},'&', 0, 0, $target_hash);
 1457: 	    &accinit($course_uri, $short_name,  $target_hash);
 1458: 	    &hiddenurls($target_hash);
 1459: 	}
 1460: 	my $errors = &get_mapalias_errors($target_hash);
 1461: 	if ($errors ne "") {
 1462: 	    throw Error::Simple("Map alias errors: ", $errors);
 1463: 	}
 1464: 
 1465: 	# Put the versions in to src:
 1466: 
 1467: 	foreach my $key (keys(%$target_hash)) {
 1468: 	    if ($key =~ /^src_/) {
 1469: 		$target_hash->{$key} = 
 1470: 		    &putinversion($target_hash->{$key}, $target_hash, $short_name);
 1471: 	    } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
 1472: 		my ($type, $url) = ($1,$2);
 1473: 		my $value = $target_hash->{$key};
 1474: 		$target_hash->{$type.&putinversion($url, $target_hash, $short_name)}=$value;
 1475: 	    }
 1476: 	}
 1477: 	#  Mark necrypted URLS.
 1478: 
 1479: 	foreach my $id (keys(%encurl)) {
 1480: 	    $target_hash->{'encrypted_'.$id}=1;
 1481: 	}
 1482: 
 1483: 	# Store first keys.
 1484: 
 1485: 	$target_hash->{'first_rid'}=$retfrid;
 1486: 	my ($mapid,$resid)=split(/\./,$retfrid);
 1487: 	$target_hash->{'first_mapurl'}=$target_hash->{'map_id_'.$mapid};
 1488: 	my $symb=&Apache::lonnet::encode_symb($target_hash->{'map_id_'.$mapid},
 1489: 					      $resid,
 1490: 					      $target_hash->{'src_'.$retfrid});
 1491: 	$retfurl=&add_get_param($target_hash->{'src_'.$retfrid},{ 'symb' => $symb });
 1492: 	if ($target_hash->{'encrypted_'.$retfrid}) {
 1493: 	    $retfurl=&Apache::lonenc::encrypted($retfurl,
 1494: 						(&Apache::lonnet::allowed('adv') ne 'F'));
 1495: 	}
 1496: 	$target_hash->{'first_url'}=$retfurl;	
 1497: 
 1498: 	# Merge in the child hashes in case the caller wants that information as well.
 1499: 
 1500: 
 1501: 	&merge_hash($target_hash, 'randompick', \%randompick);
 1502: 	&merge_hash($target_hash, 'randompickseed', \%randompick);
 1503: 	&merge_hash($target_hash, 'randomorder', \%randomorder);
 1504: 	&merge_hash($target_hash, 'encurl', \%encurl);
 1505: 	&merge_hash($target_hash, 'hiddenurl', \%hiddenurl);
 1506: 	&merge_hash($target_hash, 'param', \%parmhash);
 1507: 	&merge_conditions($target_hash);
 1508:     }
 1509:     otherwise {
 1510: 	my $e = shift;
 1511: 	&Apache::lonnet::logthis("lonmap::loadmap failed: " . $e->stringify());
 1512:     }
 1513: 
 1514: }
 1515: 
 1516: 
 1517: 1;
 1518: 
 1519: #
 1520: #  Module initialization code:
 1521: #  TODO:  Fix the pod docs below.
 1522: 
 1523: 1;
 1524: __END__
 1525: 
 1526: =head1 NAME
 1527: 
 1528: Apache::lonmap - Construct a hash that represents a course (Big Hash).
 1529: 
 1530: =head1 SYNOPSIS
 1531: 
 1532: &Apache::lonmap::loadmap($filepath, \%target_hash);
 1533: 
 1534: =head1 INTRODUCTION
 1535: 
 1536: This module reads a course filename into a hash reference.  It's up to the caller
 1537: to to things like decide the has should be tied to some external file and handle the locking
 1538: if this file should be shared amongst several Apache children.
 1539: 
 1540: =head1 SUBROUTINES
 1541: 
 1542: =over
 1543: 
 1544: =item loadmap($filepath, $targethash)
 1545: 
 1546: 
 1547: Reads the map file into a target hash.
 1548: 
 1549: =over
 1550: 
 1551: =item $filepath - The path to the map file to read.
 1552: 
 1553: =item $targethash - A reference to hash into which the course is read.
 1554: 
 1555: =back
 1556: 
 1557: =item process_versions($cenv, $hash)
 1558: 
 1559: Makes hash entries for each version of a course described by a course environment
 1560: returned from Apache::lonnet::coursedescription.
 1561: 
 1562: =over
 1563: 
 1564: =item $cenv - Reference to the environment hash returned by Apache::lonnet::coursedescription
 1565: 
 1566: =item $hash - Hash to be filled in with 'version_xxx' entries as per the big hash.
 1567: 
 1568: =back
 1569: 
 1570: =back
 1571:  
 1572: 
 1573: =cut

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