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