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