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