1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
4: # $Id: lonuserstate.pm,v 1.149.2.3 2020/03/05 22:39:57 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: package Apache::lonuserstate;
31:
32: # ------------------------------------------------- modules used by this module
33: use strict;
34: use HTML::TokeParser;
35: use Apache::lonnet;
36: use Apache::lonlocal;
37: use Apache::loncommon();
38: use GDBM_File;
39: use Apache::lonmsg;
40: use Safe;
41: use Safe::Hole;
42: use Opcode;
43: use Apache::lonenc;
44: use Fcntl qw(:flock);
45: use LONCAPA;
46: use File::Basename;
47:
48:
49:
50: # ---------------------------------------------------- Globals for this package
51:
52: my $pc; # Package counter is this what 'Guts' calls the map counter?
53: my %hash; # The big tied hash
54: my %parmhash;# The hash with the parameters
55: my @cond; # Array with all of the conditions
56: my $errtext; # variable with all errors
57: my $retfrid; # variable with the very first RID in the course
58: my $retfurl; # first URL
59: my %randompick; # randomly picked resources
60: my %randompickseed; # optional seed for randomly picking resources
61: my %randomorder; # maps to order contents randomly
62: my %randomizationcode; # code used to grade folder for bubblesheet exam
63: my %encurl; # URLs in this folder are supposed to be encrypted
64: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
65: my %rescount; # count of unhidden items in each map
66: my %mapcount; # count of unhidden maps in each map
67:
68: # ----------------------------------- Remove version from URL and store in hash
69:
70: sub versionerror {
71: my ($uri,$usedversion,$unusedversion)=@_;
72: return '<br />'.&mt('Version discrepancy: resource [_1] included in both version [_2] and version [_3]. Using version [_2].',
73: $uri,$usedversion,$unusedversion).'<br />';
74: }
75:
76: # Removes the version number from a URI and returns the resulting
77: # URI (e.g. mumbly.version.stuff => mumbly.stuff).
78: #
79: # If the URI has not been seen with a versio before the
80: # hash{'version_'.resultingURI} is set to the version number.
81: # If the URI has been seen and the version does not match and error
82: # is added to the error string.
83: #
84: # Parameters:
85: # URI potentially with a version.
86: # Returns:
87: # URI with the version cut out.
88: # See above for side effects.
89: #
90:
91: sub versiontrack {
92: my $uri=shift;
93: if ($uri=~/\.(\d+)\.\w+$/) {
94: my $version=$1;
95: $uri=~s/\.\d+\.(\w+)$/\.$1/;
96: unless ($hash{'version_'.$uri}) {
97: $hash{'version_'.$uri}=$version;
98: } elsif ($version!=$hash{'version_'.$uri}) {
99: $errtext.=&versionerror($uri,$hash{'version_'.$uri},$version);
100: }
101: }
102: return $uri;
103: }
104:
105: # -------------------------------------------------------------- Put in version
106:
107: sub putinversion {
108: my $uri=shift;
109: my $key=$env{'request.course.id'}.'_'.&Apache::lonnet::clutter($uri);
110: if ($hash{'version_'.$uri}) {
111: my $version=$hash{'version_'.$uri};
112: if ($version eq 'mostrecent') { return $uri; }
113: if ($version eq &Apache::lonnet::getversion(
114: &Apache::lonnet::filelocation('',$uri)))
115: { return $uri; }
116: $uri=~s/\.(\w+)$/\.$version\.$1/;
117: }
118: &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
119: return $uri;
120: }
121:
122: # ----------------------------------------- Processing versions file for course
123:
124: sub processversionfile {
125: my %cenv=@_;
126: my %versions=&Apache::lonnet::dump('resourceversions',
127: $cenv{'domain'},
128: $cenv{'num'});
129: foreach my $ver (keys(%versions)) {
130: if ($ver=~/^error\:/) { return; }
131: $hash{'version_'.$ver}=$versions{$ver};
132: }
133: }
134:
135: # --------------------------------------------------------- Loads from disk
136:
137:
138: #
139: # Loads a map file.
140: # Note that this may implicitly recurse via parse_resource if one of the resources
141: # is itself composed.
142: #
143: # Parameters:
144: # uri - URI of the map file.
145: # parent_rid - Resource id in the map of the parent resource (0.0 for the top level map)
146: # courseid - Course id for the course for which the map is being loaded
147: #
148: sub loadmap {
149: my ($uri,$parent_rid,$courseid)=@_;
150:
151: # Is the map already included?
152:
153: if ($hash{'map_pc_'.$uri}) {
154: $errtext.='<p class="LC_error">'.
155: &mt('Multiple use of sequence/page [_1]! The course will not function properly.','<tt>'.$uri.'</tt>').
156: '</p>';
157: return;
158: }
159: # Register the resource in it's map_pc_ [for the URL]
160: # map_id.nnn is the nesting level -> to the URI.
161:
162: $pc++;
163: my $lpc=$pc;
164: $hash{'map_pc_'.$uri}=$lpc;
165: $hash{'map_id_'.$lpc}=$uri;
166:
167: # If the parent is of the form n.m hang this map underneath it in the
168: # map hierarchy.
169:
170: if ($parent_rid =~ /^(\d+)\.\d+$/) {
171: my $parent_pc = $1;
172: if (defined($hash{'map_hierarchy_'.$parent_pc})) {
173: $hash{'map_hierarchy_'.$lpc}=$hash{'map_hierarchy_'.$parent_pc}.','.
174: $parent_pc;
175: } else {
176: $hash{'map_hierarchy_'.$lpc}=$parent_pc;
177: }
178: }
179:
180: # Determine and check filename of the sequence we need to read:
181:
182: my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
183:
184: my $ispage=($fn=~/\.page$/);
185:
186: # We can only nest sequences or pages. Anything else is an illegal nest.
187:
188: unless (($fn=~/\.sequence$/) || $ispage) {
189: $errtext.='<br />'.&mt('Invalid map: [_1]',"<tt>$fn</tt>");
190: return;
191: }
192:
193: # Read the XML that constitutes the file.
194:
195: my $instr=&Apache::lonnet::getfile($fn);
196:
197: if ($instr eq -1) {
198: $errtext.= '<br />'
199: .&mt('Map not loaded: The file [_1] does not exist.',
200: "<tt>$fn</tt>");
201: return;
202: }
203:
204: # Successfully got file, parse it
205:
206: # parse for parameter processing.
207: # Note that these are <param... / > tags
208: # so we only care about 'S' (tag start) nodes.
209:
210:
211: my $parser = HTML::TokeParser->new(\$instr);
212: $parser->attr_encoded(1);
213:
214: # first get all parameters
215:
216:
217: while (my $token = $parser->get_token) {
218: next if ($token->[0] ne 'S');
219: if ($token->[1] eq 'param') {
220: &parse_param($token,$lpc);
221: }
222: }
223:
224: # Get set to take another pass through the XML:
225: # for resources and links.
226:
227: $parser = HTML::TokeParser->new(\$instr);
228: $parser->attr_encoded(1);
229:
230: my $linkpc=0;
231:
232: $fn=~/\.(\w+)$/;
233:
234: $hash{'map_type_'.$lpc}=$1;
235:
236: my $randomize = ($randomorder{$parent_rid} =~ /^yes$/i);
237:
238: # Parse the resources, link and condition tags.
239: # Note that if randomorder or random select is chosen the links and
240: # conditions are meaningless but are determined by the randomization.
241: # This is handled in the next chunk of code.
242:
243: my @map_ids;
244: my $codechecked;
245: $rescount{$lpc} = 0;
246: $mapcount{$lpc} = 0;
247: while (my $token = $parser->get_token) {
248: next if ($token->[0] ne 'S');
249:
250: # Resource
251:
252: if ($token->[1] eq 'resource') {
253: my $resource_id = &parse_resource($token,$lpc,$ispage,$uri,$courseid);
254: if (defined $resource_id) {
255: push(@map_ids, $resource_id);
256: if ($hash{'src_'.$lpc.'.'.$resource_id}) {
257: $rescount{$lpc} ++;
258: if (($hash{'src_'.$lpc.'.'.$resource_id}=~/\.sequence$/) ||
259: ($hash{'src_'.$lpc.'.'.$resource_id}=~/\.page$/)) {
260: $mapcount{$lpc} ++;
261: }
262: }
263: unless ($codechecked) {
264: my $startsymb =
265: &Apache::lonnet::encode_symb($hash{'map_id_'.$lpc},$resource_id,
266: $hash{'src_'."$lpc.$resource_id"});
267: my $code =
268: &Apache::lonnet::EXT('resource.0.examcode',$startsymb,undef,undef,
269: undef,undef,$courseid);
270: if ($code) {
271: $randomizationcode{$parent_rid} = $code;
272: }
273: $codechecked = 1;
274: }
275: }
276:
277: # Link
278:
279: } elsif ($token->[1] eq 'link' && !$randomize) {
280: &make_link(++$linkpc,$lpc,$token->[2]->{'to'},
281: $token->[2]->{'from'},
282: $token->[2]->{'condition'}); # note ..condition may be undefined.
283:
284: # condition
285:
286: } elsif ($token->[1] eq 'condition' && !$randomize) {
287: &parse_condition($token,$lpc);
288: }
289: }
290: undef($codechecked);
291:
292: # Handle randomization and random selection
293:
294: if ($randomize) {
295: my $advanced;
296: if ($env{'request.course.id'}) {
297: $advanced = (&Apache::lonnet::allowed('adv') eq 'F');
298: } else {
299: $env{'request.course.id'} = $courseid;
300: $advanced = (&Apache::lonnet::allowed('adv') eq 'F');
301: $env{'request.course.id'} = '';
302: }
303: unless ($advanced) {
304: # Order of resources is not randomized if user has and advanced role in the course.
305: my $seed;
306:
307: # If the map's random seed parameter has been specified
308: # it is used as the basis for computing the seed ...
309:
310: if (defined($randompickseed{$parent_rid})) {
311: $seed = $randompickseed{$parent_rid};
312: } else {
313:
314: # Otherwise the parent's fully encoded symb is used.
315:
316: my ($mapid,$resid)=split(/\./,$parent_rid);
317: my $symb=
318: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
319: $resid,$hash{'src_'.$parent_rid});
320:
321: $seed = $symb;
322: }
323:
324: # TODO: Here for sure we need to pass along the username/domain
325: # so that we can impersonate users in lonprintout e.g.
326:
327: my $setcode;
328: if (defined($randomizationcode{$parent_rid})) {
329: if ($env{'form.CODE'} eq '') {
330: $env{'form.CODE'} = $randomizationcode{$parent_rid};
331: $setcode = 1;
332: }
333: }
334:
335: my $rndseed=&Apache::lonnet::rndseed($seed);
336: &Apache::lonnet::setup_random_from_rndseed($rndseed);
337:
338: if ($setcode) {
339: undef($env{'form.CODE'});
340: undef($setcode);
341: }
342:
343: # Take the set of map ids we have decoded and permute them to a
344: # random order based on the seed set above. All of this is
345: # processing the randomorder parameter if it is set, not
346: # randompick.
347:
348: @map_ids=&Math::Random::random_permutation(@map_ids);
349: }
350:
351: my $from = shift(@map_ids);
352: my $from_rid = $lpc.'.'.$from;
353: $hash{'map_start_'.$uri} = $from_rid;
354: $hash{'type_'.$from_rid}='start';
355:
356: # Create links to reflect the random re-ordering done above.
357: # In the code to process the map XML, we did not process links or conditions
358: # if randomorder was set. This means that for an instructor to choose
359:
360: while (my $to = shift(@map_ids)) {
361: &make_link(++$linkpc,$lpc,$to,$from);
362: my $to_rid = $lpc.'.'.$to;
363: $hash{'type_'.$to_rid}='normal';
364: $from = $to;
365: $from_rid = $to_rid;
366: }
367:
368: $hash{'map_finish_'.$uri}= $from_rid;
369: $hash{'type_'.$from_rid}='finish';
370: }
371:
372: $parser = HTML::TokeParser->new(\$instr);
373: $parser->attr_encoded(1);
374:
375: # last parse out the mapalias params. These provide mnemonic
376: # tags to resources that can be used in conditions
377:
378: while (my $token = $parser->get_token) {
379: next if ($token->[0] ne 'S');
380: if ($token->[1] eq 'param') {
381: &parse_mapalias_param($token,$lpc);
382: }
383: }
384: }
385:
386:
387: # -------------------------------------------------------------------- Resource
388: #
389: # Parses a resource tag to produce the value to push into the
390: # map_ids array.
391: #
392: #
393: # Information about the actual type of resource is provided by the file extension
394: # of the uri (e.g. .problem, .sequence etc. etc.).
395: #
396: # Parameters:
397: # $token - A token from HTML::TokeParser
398: # This is an array that describes the most recently parsed HTML item.
399: # $lpc - Map nesting level (?)
400: # $ispage - True if this resource is encapsulated in a .page (assembled resourcde).
401: # $uri - URI of the enclosing resource.
402: # $courseid - Course id of the course containing the resource being parsed.
403: # Returns:
404: # Value of the id attribute of the tag.
405: #
406: # Note:
407: # The token is an array that contains the following elements:
408: # [0] => 'S' indicating this is a start token
409: # [1] => 'resource' indicating this tag is a <resource> tag.
410: # [2] => Hash of attribute =>value pairs.
411: # [3] => @(keys [2]).
412: # [4] => unused.
413: #
414: # The attributes of the resourcde tag include:
415: #
416: # id - The resource id.
417: # src - The URI of the resource.
418: # type - The resource type (e.g. start and finish).
419: # title - The resource title.
420:
421:
422: sub parse_resource {
423: my ($token,$lpc,$ispage,$uri,$courseid) = @_;
424:
425: # I refuse to countenance code like this that has
426: # such a dirty side effect (and forcing this sub to be called within a loop).
427: #
428: # if ($token->[2]->{'type'} eq 'zombie') { next; }
429: #
430: # The original code both returns _and_ skips to the next pass of the >caller's<
431: # loop, that's just dirty.
432: #
433:
434: # Zombie resources don't produce anything useful.
435:
436: if ($token->[2]->{'type'} eq 'zombie') {
437: return undef;
438: }
439:
440: my $rid=$lpc.'.'.$token->[2]->{'id'}; # Resource id in hash is levelcounter.id-in-xml.
441:
442: # Save the hash element type and title:
443:
444: $hash{'kind_'.$rid}='res';
445: $hash{'title_'.$rid}=$token->[2]->{'title'};
446:
447: # Get the version free URI for the resource.
448: # If a 'version' attribute was supplied, and this resource's version
449: # information has not yet been stored, store it.
450: #
451:
452: my $turi=&versiontrack($token->[2]->{'src'});
453: if ($token->[2]->{'version'}) {
454: unless ($hash{'version_'.$turi}) {
455: $hash{'version_'.$turi}=$1;
456: }
457: }
458: # Pull out the title and do entity substitution on &colon
459: # Q: Why no other entity substitutions?
460:
461: my $title=$token->[2]->{'title'};
462: $title=~s/\&colon\;/\:/gs;
463:
464:
465:
466: # I think the point of all this code is to construct a final
467: # URI that apache and its rewrite rules can use to
468: # fetch the resource. Thi s sonly necessary if the resource
469: # is not a page. If the resource is a page then it must be
470: # assembled (at fetch time?).
471:
472: if ($ispage) {
473: if ($token->[2]->{'external'} eq 'true') { # external
474: $turi=~s{^http\://}{/ext/};
475: }
476: } else {
477: $turi=~/\.(\w+)$/;
478: my $embstyle=&Apache::loncommon::fileembstyle($1);
479: if ($token->[2]->{'external'} eq 'true') { # external
480: $turi=~s/^https?\:\/\//\/adm\/wrapper\/ext\//;
481: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
482: if (($embstyle eq 'img')
483: || ($embstyle eq 'emb')
484: || ($embstyle eq 'wrp')) {
485: $turi='/adm/wrapper'.$turi;
486: } elsif ($embstyle eq 'ssi') {
487: #do nothing with these
488: } elsif ($turi!~/\.(sequence|page)$/) {
489: $turi='/adm/coursedocs/showdoc'.$turi;
490: }
491: } elsif ($turi=~/\S/) { # normal non-empty internal resource
492: my $mapdir=$uri;
493: $mapdir=~s/[^\/]+$//;
494: $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
495: if (($embstyle eq 'img')
496: || ($embstyle eq 'emb')
497: || ($embstyle eq 'wrp')) {
498: $turi='/adm/wrapper'.$turi;
499: }
500: }
501: }
502: # Store reverse lookup, remove query string resource 'ids'_uri => resource id.
503: # If the URI appears more than one time in the sequence, it's resourcde
504: # id's are constructed as a comma spearated list.
505:
506: my $idsuri=$turi;
507: $idsuri=~s/\?.+$//;
508: if (defined($hash{'ids_'.$idsuri})) {
509: $hash{'ids_'.$idsuri}.=','.$rid;
510: } else {
511: $hash{'ids_'.$idsuri}=''.$rid;
512: }
513:
514:
515:
516: if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard|viewclasslist)$/) {
517: $turi.='?register=1';
518: }
519:
520:
521: # resource id lookup: 'src'_resourc-di => URI decorated with a query
522: # parameter as above if necessary due to the resource type.
523:
524: $hash{'src_'.$rid}=$turi;
525:
526: # Mark the external-ness of the resource:
527:
528: if ($token->[2]->{'external'} eq 'true') {
529: $hash{'ext_'.$rid}='true:';
530: } else {
531: $hash{'ext_'.$rid}='false:';
532: }
533:
534: # If the resource is a start/finish resource set those
535: # entries in the has so that navigation knows where everything starts.
536: # TODO? If there is a malformed sequence that has no start or no finish
537: # resource, should this be detected and errors thrown? How would such a
538: # resource come into being other than being manually constructed by a person
539: # and then uploaded? Could that happen if an author decided a sequence was almost
540: # right edited it by hand and then reuploaded it to 'fix it' but accidently cut the
541: # start or finish resources?
542: #
543: # All resourcess also get a type_id => (start | finish | normal) hash entr.
544: #
545: if ($token->[2]->{'type'}) {
546: $hash{'type_'.$rid}=$token->[2]->{'type'};
547: if ($token->[2]->{'type'} eq 'start') {
548: $hash{'map_start_'.$uri}="$rid";
549: }
550: if ($token->[2]->{'type'} eq 'finish') {
551: $hash{'map_finish_'.$uri}="$rid";
552: }
553: } else {
554: $hash{'type_'.$rid}='normal';
555: }
556:
557: # Sequences end pages are constructed entities. They require that the
558: # map that defines _them_ be loaded as well into the hash...with this resourcde
559: # as the base of the nesting.
560: # Resources like that are also marked with is_map_id => 1 entries.
561: #
562:
563: if (($turi=~/\.sequence$/) ||
564: ($turi=~/\.page$/)) {
565: $hash{'is_map_'.$rid}=1;
566: &loadmap($turi,$rid,$courseid);
567: }
568: return $token->[2]->{'id'};
569: }
570:
571: #-------------------------------------------------------------------- link
572: # Links define how you are allowed to move from one resource to another.
573: # They are the transition edges in the directed graph that a map is.
574: # This sub takes informatino from a <link> tag and constructs the
575: # navigation bits and pieces of a map. There is no requirement that the
576: # resources that are linke are already defined, however clearly the map is
577: # badly broken if they are not _eventually_ defined.
578: #
579: # Note that links can be unconditional or conditional.
580: #
581: # Parameters:
582: # linkpc - The link counter for this level of map nesting (this is
583: # reset to zero by loadmap prior to starting to process
584: # links for map).
585: # lpc - The map level ocounter (how deeply nested this map is in
586: # the hierarchy of maps that are recursively read in.
587: # to - resource id (within the XML) of the target of the edge.
588: # from - resource id (within the XML) of the source of the edge.
589: # condition- id of condition associated with the edge (also within the XML).
590: #
591:
592: sub make_link {
593: my ($linkpc,$lpc,$to,$from,$condition) = @_;
594:
595: # Compute fully qualified ids for the link, the
596: # and from/to by prepending lpc.
597: #
598:
599: my $linkid=$lpc.'.'.$linkpc;
600: my $goesto=$lpc.'.'.$to;
601: my $comesfrom=$lpc.'.'.$from;
602: my $undercond=0;
603:
604:
605: # If there is a condition, qualify it with the level counter.
606:
607: if ($condition) {
608: $undercond=$lpc.'.'.$condition;
609: }
610:
611: # Links are represnted by:
612: # goesto_.fuullyqualifedlinkid => fully qualified to
613: # comesfrom.fullyqualifiedlinkid => fully qualified from
614: # undercond_.fullyqualifiedlinkid => fully qualified condition id.
615:
616: $hash{'goesto_'.$linkid}=$goesto;
617: $hash{'comesfrom_'.$linkid}=$comesfrom;
618: $hash{'undercond_'.$linkid}=$undercond;
619:
620: # In addition:
621: # to_.fully qualified from => comma separated list of
622: # link ids with that from.
623: # Similarly:
624: # from_.fully qualified to => comma separated list of link ids`
625: # with that to.
626: # That allows us given a resource id to know all edges that go to it
627: # and leave from it.
628: #
629:
630: if (defined($hash{'to_'.$comesfrom})) {
631: $hash{'to_'.$comesfrom}.=','.$linkid;
632: } else {
633: $hash{'to_'.$comesfrom}=''.$linkid;
634: }
635: if (defined($hash{'from_'.$goesto})) {
636: $hash{'from_'.$goesto}.=','.$linkid;
637: } else {
638: $hash{'from_'.$goesto}=''.$linkid;
639: }
640: }
641:
642: # ------------------------------------------------------------------- Condition
643: #
644: # Processes <condition> tags, storing sufficient information about them
645: # in the hash so that they can be evaluated and used to conditionalize
646: # what is presented to the student.
647: #
648: # these can have the following attributes
649: #
650: # id = A unique identifier of the condition within the map.
651: #
652: # value = Is a perl script-let that, when evaluated in safe space
653: # determines whether or not the condition is true.
654: # Normally this takes the form of a test on an Apache::lonnet::EXT call
655: # to find the value of variable associated with a resource in the
656: # map identified by a mapalias.
657: # Here's a fragment of XML code that illustrates this:
658: #
659: # <param to="5" value="mainproblem" name="parameter_0_mapalias" type="string" />
660: # <resource src="" id="1" type="start" title="Start" />
661: # <resource src="/res/msu/albertel/b_and_c/p1.problem" id="5" title="p1.problem" />
662: # <condition value="&EXT('user.resource.resource.0.tries','mainproblem')
663: # <2 " id="61" type="stop" />
664: # <link to="5" index="1" from="1" condition="61" />
665: #
666: # In this fragment:
667: # - The param tag establishes an alias to resource id 5 of 'mainproblem'.
668: # - The resource that is the start of the map is identified.
669: # - The resource tag identifies the resource associated with this tag
670: # and gives it the id 5.
671: # - The condition is true if the tries variable associated with mainproblem
672: # is less than 2 (that is the user has had more than 2 tries).
673: # The condition type is a stop condition which inhibits(?) the associated
674: # link if the condition is false.
675: # - The link to resource 5 from resource 1 is affected by this condition.
676: #
677: # type = Type of the condition. The type determines how the condition affects the
678: # link associated with it and is one of
679: # - 'force'
680: # - 'stop'
681: # anything else including not supplied..which treated as:
682: # - 'normal'.
683: # Presumably maps get created by the resource assembly tool and therefore
684: # illegal type values won't squirm their way into the XML.
685: #
686: # Side effects:
687: # - The kind_level-qualified-condition-id hash element is set to 'cond'.
688: # - The condition text is pushed into the cond array and its element number is
689: # set in the condid_level-qualified-condition-id element of the hash.
690: # - The condition type is colon appneded to the cond array element for this condition.
691: sub parse_condition {
692: my ($token,$lpc) = @_;
693: my $rid=$lpc.'.'.$token->[2]->{'id'};
694:
695: $hash{'kind_'.$rid}='cond';
696:
697: my $condition = $token->[2]->{'value'};
698: $condition =~ s/[\n\r]+/ /gs;
699: push(@cond, $condition);
700: $hash{'condid_'.$rid}=$#cond;
701: if ($token->[2]->{'type'}) {
702: $cond[$#cond].=':'.$token->[2]->{'type'};
703: } else {
704: $cond[$#cond].=':normal';
705: }
706: }
707:
708: # ------------------------------------------------------------------- Parameter
709: # Parse a <parameter> tag in the map.
710: # Parmameters:
711: # $token Token array for a start tag from HTML::TokeParser
712: # [0] = 'S'
713: # [1] = tagname ("param")
714: # [2] = Hash of {attribute} = values.
715: # [3] = Array of the keys in [2].
716: # [4] = unused.
717: # $lpc Current map nesting level.a
718: #
719: # Typical attributes:
720: # to=n - Number of the resource the parameter applies to.
721: # type=xx - Type of parameter value (e.g. string_yesno or int_pos).
722: # name=xxx - Name of parameter (e.g. parameter_randompick or parameter_randomorder).
723: # value=xxx - value of the parameter.
724:
725: sub parse_param {
726: my ($token,$lpc) = @_;
727: my $referid=$lpc.'.'.$token->[2]->{'to'}; # Resource param applies to.
728: my $name=$token->[2]->{'name'}; # Name of parameter
729: my $part;
730:
731:
732: if ($name=~/^parameter_(.*)_/) {
733: $part=$1;
734: } else {
735: $part=0;
736: }
737:
738: # Peel the parameter_ off the parameter name.
739:
740: $name=~s/^.*_([^_]*)$/$1/;
741:
742: # The value is:
743: # type.part.name.value
744:
745: my $newparam=
746: &escape($token->[2]->{'type'}).':'.
747: &escape($part.'.'.$name).'='.
748: &escape($token->[2]->{'value'});
749:
750: # The hash key is param_resourceid.
751: # Multiple parameters for a single resource are & separated in the hash.
752:
753:
754: if (defined($hash{'param_'.$referid})) {
755: $hash{'param_'.$referid}.='&'.$newparam;
756: } else {
757: $hash{'param_'.$referid}=''.$newparam;
758: }
759: #
760: # These parameters have to do with randomly selecting
761: # resources, therefore a separate hash is also created to
762: # make it easy to locate them when actually computing the resource set later on
763: # See the code conditionalized by ($randomize) in loadmap().
764:
765: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) { # Random selection turned on
766: $randompick{$referid}=$token->[2]->{'value'};
767: }
768: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) { # Randomseed provided.
769: $randompickseed{$referid}=$token->[2]->{'value'};
770: }
771: if ($token->[2]->{'name'}=~/^parameter_(0_)*randomorder$/) { # Random order turned on.
772: $randomorder{$referid}=$token->[2]->{'value'};
773: }
774:
775: # These parameters have to do with how the URLs of resources are presented to
776: # course members(?). encrypturl presents encypted url's while
777: # hiddenresource hides the URL.
778: #
779:
780: if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
781: if ($token->[2]->{'value'}=~/^yes$/i) {
782: $encurl{$referid}=1;
783: }
784: }
785: if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
786: if ($token->[2]->{'value'}=~/^yes$/i) {
787: $hiddenurl{$referid}=1;
788: }
789: }
790: }
791: #
792: # Parse mapalias parameters.
793: # these are tags of the form:
794: # <param to="nn"
795: # value="some-alias-for-resourceid-nn"
796: # name="parameter_0_mapalias"
797: # type="string" />
798: # A map alias is a textual name for a resource:
799: # - The to attribute identifies the resource (this gets level qualified below)
800: # - The value attributes provides the alias string.
801: # - name must be of the regexp form: /^parameter_(0_)*mapalias$/
802: # - e.g. the string 'parameter_' followed by 0 or more "0_" strings
803: # terminating with the string 'mapalias'.
804: # Examples:
805: # 'parameter_mapalias', 'parameter_0_mapalias', parameter_0_0_mapalias'
806: # Invalid to ids are silently ignored.
807: #
808: # Parameters:
809: # token - The token array fromthe HMTML::TokeParser
810: # lpc - The current map level counter.
811: #
812: sub parse_mapalias_param {
813: my ($token,$lpc) = @_;
814:
815: # Fully qualify the to value and ignore the alias if there is no
816: # corresponding resource.
817:
818: my $referid=$lpc.'.'.$token->[2]->{'to'};
819: return if (!exists($hash{'src_'.$referid}));
820:
821: # If this is a valid mapalias parameter,
822: # Append the target id to the count_mapalias element for that
823: # alias so that we can detect doubly defined aliases
824: # e.g.:
825: # <param to="1" value="george" name="parameter_0_mapalias" type="string" />
826: # <param to="2" value="george" name="parameter_0_mapalias" type="string" />
827: #
828: # The example above is trivial but the case that's important has to do with
829: # constructing a map that includes a nested map where the nested map may have
830: # aliases that conflict with aliases established in the enclosing map.
831: #
832: # ...and create/update the hash mapalias entry to actually store the alias.
833: #
834:
835: if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
836: &count_mapalias($token->[2]->{'value'},$referid);
837: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
838: }
839: }
840:
841: # --------------------------------------------------------- Simplify expression
842:
843:
844: #
845: # Someone should really comment this to describe what it does to what and why.
846: #
847: sub simplify {
848: my $expression=shift;
849: # (0&1) = 1
850: $expression=~s/\(0\&([_\.\d]+)\)/$1/g;
851: # (8)=8
852: $expression=~s/\(([_\.\d]+)\)/$1/g;
853: # 8&8=8
854: $expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
855: # 8|8=8
856: $expression=~s/([^_\.\d])([_\.\d]+)(?:\|\2)+([^_\.\d])/$1$2$3/g;
857: # (5&3)&4=5&3&4
858: $expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
859: # (((5&3)|(4&6)))=((5&3)|(4&6))
860: $expression=~
861: s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
862: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
863: $expression=~
864: s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
865: return $expression;
866: }
867:
868: # -------------------------------------------------------- Build condition hash
869:
870: #
871: # Traces a route recursively through the map after it has been loaded
872: # (I believe this really visits each resourcde that is reachable fromt he
873: # start top node.
874: #
875: # - Marks hidden resources as hidden.
876: # - Marks which resource URL's must be encrypted.
877: # - Figures out (if necessary) the first resource in the map.
878: # - Further builds the chunks of the big hash that define how
879: # conditions work
880: #
881: # Note that the tracing strategy won't visit resources that are not linked to
882: # anything or islands in the map (groups of resources that form a path but are not
883: # linked in to the path that can be traced from the start resource...but that's ok
884: # because by definition, those resources are not reachable by users of the course.
885: #
886: # Parameters:
887: # sofar - _URI of the prior entry or 0 if this is the top.
888: # rid - URI of the resource to visit.
889: # beenhere - list of resources (each resource enclosed by &'s) that have
890: # already been visited.
891: # encflag - If true the resource that resulted in a recursive call to us
892: # has an encoded URL (which means contained resources should too).
893: # hdnflag - If true,the resource that resulted in a recursive call to us
894: # was hidden (which means contained resources should be hidden too).
895: # Returns
896: # new value indicating how far the map has been traversed (the sofar).
897: #
898: sub traceroute {
899: my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
900: my $newsofar=$sofar=simplify($sofar);
901:
902: unless ($beenhere=~/\&\Q$rid\E\&/) {
903: $beenhere.=$rid.'&';
904: my ($mapid,$resid)=split(/\./,$rid);
905: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
906: my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
907:
908: if ($hdnflag || lc($hidden) eq 'yes') {
909: $hiddenurl{$rid}=1;
910: }
911: if (!$hdnflag && lc($hidden) eq 'no') {
912: delete($hiddenurl{$rid});
913: }
914:
915: my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
916: if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
917:
918: if (($retfrid eq '') && ($hash{'src_'.$rid})
919: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
920: $retfrid=$rid;
921: }
922:
923: if (defined($hash{'conditions_'.$rid})) {
924: $hash{'conditions_'.$rid}=simplify(
925: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
926: } else {
927: $hash{'conditions_'.$rid}=$sofar;
928: }
929:
930: # if the expression is just the 0th condition keep it
931: # otherwise leave a pointer to this condition expression
932:
933: $newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
934:
935: # Recurse if the resource is a map:
936:
937: if (defined($hash{'is_map_'.$rid})) {
938: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
939: $sofar=$newsofar=
940: &traceroute($sofar,
941: $hash{'map_start_'.$hash{'src_'.$rid}},
942: $beenhere,
943: $encflag || $encurl{$rid},
944: $hdnflag || $hiddenurl{$rid});
945: }
946: }
947:
948: # Processes links to this resource:
949: # - verify the existence of any conditionals on the link to here.
950: # - Recurse to any resources linked to us.
951: #
952: if (defined($hash{'to_'.$rid})) {
953: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
954: my $further=$sofar;
955: #
956: # If there's a condition associated with this link be sure
957: # it's been defined else that's an error:
958: #
959: if ($hash{'undercond_'.$id}) {
960: if (defined($hash{'condid_'.$hash{'undercond_'.$id}})) {
961: $further=simplify('('.'_'.$rid.')&('.
962: $hash{'condid_'.$hash{'undercond_'.$id}}.')');
963: } else {
964: $errtext.= '<br />'.
965: &mt('Undefined condition ID: [_1]',
966: $hash{'undercond_'.$id});
967: }
968: }
969: # Recurse to resoruces that have to's to us.
970: $newsofar=&traceroute($further,$hash{'goesto_'.$id},$beenhere,
971: $encflag,$hdnflag);
972: }
973: }
974: }
975: return $newsofar;
976: }
977:
978: # ------------------------------ Cascading conditions, quick access, parameters
979:
980: #
981: # Seems a rather strangely named sub given what the comment above says it does.
982:
983:
984: sub accinit {
985: my ($uri,$short,$fn)=@_;
986: my %acchash=();
987: my %captured=();
988: my $condcounter=0;
989: $acchash{'acc.cond.'.$short.'.0'}=0;
990:
991: # This loop is only interested in conditions and
992: # parameters in the big hash:
993:
994: foreach my $key (keys(%hash)) {
995:
996: # conditions:
997:
998: if ($key=~/^conditions/) {
999: my $expr=$hash{$key};
1000:
1001: # try to find and factor out common sub-expressions
1002: # Any subexpression that is found is simplified, removed from
1003: # the original condition expression and the simplified sub-expression
1004: # substituted back in to the epxression..I'm not actually convinced this
1005: # factors anything out...but instead maybe simplifies common factors(?)
1006:
1007: foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
1008: my $orig=$sub;
1009:
1010: my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
1011: next if (!defined($factor));
1012:
1013: $sub=~s/\Q$factor\E//g;
1014: $sub=~s/^\(/\($factor\(/;
1015: $sub.=')';
1016: $sub=simplify($sub);
1017: $expr=~s/\Q$orig\E/$sub/;
1018: }
1019: $hash{$key}=$expr;
1020:
1021: # If not yet seen, record in acchash and that we've seen it.
1022:
1023: unless (defined($captured{$expr})) {
1024: $condcounter++;
1025: $captured{$expr}=$condcounter;
1026: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
1027: }
1028: # Parameters:
1029:
1030: } elsif ($key=~/^param_(\d+)\.(\d+)/) {
1031: my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
1032: $hash{'src_'.$1.'.'.$2});
1033: foreach my $param (split(/\&/,$hash{$key})) {
1034: my ($typename,$value)=split(/\=/,$param);
1035: my ($type,$name)=split(/\:/,$typename);
1036: $parmhash{$prefix.'.'.&unescape($name)}=
1037: &unescape($value);
1038: $parmhash{$prefix.'.'.&unescape($name).'.type'}=
1039: &unescape($type);
1040: }
1041: }
1042: }
1043: # This loop only processes id entries in the big hash.
1044:
1045: foreach my $key (keys(%hash)) {
1046: if ($key=~/^ids/) {
1047: foreach my $resid (split(/\,/,$hash{$key})) {
1048: my $uri=$hash{'src_'.$resid};
1049: my ($uripath,$urifile) =
1050: &Apache::lonnet::split_uri_for_cond($uri);
1051: if ($uripath) {
1052: my $uricond='0';
1053: if (defined($hash{'conditions_'.$resid})) {
1054: $uricond=$captured{$hash{'conditions_'.$resid}};
1055: }
1056: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
1057: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
1058: /(\&\Q$urifile\E\:[^\&]*)/) {
1059: my $replace=$1;
1060: my $regexp=$replace;
1061: #$regexp=~s/\|/\\\|/g;
1062: $acchash{'acc.res.'.$short.'.'.$uripath} =~
1063: s/\Q$regexp\E/$replace\|$uricond/;
1064: } else {
1065: $acchash{'acc.res.'.$short.'.'.$uripath}.=
1066: $urifile.':'.$uricond.'&';
1067: }
1068: } else {
1069: $acchash{'acc.res.'.$short.'.'.$uripath}=
1070: '&'.$urifile.':'.$uricond.'&';
1071: }
1072: }
1073: }
1074: }
1075: }
1076: $acchash{'acc.res.'.$short.'.'}='&:0&';
1077: my $courseuri=$uri;
1078: $courseuri=~s/^\/res\///;
1079: my $regexp = 1;
1080: &Apache::lonnet::delenv('(acc\.|httpref\.)',$regexp);
1081: &Apache::lonnet::appenv(\%acchash);
1082: }
1083:
1084: # ---------------- Selectively delete from randompick maps and hidden url parms
1085:
1086: sub hiddenurls {
1087: my $randomoutentry='';
1088: foreach my $rid (keys(%randompick)) {
1089: my $rndpick=$randompick{$rid};
1090: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
1091: # ------------------------------------------- put existing resources into array
1092: my @currentrids=();
1093: foreach my $key (sort(keys(%hash))) {
1094: if ($key=~/^src_($mpc\.\d+)/) {
1095: if ($hash{'src_'.$1}) { push @currentrids, $1; }
1096: }
1097: }
1098: # rids are number.number and we want to numercially sort on
1099: # the second number
1100: @currentrids=sort {
1101: my (undef,$aid)=split(/\./,$a);
1102: my (undef,$bid)=split(/\./,$b);
1103: $aid <=> $bid;
1104: } @currentrids;
1105: next if ($#currentrids<$rndpick);
1106: # -------------------------------- randomly eliminate the ones that should stay
1107: my (undef,$id)=split(/\./,$rid);
1108: if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1109: my $setcode;
1110: if (defined($randomizationcode{$rid})) {
1111: if ($env{'form.CODE'} eq '') {
1112: $env{'form.CODE'} = $randomizationcode{$rid};
1113: $setcode = 1;
1114: }
1115: }
1116: my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
1117: if ($setcode) {
1118: undef($env{'form.CODE'});
1119: undef($setcode);
1120: }
1121: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1122: my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
1123: for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
1124: #&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1125: # -------------------------------------------------------- delete the leftovers
1126: for (my $k=0; $k<=$#currentrids; $k++) {
1127: if ($currentrids[$k]) {
1128: $hash{'randomout_'.$currentrids[$k]}=1;
1129: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
1130: if ($rescount{$mapid}) {
1131: $rescount{$mapid} --;
1132: }
1133: if ($hash{'is_map_'.$currentrids[$k]}) {
1134: if ($mapcount{$mapid}) {
1135: $mapcount{$mapid} --;
1136: }
1137: }
1138: $randomoutentry.='&'.
1139: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
1140: $resid,
1141: $hash{'src_'.$currentrids[$k]}
1142: ).'&';
1143: }
1144: }
1145: }
1146: # ------------------------------ take care of explicitly hidden urls or folders
1147: foreach my $rid (keys(%hiddenurl)) {
1148: $hash{'randomout_'.$rid}=1;
1149: my ($mapid,$resid)=split(/\./,$rid);
1150: if ($rescount{$mapid}) {
1151: $rescount{$mapid} --;
1152: }
1153: if ($hash{'is_map_'.$rid}) {
1154: if ($mapcount{$mapid}) {
1155: $mapcount{$mapid} --;
1156: }
1157: }
1158: $randomoutentry.='&'.
1159: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
1160: $hash{'src_'.$rid}).'&';
1161: }
1162: # --------------------------------------- append randomout entry to environment
1163: if ($randomoutentry) {
1164: &Apache::lonnet::appenv({'acc.randomout' => $randomoutentry});
1165: }
1166: }
1167:
1168: # -------------------------------------- populate big hash with map breadcrumbs
1169:
1170: # Create map_breadcrumbs_$pc from map_hierarchy_$pc by omitting intermediate
1171: # maps not shown in Course Contents table.
1172:
1173: sub mapcrumbs {
1174: foreach my $key (keys(%rescount)) {
1175: if ($hash{'map_hierarchy_'.$key}) {
1176: my $skipnext = 0;
1177: foreach my $id (split(/,/,$hash{'map_hierarchy_'.$key}),$key) {
1178: unless ($skipnext) {
1179: $hash{'map_breadcrumbs_'.$key} .= "$id,";
1180: }
1181: unless (($id == 0) || ($id == 1)) {
1182: if ((!$rescount{$id}) || ($rescount{$id} == 1 && $mapcount{$id} == 1)) {
1183: $skipnext = 1;
1184: } else {
1185: $skipnext = 0;
1186: }
1187: }
1188: }
1189: $hash{'map_breadcrumbs_'.$key} =~ s/,$//;
1190: }
1191: }
1192: }
1193:
1194: # ---------------------------------------------------- Read map and all submaps
1195:
1196: sub readmap {
1197: my $short=shift;
1198: $short=~s/^\///;
1199:
1200: # TODO: Hidden dependency on current user:
1201:
1202: my %cenv=&Apache::lonnet::coursedescription($short,{'freshen_cache'=>1});
1203:
1204: my $fn=$cenv{'fn'};
1205: my $uri;
1206: $short=~s/\//\_/g;
1207: unless ($uri=$cenv{'url'}) {
1208: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1209: "Could not load course $short.</font>");
1210: return ('',&mt('No course data available.'));;
1211: }
1212: @cond=('true:normal');
1213:
1214: unless (open(LOCKFILE,">","$fn.db.lock")) {
1215: #
1216: # Most likely a permissions problem on the lockfile or its directory.
1217: #
1218: $retfurl = '';
1219: return ($retfurl,'<br />'.&mt('Map not loaded - Lock file could not be opened when reading map:').' <tt>'.$fn.'</tt>.');
1220: }
1221: my $lock=0;
1222: my $gotstate=0;
1223:
1224: # If we can get the lock without delay any files there are idle
1225: # and from some prior request. We'll kill them off and regenerate them:
1226:
1227: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
1228: $lock=1; # Remember that we hold the lock.
1229: &unlink_tmpfiles($fn);
1230: }
1231: undef %randompick;
1232: undef %randompickseed;
1233: undef %randomorder;
1234: undef %randomizationcode;
1235: undef %hiddenurl;
1236: undef %encurl;
1237: undef %rescount;
1238: undef %mapcount;
1239: $retfrid='';
1240: $errtext='';
1241: my ($untiedhash,$untiedparmhash,$tiedhash,$tiedparmhash); # More state flags.
1242:
1243: # if we got the lock, regenerate course regnerate empty files and tie them.
1244:
1245: if ($lock) {
1246: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
1247: $tiedhash = 1;
1248: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
1249: $tiedparmhash = 1;
1250: $gotstate = &build_tmp_hashes($uri,
1251: $fn,
1252: $short,
1253: \%cenv); # TODO: Need to provide requested user@dom
1254: unless ($gotstate) {
1255: &Apache::lonnet::logthis('Failed to write statemap at first attempt '.$fn.' for '.$uri.'.</font>');
1256: }
1257: $untiedparmhash = untie(%parmhash);
1258: unless ($untiedparmhash) {
1259: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1260: 'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
1261: }
1262: }
1263: $untiedhash = untie(%hash);
1264: unless ($untiedhash) {
1265: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1266: 'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
1267: }
1268: }
1269: flock(LOCKFILE,LOCK_UN); # RF: this is what I don't get unless there are other
1270: # unlocked places the remainder happens..seems like if we
1271: # just kept the lock here the rest of the code would have
1272: # been much easier?
1273: }
1274: unless ($lock && $tiedhash && $tiedparmhash) {
1275: # if we are here it is likely because we are already trying to
1276: # initialize the course in another child, busy wait trying to
1277: # tie the hashes for the next 90 seconds, if we succeed forward
1278: # them on to navmaps, if we fail, throw up the Could not init
1279: # course screen
1280: #
1281: # RF: I'm not seeing the case where the ties/unties can fail in a way
1282: # that can be remedied by this. Since we owned the lock seems
1283: # Tie/untie failures are a result of something like a permissions problem instead?
1284: #
1285:
1286: # In any vent, undo what we did manage to do above first:
1287: if ($lock) {
1288: # Got the lock but not the DB files
1289: flock(LOCKFILE,LOCK_UN);
1290: $lock = 0;
1291: }
1292: if ($tiedhash) {
1293: unless($untiedhash) {
1294: untie(%hash);
1295: }
1296: }
1297: if ($tiedparmhash) {
1298: unless($untiedparmhash) {
1299: untie(%parmhash);
1300: }
1301: }
1302: # Log our failure:
1303:
1304: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1305: "Could not tie coursemap $fn for $uri.</font>");
1306: $tiedhash = '';
1307: $tiedparmhash = '';
1308: my $i=0;
1309:
1310: # Keep on retrying the lock for 90 sec until we succeed.
1311:
1312: while($i<90) {
1313: $i++;
1314: sleep(1);
1315: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
1316:
1317: # Got the lock, tie the hashes...the assumption in this code is
1318: # that some other worker thread has created the db files quite recently
1319: # so no load is needed:
1320:
1321: $lock = 1;
1322: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
1323: $tiedhash = 1;
1324: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
1325: $tiedparmhash = 1;
1326: if (-e "$fn.state") {
1327: $retfurl='/adm/navmaps';
1328:
1329: # BUG BUG: Side effect!
1330: # Should conditionalize on something so that we can use this
1331: # to load maps for courses that are not current?
1332: #
1333: &Apache::lonnet::appenv({"request.course.id" => $short,
1334: "request.course.fn" => $fn,
1335: "request.course.uri" => $uri,
1336: "request.course.tied" => time});
1337:
1338: $untiedhash = untie(%hash);
1339: $untiedparmhash = untie(%parmhash);
1340: $gotstate = 1;
1341: last;
1342: }
1343: $untiedparmhash = untie(%parmhash);
1344: }
1345: $untiedhash = untie(%hash);
1346: }
1347: }
1348: }
1349: if ($lock) {
1350: flock(LOCKFILE,LOCK_UN);
1351: $lock = 0;
1352: if ($tiedparmhash) {
1353: unless ($untiedparmhash) {
1354: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1355: 'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
1356: }
1357: }
1358: if ($tiedparmhash) {
1359: unless ($untiedhash) {
1360: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1361: 'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
1362: }
1363: }
1364: }
1365: }
1366: # I think this branch of code is all about what happens if we just did the stuff above,
1367: # but found that the state file did not exist...again if we'd just held the lock
1368: # would that have made this logic simpler..as generating all the files would be
1369: # an atomic operation with respect to the lock.
1370: #
1371: unless ($gotstate) {
1372: $lock = 0;
1373: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1374: 'Could not read statemap '.$fn.' for '.$uri.'.</font>');
1375: &unlink_tmpfiles($fn);
1376: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
1377: $lock=1;
1378: }
1379: undef %randompick;
1380: undef %randompickseed;
1381: undef %randomorder;
1382: undef %randomizationcode;
1383: undef %hiddenurl;
1384: undef %encurl;
1385: undef %rescount;
1386: undef %mapcount;
1387: $errtext='';
1388: $retfrid='';
1389: #
1390: # Once more through the routine of tying and loading and so on.
1391: #
1392: if ($lock) {
1393: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
1394: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
1395: $gotstate = &build_tmp_hashes($uri,$fn,$short,\%cenv); # TODO: User dependent?
1396: unless ($gotstate) {
1397: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1398: 'Failed to write statemap at second attempt '.$fn.' for '.$uri.'.</font>');
1399: }
1400: unless (untie(%parmhash)) {
1401: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1402: 'Could not untie coursemap parmhash '.$fn.'.db for '.$uri.'.</font>');
1403: }
1404: } else {
1405: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1406: 'Could not tie coursemap '.$fn.'__parms.db for '.$uri.'.</font>');
1407: }
1408: unless (untie(%hash)) {
1409: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1410: 'Could not untie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
1411: }
1412: } else {
1413: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1414: 'Could not tie coursemap '.$fn.'.db for '.$uri.'.</font>');
1415: }
1416: flock(LOCKFILE,LOCK_UN);
1417: $lock = 0;
1418: } else {
1419: # Failed to get the immediate lock.
1420:
1421: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1422: 'Could not obtain lock to tie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
1423: }
1424: }
1425: close(LOCKFILE);
1426: unless (($errtext eq '') || ($env{'request.course.uri'} =~ m{^/uploaded/})) {
1427: &Apache::lonmsg::author_res_msg($env{'request.course.uri'},
1428: $errtext); # TODO: User dependent?
1429: }
1430: # ------------------------------------------------- Check for critical messages
1431:
1432: # Depends on user must parameterize this as well..or separate as this is:
1433: # more part of determining what someone sees on entering a course?
1434:
1435: my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
1436: $env{'user.name'});
1437: if ($what[0]) {
1438: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1439: $retfurl='/adm/email?critical=display';
1440: }
1441: }
1442: return ($retfurl,$errtext);
1443: }
1444:
1445: #
1446: # This sub is called when the course hash and the param hash have been tied and
1447: # their lock file is held.
1448: # Parameters:
1449: # $uri - URI that identifies the course.
1450: # $fn - The base path/filename of the files that make up the context
1451: # being built.
1452: # $short - Short course name.
1453: # $cenvref - Reference to the course environment hash returned by
1454: # Apache::lonnet::coursedescription
1455: #
1456: # Assumptions:
1457: # The globals
1458: # %hash, %paramhash are tied to their gdbm files and we hold the lock on them.
1459: #
1460: sub build_tmp_hashes {
1461: my ($uri,$fn,$short,$cenvref) = @_;
1462:
1463: unless(ref($cenvref) eq 'HASH') {
1464: return;
1465: }
1466: my %cenv = %{$cenvref};
1467: my $gotstate = 0;
1468: %hash=(); # empty the global course and parameter hashes.
1469: %parmhash=();
1470: $errtext=''; # No error messages yet.
1471: $pc=0;
1472: &clear_mapalias_count();
1473: &processversionfile(%cenv);
1474:
1475: # URI Of the map file.
1476:
1477: my $furi=&Apache::lonnet::clutter($uri);
1478: #
1479: # the map staring points.
1480: #
1481: $hash{'src_0.0'}=&versiontrack($furi);
1482: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
1483: $hash{'ids_'.$furi}='0.0';
1484: $hash{'is_map_0.0'}=1;
1485:
1486: # Load the map.. note that loadmap may implicitly recurse if the map contains
1487: # sub-maps.
1488:
1489:
1490: &loadmap($uri,'0.0',$short);
1491:
1492: # The code below only executes if there is a starting point for the map>
1493: # Q/BUG??? If there is no start resource for the map should that be an error?
1494: #
1495:
1496: if (defined($hash{'map_start_'.$uri})) {
1497: &Apache::lonnet::appenv({"request.course.id" => $short,
1498: "request.course.fn" => $fn,
1499: "request.course.uri" => $uri,
1500: "request.course.tied" => time});
1501: $env{'request.course.id'}=$short;
1502: &traceroute('0',$hash{'map_start_'.$uri},'&');
1503: &accinit($uri,$short,$fn);
1504: &hiddenurls();
1505: &mapcrumbs();
1506: }
1507: $errtext .= &get_mapalias_errors();
1508: # ------------------------------------------------------- Put versions into src
1509: foreach my $key (keys(%hash)) {
1510: if ($key=~/^src_/) {
1511: $hash{$key}=&putinversion($hash{$key});
1512: } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
1513: my ($type, $url) = ($1,$2);
1514: my $value = $hash{$key};
1515: $hash{$type.&putinversion($url)}=$value;
1516: }
1517: }
1518: # ---------------------------------------------------------------- Encrypt URLs
1519: foreach my $id (keys(%encurl)) {
1520: # $hash{'src_'.$id}=&Apache::lonenc::encrypted($hash{'src_'.$id});
1521: $hash{'encrypted_'.$id}=1;
1522: }
1523: # ----------------------------------------------- Close hashes to finally store
1524: # --------------------------------- Routine must pass this point, no early outs
1525: $hash{'first_rid'}=$retfrid;
1526: my ($mapid,$resid)=split(/\./,$retfrid);
1527: $hash{'first_mapurl'}=$hash{'map_id_'.$mapid};
1528: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$retfrid});
1529: $retfurl=&add_get_param($hash{'src_'.$retfrid},{ 'symb' => $symb });
1530: if ($hash{'encrypted_'.$retfrid}) {
1531: $retfurl=&Apache::lonenc::encrypted($retfurl,(&Apache::lonnet::allowed('adv') ne 'F'));
1532: }
1533: $hash{'first_url'}=$retfurl;
1534: # ---------------------------------------------------- Store away initial state
1535: {
1536: my $cfh;
1537: if (open($cfh,">","$fn.state")) {
1538: print $cfh join("\n",@cond);
1539: $gotstate = 1;
1540: } else {
1541: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1542: "Could not write statemap $fn for $uri.</font>");
1543: }
1544: }
1545: return $gotstate;
1546: }
1547:
1548: sub unlink_tmpfiles {
1549: my ($fn) = @_;
1550: my $file_dir = dirname($fn);
1551:
1552: if ("$file_dir/" eq LONCAPA::tempdir()) {
1553: my @files = qw (.db _symb.db .state _parms.db);
1554: foreach my $file (@files) {
1555: if (-e $fn.$file) {
1556: unless (unlink($fn.$file)) {
1557: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1558: "Could not unlink ".$fn.$file."</font>");
1559: }
1560: }
1561: }
1562: }
1563: return;
1564: }
1565:
1566: # ------------------------------------------------------- Evaluate state string
1567:
1568: sub evalstate {
1569: my $fn=$env{'request.course.fn'}.'.state';
1570: my $state='';
1571: if (-e $fn) {
1572: my @conditions=();
1573: {
1574: open(my $fh,"<",$fn);
1575: @conditions=<$fh>;
1576: close($fh);
1577: }
1578: my $safeeval = new Safe;
1579: my $safehole = new Safe::Hole;
1580: $safeeval->permit("entereval");
1581: $safeeval->permit(":base_math");
1582: $safeeval->deny(":base_io");
1583: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1584: foreach my $line (@conditions) {
1585: chomp($line);
1586: my ($condition,$weight)=split(/\:/,$line);
1587: if ($safeeval->reval($condition)) {
1588: if ($weight eq 'force') {
1589: $state.='3';
1590: } else {
1591: $state.='2';
1592: }
1593: } else {
1594: if ($weight eq 'stop') {
1595: $state.='0';
1596: } else {
1597: $state.='1';
1598: }
1599: }
1600: }
1601: }
1602: &Apache::lonnet::appenv({'user.state.'.$env{'request.course.id'} => $state});
1603: return $state;
1604: }
1605:
1606: # This block seems to have code to manage/detect doubly defined
1607: # aliases in maps.
1608:
1609: {
1610: my %mapalias_cache;
1611: sub count_mapalias {
1612: my ($value,$resid) = @_;
1613: push(@{ $mapalias_cache{$value} }, $resid);
1614: }
1615:
1616: sub get_mapalias_errors {
1617: my $error_text;
1618: foreach my $mapalias (sort(keys(%mapalias_cache))) {
1619: next if (scalar(@{ $mapalias_cache{$mapalias} } ) == 1);
1620: my $count;
1621: my $which =
1622: join('</li><li>',
1623: map {
1624: my $id = $_;
1625: if (exists($hash{'src_'.$id})) {
1626: $count++;
1627: }
1628: my ($mapid) = split(/\./,$id);
1629: &mt('Resource [_1][_2]in Map [_3]',
1630: $hash{'title_'.$id},'<br />',
1631: $hash{'title_'.$hash{'ids_'.$hash{'map_id_'.$mapid}}});
1632: } (@{ $mapalias_cache{$mapalias} }));
1633: next if ($count < 2);
1634: $error_text .= '<div class="LC_error">'.
1635: &mt('Error: Found the mapalias "[_1]" defined multiple times.',
1636: $mapalias).
1637: '</div><ul><li>'.$which.'</li></ul>';
1638: }
1639: &clear_mapalias_count();
1640: return $error_text;
1641: }
1642: sub clear_mapalias_count {
1643: undef(%mapalias_cache);
1644: }
1645: }
1646: 1;
1647: __END__
1648:
1649: =head1 NAME
1650:
1651: Apache::lonuserstate - Construct and maintain state and binary representation
1652: of course for user
1653:
1654: =head1 SYNOPSIS
1655:
1656: Invoked by lonroles.pm.
1657:
1658: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1659:
1660: =head1 INTRODUCTION
1661:
1662: This module constructs and maintains state and binary representation
1663: of course for user.
1664:
1665: This is part of the LearningOnline Network with CAPA project
1666: described at http://www.lon-capa.org.
1667:
1668: =head1 SUBROUTINES
1669:
1670: =over
1671:
1672: =item loadmap()
1673:
1674: Loads map from disk
1675:
1676: =item simplify()
1677:
1678: Simplify expression
1679:
1680: =item traceroute()
1681:
1682: Build condition hash
1683:
1684: =item accinit()
1685:
1686: Cascading conditions, quick access, parameters
1687:
1688: =item readmap()
1689:
1690: Read map and all submaps
1691:
1692: =item evalstate()
1693:
1694: Evaluate state string
1695:
1696: =back
1697:
1698: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>