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