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