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