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