File:  [LON-CAPA] / loncom / lonmap.pm
Revision 1.6: download - view: text, annotated - select for diffs
Tue Nov 29 11:50:53 2011 UTC (12 years, 4 months ago) by foxr
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
BZ 6454 - Get random ordering to 1. Know when to randomize 2. generate random seeds consistent with lonuserstate.pm

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

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