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