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