File:
[LON-CAPA] /
rat /
lonuserstate.pm
Revision
1.118.2.1:
download - view:
text,
annotated -
select for diffs
Fri Jun 8 19:33:06 2007 UTC (17 years, 2 months ago) by
albertel
Branches:
randomize
Diff to branchpoint 1.118:
preferred,
unified
- implements the internals for randomizing the order of resources in a map
- works by ignoring all <link>s in the map, collecting the list
of resources and then <link>s them in a random order
it throws all of that away and then makes a random linear map
- works correctly with 'pick n resources'
- TODO
- currently always on and does all maps, needs conversion to using a parm
- currently different ordering everytime you reinit a course
- currently affects everyone
1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
4: # $Id: lonuserstate.pm,v 1.118.2.1 2007/06/08 19:33:06 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: package Apache::lonuserstate;
31:
32: # ------------------------------------------------- modules used by this module
33: use strict;
34: use HTML::TokeParser;
35: use Apache::lonnet;
36: use Apache::lonlocal;
37: use Apache::loncommon();
38: use GDBM_File;
39: use Apache::lonmsg;
40: use Safe;
41: use Safe::Hole;
42: use Opcode;
43: use Apache::lonenc;
44: use Fcntl qw(:flock);
45: use LONCAPA;
46:
47:
48: # ---------------------------------------------------- Globals for this package
49:
50: my $pc; # Package counter
51: my %hash; # The big tied hash
52: my %parmhash;# The hash with the parameters
53: my @cond; # Array with all of the conditions
54: my $errtext; # variable with all errors
55: my $retfrid; # variable with the very first RID in the course
56: my $retfurl; # first URL
57: my %randompick; # randomly picked resources
58: my %randompickseed; # optional seed for randomly picking resources
59: my %encurl; # URLs in this folder are supposed to be encrypted
60: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
61:
62: # ----------------------------------- Remove version from URL and store in hash
63:
64: sub versiontrack {
65: my $uri=shift;
66: if ($uri=~/\.(\d+)\.\w+$/) {
67: my $version=$1;
68: $uri=~s/\.\d+\.(\w+)$/\.$1/;
69: unless ($hash{'version_'.$uri}) {
70: $hash{'version_'.$uri}=$version;
71: }
72: }
73: return $uri;
74: }
75:
76: # -------------------------------------------------------------- Put in version
77:
78: sub putinversion {
79: my $uri=shift;
80: my $key=$env{'request.course.id'}.'_'.&Apache::lonnet::clutter($uri);
81: if ($hash{'version_'.$uri}) {
82: my $version=$hash{'version_'.$uri};
83: if ($version eq 'mostrecent') { return $uri; }
84: if ($version eq &Apache::lonnet::getversion(
85: &Apache::lonnet::filelocation('',$uri)))
86: { return $uri; }
87: $uri=~s/\.(\w+)$/\.$version\.$1/;
88: }
89: &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
90: return $uri;
91: }
92:
93: # ----------------------------------------- Processing versions file for course
94:
95: sub processversionfile {
96: my %cenv=@_;
97: my %versions=&Apache::lonnet::dump('resourceversions',
98: $cenv{'domain'},
99: $cenv{'num'});
100: foreach my $ver (keys(%versions)) {
101: if ($ver=~/^error\:/) { return; }
102: $hash{'version_'.$ver}=$versions{$ver};
103: }
104: }
105:
106: # --------------------------------------------------------- Loads map from disk
107:
108: sub loadmap {
109: my $uri=shift;
110: if ($hash{'map_pc_'.$uri}) {
111: $errtext.=&mt('<br />Multiple use of sequence/page <tt>[_1]</tt>! The course will not function properly.',$uri);
112: return;
113: }
114: $pc++;
115: my $lpc=$pc;
116: $hash{'map_pc_'.$uri}=$lpc;
117: $hash{'map_id_'.$lpc}=$uri;
118:
119: # Determine and check filename
120: my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
121:
122: my $ispage=($fn=~/\.page$/);
123:
124: unless (($fn=~/\.sequence$/) ||
125: ($fn=~/\.page$/)) {
126: $errtext.=&mt("<br />Invalid map: <tt>[_1]</tt>",$fn);
127: return;
128: }
129:
130: my $instr=&Apache::lonnet::getfile($fn);
131:
132: if ($instr eq -1) {
133: $errtext.=&mt('<br />Map not loaded: The file <tt>[_1]</tt> does not exist.',$fn);
134: return;
135: }
136:
137: # Successfully got file, parse it
138:
139: my $parser = HTML::TokeParser->new(\$instr);
140: $parser->attr_encoded(1);
141:
142: my $linkpc=0;
143:
144: $fn=~/\.(\w+)$/;
145:
146: $hash{'map_type_'.$lpc}=$1;
147:
148: my $randomize=1;
149: my @map_ids;
150: while (my $token = $parser->get_token) {
151: next if ($token->[0] ne 'S');
152: if ($token->[1] eq 'resource') {
153: push(@map_ids,&parse_resource($token,$lpc,$ispage,$uri));
154: } elsif ($token->[1] eq 'link' && !$randomize) {
155: # ----------------------------------------------------------------------- Links
156: &make_link(++$linkpc,$lpc,$token->[2]->{'to'},
157: $token->[2]->{'from'},
158: $token->[2]->{'condition'});
159: } elsif ($token->[1] eq 'condition' && !$randomize) {
160: &parse_condition($token,$lpc);
161: } elsif ($token->[1] eq 'param') {
162: &parse_param($token,$lpc);
163: }
164: }
165:
166: if ($randomize) {
167: my @map_ids=&Math::Random::random_permutation(@map_ids);
168: my $from = shift(@map_ids);
169: my $from_rid = $lpc.'.'.$from;
170: $hash{'map_start_'.$uri} = $from_rid;
171: $hash{'type_'.$from_rid}='start';
172:
173: while (my $to = shift(@map_ids)) {
174: &make_link(++$linkpc,$lpc,$to,$from);
175: my $to_rid = $lpc.'.'.$to;
176: $hash{'type_'.$to_rid}='normal';
177: $from = $to;
178: $from_rid = $to_rid;
179: }
180:
181: $hash{'map_finish_'.$uri}= $from_rid;
182: $hash{'type_'.$from_rid}='finish';
183: }
184: }
185:
186:
187: # -------------------------------------------------------------------- Resource
188: sub parse_resource {
189: my ($token,$lpc,$ispage,$uri) = @_;
190: if ($token->[2]->{'type'} eq 'zombie') { next; }
191: my $rid=$lpc.'.'.$token->[2]->{'id'};
192:
193: $hash{'kind_'.$rid}='res';
194: $hash{'title_'.$rid}=$token->[2]->{'title'};
195: my $turi=&versiontrack($token->[2]->{'src'});
196: if ($token->[2]->{'version'}) {
197: unless ($hash{'version_'.$turi}) {
198: $hash{'version_'.$turi}=$1;
199: }
200: }
201: my $title=$token->[2]->{'title'};
202: $title=~s/\&colon\;/\:/gs;
203: # my $symb=&Apache::lonnet::encode_symb($uri,
204: # $token->[2]->{'id'},
205: # $turi);
206: # &Apache::lonnet::do_cache_new('title',$symb,$title);
207: unless ($ispage) {
208: $turi=~/\.(\w+)$/;
209: my $embstyle=&Apache::loncommon::fileembstyle($1);
210: if ($token->[2]->{'external'} eq 'true') { # external
211: $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
212: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
213: if (($embstyle eq 'img')
214: || ($embstyle eq 'emb')
215: || ($embstyle eq 'wrp')) {
216: $turi='/adm/wrapper'.$turi;
217: } elsif ($embstyle eq 'ssi') {
218: #do nothing with these
219: } elsif ($turi!~/\.(sequence|page)$/) {
220: $turi='/adm/coursedocs/showdoc'.$turi;
221: }
222: } elsif ($turi=~/\S/) { # normal non-empty internal resource
223: my $mapdir=$uri;
224: $mapdir=~s/[^\/]+$//;
225: $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
226: if (($embstyle eq 'img')
227: || ($embstyle eq 'emb')
228: || ($embstyle eq 'wrp')) {
229: $turi='/adm/wrapper'.$turi;
230: }
231: }
232: }
233: # Store reverse lookup, remove query string
234: my $idsuri=$turi;
235: $idsuri=~s/\?.+$//;
236: if (defined($hash{'ids_'.$idsuri})) {
237: $hash{'ids_'.$idsuri}.=','.$rid;
238: } else {
239: $hash{'ids_'.$idsuri}=''.$rid;
240: }
241:
242: if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/) {
243: $turi.='?register=1';
244: }
245:
246: $hash{'src_'.$rid}=$turi;
247:
248: if ($token->[2]->{'external'} eq 'true') {
249: $hash{'ext_'.$rid}='true:';
250: } else {
251: $hash{'ext_'.$rid}='false:';
252: }
253: if ($token->[2]->{'type'}) {
254: $hash{'type_'.$rid}=$token->[2]->{'type'};
255: if ($token->[2]->{'type'} eq 'start') {
256: $hash{'map_start_'.$uri}="$rid";
257: }
258: if ($token->[2]->{'type'} eq 'finish') {
259: $hash{'map_finish_'.$uri}="$rid";
260: }
261: } else {
262: $hash{'type_'.$rid}='normal';
263: }
264:
265: if (($turi=~/\.sequence$/) ||
266: ($turi=~/\.page$/)) {
267: $hash{'is_map_'.$rid}=1;
268: &loadmap($turi);
269: }
270: return $token->[2]->{'id'};
271: }
272:
273: sub make_link {
274: my ($linkpc,$lpc,$to,$from,$condition) = @_;
275:
276: my $linkid=$lpc.'.'.$linkpc;
277: my $goesto=$lpc.'.'.$to;
278: my $comesfrom=$lpc.'.'.$from;
279: my $undercond=0;
280:
281: if ($condition) {
282: $undercond=$lpc.'.'.$condition;
283: }
284:
285: $hash{'goesto_'.$linkid}=$goesto;
286: $hash{'comesfrom_'.$linkid}=$comesfrom;
287: $hash{'undercond_'.$linkid}=$undercond;
288:
289: if (defined($hash{'to_'.$comesfrom})) {
290: $hash{'to_'.$comesfrom}.=','.$linkid;
291: } else {
292: $hash{'to_'.$comesfrom}=''.$linkid;
293: }
294: if (defined($hash{'from_'.$goesto})) {
295: $hash{'from_'.$goesto}.=','.$linkid;
296: } else {
297: $hash{'from_'.$goesto}=''.$linkid;
298: }
299: }
300:
301: # ------------------------------------------------------------------- Condition
302: sub parse_condition {
303: my ($token,$lpc) = @_;
304: my $rid=$lpc.'.'.$token->[2]->{'id'};
305:
306: $hash{'kind_'.$rid}='cond';
307: $cond[$#cond+1]=$token->[2]->{'value'};
308: $hash{'condid_'.$rid}=$#cond;
309: if ($token->[2]->{'type'}) {
310: $cond[$#cond].=':'.$token->[2]->{'type'};
311: } else {
312: $cond[$#cond].=':normal';
313: }
314: }
315:
316: # ------------------------------------------------------------------- Parameter
317:
318: sub parse_param {
319: my ($token,$lpc) = @_;
320: my $referid=$lpc.'.'.$token->[2]->{'to'};
321: my $name=$token->[2]->{'name'};
322: my $part;
323: if ($name=~/^parameter_(.*)_/) {
324: $part=$1;
325: } else {
326: $part=0;
327: }
328: $name=~s/^.*_([^_]*)$/$1/;
329: my $newparam=
330: &escape($token->[2]->{'type'}).':'.
331: &escape($part.'.'.$name).'='.
332: &escape($token->[2]->{'value'});
333: if (defined($hash{'param_'.$referid})) {
334: $hash{'param_'.$referid}.='&'.$newparam;
335: } else {
336: $hash{'param_'.$referid}=''.$newparam;
337: }
338: if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
339: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
340: }
341: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) {
342: $randompick{$referid}=$token->[2]->{'value'};
343: }
344: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) {
345: $randompick{$referid}=$token->[2]->{'value'};
346: }
347: if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
348: if ($token->[2]->{'value'}=~/^yes$/i) {
349: $encurl{$referid}=1;
350: }
351: }
352: if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
353: if ($token->[2]->{'value'}=~/^yes$/i) {
354: $hiddenurl{$referid}=1;
355: }
356: }
357: }
358:
359: # --------------------------------------------------------- Simplify expression
360:
361: sub simplify {
362: my $expression=shift;
363: # (0&1) = 1
364: $expression=~s/\(0\&([_\.\d]+)\)/$1/g;
365: # (8)=8
366: $expression=~s/\(([_\.\d]+)\)/$1/g;
367: # 8&8=8
368: $expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
369: # 8|8=8
370: $expression=~s/([^_\.\d])([_\.\d]+)\|\2([^_\.\d])/$1$2$3/g;
371: # (5&3)&4=5&3&4
372: $expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
373: # (((5&3)|(4&6)))=((5&3)|(4&6))
374: $expression=~
375: s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
376: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
377: $expression=~
378: s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
379: return $expression;
380: }
381:
382: # -------------------------------------------------------- Build condition hash
383:
384: sub traceroute {
385: my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
386: my $newsofar=$sofar=simplify($sofar);
387: unless ($beenhere=~/\&$rid\&/) {
388: $beenhere.=$rid.'&';
389: my ($mapid,$resid)=split(/\./,$rid);
390: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
391: my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
392:
393: if ($hdnflag || lc($hidden) eq 'yes') {
394: $hiddenurl{$rid}=1;
395: }
396: if (!$hdnflag && lc($hidden) eq 'no') {
397: delete($hiddenurl{$rid});
398: }
399:
400: my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
401: if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
402: if (($retfrid eq '') && ($hash{'src_'.$rid})
403: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
404: $retfrid=$rid;
405: }
406: if (defined($hash{'conditions_'.$rid})) {
407: $hash{'conditions_'.$rid}=simplify(
408: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
409: } else {
410: $hash{'conditions_'.$rid}=$sofar;
411: }
412:
413: # if the expression is just the 0th condition keep it
414: # otherwise leave a pointer to this condition expression
415: $newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
416:
417: if (defined($hash{'is_map_'.$rid})) {
418: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
419: $sofar=$newsofar=
420: &traceroute($sofar,
421: $hash{'map_start_'.$hash{'src_'.$rid}},'&',
422: $encflag || $encurl{$rid},
423: $hdnflag || $hiddenurl{$rid});
424: }
425: }
426: if (defined($hash{'to_'.$rid})) {
427: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
428: my $further=$sofar;
429: if ($hash{'undercond_'.$id}) {
430: if (defined($hash{'condid_'.$hash{'undercond_'.$id}})) {
431: $further=simplify('('.'_'.$rid.')&('.
432: $hash{'condid_'.$hash{'undercond_'.$id}}.')');
433: } else {
434: $errtext.=&mt('<br />Undefined condition ID: [_1]',$hash{'undercond_'.$id});
435: }
436: }
437: $newsofar=&traceroute($further,$hash{'goesto_'.$id},$beenhere,
438: $encflag,$hdnflag);
439: }
440: }
441: }
442: return $newsofar;
443: }
444:
445: # ------------------------------ Cascading conditions, quick access, parameters
446:
447: sub accinit {
448: my ($uri,$short,$fn)=@_;
449: my %acchash=();
450: my %captured=();
451: my $condcounter=0;
452: $acchash{'acc.cond.'.$short.'.0'}=0;
453: foreach my $key (keys(%hash)) {
454: if ($key=~/^conditions/) {
455: my $expr=$hash{$key};
456: # try to find and factor out common sub-expressions
457: foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
458: my $orig=$sub;
459:
460: my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
461: next if (!defined($factor));
462:
463: $sub=~s/\Q$factor\E//g;
464: $sub=~s/^\(/\($factor\(/;
465: $sub.=')';
466: $sub=simplify($sub);
467: $expr=~s/\Q$orig\E/$sub/;
468: }
469: $hash{$key}=$expr;
470: unless (defined($captured{$expr})) {
471: $condcounter++;
472: $captured{$expr}=$condcounter;
473: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
474: }
475: } elsif ($key=~/^param_(\d+)\.(\d+)/) {
476: my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
477: $hash{'src_'.$1.'.'.$2});
478: foreach my $param (split(/\&/,$hash{$key})) {
479: my ($typename,$value)=split(/\=/,$param);
480: my ($type,$name)=split(/\:/,$typename);
481: $parmhash{$prefix.'.'.&unescape($name)}=
482: &unescape($value);
483: $parmhash{$prefix.'.'.&unescape($name).'.type'}=
484: &unescape($type);
485: }
486: }
487: }
488: foreach my $key (keys(%hash)) {
489: if ($key=~/^ids/) {
490: foreach my $resid (split(/\,/,$hash{$key})) {
491: my $uri=$hash{'src_'.$resid};
492: my ($uripath,$urifile) =
493: &Apache::lonnet::split_uri_for_cond($uri);
494: if ($uripath) {
495: my $uricond='0';
496: if (defined($hash{'conditions_'.$resid})) {
497: $uricond=$captured{$hash{'conditions_'.$resid}};
498: }
499: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
500: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
501: /(\&\Q$urifile\E\:[^\&]*)/) {
502: my $replace=$1;
503: my $regexp=$replace;
504: #$regexp=~s/\|/\\\|/g;
505: $acchash{'acc.res.'.$short.'.'.$uripath} =~
506: s/\Q$regexp\E/$replace\|$uricond/;
507: } else {
508: $acchash{'acc.res.'.$short.'.'.$uripath}.=
509: $urifile.':'.$uricond.'&';
510: }
511: } else {
512: $acchash{'acc.res.'.$short.'.'.$uripath}=
513: '&'.$urifile.':'.$uricond.'&';
514: }
515: }
516: }
517: }
518: }
519: $acchash{'acc.res.'.$short.'.'}='&:0&';
520: my $courseuri=$uri;
521: $courseuri=~s/^\/res\///;
522: &Apache::lonnet::delenv('(acc\.|httpref\.)');
523: &Apache::lonnet::appenv(%acchash);
524: }
525:
526: # ---------------- Selectively delete from randompick maps and hidden url parms
527:
528: sub hiddenurls {
529: my $randomoutentry='';
530: foreach my $rid (keys %randompick) {
531: my $rndpick=$randompick{$rid};
532: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
533: # ------------------------------------------- put existing resources into array
534: my @currentrids=();
535: foreach my $key (sort(keys(%hash))) {
536: if ($key=~/^src_($mpc\.\d+)/) {
537: if ($hash{'src_'.$1}) { push @currentrids, $1; }
538: }
539: }
540: # rids are number.number and we want to numercially sort on
541: # the second number
542: @currentrids=sort {
543: my (undef,$aid)=split(/\./,$a);
544: my (undef,$bid)=split(/\./,$b);
545: $aid <=> $bid;
546: } @currentrids;
547: next if ($#currentrids<$rndpick);
548: # -------------------------------- randomly eliminate the ones that should stay
549: my (undef,$id)=split(/\./,$rid);
550: if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
551: my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
552: &Apache::lonnet::setup_random_from_rndseed($rndseed);
553: my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
554: for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
555: #&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
556: # -------------------------------------------------------- delete the leftovers
557: for (my $k=0; $k<=$#currentrids; $k++) {
558: if ($currentrids[$k]) {
559: $hash{'randomout_'.$currentrids[$k]}=1;
560: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
561: $randomoutentry.='&'.
562: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
563: $resid,
564: $hash{'src_'.$currentrids[$k]}
565: ).'&';
566: }
567: }
568: }
569: # ------------------------------ take care of explicitly hidden urls or folders
570: foreach my $rid (keys %hiddenurl) {
571: $hash{'randomout_'.$rid}=1;
572: my ($mapid,$resid)=split(/\./,$rid);
573: $randomoutentry.='&'.
574: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
575: $hash{'src_'.$rid}).'&';
576: }
577: # --------------------------------------- append randomout entry to environment
578: if ($randomoutentry) {
579: &Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
580: }
581: }
582:
583: # ---------------------------------------------------- Read map and all submaps
584:
585: sub readmap {
586: my $short=shift;
587: $short=~s/^\///;
588: my %cenv=&Apache::lonnet::coursedescription($short,{'freshen_cache'=>1});
589: my $fn=$cenv{'fn'};
590: my $uri;
591: $short=~s/\//\_/g;
592: unless ($uri=$cenv{'url'}) {
593: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
594: "Could not load course $short.</font>");
595: return ('',&mt('No course data available.'));;
596: }
597: @cond=('true:normal');
598:
599: open(LOCKFILE,">$fn.db.lock");
600: my $lock=0;
601: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
602: $lock=1;
603: unlink($fn.'.db');
604: unlink($fn.'_symb.db');
605: unlink($fn.'.state');
606: unlink($fn.'parms.db');
607: }
608: undef %randompick;
609: undef %hiddenurl;
610: undef %encurl;
611: $retfrid='';
612: if ($lock && (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
613: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
614: %hash=();
615: %parmhash=();
616: $errtext='';
617: $pc=0;
618: &processversionfile(%cenv);
619: my $furi=&Apache::lonnet::clutter($uri);
620: $hash{'src_0.0'}=&versiontrack($furi);
621: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
622: $hash{'ids_'.$furi}='0.0';
623: $hash{'is_map_0.0'}=1;
624: loadmap($uri);
625: if (defined($hash{'map_start_'.$uri})) {
626: &Apache::lonnet::appenv("request.course.id" => $short,
627: "request.course.fn" => $fn,
628: "request.course.uri" => $uri);
629: $env{'request.course.id'}=$short;
630: &traceroute('0',$hash{'map_start_'.$uri},'&');
631: &accinit($uri,$short,$fn);
632: &hiddenurls();
633: }
634: # ------------------------------------------------------- Put versions into src
635: foreach my $key (keys(%hash)) {
636: if ($key=~/^src_/) {
637: $hash{$key}=&putinversion($hash{$key});
638: } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
639: my ($type, $url) = ($1,$2);
640: my $value = $hash{$key};
641: $hash{$type.&putinversion($url)}=$value;
642: }
643: }
644: # ---------------------------------------------------------------- Encrypt URLs
645: foreach my $id (keys(%encurl)) {
646: # $hash{'src_'.$id}=&Apache::lonenc::encrypted($hash{'src_'.$id});
647: $hash{'encrypted_'.$id}=1;
648: }
649: # ----------------------------------------------- Close hashes to finally store
650: # --------------------------------- Routine must pass this point, no early outs
651: $hash{'first_rid'}=$retfrid;
652: my ($mapid,$resid)=split(/\./,$retfrid);
653: $hash{'first_mapurl'}=$hash{'map_id_'.$mapid};
654: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$retfrid});
655: $retfurl=&add_get_param($hash{'src_'.$retfrid},{ 'symb' => $symb });
656: if ($hash{'encrypted_'.$retfrid}) {
657: $retfurl=&Apache::lonenc::encrypted($retfurl,(&Apache::lonnet::allowed('adv') ne 'F'));
658: }
659: $hash{'first_url'}=$retfurl;
660: unless ((untie(%hash)) && (untie(%parmhash))) {
661: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
662: "Could not untie coursemap $fn for $uri.</font>");
663: }
664: # ---------------------------------------------------- Store away initial state
665: {
666: my $cfh;
667: if (open($cfh,">$fn.state")) {
668: print $cfh join("\n",@cond);
669: } else {
670: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
671: "Could not write statemap $fn for $uri.</font>");
672: }
673: }
674: flock(LOCKFILE,LOCK_UN);
675: close(LOCKFILE);
676: } else {
677: # if we are here it is likely because we are already trying to
678: # initialize the course in another child, busy wait trying to
679: # tie the hashes for the next 90 seconds, if we succeed forward
680: # them on to navmaps, if we fail, throw up the Could not init
681: # course screen
682: if ($lock) {
683: # Got the lock but not the DB files
684: flock(LOCKFILE,LOCK_UN);
685: }
686: untie(%hash);
687: untie(%parmhash);
688: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
689: "Could not tie coursemap $fn for $uri.</font>");
690: my $i=0;
691: while($i<90) {
692: $i++;
693: sleep(1);
694: if (flock(LOCKFILE,LOCK_EX|LOCK_NB) &&
695: (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640))) {
696: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
697: $retfurl='/adm/navmaps';
698: &Apache::lonnet::appenv("request.course.id" => $short,
699: "request.course.fn" => $fn,
700: "request.course.uri" => $uri);
701: untie(%hash);
702: untie(%parmhash);
703: last;
704: }
705: }
706: untie(%hash);
707: untie(%parmhash);
708: }
709: flock(LOCKFILE,LOCK_UN);
710: close(LOCKFILE);
711: }
712: &Apache::lonmsg::author_res_msg($env{'request.course.uri'},$errtext);
713: # ------------------------------------------------- Check for critical messages
714:
715: my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
716: $env{'user.name'});
717: if ($what[0]) {
718: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
719: $retfurl='/adm/email?critical=display';
720: }
721: }
722: return ($retfurl,$errtext);
723: }
724:
725: # ------------------------------------------------------- Evaluate state string
726:
727: sub evalstate {
728: my $fn=$env{'request.course.fn'}.'.state';
729: my $state='';
730: if (-e $fn) {
731: my @conditions=();
732: {
733: open(my $fh,"<$fn");
734: @conditions=<$fh>;
735: close($fh);
736: }
737: my $safeeval = new Safe;
738: my $safehole = new Safe::Hole;
739: $safeeval->permit("entereval");
740: $safeeval->permit(":base_math");
741: $safeeval->deny(":base_io");
742: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
743: foreach my $line (@conditions) {
744: chomp($line);
745: my ($condition,$weight)=split(/\:/,$line);
746: if ($safeeval->reval($condition)) {
747: if ($weight eq 'force') {
748: $state.='3';
749: } else {
750: $state.='2';
751: }
752: } else {
753: if ($weight eq 'stop') {
754: $state.='0';
755: } else {
756: $state.='1';
757: }
758: }
759: }
760: }
761: &Apache::lonnet::appenv('user.state.'.$env{'request.course.id'} => $state);
762: return $state;
763: }
764:
765: 1;
766: __END__
767:
768: =head1 NAME
769:
770: Apache::lonuserstate - Construct and maintain state and binary representation
771: of course for user
772:
773: =head1 SYNOPSIS
774:
775: Invoked by lonroles.pm.
776:
777: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
778:
779: =head1 INTRODUCTION
780:
781: This module constructs and maintains state and binary representation
782: of course for user.
783:
784: This is part of the LearningOnline Network with CAPA project
785: described at http://www.lon-capa.org.
786:
787: =head1 HANDLER SUBROUTINE
788:
789: There is no handler subroutine.
790:
791: =head1 OTHER SUBROUTINES
792:
793: =over 4
794:
795: =item *
796:
797: loadmap() : Loads map from disk
798:
799: =item *
800:
801: simplify() : Simplify expression
802:
803: =item *
804:
805: traceroute() : Build condition hash
806:
807: =item *
808:
809: accinit() : Cascading conditions, quick access, parameters
810:
811: =item *
812:
813: readmap() : Read map and all submaps
814:
815: =item *
816:
817: evalstate() : Evaluate state string
818:
819: =back
820:
821: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>