Annotation of rat/lonuserstate.pm, revision 1.128.2.3
1.1 www 1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
1.128.2.3! raeburn 4: # $Id: lonuserstate.pm,v 1.128.2.2 2009/03/21 06:12:05 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.114 www 45: use LONCAPA;
46:
1.15 www 47:
1.1 www 48: # ---------------------------------------------------- Globals for this package
49:
50: my $pc; # Package counter
51: my %hash; # The big tied hash
1.19 www 52: my %parmhash;# The hash with the parameters
1.1 www 53: my @cond; # Array with all of the conditions
54: my $errtext; # variable with all errors
1.116 www 55: my $retfrid; # variable with the very first RID in the course
56: my $retfurl; # first URL
1.29 www 57: my %randompick; # randomly picked resources
1.51 www 58: my %randompickseed; # optional seed for randomly picking resources
1.124 albertel 59: my %randomorder; # maps to order contents randomly
1.73 www 60: my %encurl; # URLs in this folder are supposed to be encrypted
61: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
1.61 www 62:
63: # ----------------------------------- Remove version from URL and store in hash
64:
65: sub versiontrack {
66: my $uri=shift;
67: if ($uri=~/\.(\d+)\.\w+$/) {
68: my $version=$1;
69: $uri=~s/\.\d+\.(\w+)$/\.$1/;
1.62 www 70: unless ($hash{'version_'.$uri}) {
71: $hash{'version_'.$uri}=$version;
72: }
1.61 www 73: }
74: return $uri;
75: }
76:
77: # -------------------------------------------------------------- Put in version
78:
79: sub putinversion {
80: my $uri=shift;
1.93 www 81: my $key=$env{'request.course.id'}.'_'.&Apache::lonnet::clutter($uri);
1.61 www 82: if ($hash{'version_'.$uri}) {
83: my $version=$hash{'version_'.$uri};
1.65 www 84: if ($version eq 'mostrecent') { return $uri; }
1.66 www 85: if ($version eq &Apache::lonnet::getversion(
86: &Apache::lonnet::filelocation('',$uri)))
87: { return $uri; }
1.61 www 88: $uri=~s/\.(\w+)$/\.$version\.$1/;
89: }
1.93 www 90: &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
1.61 www 91: return $uri;
92: }
93:
94: # ----------------------------------------- Processing versions file for course
95:
96: sub processversionfile {
1.64 www 97: my %cenv=@_;
1.61 www 98: my %versions=&Apache::lonnet::dump('resourceversions',
99: $cenv{'domain'},
100: $cenv{'num'});
1.106 albertel 101: foreach my $ver (keys(%versions)) {
102: if ($ver=~/^error\:/) { return; }
103: $hash{'version_'.$ver}=$versions{$ver};
1.61 www 104: }
105: }
1.45 www 106:
1.1 www 107: # --------------------------------------------------------- Loads map from disk
108:
109: sub loadmap {
1.124 albertel 110: my ($uri,$parent_rid)=@_;
1.114 www 111: if ($hash{'map_pc_'.$uri}) {
1.120 albertel 112: $errtext.='<p class="LC_error">'.
113: &mt('Multiple use of sequence/page [_1]! The course will not function properly.','<tt>'.$uri.'</tt>').
114: '</p>';
1.114 www 115: return;
116: }
1.1 www 117: $pc++;
118: my $lpc=$pc;
119: $hash{'map_pc_'.$uri}=$lpc;
120: $hash{'map_id_'.$lpc}=$uri;
121:
1.37 www 122: # Determine and check filename
1.62 www 123: my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
1.37 www 124:
125: my $ispage=($fn=~/\.page$/);
1.1 www 126:
1.10 www 127: unless (($fn=~/\.sequence$/) ||
1.1 www 128: ($fn=~/\.page$/)) {
1.114 www 129: $errtext.=&mt("<br />Invalid map: <tt>[_1]</tt>",$fn);
1.98 albertel 130: return;
1.1 www 131: }
132:
1.37 www 133: my $instr=&Apache::lonnet::getfile($fn);
134:
1.124 albertel 135: if ($instr eq -1) {
136: $errtext.=&mt('<br />Map not loaded: The file <tt>[_1]</tt> does not exist.',$fn);
137: return;
138: }
1.22 www 139:
1.37 www 140: # Successfully got file, parse it
1.1 www 141:
1.124 albertel 142: my $parser = HTML::TokeParser->new(\$instr);
143: $parser->attr_encoded(1);
144: # first get all parameters
145: while (my $token = $parser->get_token) {
146: next if ($token->[0] ne 'S');
147: if ($token->[1] eq 'param') {
148: &parse_param($token,$lpc);
149: }
150: }
151: #reset parser
152: $parser = HTML::TokeParser->new(\$instr);
153: $parser->attr_encoded(1);
1.1 www 154:
1.124 albertel 155: my $linkpc=0;
1.1 www 156:
1.124 albertel 157: $fn=~/\.(\w+)$/;
1.1 www 158:
1.124 albertel 159: $hash{'map_type_'.$lpc}=$1;
1.1 www 160:
1.124 albertel 161: my $randomize = ($randomorder{$parent_rid} =~ /^yes$/i);
1.1 www 162:
1.124 albertel 163: my @map_ids;
164: while (my $token = $parser->get_token) {
165: next if ($token->[0] ne 'S');
166: if ($token->[1] eq 'resource') {
167: push(@map_ids,&parse_resource($token,$lpc,$ispage,$uri));
168: } elsif ($token->[1] eq 'link' && !$randomize) {
1.1 www 169: # ----------------------------------------------------------------------- Links
1.124 albertel 170: &make_link(++$linkpc,$lpc,$token->[2]->{'to'},
171: $token->[2]->{'from'},
172: $token->[2]->{'condition'});
173: } elsif ($token->[1] eq 'condition' && !$randomize) {
174: &parse_condition($token,$lpc);
175: }
176: }
1.1 www 177:
1.124 albertel 178: if ($randomize) {
179: if (!$env{'request.role.adv'}) {
180: my $seed;
181: if (defined($randompickseed{$parent_rid})) {
182: $seed = $randompickseed{$parent_rid};
183: } else {
184: my ($mapid,$resid)=split(/\./,$parent_rid);
185: my $symb=
186: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
187: $resid,$hash{'src_'.$parent_rid});
1.85 albertel 188:
1.124 albertel 189: $seed = $symb;
190: }
191:
192: my $rndseed=&Apache::lonnet::rndseed($seed);
193: &Apache::lonnet::setup_random_from_rndseed($rndseed);
194: @map_ids=&Math::Random::random_permutation(@map_ids);
195: }
196: my $from = shift(@map_ids);
197: my $from_rid = $lpc.'.'.$from;
198: $hash{'map_start_'.$uri} = $from_rid;
199: $hash{'type_'.$from_rid}='start';
200:
201: while (my $to = shift(@map_ids)) {
202: &make_link(++$linkpc,$lpc,$to,$from);
203: my $to_rid = $lpc.'.'.$to;
204: $hash{'type_'.$to_rid}='normal';
205: $from = $to;
206: $from_rid = $to_rid;
207: }
1.1 www 208:
1.124 albertel 209: $hash{'map_finish_'.$uri}= $from_rid;
210: $hash{'type_'.$from_rid}='finish';
1.1 www 211: }
1.121 albertel 212:
1.127 albertel 213: $parser = HTML::TokeParser->new(\$instr);
1.121 albertel 214: $parser->attr_encoded(1);
215: # last parse out the mapalias params so as to ignore anything
216: # refering to non-existant resources
217: while (my $token = $parser->get_token) {
218: next if ($token->[0] ne 'S');
219: if ($token->[1] eq 'param') {
220: &parse_mapalias_param($token,$lpc);
221: }
222: }
223: }
224:
1.124 albertel 225:
226: # -------------------------------------------------------------------- Resource
227: sub parse_resource {
228: my ($token,$lpc,$ispage,$uri) = @_;
229: if ($token->[2]->{'type'} eq 'zombie') { next; }
230: my $rid=$lpc.'.'.$token->[2]->{'id'};
231:
232: $hash{'kind_'.$rid}='res';
233: $hash{'title_'.$rid}=$token->[2]->{'title'};
234: my $turi=&versiontrack($token->[2]->{'src'});
235: if ($token->[2]->{'version'}) {
236: unless ($hash{'version_'.$turi}) {
237: $hash{'version_'.$turi}=$1;
238: }
239: }
240: my $title=$token->[2]->{'title'};
241: $title=~s/\&colon\;/\:/gs;
242: # my $symb=&Apache::lonnet::encode_symb($uri,
243: # $token->[2]->{'id'},
244: # $turi);
245: # &Apache::lonnet::do_cache_new('title',$symb,$title);
246: unless ($ispage) {
247: $turi=~/\.(\w+)$/;
248: my $embstyle=&Apache::loncommon::fileembstyle($1);
249: if ($token->[2]->{'external'} eq 'true') { # external
1.128.2.1 raeburn 250: $turi=~s/^https?\:\/\//\/adm\/wrapper\/ext\//;
1.124 albertel 251: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
252: if (($embstyle eq 'img')
253: || ($embstyle eq 'emb')
254: || ($embstyle eq 'wrp')) {
255: $turi='/adm/wrapper'.$turi;
256: } elsif ($embstyle eq 'ssi') {
257: #do nothing with these
258: } elsif ($turi!~/\.(sequence|page)$/) {
259: $turi='/adm/coursedocs/showdoc'.$turi;
260: }
261: } elsif ($turi=~/\S/) { # normal non-empty internal resource
262: my $mapdir=$uri;
263: $mapdir=~s/[^\/]+$//;
264: $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
265: if (($embstyle eq 'img')
266: || ($embstyle eq 'emb')
267: || ($embstyle eq 'wrp')) {
268: $turi='/adm/wrapper'.$turi;
269: }
270: }
271: }
272: # Store reverse lookup, remove query string
273: my $idsuri=$turi;
274: $idsuri=~s/\?.+$//;
275: if (defined($hash{'ids_'.$idsuri})) {
276: $hash{'ids_'.$idsuri}.=','.$rid;
277: } else {
278: $hash{'ids_'.$idsuri}=''.$rid;
279: }
280:
281: if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/) {
282: $turi.='?register=1';
283: }
284:
285: $hash{'src_'.$rid}=$turi;
286:
287: if ($token->[2]->{'external'} eq 'true') {
288: $hash{'ext_'.$rid}='true:';
289: } else {
290: $hash{'ext_'.$rid}='false:';
291: }
292: if ($token->[2]->{'type'}) {
293: $hash{'type_'.$rid}=$token->[2]->{'type'};
294: if ($token->[2]->{'type'} eq 'start') {
295: $hash{'map_start_'.$uri}="$rid";
296: }
297: if ($token->[2]->{'type'} eq 'finish') {
298: $hash{'map_finish_'.$uri}="$rid";
299: }
300: } else {
301: $hash{'type_'.$rid}='normal';
302: }
303:
304: if (($turi=~/\.sequence$/) ||
305: ($turi=~/\.page$/)) {
306: $hash{'is_map_'.$rid}=1;
307: &loadmap($turi,$rid);
308: }
309: return $token->[2]->{'id'};
310: }
311:
312: sub make_link {
313: my ($linkpc,$lpc,$to,$from,$condition) = @_;
314:
315: my $linkid=$lpc.'.'.$linkpc;
316: my $goesto=$lpc.'.'.$to;
317: my $comesfrom=$lpc.'.'.$from;
318: my $undercond=0;
319:
320: if ($condition) {
321: $undercond=$lpc.'.'.$condition;
322: }
323:
324: $hash{'goesto_'.$linkid}=$goesto;
325: $hash{'comesfrom_'.$linkid}=$comesfrom;
326: $hash{'undercond_'.$linkid}=$undercond;
327:
328: if (defined($hash{'to_'.$comesfrom})) {
329: $hash{'to_'.$comesfrom}.=','.$linkid;
330: } else {
331: $hash{'to_'.$comesfrom}=''.$linkid;
332: }
333: if (defined($hash{'from_'.$goesto})) {
334: $hash{'from_'.$goesto}.=','.$linkid;
335: } else {
336: $hash{'from_'.$goesto}=''.$linkid;
337: }
338: }
339:
340: # ------------------------------------------------------------------- Condition
341: sub parse_condition {
342: my ($token,$lpc) = @_;
343: my $rid=$lpc.'.'.$token->[2]->{'id'};
344:
345: $hash{'kind_'.$rid}='cond';
346:
347: my $condition = $token->[2]->{'value'};
348: $condition =~ s/[\n\r]+/ /gs;
349: push(@cond, $condition);
350: $hash{'condid_'.$rid}=$#cond;
351: if ($token->[2]->{'type'}) {
352: $cond[$#cond].=':'.$token->[2]->{'type'};
353: } else {
354: $cond[$#cond].=':normal';
355: }
356: }
357:
358: # ------------------------------------------------------------------- Parameter
359:
360: sub parse_param {
361: my ($token,$lpc) = @_;
362: my $referid=$lpc.'.'.$token->[2]->{'to'};
363: my $name=$token->[2]->{'name'};
364: my $part;
365: if ($name=~/^parameter_(.*)_/) {
366: $part=$1;
367: } else {
368: $part=0;
369: }
370: $name=~s/^.*_([^_]*)$/$1/;
371: my $newparam=
372: &escape($token->[2]->{'type'}).':'.
373: &escape($part.'.'.$name).'='.
374: &escape($token->[2]->{'value'});
375: if (defined($hash{'param_'.$referid})) {
376: $hash{'param_'.$referid}.='&'.$newparam;
377: } else {
378: $hash{'param_'.$referid}=''.$newparam;
379: }
380: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) {
381: $randompick{$referid}=$token->[2]->{'value'};
382: }
383: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) {
384: $randompickseed{$referid}=$token->[2]->{'value'};
385: }
386: if ($token->[2]->{'name'}=~/^parameter_(0_)*randomorder$/) {
387: $randomorder{$referid}=$token->[2]->{'value'};
388: }
389: if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
390: if ($token->[2]->{'value'}=~/^yes$/i) {
391: $encurl{$referid}=1;
392: }
393: }
394: if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
395: if ($token->[2]->{'value'}=~/^yes$/i) {
396: $hiddenurl{$referid}=1;
397: }
398: }
399: }
400:
1.121 albertel 401: sub parse_mapalias_param {
402: my ($token,$lpc) = @_;
403: my $referid=$lpc.'.'.$token->[2]->{'to'};
404: return if (!exists($hash{'src_'.$referid}));
405:
406: if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
1.122 albertel 407: &count_mapalias($token->[2]->{'value'},$referid);
1.121 albertel 408: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
409: }
1.1 www 410: }
411:
1.3 www 412: # --------------------------------------------------------- Simplify expression
413:
414: sub simplify {
1.85 albertel 415: my $expression=shift;
1.101 albertel 416: # (0&1) = 1
1.105 albertel 417: $expression=~s/\(0\&([_\.\d]+)\)/$1/g;
1.3 www 418: # (8)=8
1.105 albertel 419: $expression=~s/\(([_\.\d]+)\)/$1/g;
1.3 www 420: # 8&8=8
1.105 albertel 421: $expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
1.3 www 422: # 8|8=8
1.105 albertel 423: $expression=~s/([^_\.\d])([_\.\d]+)\|\2([^_\.\d])/$1$2$3/g;
1.3 www 424: # (5&3)&4=5&3&4
1.105 albertel 425: $expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
1.3 www 426: # (((5&3)|(4&6)))=((5&3)|(4&6))
1.105 albertel 427: $expression=~
428: s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
1.3 www 429: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
1.85 albertel 430: $expression=~
1.105 albertel 431: s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
1.85 albertel 432: return $expression;
1.3 www 433: }
434:
1.2 www 435: # -------------------------------------------------------- Build condition hash
436:
437: sub traceroute {
1.77 www 438: my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
1.81 albertel 439: my $newsofar=$sofar=simplify($sofar);
1.120 albertel 440: unless ($beenhere=~/\&\Q$rid\E\&/) {
1.85 albertel 441: $beenhere.=$rid.'&';
442: my ($mapid,$resid)=split(/\./,$rid);
443: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
444: my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
1.91 albertel 445:
1.90 albertel 446: if ($hdnflag || lc($hidden) eq 'yes') {
447: $hiddenurl{$rid}=1;
1.91 albertel 448: }
449: if (!$hdnflag && lc($hidden) eq 'no') {
1.90 albertel 450: delete($hiddenurl{$rid});
451: }
1.91 albertel 452:
1.85 albertel 453: my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
454: if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
1.116 www 455: if (($retfrid eq '') && ($hash{'src_'.$rid})
1.85 albertel 456: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
1.116 www 457: $retfrid=$rid;
1.85 albertel 458: }
459: if (defined($hash{'conditions_'.$rid})) {
460: $hash{'conditions_'.$rid}=simplify(
1.103 albertel 461: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.85 albertel 462: } else {
463: $hash{'conditions_'.$rid}=$sofar;
464: }
1.107 albertel 465:
466: # if the expression is just the 0th condition keep it
467: # otherwise leave a pointer to this condition expression
468: $newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
469:
1.85 albertel 470: if (defined($hash{'is_map_'.$rid})) {
471: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
472: $sofar=$newsofar=
473: &traceroute($sofar,
1.126 albertel 474: $hash{'map_start_'.$hash{'src_'.$rid}},
475: $beenhere,
1.85 albertel 476: $encflag || $encurl{$rid},
477: $hdnflag || $hiddenurl{$rid});
478: }
479: }
480: if (defined($hash{'to_'.$rid})) {
1.106 albertel 481: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
1.2 www 482: my $further=$sofar;
1.106 albertel 483: if ($hash{'undercond_'.$id}) {
484: if (defined($hash{'condid_'.$hash{'undercond_'.$id}})) {
1.105 albertel 485: $further=simplify('('.'_'.$rid.')&('.
1.106 albertel 486: $hash{'condid_'.$hash{'undercond_'.$id}}.')');
1.85 albertel 487: } else {
1.114 www 488: $errtext.=&mt('<br />Undefined condition ID: [_1]',$hash{'undercond_'.$id});
1.85 albertel 489: }
1.2 www 490: }
1.106 albertel 491: $newsofar=&traceroute($further,$hash{'goesto_'.$id},$beenhere,
1.81 albertel 492: $encflag,$hdnflag);
1.85 albertel 493: }
494: }
1.2 www 495: }
1.81 albertel 496: return $newsofar;
1.2 www 497: }
1.1 www 498:
1.19 www 499: # ------------------------------ Cascading conditions, quick access, parameters
1.4 www 500:
501: sub accinit {
502: my ($uri,$short,$fn)=@_;
503: my %acchash=();
504: my %captured=();
505: my $condcounter=0;
1.5 www 506: $acchash{'acc.cond.'.$short.'.0'}=0;
1.104 albertel 507: foreach my $key (keys(%hash)) {
508: if ($key=~/^conditions/) {
509: my $expr=$hash{$key};
1.109 albertel 510: # try to find and factor out common sub-expressions
1.105 albertel 511: foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
1.104 albertel 512: my $orig=$sub;
1.109 albertel 513:
514: my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
515: next if (!defined($factor));
516:
517: $sub=~s/\Q$factor\E//g;
1.85 albertel 518: $sub=~s/^\(/\($factor\(/;
519: $sub.=')';
520: $sub=simplify($sub);
1.109 albertel 521: $expr=~s/\Q$orig\E/$sub/;
1.85 albertel 522: }
1.104 albertel 523: $hash{$key}=$expr;
1.85 albertel 524: unless (defined($captured{$expr})) {
525: $condcounter++;
526: $captured{$expr}=$condcounter;
527: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
528: }
1.104 albertel 529: } elsif ($key=~/^param_(\d+)\.(\d+)/) {
1.86 albertel 530: my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
531: $hash{'src_'.$1.'.'.$2});
1.104 albertel 532: foreach my $param (split(/\&/,$hash{$key})) {
533: my ($typename,$value)=split(/\=/,$param);
1.85 albertel 534: my ($type,$name)=split(/\:/,$typename);
1.114 www 535: $parmhash{$prefix.'.'.&unescape($name)}=
536: &unescape($value);
537: $parmhash{$prefix.'.'.&unescape($name).'.type'}=
538: &unescape($type);
1.85 albertel 539: }
540: }
1.26 harris41 541: }
1.104 albertel 542: foreach my $key (keys(%hash)) {
543: if ($key=~/^ids/) {
544: foreach my $resid (split(/\,/,$hash{$key})) {
1.85 albertel 545: my $uri=$hash{'src_'.$resid};
1.100 albertel 546: my ($uripath,$urifile) =
547: &Apache::lonnet::split_uri_for_cond($uri);
1.85 albertel 548: if ($uripath) {
549: my $uricond='0';
550: if (defined($hash{'conditions_'.$resid})) {
551: $uricond=$captured{$hash{'conditions_'.$resid}};
552: }
553: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
554: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
555: /(\&\Q$urifile\E\:[^\&]*)/) {
556: my $replace=$1;
557: my $regexp=$replace;
558: #$regexp=~s/\|/\\\|/g;
1.105 albertel 559: $acchash{'acc.res.'.$short.'.'.$uripath} =~
1.104 albertel 560: s/\Q$regexp\E/$replace\|$uricond/;
1.85 albertel 561: } else {
562: $acchash{'acc.res.'.$short.'.'.$uripath}.=
563: $urifile.':'.$uricond.'&';
564: }
565: } else {
566: $acchash{'acc.res.'.$short.'.'.$uripath}=
567: '&'.$urifile.':'.$uricond.'&';
568: }
569: }
570: }
571: }
1.26 harris41 572: }
1.24 www 573: $acchash{'acc.res.'.$short.'.'}='&:0&';
1.8 www 574: my $courseuri=$uri;
575: $courseuri=~s/^\/res\///;
1.128.2.2 raeburn 576: my $regexp = 1;
577: &Apache::lonnet::delenv('(acc\.|httpref\.)',$regexp);
1.128 raeburn 578: &Apache::lonnet::appenv(\%acchash);
1.4 www 579: }
580:
1.73 www 581: # ---------------- Selectively delete from randompick maps and hidden url parms
1.29 www 582:
1.73 www 583: sub hiddenurls {
1.31 www 584: my $randomoutentry='';
1.29 www 585: foreach my $rid (keys %randompick) {
586: my $rndpick=$randompick{$rid};
587: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
588: # ------------------------------------------- put existing resources into array
589: my @currentrids=();
1.106 albertel 590: foreach my $key (sort(keys(%hash))) {
591: if ($key=~/^src_($mpc\.\d+)/) {
1.29 www 592: if ($hash{'src_'.$1}) { push @currentrids, $1; }
593: }
594: }
1.50 albertel 595: # rids are number.number and we want to numercially sort on
596: # the second number
597: @currentrids=sort {
598: my (undef,$aid)=split(/\./,$a);
599: my (undef,$bid)=split(/\./,$b);
600: $aid <=> $bid;
601: } @currentrids;
1.29 www 602: next if ($#currentrids<$rndpick);
603: # -------------------------------- randomly eliminate the ones that should stay
1.50 albertel 604: my (undef,$id)=split(/\./,$rid);
1.51 www 605: if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1.50 albertel 606: my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
1.58 albertel 607: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.50 albertel 608: my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
609: for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
610: #&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1.29 www 611: # -------------------------------------------------------- delete the leftovers
612: for (my $k=0; $k<=$#currentrids; $k++) {
613: if ($currentrids[$k]) {
614: $hash{'randomout_'.$currentrids[$k]}=1;
1.32 www 615: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
616: $randomoutentry.='&'.
1.86 albertel 617: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
618: $resid,
619: $hash{'src_'.$currentrids[$k]}
620: ).'&';
1.29 www 621: }
622: }
1.31 www 623: }
1.73 www 624: # ------------------------------ take care of explicitly hidden urls or folders
625: foreach my $rid (keys %hiddenurl) {
626: $hash{'randomout_'.$rid}=1;
627: my ($mapid,$resid)=split(/\./,$rid);
628: $randomoutentry.='&'.
1.86 albertel 629: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
630: $hash{'src_'.$rid}).'&';
1.73 www 631: }
632: # --------------------------------------- append randomout entry to environment
1.31 www 633: if ($randomoutentry) {
1.128 raeburn 634: &Apache::lonnet::appenv({'acc.randomout' => $randomoutentry});
1.29 www 635: }
636: }
637:
1.1 www 638: # ---------------------------------------------------- Read map and all submaps
639:
640: sub readmap {
1.85 albertel 641: my $short=shift;
642: $short=~s/^\///;
1.108 albertel 643: my %cenv=&Apache::lonnet::coursedescription($short,{'freshen_cache'=>1});
1.85 albertel 644: my $fn=$cenv{'fn'};
645: my $uri;
646: $short=~s/\//\_/g;
647: unless ($uri=$cenv{'url'}) {
1.128.2.3! raeburn 648: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
1.85 albertel 649: "Could not load course $short.</font>");
1.114 www 650: return ('',&mt('No course data available.'));;
1.85 albertel 651: }
652: @cond=('true:normal');
1.96 albertel 653:
1.128.2.3! raeburn 654: unless (open(LOCKFILE,">$fn.db.lock")) {
! 655: $errtext.='<br />'.&mt('Map not loaded - Lock file could not be opened when reading map:').' <tt>'.$fn.'</tt>.';
! 656: $retfurl = '';
! 657: return ($retfurl,$errtext);
! 658: }
1.96 albertel 659: my $lock=0;
1.128.2.3! raeburn 660: my $gotstate=0;
1.96 albertel 661: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
662: $lock=1;
1.128.2.3! raeburn 663: &unlink_tmpfiles($fn);
1.96 albertel 664: }
1.85 albertel 665: undef %randompick;
666: undef %hiddenurl;
667: undef %encurl;
1.116 www 668: $retfrid='';
1.128.2.3! raeburn 669: my ($untiedhash,$untiedparmhash,$tiedhash,$tiedparmhash);
! 670: if ($lock) {
! 671: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
! 672: $tiedhash = 1;
! 673: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
! 674: $tiedparmhash = 1;
! 675: $gotstate = &build_tmp_hashes($uri,$fn,$short,\%cenv);
! 676: unless ($gotstate) {
! 677: &Apache::lonnet::logthis('Failed to write statemap at first attempt '.$fn.' for '.$uri.'.</font>');
! 678: }
! 679: $untiedparmhash = untie(%parmhash);
! 680: unless ($untiedparmhash) {
! 681: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 682: 'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
! 683: }
! 684: }
! 685: $untiedhash = untie(%hash);
! 686: unless ($untiedhash) {
! 687: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 688: 'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
! 689: }
! 690: }
! 691: flock(LOCKFILE,LOCK_UN);
! 692: }
! 693: unless ($lock && $tiedhash && $tiedparmhash) {
1.87 albertel 694: # if we are here it is likely because we are already trying to
695: # initialize the course in another child, busy wait trying to
696: # tie the hashes for the next 90 seconds, if we succeed forward
697: # them on to navmaps, if we fail, throw up the Could not init
698: # course screen
1.96 albertel 699: if ($lock) {
700: # Got the lock but not the DB files
701: flock(LOCKFILE,LOCK_UN);
1.128.2.3! raeburn 702: $lock = 0;
1.96 albertel 703: }
1.128.2.3! raeburn 704: if ($tiedhash) {
! 705: unless($untiedhash) {
! 706: untie(%hash);
! 707: }
! 708: }
! 709: if ($tiedparmhash) {
! 710: unless($untiedparmhash) {
! 711: untie(%parmhash);
! 712: }
! 713: }
! 714: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 715: "Could not tie coursemap $fn for $uri.</font>");
! 716: $tiedhash = '';
! 717: $tiedparmhash = '';
1.87 albertel 718: my $i=0;
719: while($i<90) {
720: $i++;
721: sleep(1);
1.128.2.3! raeburn 722: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
! 723: $lock = 1;
! 724: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
! 725: $tiedhash = 1;
! 726: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
! 727: $tiedparmhash = 1;
! 728: if (-e "$fn.state") {
! 729: $retfurl='/adm/navmaps';
! 730: &Apache::lonnet::appenv({"request.course.id" => $short,
! 731: "request.course.fn" => $fn,
! 732: "request.course.uri" => $uri});
! 733: $untiedhash = untie(%hash);
! 734: $untiedparmhash = untie(%parmhash);
! 735: $gotstate = 1;
! 736: last;
! 737: }
! 738: $untiedparmhash = untie(%parmhash);
! 739: }
! 740: $untiedhash = untie(%hash);
1.87 albertel 741: }
742: }
743: }
1.128.2.3! raeburn 744: if ($lock) {
! 745: flock(LOCKFILE,LOCK_UN);
! 746: $lock = 0;
! 747: if ($tiedparmhash) {
! 748: unless ($untiedparmhash) {
! 749: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 750: 'Could not untie coursemap parmhash '.$fn.' for '.$uri.'.</font>');
! 751: }
! 752: }
! 753: if ($tiedparmhash) {
! 754: unless ($untiedhash) {
! 755: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 756: 'Could not untie coursemap hash '.$fn.' for '.$uri.'.</font>');
! 757: }
! 758: }
! 759: }
! 760: }
! 761: unless ($gotstate) {
! 762: $lock = 0;
! 763: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 764: 'Could not read statemap '.$fn.' for '.$uri.'.</font>');
! 765: &unlink_tmpfiles($fn);
! 766: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
! 767: $lock=1;
! 768: }
! 769: undef %randompick;
! 770: undef %hiddenurl;
! 771: undef %encurl;
! 772: $retfrid='';
! 773: if ($lock) {
! 774: if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) {
! 775: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640)) {
! 776: $gotstate = &build_tmp_hashes($uri,$fn,$short,\%cenv);
! 777: unless ($gotstate) {
! 778: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 779: 'Failed to write statemap at second attempt '.$fn.' for '.$uri.'.</font>');
! 780: }
! 781: unless (untie(%parmhash)) {
! 782: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 783: 'Could not untie coursemap parmhash '.$fn.'.db for '.$uri.'.</font>');
! 784: }
! 785: } else {
! 786: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 787: 'Could not tie coursemap '.$fn.'__parms.db for '.$uri.'.</font>');
! 788: }
! 789: unless (untie(%hash)) {
! 790: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 791: 'Could not untie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
! 792: }
! 793: } else {
! 794: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 795: 'Could not tie coursemap '.$fn.'.db for '.$uri.'.</font>');
! 796: }
! 797: flock(LOCKFILE,LOCK_UN);
! 798: $lock = 0;
! 799: } else {
! 800: &Apache::lonnet::logthis('<font color="blue">WARNING: '.
! 801: 'Could not obtain lock to tie coursemap hash '.$fn.'.db for '.$uri.'.</font>');
! 802: }
! 803: }
! 804: close(LOCKFILE);
! 805: unless (($errtext eq '') || ($env{'request.course.uri'} =~ m{^/uploaded/})) {
! 806: &Apache::lonmsg::author_res_msg($env{'request.course.uri'},
! 807: $errtext);
1.1 www 808: }
1.46 www 809: # ------------------------------------------------- Check for critical messages
810:
1.89 albertel 811: my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
812: $env{'user.name'});
1.46 www 813: if ($what[0]) {
814: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
815: $retfurl='/adm/email?critical=display';
816: }
817: }
1.85 albertel 818: return ($retfurl,$errtext);
1.1 www 819: }
1.15 www 820:
1.128.2.3! raeburn 821: sub build_tmp_hashes {
! 822: my ($uri,$fn,$short,$cenvref) = @_;
! 823: unless(ref($cenvref) eq 'HASH') {
! 824: return;
! 825: }
! 826: my %cenv = %{$cenvref};
! 827: my $gotstate = 0;
! 828: %hash=();
! 829: %parmhash=();
! 830: $errtext='';
! 831: $pc=0;
! 832: &clear_mapalias_count();
! 833: &processversionfile(%cenv);
! 834: my $furi=&Apache::lonnet::clutter($uri);
! 835: $hash{'src_0.0'}=&versiontrack($furi);
! 836: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
! 837: $hash{'ids_'.$furi}='0.0';
! 838: $hash{'is_map_0.0'}=1;
! 839: &loadmap($uri,'0.0');
! 840: if (defined($hash{'map_start_'.$uri})) {
! 841: &Apache::lonnet::appenv({"request.course.id" => $short,
! 842: "request.course.fn" => $fn,
! 843: "request.course.uri" => $uri});
! 844: $env{'request.course.id'}=$short;
! 845: &traceroute('0',$hash{'map_start_'.$uri},'&');
! 846: &accinit($uri,$short,$fn);
! 847: &hiddenurls();
! 848: }
! 849: $errtext .= &get_mapalias_errors();
! 850: # ------------------------------------------------------- Put versions into src
! 851: foreach my $key (keys(%hash)) {
! 852: if ($key=~/^src_/) {
! 853: $hash{$key}=&putinversion($hash{$key});
! 854: } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
! 855: my ($type, $url) = ($1,$2);
! 856: my $value = $hash{$key};
! 857: $hash{$type.&putinversion($url)}=$value;
! 858: }
! 859: }
! 860: # ---------------------------------------------------------------- Encrypt URLs
! 861: foreach my $id (keys(%encurl)) {
! 862: # $hash{'src_'.$id}=&Apache::lonenc::encrypted($hash{'src_'.$id});
! 863: $hash{'encrypted_'.$id}=1;
! 864: }
! 865: # ----------------------------------------------- Close hashes to finally store
! 866: # --------------------------------- Routine must pass this point, no early outs
! 867: $hash{'first_rid'}=$retfrid;
! 868: my ($mapid,$resid)=split(/\./,$retfrid);
! 869: $hash{'first_mapurl'}=$hash{'map_id_'.$mapid};
! 870: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$retfrid});
! 871: $retfurl=&add_get_param($hash{'src_'.$retfrid},{ 'symb' => $symb });
! 872: if ($hash{'encrypted_'.$retfrid}) {
! 873: $retfurl=&Apache::lonenc::encrypted($retfurl,(&Apache::lonnet::allowed('adv') ne 'F'));
! 874: }
! 875: $hash{'first_url'}=$retfurl;
! 876: # ---------------------------------------------------- Store away initial state
! 877: {
! 878: my $cfh;
! 879: if (open($cfh,">$fn.state")) {
! 880: print $cfh join("\n",@cond);
! 881: $gotstate = 1;
! 882: } else {
! 883: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
! 884: "Could not write statemap $fn for $uri.</font>");
! 885: }
! 886: }
! 887: return $gotstate;
! 888: }
! 889:
! 890: sub unlink_tmpfiles {
! 891: my ($fn) = @_;
! 892: if ($fn =~ m{^\Q$Apache::lonnet::perlvar{'lonUsersDir'}\E/tmp/}) {
! 893: my @files = qw (.db _symb.db .state _parms.db);
! 894: foreach my $file (@files) {
! 895: if (-e $fn.$file) {
! 896: unless (unlink($fn.$file)) {
! 897: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
! 898: "Could not unlink ".$fn.$file."</font>");
! 899: }
! 900: }
! 901: }
! 902: }
! 903: return;
! 904: }
! 905:
1.15 www 906: # ------------------------------------------------------- Evaluate state string
907:
908: sub evalstate {
1.89 albertel 909: my $fn=$env{'request.course.fn'}.'.state';
1.80 albertel 910: my $state='';
1.15 www 911: if (-e $fn) {
1.80 albertel 912: my @conditions=();
913: {
1.115 raeburn 914: open(my $fh,"<$fn");
1.80 albertel 915: @conditions=<$fh>;
1.115 raeburn 916: close($fh);
1.80 albertel 917: }
918: my $safeeval = new Safe;
919: my $safehole = new Safe::Hole;
920: $safeeval->permit("entereval");
921: $safeeval->permit(":base_math");
922: $safeeval->deny(":base_io");
923: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
924: foreach my $line (@conditions) {
925: chomp($line);
926: my ($condition,$weight)=split(/\:/,$line);
927: if ($safeeval->reval($condition)) {
928: if ($weight eq 'force') {
929: $state.='3';
930: } else {
931: $state.='2';
932: }
933: } else {
934: if ($weight eq 'stop') {
935: $state.='0';
936: } else {
937: $state.='1';
938: }
939: }
940: }
1.15 www 941: }
1.128 raeburn 942: &Apache::lonnet::appenv({'user.state.'.$env{'request.course.id'} => $state});
1.15 www 943: return $state;
944: }
945:
1.122 albertel 946: {
947: my %mapalias_cache;
948: sub count_mapalias {
949: my ($value,$resid) = @_;
950: push(@{ $mapalias_cache{$value} }, $resid);
951: }
952:
953: sub get_mapalias_errors {
954: my $error_text;
955: foreach my $mapalias (sort(keys(%mapalias_cache))) {
956: next if (scalar(@{ $mapalias_cache{$mapalias} } ) == 1);
957: my $count;
958: my $which =
959: join('</li><li>',
960: map {
961: my $id = $_;
962: if (exists($hash{'src_'.$id})) {
963: $count++;
964: }
965: my ($mapid) = split(/\./,$id);
1.125 albertel 966: &mt('Resource "[_1]" <br /> in Map "[_2]"',
967: $hash{'title_'.$id},
1.122 albertel 968: $hash{'title_'.$hash{'ids_'.$hash{'map_id_'.$mapid}}});
969: } (@{ $mapalias_cache{$mapalias} }));
970: next if ($count < 2);
971: $error_text .= '<div class="LC_error">'.
972: &mt('Error: Found the mapalias "[_1]" defined multiple times.',
973: $mapalias).
974: '</div><ul><li>'.$which.'</li></ul>';
975: }
976: &clear_mapalias_count();
977: return $error_text;
978: }
979: sub clear_mapalias_count {
980: undef(%mapalias_cache);
981: }
982: }
1.1 www 983: 1;
984: __END__
985:
1.26 harris41 986: =head1 NAME
987:
988: Apache::lonuserstate - Construct and maintain state and binary representation
989: of course for user
990:
991: =head1 SYNOPSIS
992:
993: Invoked by lonroles.pm.
994:
995: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
996:
997: =head1 INTRODUCTION
998:
999: This module constructs and maintains state and binary representation
1000: of course for user.
1001:
1002: This is part of the LearningOnline Network with CAPA project
1003: described at http://www.lon-capa.org.
1004:
1005: =head1 HANDLER SUBROUTINE
1006:
1007: There is no handler subroutine.
1008:
1009: =head1 OTHER SUBROUTINES
1010:
1011: =over 4
1012:
1013: =item *
1014:
1015: loadmap() : Loads map from disk
1016:
1017: =item *
1018:
1019: simplify() : Simplify expression
1020:
1021: =item *
1022:
1023: traceroute() : Build condition hash
1024:
1025: =item *
1026:
1027: accinit() : Cascading conditions, quick access, parameters
1.1 www 1028:
1.26 harris41 1029: =item *
1.1 www 1030:
1.26 harris41 1031: readmap() : Read map and all submaps
1.1 www 1032:
1.26 harris41 1033: =item *
1.1 www 1034:
1.26 harris41 1035: evalstate() : Evaluate state string
1.1 www 1036:
1.26 harris41 1037: =back
1.1 www 1038:
1.26 harris41 1039: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>