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