Annotation of rat/lonuserstate.pm, revision 1.62
1.1 www 1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
1.62 ! www 4: # $Id: lonuserstate.pm,v 1.61 2003/10/22 16:10:56 www Exp $
1.25 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 www 28: # (Server for RAT Maps
29: #
30: # (Edit Handler for RAT Maps
31: # (TeX Content Handler
32: #
1.26 harris41 33: # YEAR=2000
1.1 www 34: # 05/29/00,05/30 Gerd Kortemeyer)
35: # 7/1 Gerd Kortemeyer)
36: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
37: #
1.9 www 38: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
1.19 www 39: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
1.22 www 40: # 11/1,11/2,11/14,11/16,11/22,12/28,
1.26 harris41 41: # YEAR=2001
1.24 www 42: # 07/05/01,08/30,08/31 Gerd Kortemeyer
1.26 harris41 43: #
44: ###
1.1 www 45:
46: package Apache::lonuserstate;
47:
1.26 harris41 48: # ------------------------------------------------- modules used by this module
1.1 www 49: use strict;
50: use Apache::Constants qw(:common :http);
51: use Apache::File;
52: use HTML::TokeParser;
53: use Apache::lonnet();
1.26 harris41 54: use Apache::loncommon();
1.1 www 55: use GDBM_File;
1.12 www 56: use Apache::lonmsg;
1.15 www 57: use Safe;
1.21 www 58: use Safe::Hole;
1.15 www 59: use Opcode;
60:
1.1 www 61: # ---------------------------------------------------- Globals for this package
62:
63: my $pc; # Package counter
64: my %hash; # The big tied hash
1.19 www 65: my %parmhash;# The hash with the parameters
1.1 www 66: my @cond; # Array with all of the conditions
67: my $errtext; # variable with all errors
1.21 www 68: my $retfurl; # variable with the very first URL in the course
1.29 www 69: my %randompick; # randomly picked resources
1.51 www 70: my %randompickseed; # optional seed for randomly picking resources
1.61 www 71:
72: # ----------------------------------- Remove version from URL and store in hash
73:
74: sub versiontrack {
75: my $uri=shift;
76: if ($uri=~/\.(\d+)\.\w+$/) {
77: my $version=$1;
78: $uri=~s/\.\d+\.(\w+)$/\.$1/;
1.62 ! www 79: unless ($hash{'version_'.$uri}) {
! 80: $hash{'version_'.$uri}=$version;
! 81: }
1.61 www 82: }
83: return $uri;
84: }
85:
86: # -------------------------------------------------------------- Put in version
87:
88: sub putinversion {
89: my $uri=shift;
90: if ($hash{'version_'.$uri}) {
91: my $version=$hash{'version_'.$uri};
1.62 ! www 92: if ($version eq 'current') { return $uri; }
1.61 www 93: $uri=~s/\.(\w+)$/\.$version\.$1/;
94: }
95: return $uri;
96: }
97:
98: # ----------------------------------------- Processing versions file for course
99:
100: sub processversionfile {
101: my %cenv=shift;
102: my %versions=&Apache::lonnet::dump('resourceversions',
103: $cenv{'domain'},
104: $cenv{'num'});
105: foreach (keys %versions) {
106: if ($_=~/^error\:/) { return; }
1.62 ! www 107: $hash{'version_'.$_}=$versions{$_};
1.61 www 108: }
109: }
1.45 www 110:
1.1 www 111: # --------------------------------------------------------- Loads map from disk
112:
113: sub loadmap {
114: my $uri=shift;
115: if ($hash{'map_pc_'.$uri}) { return OK; }
116:
117: $pc++;
118: my $lpc=$pc;
119: $hash{'map_pc_'.$uri}=$lpc;
120: $hash{'map_id_'.$lpc}=$uri;
121:
1.37 www 122: # Determine and check filename
1.62 ! www 123: my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
1.37 www 124:
125: my $ispage=($fn=~/\.page$/);
1.1 www 126:
1.10 www 127: unless (($fn=~/\.sequence$/) ||
1.1 www 128: ($fn=~/\.page$/)) {
129: $errtext.="Invalid map: $fn\n";
130: return OK;
131: }
132:
1.37 www 133: my $instr=&Apache::lonnet::getfile($fn);
134:
1.57 albertel 135: unless ($instr eq -1) {
1.22 www 136:
1.37 www 137: # Successfully got file, parse it
1.1 www 138:
139: my $parser = HTML::TokeParser->new(\$instr);
140: my $token;
141:
142: my $linkpc=0;
143:
144: $fn=~/\.(\w+)$/;
145:
146: $hash{'map_type_'.$lpc}=$1;
147:
148: while ($token = $parser->get_token) {
149: if ($token->[0] eq 'S') {
150: if ($token->[1] eq 'resource') {
151: # -------------------------------------------------------------------- Resource
152:
153: my $rid=$lpc.'.'.$token->[2]->{'id'};
154:
155: $hash{'kind_'.$rid}='res';
156: $hash{'title_'.$rid}=$token->[2]->{'title'};
1.61 www 157: my $turi=&versiontrack($token->[2]->{'src'});
158: if ($token->[2]->{'version'}) {
1.62 ! www 159: unless ($hash{'version_'.$turi}) {
! 160: $hash{'version_'.$turi}=$1;
! 161: }
1.61 www 162: }
1.60 albertel 163: &Apache::lonnet::do_cache(\%Apache::lonnet::titlecache,
164: &Apache::lonnet::symbclean(
165: &Apache::lonnet::declutter($uri).'___'.
166: $token->[2]->{'id'}.'___'.
167: &Apache::lonnet::declutter($turi)),
168: $token->[2]->{'title'});
1.22 www 169: unless ($ispage) {
170: $turi=~/\.(\w+)$/;
1.26 harris41 171: my $embstyle=&Apache::loncommon::fileembstyle($1);
1.40 www 172: if ($token->[2]->{'external'} eq 'true') { # external
1.22 www 173: $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
1.40 www 174: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
175: if (($embstyle eq 'img') || ($embstyle eq 'emb')
176: || ($embstyle eq 'ssi')) {
177: $turi='/adm/wrapper'.$turi;
1.41 www 178: } elsif ($turi!~/\.(sequence|page)$/) {
1.42 www 179: $turi='/adm/coursedocs/showdoc'.$turi;
1.40 www 180: }
181: } else { # normal internal resource
1.53 www 182: if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
183: $turi='/adm/wrapper'.$turi;
1.22 www 184: }
185: }
186: }
1.1 www 187:
188: if (defined($hash{'ids_'.$turi})) {
189: $hash{'ids_'.$turi}.=','.$rid;
190: } else {
191: $hash{'ids_'.$turi}=''.$rid;
192: }
1.53 www 193:
194: if
195: ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/) {
196: $turi.='?register=1';
197: }
198:
199: $hash{'src_'.$rid}=$turi;
1.1 www 200:
1.22 www 201: if ($token->[2]->{'external'} eq 'true') {
1.1 www 202: $hash{'ext_'.$rid}='true:';
203: } else {
204: $hash{'ext_'.$rid}='false:';
205: }
206: if ($token->[2]->{'type'}) {
207: $hash{'type_'.$rid}=$token->[2]->{'type'};
1.2 www 208: if ($token->[2]->{'type'} eq 'start') {
209: $hash{'map_start_'.$uri}="$rid";
210: }
211: if ($token->[2]->{'type'} eq 'finish') {
212: $hash{'map_finish_'.$uri}="$rid";
213: }
1.1 www 214: } else {
215: $hash{'type_'.$rid}='normal';
216: }
217:
1.10 www 218: if (($turi=~/\.sequence$/) ||
1.1 www 219: ($turi=~/\.page$/)) {
1.2 www 220: $hash{'is_map_'.$rid}=1;
1.1 www 221: &loadmap($turi);
222: }
223:
224: } elsif ($token->[1] eq 'condition') {
225: # ------------------------------------------------------------------- Condition
226:
227: my $rid=$lpc.'.'.$token->[2]->{'id'};
228:
229: $hash{'kind_'.$rid}='cond';
1.2 www 230: $cond[$#cond+1]=$token->[2]->{'value'};
231: $hash{'condid_'.$rid}=$#cond;
1.1 www 232: if ($token->[2]->{'type'}) {
1.2 www 233: $cond[$#cond].=':'.$token->[2]->{'type'};
1.1 www 234: } else {
1.2 www 235: $cond[$#cond].=':normal';
1.1 www 236: }
237:
238: } elsif ($token->[1] eq 'link') {
239: # ----------------------------------------------------------------------- Links
240:
241: $linkpc++;
242: my $linkid=$lpc.'.'.$linkpc;
243:
244: my $goesto=$lpc.'.'.$token->[2]->{'to'};
245: my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
246: my $undercond=0;
247:
248: if ($token->[2]->{'condition'}) {
249: $undercond=$lpc.'.'.$token->[2]->{'condition'};
250: }
251:
252: $hash{'goesto_'.$linkid}=$goesto;
253: $hash{'comesfrom_'.$linkid}=$comesfrom;
254: $hash{'undercond_'.$linkid}=$undercond;
255:
256: if (defined($hash{'to_'.$comesfrom})) {
257: $hash{'to_'.$comesfrom}.=','.$linkid;
258: } else {
259: $hash{'to_'.$comesfrom}=''.$linkid;
260: }
261: if (defined($hash{'from_'.$goesto})) {
262: $hash{'from_'.$goesto}.=','.$linkid;
263: } else {
264: $hash{'from_'.$goesto}=''.$linkid;
265: }
1.18 www 266: } elsif ($token->[1] eq 'param') {
267: # ------------------------------------------------------------------- Parameter
268:
269: my $referid=$lpc.'.'.$token->[2]->{'to'};
1.20 www 270: my $part=$token->[2]->{'part'};
271: unless ($part) { $part=0; }
1.18 www 272: my $newparam=
273: &Apache::lonnet::escape($token->[2]->{'type'}).':'.
1.20 www 274: &Apache::lonnet::escape($part.'.'.
275: $token->[2]->{'name'}).'='.
1.18 www 276: &Apache::lonnet::escape($token->[2]->{'value'});
277: if (defined($hash{'param_'.$referid})) {
278: $hash{'param_'.$referid}.='&'.$newparam;
279: } else {
280: $hash{'param_'.$referid}=''.$newparam;
281: }
1.29 www 282: if ($token->[2]->{'name'} eq 'parameter_mapalias') {
1.28 www 283: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
284: }
1.29 www 285: if ($token->[2]->{'name'} eq 'parameter_randompick') {
286: $randompick{$referid}=$token->[2]->{'value'};
287: }
1.51 www 288: if ($token->[2]->{'name'} eq 'parameter_randompickseed') {
289: $randompick{$referid}=$token->[2]->{'value'};
290: }
1.1 www 291: }
292:
293: }
294: }
295:
296: } else {
297: $errtext.='Map not loaded: The file does not exist. ';
298: }
299: }
300:
1.3 www 301: # --------------------------------------------------------- Simplify expression
302:
303: sub simplify {
304: my $expression=shift;
305: # (8)=8
306: $expression=~s/\((\d+)\)/$1/g;
307: # 8&8=8
1.7 www 308: $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
1.3 www 309: # 8|8=8
1.7 www 310: $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
1.3 www 311: # (5&3)&4=5&3&4
1.7 www 312: $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
1.3 www 313: # (((5&3)|(4&6)))=((5&3)|(4&6))
314: $expression=~
315: s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
316: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
317: $expression=~
318: s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
319: return $expression;
320: }
321:
1.2 www 322: # -------------------------------------------------------- Build condition hash
323:
324: sub traceroute {
1.3 www 325: my ($sofar,$rid,$beenhere)=@_;
326: $sofar=simplify($sofar);
1.2 www 327: unless ($beenhere=~/\&$rid\&/) {
328: $beenhere.=$rid.'&';
1.48 www 329: if (($retfurl eq '') && ($hash{'src_'.$rid})
330: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
1.29 www 331: my ($mapid,$resid)=split(/\./,$rid);
1.35 www 332: $retfurl=$hash{'src_'.$rid}.
333: (($hash{'src_'.$rid}=~/\?/)?'&':'?').'symb='.
1.29 www 334: &Apache::lonnet::symbclean(
335: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
336: '___'.$resid.'___'.
337: &Apache::lonnet::declutter($hash{'src_'.$rid}));
1.21 www 338: }
1.2 www 339: if (defined($hash{'conditions_'.$rid})) {
1.3 www 340: $hash{'conditions_'.$rid}=simplify(
341: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.2 www 342: } else {
343: $hash{'conditions_'.$rid}=$sofar;
344: }
345: if (defined($hash{'is_map_'.$rid})) {
1.3 www 346: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
347: &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
348: if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
349: $sofar=
350: $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
351: }
1.2 www 352: }
353: }
354: if (defined($hash{'to_'.$rid})) {
1.26 harris41 355: foreach (split(/\,/,$hash{'to_'.$rid})) {
1.2 www 356: my $further=$sofar;
357: if ($hash{'undercond_'.$_}) {
358: if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
1.3 www 359: $further=simplify('('.$further.')&('.
360: $hash{'condid_'.$hash{'undercond_'.$_}}.')');
1.2 www 361: } else {
362: $errtext.='Undefined condition ID: '
363: .$hash{'undercond_'.$_}.'. ';
364: }
365: }
366: &traceroute($further,$hash{'goesto_'.$_},$beenhere);
1.26 harris41 367: }
1.2 www 368: }
369: }
370: }
1.1 www 371:
1.19 www 372: # ------------------------------ Cascading conditions, quick access, parameters
1.4 www 373:
374: sub accinit {
375: my ($uri,$short,$fn)=@_;
376: my %acchash=();
377: my %captured=();
378: my $condcounter=0;
1.5 www 379: $acchash{'acc.cond.'.$short.'.0'}=0;
1.26 harris41 380: foreach (keys %hash) {
1.4 www 381: if ($_=~/^conditions/) {
382: my $expr=$hash{$_};
1.26 harris41 383: foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
1.4 www 384: my $sub=$_;
385: my $orig=$_;
1.13 www 386: $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
1.4 www 387: my $factor=$1;
1.7 www 388: $sub=~s/$factor//g;
389: $sub=~s/^\(/\($factor\(/;
1.4 www 390: $sub.=')';
391: $sub=simplify($sub);
392: $orig=~s/(\W)/\\$1/g;
1.7 www 393: $expr=~s/$orig/$sub/;
1.26 harris41 394: }
1.4 www 395: $hash{$_}=$expr;
396: unless (defined($captured{$expr})) {
397: $condcounter++;
398: $captured{$expr}=$condcounter;
1.5 www 399: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
1.4 www 400: }
1.19 www 401: } elsif ($_=~/^param_(\d+)\.(\d+)/) {
402: my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
403: '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
1.26 harris41 404: foreach (split(/\&/,$hash{$_})) {
1.19 www 405: my ($typename,$value)=split(/\=/,$_);
406: my ($type,$name)=split(/\:/,$typename);
407: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
408: &Apache::lonnet::unescape($value);
409: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
410: &Apache::lonnet::unescape($type);
1.26 harris41 411: }
1.19 www 412: }
1.26 harris41 413: }
414: foreach (keys %hash) {
1.4 www 415: if ($_=~/^ids/) {
1.26 harris41 416: foreach (split(/\,/,$hash{$_})) {
1.13 www 417: my $resid=$_;
1.4 www 418: my $uri=$hash{'src_'.$resid};
1.22 www 419: $uri=~s/^\/adm\/wrapper//;
1.55 www 420: $uri=&Apache::lonnet::declutter($uri);
1.4 www 421: my @uriparts=split(/\//,$uri);
422: my $urifile=$uriparts[$#uriparts];
423: $#uriparts--;
424: my $uripath=join('/',@uriparts);
1.23 www 425: if ($uripath) {
1.13 www 426: my $uricond='0';
1.4 www 427: if (defined($hash{'conditions_'.$resid})) {
1.13 www 428: $uricond=$captured{$hash{'conditions_'.$resid}};
1.4 www 429: }
1.5 www 430: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
1.13 www 431: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
1.56 albertel 432: /(\&\Q$urifile\E\:[^\&]*)/) {
1.13 www 433: my $replace=$1;
1.27 www 434: my $regexp=$replace;
435: $regexp=~s/\|/\\\|/g;
1.13 www 436: $acchash{'acc.res.'.$short.'.'.$uripath}
1.27 www 437: =~s/$regexp/$replace\|$uricond/;
1.13 www 438: } else {
439: $acchash{'acc.res.'.$short.'.'.$uripath}.=
440: $urifile.':'.$uricond.'&';
441: }
1.4 www 442: } else {
1.13 www 443: $acchash{'acc.res.'.$short.'.'.$uripath}=
444: '&'.$urifile.':'.$uricond.'&';
1.23 www 445: }
446: }
1.26 harris41 447: }
1.13 www 448: }
1.26 harris41 449: }
1.24 www 450: $acchash{'acc.res.'.$short.'.'}='&:0&';
1.8 www 451: my $courseuri=$uri;
452: $courseuri=~s/^\/res\///;
1.19 www 453: &Apache::lonnet::delenv('(acc\.|httpref\.)');
1.4 www 454: &Apache::lonnet::appenv(%acchash,
1.9 www 455: "request.course.id" => $short,
1.8 www 456: "request.course.fn" => $fn,
457: "request.course.uri" => $courseuri);
1.4 www 458: }
459:
1.29 www 460: # ------------------------------------- Selectively delete from randompick maps
461:
462: sub pickrandom {
1.31 www 463: my $randomoutentry='';
1.29 www 464: foreach my $rid (keys %randompick) {
465: my $rndpick=$randompick{$rid};
466: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
467: # ------------------------------------------- put existing resources into array
468: my @currentrids=();
1.50 albertel 469: foreach (sort(keys(%hash))) {
1.29 www 470: if ($_=~/^src_($mpc\.\d+)/) {
471: if ($hash{'src_'.$1}) { push @currentrids, $1; }
472: }
473: }
1.50 albertel 474: # rids are number.number and we want to numercially sort on
475: # the second number
476: @currentrids=sort {
477: my (undef,$aid)=split(/\./,$a);
478: my (undef,$bid)=split(/\./,$b);
479: $aid <=> $bid;
480: } @currentrids;
1.29 www 481: next if ($#currentrids<$rndpick);
482: # -------------------------------- randomly eliminate the ones that should stay
1.50 albertel 483: my (undef,$id)=split(/\./,$rid);
1.51 www 484: if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1.50 albertel 485: my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
1.58 albertel 486: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.50 albertel 487: my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
488: for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
489: #&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1.29 www 490: # -------------------------------------------------------- delete the leftovers
491: for (my $k=0; $k<=$#currentrids; $k++) {
492: if ($currentrids[$k]) {
493: $hash{'randomout_'.$currentrids[$k]}=1;
1.32 www 494: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
495: $randomoutentry.='&'.
496: &Apache::lonnet::symbclean(
497: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
498: '___'.$resid.'___'.
499: &Apache::lonnet::declutter($hash{'src_'.$currentrids[$k]})
500: ).'&';
1.29 www 501: }
502: }
1.31 www 503: }
504: if ($randomoutentry) {
505: &Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
1.29 www 506: }
507: }
508:
1.1 www 509: # ---------------------------------------------------- Read map and all submaps
510:
511: sub readmap {
1.9 www 512: my $short=shift;
513: $short=~s/^\///;
514: my %cenv=&Apache::lonnet::coursedescription($short);
515: my $fn=$cenv{'fn'};
516: my $uri;
517: $short=~s/\//\_/g;
518: unless ($uri=$cenv{'url'}) {
519: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
520: "Could not load course $short.</font>");
521: return 'No course data available.';
522: }
1.3 www 523: @cond=('true:normal');
1.11 www 524: unlink($fn.'.db');
525: unlink($fn.'_symb.db');
526: unlink($fn.'.state');
1.19 www 527: unlink($fn.'parms.db');
1.29 www 528: undef %randompick;
1.21 www 529: $retfurl='';
1.36 albertel 530: if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
531: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
1.4 www 532: %hash=();
1.19 www 533: %parmhash=();
1.4 www 534: $errtext='';
535: $pc=0;
1.62 ! www 536: &processversionfile(%cenv);
1.38 www 537: my $furi=&Apache::lonnet::clutter($uri);
1.61 www 538: $hash{'src_0.0'}=&versiontrack($furi);
1.30 www 539: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
540: $hash{'ids_'.$furi}='0.0';
541: $hash{'is_map_0.0'}=1;
1.4 www 542: loadmap($uri);
543: if (defined($hash{'map_start_'.$uri})) {
544: &traceroute('0',$hash{'map_start_'.$uri},'&');
545: &accinit($uri,$short,$fn);
1.29 www 546: &pickrandom();
1.45 www 547: }
1.62 ! www 548: # ------------------------------------------------------- Put versions into src
1.61 www 549: foreach (keys %hash) {
550: if ($_=~/^src\_/) {
551: $hash{$_}=&putinversion($hash{$_});
552: }
553: }
1.19 www 554: unless ((untie(%hash)) && (untie(%parmhash))) {
1.4 www 555: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
556: "Could not untie coursemap $fn for $uri.</font>");
1.1 www 557: }
1.4 www 558: {
559: my $cfh;
560: if ($cfh=Apache::File->new(">$fn.state")) {
561: print $cfh join("\n",@cond);
562: } else {
1.6 www 563: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4 www 564: "Could not write statemap $fn for $uri.</font>");
565: }
566: }
567: } else {
1.6 www 568: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4 www 569: "Could not tie coursemap $fn for $uri.</font>");
570: }
1.12 www 571: &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
1.46 www 572: # ------------------------------------------------- Check for critical messages
573:
574: my @what=&Apache::lonnet::dump('critical',$ENV{'user.domain'},
575: $ENV{'user.name'});
576: if ($what[0]) {
577: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
578: $retfurl='/adm/email?critical=display';
579: }
580: }
1.21 www 581: return ($retfurl,$errtext);
1.1 www 582: }
1.15 www 583:
584: # ------------------------------------------------------- Evaluate state string
585:
586: sub evalstate {
1.21 www 587:
1.15 www 588: my $fn=$ENV{'request.course.fn'}.'.state';
589: my $state='2';
590: if (-e $fn) {
591: my @conditions=();
592: {
593: my $fh=Apache::File->new($fn);
594: @conditions=<$fh>;
595: }
1.21 www 596: my $safeeval = new Safe;
597: my $safehole = new Safe::Hole;
1.15 www 598: $safeeval->permit("entereval");
599: $safeeval->permit(":base_math");
600: $safeeval->deny(":base_io");
1.21 www 601: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.26 harris41 602: foreach (@conditions) {
1.15 www 603: my $line=$_;
604: chomp($line);
605: my ($condition,$weight)=split(/\:/,$_);
606: if ($safeeval->reval($condition)) {
607: if ($weight eq 'force') {
608: $state.='3';
609: } else {
610: $state.='2';
611: }
612: } else {
613: if ($weight eq 'stop') {
614: $state.='0';
615: } else {
616: $state.='1';
617: }
618: }
1.26 harris41 619: }
1.15 www 620: }
621: &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
622: return $state;
623: }
624:
1.1 www 625: 1;
626: __END__
627:
1.26 harris41 628: =head1 NAME
629:
630: Apache::lonuserstate - Construct and maintain state and binary representation
631: of course for user
632:
633: =head1 SYNOPSIS
634:
635: Invoked by lonroles.pm.
636:
637: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
638:
639: =head1 INTRODUCTION
640:
641: This module constructs and maintains state and binary representation
642: of course for user.
643:
644: This is part of the LearningOnline Network with CAPA project
645: described at http://www.lon-capa.org.
646:
647: =head1 HANDLER SUBROUTINE
648:
649: There is no handler subroutine.
650:
651: =head1 OTHER SUBROUTINES
652:
653: =over 4
654:
655: =item *
656:
657: loadmap() : Loads map from disk
658:
659: =item *
660:
661: simplify() : Simplify expression
662:
663: =item *
664:
665: traceroute() : Build condition hash
666:
667: =item *
668:
669: accinit() : Cascading conditions, quick access, parameters
1.1 www 670:
1.26 harris41 671: =item *
1.1 www 672:
1.26 harris41 673: readmap() : Read map and all submaps
1.1 www 674:
1.26 harris41 675: =item *
1.1 www 676:
1.26 harris41 677: evalstate() : Evaluate state string
1.1 www 678:
1.26 harris41 679: =back
1.1 www 680:
1.26 harris41 681: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>