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