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