Annotation of loncom/lonmap.pm, revision 1.13
1.1 foxr 1: # The LearningOnline Network
2: #
3: # Read maps into a 'big hash'.
4: #
1.13 ! bisitz 5: # $Id: lonmap.pm,v 1.12 2013/07/19 18:24:13 bisitz 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);
1.13 ! bisitz 216: &mt('Resource [_1][_2]in Map [_3]',
! 217: '"'.$hash->{'title_'.$id}.'"',
! 218: '<br />',
! 219: '"'.$hash->{'title_'.$hash->{'ids_'.$hash->{'map_id_'.$mapid}}}.'"');
1.1 foxr 220: } (@{ $mapalias_cache{$mapalias} }));
221: next if ($count < 2);
222: $error_text .= '<div class="LC_error">'.
223: &mt('Error: Found the mapalias "[_1]" defined multiple times.',
224: $mapalias).
225: '</div><ul><li>'.$which.'</li></ul>';
226: }
227: &clear_mapalias_count();
228: return $error_text;
229: }
230: #
231: # Clears the map aliase hash.
232: #
233: sub clear_mapalias_count {
234: undef(%mapalias_cache);
235: }
236:
237: #----------------------------------------------------------------
238: #
239: # Code dealing with resource versions.
240: #
241:
242: #
1.2 foxr 243: # Put a version into a src element of a hash or url:
244: #
245: # Parameters:
246: # uri - URI into which the version must be added.
247: # hash - Reference to the hash being built up.
248: # short- Short coursename.
249: #
250:
251: sub putinversion {
252: my ($uri, $hash, $short) = @_;
253: my $key=$short.'_'.&Apache::lonnet::clutter($uri);
254: if ($hash->{'version_'.$uri}) {
255: my $version=$hash->{'version_'.$uri};
256: if ($version eq 'mostrecent') { return $uri; }
257: if ($version eq &Apache::lonnet::getversion(
258: &Apache::lonnet::filelocation('',$uri)))
259: { return $uri; }
260: $uri=~s/\.(\w+)$/\.$version\.$1/;
261: }
262: &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
263: return $uri;
264: }
265:
266:
267: #
1.1 foxr 268: # Create hash entries for each version of the course.
269: # Parameters:
270: # $cenv - Reference to a course environment from lonnet::coursedescription.
271: # $hash - Reference to a hash that will be populated.
272:
273: #
274: sub process_versions {
275: my ($cenv, $hash) = @_;
276:
277:
278: my %versions = &Apache::lonnet::dump('resourceversions',
279: $cenv->{'domain'},
280: $cenv->{'num'});
281:
282: foreach my $ver (keys (%versions)) {
283: if ($ver =~/^error\:/) { # lonc/lond transaction failed.
284: throw Error::Simple('lonc/lond returned error: ' . $ver);
285: }
286: $hash->{'version_'.$ver} = $versions{$ver};
287: }
288: }
289:
290: #
291: # Generate text for a version discrepancy error:
292: # Parameters:
293: # $uri - URI of the resource.
294: # $used - Version used.
295: # $unused - Veresion of duplicate.
296: #
297: sub versionerror {
298: my ($uri, $used, $unused) = @_;
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') {
1.11 raeburn 621: my $ignorehidden;
622: if (defined($hash->{'is_map_'.$rid})) {
623: if (($hash->{'context.nohideurl'}) && ($hash->{'context.nohideurl'} eq $hash->{'src_'.$rid})) {
624: $ignorehidden = 1; # Hidden parameter explicitly deleted
625: # if printing/grading bubblesheet exam
626: }
627: }
628: unless ($ignorehidden) {
629: $hiddenurl{$rid}=1;
630: }
1.1 foxr 631: }
632: if (!$hdnflag && lc($hidden) eq 'no') {
633: delete($hiddenurl{$rid});
634: }
635:
636: my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
637: if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
638:
639: if (($retfrid eq '') && ($hash->{'src_'.$rid})
640: && ($hash->{'src_'.$rid}!~/\.sequence$/)) {
641: $retfrid=$rid;
642: }
643:
644: if (defined($hash->{'conditions_'.$rid})) {
645: $hash->{'conditions_'.$rid}=simplify(
646: '('.$hash->{'conditions_'.$rid}.')|('.$sofar.')');
647: } else {
648: $hash->{'conditions_'.$rid}=$sofar;
649: }
650:
651: # if the expression is just the 0th condition keep it
652: # otherwise leave a pointer to this condition expression
653:
654: $newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
655:
656: # Recurse if the resource is a map:
657:
658: if (defined($hash->{'is_map_'.$rid})) {
659: if (defined($hash->{'map_start_'.$hash->{'src_'.$rid}})) {
660: $sofar=$newsofar=
661: &traceroute($sofar,
662: $hash->{'map_start_'.$hash->{'src_'.$rid}},
663: $beenhere,
664: $encflag || $encurl{$rid},
1.3 foxr 665: $hdnflag || $hiddenurl{$rid}, $hash);
1.1 foxr 666: }
667: }
668:
669: # Processes links to this resource:
670: # - verify the existence of any conditionals on the link to here.
671: # - Recurse to any resources linked to us.
672: #
673: if (defined($hash->{'to_'.$rid})) {
674: foreach my $id (split(/\,/,$hash->{'to_'.$rid})) {
675: my $further=$sofar;
676: #
677: # If there's a condition associated with this link be sure
678: # it's been defined else that's an error:
679: #
680: if ($hash->{'undercond_'.$id}) {
681: if (defined($hash->{'condid_'.$hash->{'undercond_'.$id}})) {
682: $further=simplify('('.'_'.$rid.')&('.
683: $hash->{'condid_'.$hash->{'undercond_'.$id}}.')');
684: } else {
1.12 bisitz 685: my $errtext.='<br />'.&mt('Undefined condition ID: [_1]',$hash->{'undercond_'.$id});
1.1 foxr 686: throw Error::Simple($errtext);
687: }
688: }
689: # Recurse to resoruces that have to's to us.
690: $newsofar=&traceroute($further,$hash->{'goesto_'.$id},$beenhere,
1.3 foxr 691: $encflag,$hdnflag, $hash);
1.1 foxr 692: }
693: }
694: }
695: return $newsofar;
696: }
697:
698:
699: #---------------------------------------------------------------------------------
700: #
701: # Map parsing code:
702: #
703:
704: #
705: # Parse the <param> tag. for most parameters, the only action is to define/extend
706: # a has entry for {'param_{refid}'} where refid is the resource the parameter is
707: # attached to and the value built up is an & separated list of parameters of the form:
708: # type:part.name=value
709: #
710: # In addition there is special case code for:
711: # - randompick
712: # - randompickseed
713: # - randomorder
714: #
715: # - encrypturl
716: # - hiddenresource
717: #
718: # Parameters:
719: # token - The token array from HTML::TokeParse we mostly care about element [2]
720: # which is a hash of attribute => values supplied in the tag
721: # (remember this sub is only processing start tag tokens).
722: # mno - Map number. This is used to qualify resource ids within a map
723: # to make them unique course wide (a process known as uniquifaction).
724: # hash - Reference to the hash we are building.
725: #
726: sub parse_param {
727: my ($token, $mno, $hash) = @_;
728:
729: # Qualify the reference and name by the map number and part number.
730: # if no explicit part number is supplied, 0 is the implicit part num.
731:
732: my $referid=$mno.'.'.$token->[2]->{'to'}; # Resource param applies to.
733: my $name=$token->[2]->{'name'}; # Name of parameter
734: my $part;
735:
736:
737: if ($name=~/^parameter_(.*)_/) {
738: $part=$1;
739: } else {
740: $part=0;
741: }
742:
743: # Peel the parameter_ off the parameter name.
744:
745: $name=~s/^.*_([^_]*)$/$1/;
746:
747: # The value is:
748: # type.part.name.value
749:
750: my $newparam=
751: &escape($token->[2]->{'type'}).':'.
752: &escape($part.'.'.$name).'='.
753: &escape($token->[2]->{'value'});
754:
755: # The hash key is param_resourceid.
756: # Multiple parameters for a single resource are & separated in the hash.
757:
758:
759: if (defined($hash->{'param_'.$referid})) {
760: $hash->{'param_'.$referid}.='&'.$newparam;
761: } else {
762: $hash->{'param_'.$referid}=''.$newparam;
763: }
764: #
765: # These parameters have to do with randomly selecting
766: # resources, therefore a separate hash is also created to
767: # make it easy to locate them when actually computing the resource set later on
768: # See the code conditionalized by ($randomize) in read_map().
769:
770: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) { # Random selection turned on
771: $randompick{$referid}=$token->[2]->{'value'};
772: }
773: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) { # Randomseed provided.
774: $randompickseed{$referid}=$token->[2]->{'value'};
775: }
776: if ($token->[2]->{'name'}=~/^parameter_(0_)*randomorder$/) { # Random order turned on.
777: $randomorder{$referid}=$token->[2]->{'value'};
778: }
779:
780: # These parameters have to do with how the URLs of resources are presented to
781: # course members(?). encrypturl presents encypted url's while
782: # hiddenresource hides the URL.
783: #
784:
785: if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
786: if ($token->[2]->{'value'}=~/^yes$/i) {
787: $encurl{$referid}=1;
788: }
789: }
790: if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
791: if ($token->[2]->{'value'}=~/^yes$/i) {
792: $hiddenurl{$referid}=1;
793: }
794: }
795: }
796:
797:
798: #
799: # Parses a resource tag to produce the value to push into the
800: # map_ids array.
801: #
802: #
803: # Information about the actual type of resource is provided by the file extension
804: # of the uri (e.g. .problem, .sequence etc. etc.).
805: #
806: # Parameters:
807: # $token - A token from HTML::TokeParser
808: # This is an array that describes the most recently parsed HTML item.
809: # $lpc - Map nesting level (?)
810: # $ispage - True if this resource is encapsulated in a .page (assembled resourcde).
811: # $uri - URI of the enclosing resource.
1.7 raeburn 812: # $code - CODE for which resource is being parsed (CODEd assignments).
1.1 foxr 813: # $hash - Reference to the hash we are building.
814: #
815: # Returns:
816: # Value of the id attribute of the tag.
817: #
818: # Note:
819: # The token is an array that contains the following elements:
820: # [0] => 'S' indicating this is a start token
821: # [1] => 'resource' indicating this tag is a <resource> tag.
822: # [2] => Hash of attribute =>value pairs.
823: # [3] => @(keys [2]).
824: # [4] => unused.
825: #
826: # The attributes of the resourcde tag include:
827: #
828: # id - The resource id.
829: # src - The URI of the resource.
830: # type - The resource type (e.g. start and finish).
831: # title - The resource title.
832: #
833:
834: sub parse_resource {
1.7 raeburn 835: my ($token,$lpc,$ispage,$uri,$code,$hash) = @_;
1.1 foxr 836:
837: # I refuse to countenance code like this that has
838: # such a dirty side effect (and forcing this sub to be called within a loop).
839: #
840: # if ($token->[2]->{'type'} eq 'zombie') { next; }
841: #
842: # The original code both returns _and_ skips to the next pass of the >caller's<
843: # loop, that's just dirty.
844: #
845:
846: # Zombie resources don't produce anything useful.
847:
848: if ($token->[2]->{'type'} eq 'zombie') {
849: return undef;
850: }
851:
852: my $rid=$lpc.'.'.$token->[2]->{'id'}; # Resource id in hash is levelcounter.id-in-xml.
853:
854: # Save the hash element type and title:
855:
856: $hash->{'kind_'.$rid}='res';
857: $hash->{'title_'.$rid}=$token->[2]->{'title'};
858:
859: # Get the version free URI for the resource.
860: # If a 'version' attribute was supplied, and this resource's version
861: # information has not yet been stored, store it.
862: #
863:
864:
865: my $turi=&versiontrack($token->[2]->{'src'});
866: if ($token->[2]->{'version'}) {
867: unless ($hash->{'version_'.$turi}) {
868:
869: #Where does the value of $1 below come from?
870: #$1 for the regexps in versiontrack should have gone out of scope.
871: #
872: # I think this may be dead code since versiontrack ought to set
873: # this hash element(?).
874: #
875: $hash->{'version_'.$turi}=$1;
876: }
877: }
878: # Pull out the title and do entity substitution on &colon
879: # Q: Why no other entity substitutions?
880:
881: my $title=$token->[2]->{'title'};
882: $title=~s/\&colon\;/\:/gs;
883:
884:
885:
886: # I think the point of all this code is to construct a final
887: # URI that apache and its rewrite rules can use to
888: # fetch the resource. Thi s sonly necessary if the resource
889: # is not a page. If the resource is a page then it must be
890: # assembled (at fetch time?).
891:
892: unless ($ispage) {
893: $turi=~/\.(\w+)$/;
894: my $embstyle=&Apache::loncommon::fileembstyle($1);
895: if ($token->[2]->{'external'} eq 'true') { # external
896: $turi=~s/^https?\:\/\//\/adm\/wrapper\/ext\//;
897: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
898: if (($embstyle eq 'img')
899: || ($embstyle eq 'emb')
900: || ($embstyle eq 'wrp')) {
901: $turi='/adm/wrapper'.$turi;
902: } elsif ($embstyle eq 'ssi') {
903: #do nothing with these
904: } elsif ($turi!~/\.(sequence|page)$/) {
905: $turi='/adm/coursedocs/showdoc'.$turi;
906: }
907: } elsif ($turi=~/\S/) { # normal non-empty internal resource
908: my $mapdir=$uri;
909: $mapdir=~s/[^\/]+$//;
910: $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
911: if (($embstyle eq 'img')
912: || ($embstyle eq 'emb')
913: || ($embstyle eq 'wrp')) {
914: $turi='/adm/wrapper'.$turi;
915: }
916: }
917: }
918: # Store reverse lookup, remove query string resource 'ids'_uri => resource id.
919: # If the URI appears more than one time in the sequence, it's resourcde
920: # id's are constructed as a comma spearated list.
921:
922: my $idsuri=$turi;
923: $idsuri=~s/\?.+$//;
924: if (defined($hash->{'ids_'.$idsuri})) {
925: $hash->{'ids_'.$idsuri}.=','.$rid;
926: } else {
927: $hash->{'ids_'.$idsuri}=''.$rid;
928: }
929:
930:
931:
932: if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard|viewclasslist)$/) {
933: $turi.='?register=1';
934: }
935:
936:
937: # resource id lookup: 'src'_resourc-di => URI decorated with a query
938: # parameter as above if necessary due to the resource type.
939:
940: $hash->{'src_'.$rid}=$turi;
941:
942: # Mark the external-ness of the resource:
943:
944: if ($token->[2]->{'external'} eq 'true') {
945: $hash->{'ext_'.$rid}='true:';
946: } else {
947: $hash->{'ext_'.$rid}='false:';
948: }
949:
950: # If the resource is a start/finish resource set those
951: # entries in the has so that navigation knows where everything starts.
952: # If there is a malformed sequence that has no start or no finish
953: # resource, should this be detected and errors thrown? How would such a
954: # resource come into being other than being manually constructed by a person
955: # and then uploaded? Could that happen if an author decided a sequence was almost
956: # right edited it by hand and then reuploaded it to 'fix it' but accidently cut the
957: # start or finish resources?
958: #
959: # All resourcess also get a type_id => (start | finish | normal) hash entr.
960: #
961: if ($token->[2]->{'type'}) {
962: $hash->{'type_'.$rid}=$token->[2]->{'type'};
963: if ($token->[2]->{'type'} eq 'start') {
964: $hash->{'map_start_'.$uri}="$rid";
965: }
966: if ($token->[2]->{'type'} eq 'finish') {
967: $hash->{'map_finish_'.$uri}="$rid";
968: }
969: } else {
970: $hash->{'type_'.$rid}='normal';
971: }
972:
973: # Sequences end pages are constructed entities. They require that the
974: # map that defines _them_ be loaded as well into the hash...with this resourcde
975: # as the base of the nesting.
976: # Resources like that are also marked with is_map_id => 1 entries.
977: #
978:
979: if (($turi=~/\.sequence$/) ||
980: ($turi=~/\.page$/)) {
1.3 foxr 981: $hash->{'is_map_'.$rid}='1'; # String in lonuserstate.
1.11 raeburn 982: if ($hiddenurl{$rid}) {
983: if (($hash->{'context.nohideurl'}) &&
984: ($hash->{'context.nohideurl'} eq $hash->{'src_'.$rid})) {
985: delete($hiddenurl{$rid}); # Hidden parameter explicitly deleted
986: # if printing/grading bubblesheet exam
987: }
988: }
989:
1.7 raeburn 990: &read_map($turi,$rid,$code,$hash);
1.1 foxr 991: }
992: return $token->[2]->{'id'};
993: }
994:
995: # Links define how you are allowed to move from one resource to another.
996: # They are the transition edges in the directed graph that a map is.
997: # This sub takes informatino from a <link> tag and constructs the
998: # navigation bits and pieces of a map. There is no requirement that the
999: # resources that are linke are already defined, however clearly the map is
1000: # badly broken if they are not _eventually_ defined.
1001: #
1002: # Note that links can be unconditional or conditional.
1003: #
1004: # Parameters:
1005: # linkpc - The link counter for this level of map nesting (this is
1006: # reset to zero by read_map prior to starting to process
1007: # links for map).
1008: # lpc - The map level ocounter (how deeply nested this map is in
1009: # the hierarchy of maps that are recursively read in.
1010: # to - resource id (within the XML) of the target of the edge.
1011: # from - resource id (within the XML) of the source of the edge.
1012: # condition- id of condition associated with the edge (also within the XML).
1013: # hash - reference to the hash we are building.
1014:
1015: #
1016:
1017: sub make_link {
1018: my ($linkpc,$lpc,$to,$from,$condition, $hash) = @_;
1019:
1020: # Compute fully qualified ids for the link, the
1021: # and from/to by prepending lpc.
1022: #
1023:
1024: my $linkid=$lpc.'.'.$linkpc;
1025: my $goesto=$lpc.'.'.$to;
1026: my $comesfrom=$lpc.'.'.$from;
1.3 foxr 1027: my $undercond='0';
1.1 foxr 1028:
1029:
1030: # If there is a condition, qualify it with the level counter.
1031:
1032: if ($condition) {
1033: $undercond=$lpc.'.'.$condition;
1034: }
1035:
1036: # Links are represnted by:
1037: # goesto_.fuullyqualifedlinkid => fully qualified to
1038: # comesfrom.fullyqualifiedlinkid => fully qualified from
1039: # undercond_.fullyqualifiedlinkid => fully qualified condition id.
1040:
1041: $hash->{'goesto_'.$linkid}=$goesto;
1042: $hash->{'comesfrom_'.$linkid}=$comesfrom;
1043: $hash->{'undercond_'.$linkid}=$undercond;
1044:
1045: # In addition:
1046: # to_.fully qualified from => comma separated list of
1047: # link ids with that from.
1048: # Similarly:
1049: # from_.fully qualified to => comma separated list of link ids`
1050: # with that to.
1051: # That allows us given a resource id to know all edges that go to it
1052: # and leave from it.
1053: #
1054:
1055: if (defined($hash->{'to_'.$comesfrom})) {
1056: $hash->{'to_'.$comesfrom}.=','.$linkid;
1057: } else {
1058: $hash->{'to_'.$comesfrom}=''.$linkid;
1059: }
1060: if (defined($hash->{'from_'.$goesto})) {
1061: $hash->{'from_'.$goesto}.=','.$linkid;
1062: } else {
1063: $hash->{'from_'.$goesto}=''.$linkid;
1064: }
1065: }
1066:
1067: # ------------------------------------------------------------------- Condition
1068: #
1069: # Processes <condition> tags, storing sufficient information about them
1070: # in the hash so that they can be evaluated and used to conditionalize
1071: # what is presented to the student.
1072: #
1073: # these can have the following attributes
1074: #
1075: # id = A unique identifier of the condition within the map.
1076: #
1077: # value = Is a perl script-let that, when evaluated in safe space
1078: # determines whether or not the condition is true.
1079: # Normally this takes the form of a test on an Apache::lonnet::EXT call
1080: # to find the value of variable associated with a resource in the
1081: # map identified by a mapalias.
1082: # Here's a fragment of XML code that illustrates this:
1083: #
1084: # <param to="5" value="mainproblem" name="parameter_0_mapalias" type="string" />
1085: # <resource src="" id="1" type="start" title="Start" />
1086: # <resource src="/res/msu/albertel/b_and_c/p1.problem" id="5" title="p1.problem" />
1087: # <condition value="&EXT('user.resource.resource.0.tries','mainproblem')
1088: # <2 " id="61" type="stop" />
1089: # <link to="5" index="1" from="1" condition="61" />
1090: #
1091: # In this fragment:
1092: # - The param tag establishes an alias to resource id 5 of 'mainproblem'.
1093: # - The resource that is the start of the map is identified.
1094: # - The resource tag identifies the resource associated with this tag
1095: # and gives it the id 5.
1096: # - The condition is true if the tries variable associated with mainproblem
1097: # is less than 2 (that is the user has had more than 2 tries).
1098: # The condition type is a stop condition which inhibits(?) the associated
1099: # link if the condition is false.
1100: # - The link to resource 5 from resource 1 is affected by this condition.
1101: #
1102: # type = Type of the condition. The type determines how the condition affects the
1103: # link associated with it and is one of
1104: # - 'force'
1105: # - 'stop'
1106: # anything else including not supplied..which treated as:
1107: # - 'normal'.
1108: # Presumably maps get created by the resource assembly tool and therefore
1109: # illegal type values won't squirm their way into the XML.
1110: # hash - Reference to the hash we are trying to build up.
1111: #
1112: # Side effects:
1113: # - The kind_level-qualified-condition-id hash element is set to 'cond'.
1114: # - The condition text is pushed into the cond array and its element number is
1115: # set in the condid_level-qualified-condition-id element of the hash.
1116: # - The condition type is colon appneded to the cond array element for this condition.
1117: sub parse_condition {
1118: my ($token, $lpc, $hash) = @_;
1119: my $rid=$lpc.'.'.$token->[2]->{'id'};
1120:
1121: $hash->{'kind_'.$rid}='cond';
1122:
1123: my $condition = $token->[2]->{'value'};
1124: $condition =~ s/[\n\r]+/ /gs;
1125: push(@cond, $condition);
1126: $hash->{'condid_'.$rid}=$#cond;
1127: if ($token->[2]->{'type'}) {
1128: $cond[$#cond].=':'.$token->[2]->{'type'};
1129: } else {
1130: $cond[$#cond].=':normal';
1131: }
1132: }
1133:
1134: #
1135: # Parse mapalias parameters.
1136: # these are tags of the form:
1137: # <param to="nn"
1138: # value="some-alias-for-resourceid-nn"
1139: # name="parameter_0_mapalias"
1140: # type="string" />
1141: # A map alias is a textual name for a resource:
1142: # - The to attribute identifies the resource (this gets level qualified below)
1143: # - The value attributes provides the alias string.
1144: # - name must be of the regexp form: /^parameter_(0_)*mapalias$/
1145: # - e.g. the string 'parameter_' followed by 0 or more "0_" strings
1146: # terminating with the string 'mapalias'.
1147: # Examples:
1148: # 'parameter_mapalias', 'parameter_0_mapalias', parameter_0_0_mapalias'
1149: # Invalid to ids are silently ignored.
1150: #
1151: # Parameters:
1152: # token - The token array fromthe HMTML::TokeParser
1153: # lpc - The current map level counter.
1154: # hash - Reference to the hash that we are building.
1155: #
1156: sub parse_mapalias_param {
1157: my ($token, $lpc, $hash) = @_;
1158:
1159: # Fully qualify the to value and ignore the alias if there is no
1160: # corresponding resource.
1161:
1162: my $referid=$lpc.'.'.$token->[2]->{'to'};
1163: return if (!exists($hash->{'src_'.$referid}));
1164:
1165: # If this is a valid mapalias parameter,
1166: # Append the target id to the count_mapalias element for that
1167: # alias so that we can detect doubly defined aliases
1168: # e.g.:
1169: # <param to="1" value="george" name="parameter_0_mapalias" type="string" />
1170: # <param to="2" value="george" name="parameter_0_mapalias" type="string" />
1171: #
1172: # The example above is trivial but the case that's important has to do with
1173: # constructing a map that includes a nested map where the nested map may have
1174: # aliases that conflict with aliases established in the enclosing map.
1175: #
1176: # ...and create/update the hash mapalias entry to actually store the alias.
1177: #
1178:
1179: if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
1180: &count_mapalias($token->[2]->{'value'},$referid);
1181: $hash->{'mapalias_'.$token->[2]->{'value'}}=$referid;
1182: }
1183: }
1184:
1185:
1186: #---------------------------------------------------------------------------------
1187: #
1188: # Code to process the map file.
1189:
1190: # read a map file and add it to the hash. Since a course map can contain resources
1191: # that are themselves maps, read_map might be recursively called.
1192: #
1193: # Parameters:
1194: # $uri - URI of the course itself (not the map file).
1195: # $parent_rid - map number qualified id of the parent of the map being read.
1196: # For the top level course map this is 0.0. For the first nested
1197: # map 1.n where n is the id of the resource within the
1.7 raeburn 1198: # top level map and so on.
1199: # $code - CODE for which map is being read (CODEd assignments).
1.1 foxr 1200: # $hash - Reference to a hash that will become the big hash for the course
1201: # This hash is modified as per the map description.
1202: # Side-effects:
1203: # $map_number - Will be incremented. This keeps track of the number of the map
1204: # we are currently working on (see parent_rid above, the number to the
1205: # left of the . in $parent_rid is the map number).
1206: #
1207: #
1208: sub read_map {
1.7 raeburn 1209: my ($uri, $parent_rid, $code, $hash) = @_;
1.1 foxr 1210:
1.3 foxr 1211:
1.1 foxr 1212: # Check for duplication: A map may only be included once.
1213:
1214: if($hash->{'map_pc_' . $uri}) {
1.2 foxr 1215: throw Error::Simple('Duplicate map: ', $uri);
1.1 foxr 1216: }
1217: # count the map number and save it locally so that we don't lose it
1218: # when we recurse.
1219:
1220: $map_number++;
1221: my $lmap_no = $map_number;
1222:
1223: # save the map_pc and map_id elements of the hash for this map:
1224: # map_pc_uri is the map number of the map with that URI.
1225: # map_id_$lmap_no is the URI for this map level.
1226: #
1.3 foxr 1227: $hash->{'map_pc_' . $uri} = "$lmap_no"; # string form in lonuserstate.
1228: $hash->{'map_id_' . $lmap_no} = "$uri";
1.1 foxr 1229:
1230: # Create the path up to the top of the course.
1231: # this is in 'map_hierarchy_mapno' that's a comma separated path down to us
1232: # in the hierarchy:
1233:
1234: if ($parent_rid =~/^(\d+).\d+$/) {
1235: my $parent_no = $1; # Parent's map number.
1236: if (defined($hash->{'map_hierarchy_' . $parent_no})) {
1237: $hash->{'map_hierarchy_' . $lmap_no} =
1.2 foxr 1238: $hash->{'map_hierarchy_' . $parent_no} . ',' . $parent_no;
1.1 foxr 1239: } else {
1240: # Only 1 level deep ..nothing to append to:
1241:
1242: $hash->{'map_hierarchy_' . $lmap_no} = $parent_no;
1243: }
1244: }
1245:
1246: # figure out the name of the map file we need to read.
1247: # ensure that it is a .page or a .sequence as those are the only
1248: # sorts of files that make sense for this sub
1249:
1250: my $filename = &Apache::lonnet::filelocation('', &append_version($uri, $hash));
1.3 foxr 1251:
1252:
1.1 foxr 1253: my $ispage = ($filename =~/\.page$/);
1.2 foxr 1254: unless ($ispage || ($filename =~ /\.sequence$/)) {
1.6 foxr 1255: &Apache::lonnet::logthis("invalid: $filename : $uri");
1.12 bisitz 1256: throw Error::Simple('<br />'.&mt('Invalid map: [_1]','<span class="LC_filename">'.$filename.'</span>'));
1.1 foxr 1257: }
1258:
1259: $filename =~ /\.(\w+)$/;
1260:
1.2 foxr 1261: $hash->{'map_type_'.$lmap_no}=$1;
1.1 foxr 1262:
1263: # Repcopy the file and get its contents...report errors if we can't
1264:
1.3 foxr 1265: my $contents = &Apache::lonnet::getfile($filename);
1.1 foxr 1266: if($contents eq -1) {
1.12 bisitz 1267: throw Error::Simple('<br />'.&mt('Map not loaded: The file [_1] does not exist.',
1268: '<span class="LC_filename">'.$filename.'</span>'));
1.1 foxr 1269: }
1270: # Now that we succesfully retrieved the file we can make our parsing passes over it:
1271: # parsing is done in passes:
1272: # 1. Parameters are parsed.
1273: # 2. Resource, links and conditions are parsed.
1274: #
1275: # post processing takes care of the case where the sequence is random ordered
1276: # or randomselected.
1277:
1278: # Parse the parameters, This pass only cares about start tags for <param>
1279: # tags.. this is because there is no body to a <param> tag.
1280: #
1281:
1.2 foxr 1282: my $parser = HTML::TokeParser->new(\$contents);
1.1 foxr 1283: $parser->attr_encoded(1); # Don't interpret entities in attributes (leave &xyz; alone).
1284:
1285: while (my $token = $parser->get_token()) {
1286: if (($token->[0] eq 'S') && ($token->[1] eq 'param')) {
1287: &parse_param($token, $map_number, $hash);
1288: }
1289: }
1290:
1291: # ready for pass 2: Resource links and conditions.
1292: # Note that if the map is random-ordered link tags are computed by randomizing
1293: # resource order. Furthermore, since conditions are set on links rather than
1294: # resources, they are also not processed if random order is turned on.
1295: #
1296:
1.2 foxr 1297: $parser = HTML::TokeParser->new(\$contents); # no way to reset the existing parser
1.1 foxr 1298: $parser->attr_encoded(1);
1299:
1300: my $linkpc=0;
1301: my $randomize = ($randomorder{$parent_rid} =~ /^yes$/i);
1302:
1303: my @map_ids;
1304: while (my $token = $parser->get_token) {
1305: next if ($token->[0] ne 'S');
1306:
1307: # Resource
1308:
1309: if ($token->[1] eq 'resource') {
1.7 raeburn 1310: my $resource_id = &parse_resource($token,$lmap_no,$ispage,$uri,$code,$hash);
1.1 foxr 1311: if (defined $resource_id) {
1312: push(@map_ids, $resource_id);
1313: }
1314:
1315: # Link
1316:
1317: } elsif ($token->[1] eq 'link' && !$randomize) {
1.2 foxr 1318: &make_link(++$linkpc,$lmap_no,$token->[2]->{'to'},
1.1 foxr 1319: $token->[2]->{'from'},
1320: $token->[2]->{'condition'}, $hash); # note ..condition may be undefined.
1321:
1322: # condition
1323:
1324: } elsif ($token->[1] eq 'condition' && !$randomize) {
1.2 foxr 1325: &parse_condition($token,$lmap_no, $hash);
1.1 foxr 1326: }
1327: }
1328:
1329: # This section handles random ordering by permuting the
1330: # IDs of the map according to the user's random seed.
1331: #
1332:
1333: if ($randomize) {
1.7 raeburn 1334: if (!&has_advanced_role($username, $userdomain) || $code) {
1.1 foxr 1335: my $seed;
1336:
1337: # In the advanced role, the map's random seed
1338: # parameter is used as the basis for computing the
1339: # seed ... if it has been specified:
1340:
1341: if (defined($randompickseed{$parent_rid})) {
1342: $seed = $randompickseed{$parent_rid};
1343: } else {
1344:
1345: # Otherwise the parent's fully encoded symb is used.
1346:
1347: my ($mapid,$resid)=split(/\./,$parent_rid);
1348: my $symb=
1349: &Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
1350: $resid,$hash->{'src_'.$parent_rid});
1351:
1352: $seed = $symb;
1353: }
1354:
1355:
1.6 foxr 1356: my $rndseed=&Apache::lonnet::rndseed($seed, '',
1357: $userdomain, $username,
1358: \%cenv);
1359:
1360:
1.1 foxr 1361: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1362:
1363: # Take the set of map ids we have decoded and permute them to a
1364: # random order based on the seed set above. All of this is
1365: # processing the randomorder parameter if it is set, not
1366: # randompick.
1367:
1.6 foxr 1368: @map_ids=&Math::Random::random_permutation(@map_ids);
1.1 foxr 1369: }
1370: my $from = shift(@map_ids);
1.2 foxr 1371: my $from_rid = $lmap_no.'.'.$from;
1.1 foxr 1372: $hash->{'map_start_'.$uri} = $from_rid;
1373: $hash->{'type_'.$from_rid}='start';
1374:
1375: # Create links to reflect the random re-ordering done above.
1376: # In the code to process the map XML, we did not process links or conditions
1377: # if randomorder was set. This means that for an instructor to choose
1378:
1379: while (my $to = shift(@map_ids)) {
1.3 foxr 1380: &make_link(++$linkpc,$lmap_no,$to,$from, 0, $hash);
1.2 foxr 1381: my $to_rid = $lmap_no.'.'.$to;
1.1 foxr 1382: $hash->{'type_'.$to_rid}='normal';
1383: $from = $to;
1384: $from_rid = $to_rid;
1385: }
1386:
1387: $hash->{'map_finish_'.$uri}= $from_rid;
1388: $hash->{'type_'.$from_rid}='finish';
1389: }
1390:
1.6 foxr 1391:
1.1 foxr 1392: # The last parsing pass parses the <mapalias> tags that associate a name
1393: # with resource ids.
1394:
1395: $parser = HTML::TokeParser->new(\$contents);
1396: $parser->attr_encoded(1);
1397:
1398: while (my $token = $parser->get_token) {
1399: next if ($token->[0] ne 'S');
1400: if ($token->[1] eq 'param') {
1.2 foxr 1401: &parse_mapalias_param($token,$lmap_no, $hash);
1.1 foxr 1402: }
1403: }
1404:
1405: }
1406:
1407:
1408: #
1409: # Load a map from file into a target hash. This is done by first parsing the
1410: # map file into local hashes and then unrolling those hashes into the big hash.
1411: #
1412: # Parameters:
1413: #
1414: # $cnum - number of course being read.
1415: # $cdom - Domain in which the course is evaluated.
1416: # $uname - Name of the user for whom the course is being read
1417: # $udom - Name of the domain of the user for whom the course is being read.
1.7 raeburn 1418: # $code - CODE for which course is being read (CODEd assignments)
1.11 raeburn 1419: # $nohideurl - URL for an exam folder for which hidden state is to be ignored.
1.1 foxr 1420: # $target_hash- Reference to the target hash into which all of this is read.
1421: # Note tht some of the hash entries we need to build require knowledge of the
1422: # course URI.. these are expected to be filled in by the caller.
1423: #
1424: # Errors are logged to lonnet and are managed via the Perl structured exception package.
1425: #
1426: #
1427: sub loadmap {
1.11 raeburn 1428: my ($cnum, $cdom, $uname, $udom, $code, $nohideurl, $target_hash) = @_;
1.3 foxr 1429:
1430:
1.1 foxr 1431:
1.8 raeburn 1432: # Clear the auxiliary hashes and the cond array.
1.1 foxr 1433:
1434:
1435: %randompick = ();
1436: %randompickseed = ();
1437: %encurl = ();
1438: %hiddenurl = ();
1.2 foxr 1439: %parmhash = ();
1.4 foxr 1440: @cond = ('true:normal'); # Initial value for cond 0.
1.2 foxr 1441: $retfrid = '';
1.3 foxr 1442: $username = '';
1443: $userdomain = '';
1444: %mapalias_cache = ();
1445: %cenv = ();
1.10 raeburn 1446: $map_number = 0;
1.11 raeburn 1447:
1.1 foxr 1448: #
1449:
1450: $username = $uname;
1451: $userdomain = $udom;
1452:
1.6 foxr 1453: $short_name = $cdom .'/' . $cnum;
1.3 foxr 1454: my $retfurl;
1.1 foxr 1455:
1456: try {
1457:
1458:
1459: # Get the information we need about the course.
1.6 foxr 1460: # Return without filling in anything if we can't get any info:
1461:
1462: %cenv = &Apache::lonnet::coursedescription($short_name,
1463: {'freshen_cache' => 1,
1464: 'user' => $uname});
1465:
1.10 raeburn 1466: unless ($cenv{'url'}) {
1.6 foxr 1467: &Apache::lonnet::logthis("lonmap::loadmap failed: $cnum/$cdom - did not get url");
1468: return;
1469: }
1470:
1471: $course_id = $cdom . '_' . $cnum; # Long course id.
1472:
1473: # Load the version information into the hash
1474:
1475:
1.1 foxr 1476: &process_versions(\%cenv, $target_hash);
1477:
1478:
1479: # Figure out the map filename's URI, and set up some starting points for the map.
1480:
1.2 foxr 1481: my $course_uri = $cenv{'url'};
1482: my $map_uri = &Apache::lonnet::clutter($course_uri);
1.1 foxr 1483:
1484: $target_hash->{'src_0.0'} = &versiontrack($map_uri, $target_hash);
1485: $target_hash->{'title_0.0'} = &Apache::lonnet::metadata($course_uri, 'title');
1.3 foxr 1486: if(!defined $target_hash->{'title_0.0'}) {
1487: $target_hash->{'title_0.0'} = '';
1488: }
1.2 foxr 1489: $target_hash->{'ids_'.$map_uri} = '0.0';
1.3 foxr 1490: $target_hash->{'is_map_0.0'} = '1';
1491:
1492: # In some places we need a username a domain and the courseid...store that
1493: # in the target hash in the context.xxxx keys:
1494:
1495: $target_hash->{'context.username'} = $username;
1496: $target_hash->{'context.userdom'} = $userdomain;
1497: $target_hash->{'context.courseid'} = $course_id;
1.11 raeburn 1498:
1499: # When grading or printing a bubblesheet exam ignore
1500: # "hidden" parameter set in the map containing the exam folder.
1501: $target_hash->{'context.nohideurl'} = $nohideurl;
1.6 foxr 1502:
1.7 raeburn 1503: &read_map($course_uri, '0.0', $code, $target_hash);
1.1 foxr 1504:
1.2 foxr 1505: if (defined($target_hash->{'map_start_'.$map_uri})) {
1.1 foxr 1506:
1.3 foxr 1507: &traceroute('0',$target_hash->{'map_start_'.$course_uri},'&', 0, 0, $target_hash);
1508: &accinit($course_uri, $short_name, $target_hash);
1.2 foxr 1509: &hiddenurls($target_hash);
1510: }
1511: my $errors = &get_mapalias_errors($target_hash);
1512: if ($errors ne "") {
1513: throw Error::Simple("Map alias errors: ", $errors);
1514: }
1515:
1516: # Put the versions in to src:
1517:
1518: foreach my $key (keys(%$target_hash)) {
1519: if ($key =~ /^src_/) {
1520: $target_hash->{$key} =
1521: &putinversion($target_hash->{$key}, $target_hash, $short_name);
1522: } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
1523: my ($type, $url) = ($1,$2);
1524: my $value = $target_hash->{$key};
1525: $target_hash->{$type.&putinversion($url, $target_hash, $short_name)}=$value;
1526: }
1.1 foxr 1527: }
1.3 foxr 1528: # Mark necrypted URLS.
1529:
1530: foreach my $id (keys(%encurl)) {
1531: $target_hash->{'encrypted_'.$id}=1;
1532: }
1533:
1534: # Store first keys.
1535:
1536: $target_hash->{'first_rid'}=$retfrid;
1537: my ($mapid,$resid)=split(/\./,$retfrid);
1538: $target_hash->{'first_mapurl'}=$target_hash->{'map_id_'.$mapid};
1539: my $symb=&Apache::lonnet::encode_symb($target_hash->{'map_id_'.$mapid},
1540: $resid,
1541: $target_hash->{'src_'.$retfrid});
1542: $retfurl=&add_get_param($target_hash->{'src_'.$retfrid},{ 'symb' => $symb });
1543: if ($target_hash->{'encrypted_'.$retfrid}) {
1544: $retfurl=&Apache::lonenc::encrypted($retfurl,
1545: (&Apache::lonnet::allowed('adv') ne 'F'));
1546: }
1547: $target_hash->{'first_url'}=$retfurl;
1.1 foxr 1548:
1549: # Merge in the child hashes in case the caller wants that information as well.
1550:
1551:
1.2 foxr 1552: &merge_hash($target_hash, 'randompick', \%randompick);
1.10 raeburn 1553: &merge_hash($target_hash, 'randompickseed', \%randompickseed);
1.2 foxr 1554: &merge_hash($target_hash, 'randomorder', \%randomorder);
1555: &merge_hash($target_hash, 'encurl', \%encurl);
1556: &merge_hash($target_hash, 'hiddenurl', \%hiddenurl);
1557: &merge_hash($target_hash, 'param', \%parmhash);
1558: &merge_conditions($target_hash);
1.1 foxr 1559: }
1560: otherwise {
1561: my $e = shift;
1562: &Apache::lonnet::logthis("lonmap::loadmap failed: " . $e->stringify());
1563: }
1564:
1565: }
1566:
1567:
1568: 1;
1569:
1570: #
1571: # Module initialization code:
1.3 foxr 1572: # TODO: Fix the pod docs below.
1.1 foxr 1573:
1574: 1;
1575: __END__
1576:
1577: =head1 NAME
1578:
1579: Apache::lonmap - Construct a hash that represents a course (Big Hash).
1580:
1581: =head1 SYNOPSIS
1582:
1.11 raeburn 1583: &Apache::lonmap::loadmap($cnum, $cdom, $uname, $udom, $code, $nohideurl, \%target_hash);
1.1 foxr 1584:
1585: =head1 INTRODUCTION
1586:
1587: This module reads a course filename into a hash reference. It's up to the caller
1.11 raeburn 1588: to do things like decide that the hash should be tied to some external file and handle the
1589: the locking if this file should be shared amongst several Apache children.
1.1 foxr 1590:
1591: =head1 SUBROUTINES
1592:
1593: =over
1594:
1.11 raeburn 1595: =item loadmap($cnum, $cdom, $uname, $udom, $code, $nohideurl, $targethash)
1.1 foxr 1596:
1597:
1.11 raeburn 1598: Reads the top-level map file into a target hash. This is done by first parsing the
1599: map file into local hashes and then unrolling those hashes into the big hash.
1.1 foxr 1600:
1601: =over
1602:
1.11 raeburn 1603: =item $cnum - number of course being read.
1604:
1605: =item $cdom - domain in which the course is evaluated.
1606:
1607: =item $uname - name of the user for whom the course is being read.
1608:
1609: =item $udom - name of the domain of the user for whom the course is being read.
1610:
1611: =item $code - CODE for which course is being read (CODEd assignments).
1612:
1613: =item $nohideurl - URL for an exam folder for which hidden state is to be ignored.
1.1 foxr 1614:
1.11 raeburn 1615: =item $targethash - A reference to hash into which the course is read
1.1 foxr 1616:
1617: =back
1618:
1619: =item process_versions($cenv, $hash)
1620:
1621: Makes hash entries for each version of a course described by a course environment
1622: returned from Apache::lonnet::coursedescription.
1623:
1624: =over
1625:
1626: =item $cenv - Reference to the environment hash returned by Apache::lonnet::coursedescription
1627:
1628: =item $hash - Hash to be filled in with 'version_xxx' entries as per the big hash.
1629:
1630: =back
1631:
1632: =back
1633:
1634:
1635: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>