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