Annotation of rat/lonuserstate.pm, revision 1.117
1.1 www 1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
1.117 ! raeburn 4: # $Id: lonuserstate.pm,v 1.116 2006/05/30 19:47:40 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 HTML::TokeParser;
1.89 albertel 35: use Apache::lonnet;
1.114 www 36: use Apache::lonlocal;
1.26 harris41 37: use Apache::loncommon();
1.1 www 38: use GDBM_File;
1.12 www 39: use Apache::lonmsg;
1.15 www 40: use Safe;
1.21 www 41: use Safe::Hole;
1.15 www 42: use Opcode;
1.73 www 43: use Apache::lonenc;
1.96 albertel 44: use Fcntl qw(:flock);
1.114 www 45: use LONCAPA;
46:
1.15 www 47:
1.1 www 48: # ---------------------------------------------------- Globals for this package
49:
50: my $pc; # Package counter
51: my %hash; # The big tied hash
1.19 www 52: my %parmhash;# The hash with the parameters
1.1 www 53: my @cond; # Array with all of the conditions
54: my $errtext; # variable with all errors
1.116 www 55: my $retfrid; # variable with the very first RID in the course
56: my $retfurl; # first URL
1.29 www 57: my %randompick; # randomly picked resources
1.51 www 58: my %randompickseed; # optional seed for randomly picking resources
1.73 www 59: my %encurl; # URLs in this folder are supposed to be encrypted
60: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
1.61 www 61:
62: # ----------------------------------- Remove version from URL and store in hash
63:
64: sub versiontrack {
65: my $uri=shift;
66: if ($uri=~/\.(\d+)\.\w+$/) {
67: my $version=$1;
68: $uri=~s/\.\d+\.(\w+)$/\.$1/;
1.62 www 69: unless ($hash{'version_'.$uri}) {
70: $hash{'version_'.$uri}=$version;
71: }
1.61 www 72: }
73: return $uri;
74: }
75:
76: # -------------------------------------------------------------- Put in version
77:
78: sub putinversion {
79: my $uri=shift;
1.93 www 80: my $key=$env{'request.course.id'}.'_'.&Apache::lonnet::clutter($uri);
1.61 www 81: if ($hash{'version_'.$uri}) {
82: my $version=$hash{'version_'.$uri};
1.65 www 83: if ($version eq 'mostrecent') { return $uri; }
1.66 www 84: if ($version eq &Apache::lonnet::getversion(
85: &Apache::lonnet::filelocation('',$uri)))
86: { return $uri; }
1.61 www 87: $uri=~s/\.(\w+)$/\.$version\.$1/;
88: }
1.93 www 89: &Apache::lonnet::do_cache_new('courseresversion',$key,&Apache::lonnet::declutter($uri),600);
1.61 www 90: return $uri;
91: }
92:
93: # ----------------------------------------- Processing versions file for course
94:
95: sub processversionfile {
1.64 www 96: my %cenv=@_;
1.61 www 97: my %versions=&Apache::lonnet::dump('resourceversions',
98: $cenv{'domain'},
99: $cenv{'num'});
1.106 albertel 100: foreach my $ver (keys(%versions)) {
101: if ($ver=~/^error\:/) { return; }
102: $hash{'version_'.$ver}=$versions{$ver};
1.61 www 103: }
104: }
1.45 www 105:
1.1 www 106: # --------------------------------------------------------- Loads map from disk
107:
108: sub loadmap {
109: my $uri=shift;
1.114 www 110: if ($hash{'map_pc_'.$uri}) {
111: $errtext.=&mt('<br />Multiple use of sequence/page <tt>[_1]</tt>! The course will not function properly.',$uri);
112: return;
113: }
1.1 www 114: $pc++;
115: my $lpc=$pc;
116: $hash{'map_pc_'.$uri}=$lpc;
117: $hash{'map_id_'.$lpc}=$uri;
118:
1.37 www 119: # Determine and check filename
1.62 www 120: my $fn=&Apache::lonnet::filelocation('',&putinversion($uri));
1.37 www 121:
122: my $ispage=($fn=~/\.page$/);
1.1 www 123:
1.10 www 124: unless (($fn=~/\.sequence$/) ||
1.1 www 125: ($fn=~/\.page$/)) {
1.114 www 126: $errtext.=&mt("<br />Invalid map: <tt>[_1]</tt>",$fn);
1.98 albertel 127: return;
1.1 www 128: }
129:
1.37 www 130: my $instr=&Apache::lonnet::getfile($fn);
131:
1.57 albertel 132: unless ($instr eq -1) {
1.22 www 133:
1.37 www 134: # Successfully got file, parse it
1.1 www 135:
136: my $parser = HTML::TokeParser->new(\$instr);
1.95 www 137: $parser->attr_encoded(1);
1.1 www 138: my $token;
139:
140: my $linkpc=0;
141:
142: $fn=~/\.(\w+)$/;
143:
144: $hash{'map_type_'.$lpc}=$1;
145:
146: while ($token = $parser->get_token) {
147: if ($token->[0] eq 'S') {
148: if ($token->[1] eq 'resource') {
149: # -------------------------------------------------------------------- Resource
1.92 www 150: if ($token->[2]->{'type'} eq 'zombie') { next; }
1.1 www 151: my $rid=$lpc.'.'.$token->[2]->{'id'};
152:
153: $hash{'kind_'.$rid}='res';
154: $hash{'title_'.$rid}=$token->[2]->{'title'};
1.61 www 155: my $turi=&versiontrack($token->[2]->{'src'});
156: if ($token->[2]->{'version'}) {
1.62 www 157: unless ($hash{'version_'.$turi}) {
158: $hash{'version_'.$turi}=$1;
159: }
1.61 www 160: }
1.83 albertel 161: my $title=$token->[2]->{'title'};
162: $title=~s/\&colon\;/\:/gs;
1.84 albertel 163: # my $symb=&Apache::lonnet::encode_symb($uri,
164: # $token->[2]->{'id'},
165: # $turi);
166: # &Apache::lonnet::do_cache_new('title',$symb,$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
1.97 www 173: if (($embstyle eq 'img')
174: || ($embstyle eq 'emb')
1.99 albertel 175: || ($embstyle eq 'wrp')) {
1.76 albertel 176: $turi='/adm/wrapper'.$turi;
1.78 albertel 177: } elsif ($embstyle eq 'ssi') {
178: #do nothing with these
179: } elsif ($turi!~/\.(sequence|page)$/) {
1.42 www 180: $turi='/adm/coursedocs/showdoc'.$turi;
1.40 www 181: }
1.70 www 182: } elsif ($turi=~/\S/) { # normal non-empty internal resource
1.68 www 183: my $mapdir=$uri;
184: $mapdir=~s/[^\/]+$//;
185: $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
1.99 albertel 186: if (($embstyle eq 'img')
187: || ($embstyle eq 'emb')
188: || ($embstyle eq 'wrp')) {
1.68 www 189: $turi='/adm/wrapper'.$turi;
190: }
1.22 www 191: }
192: }
1.71 www 193: # Store reverse lookup, remove query string
194: my $idsuri=$turi;
195: $idsuri=~s/\?.+$//;
196: if (defined($hash{'ids_'.$idsuri})) {
197: $hash{'ids_'.$idsuri}.=','.$rid;
1.1 www 198: } else {
1.71 www 199: $hash{'ids_'.$idsuri}=''.$rid;
1.1 www 200: }
1.53 www 201:
1.117 ! raeburn 202: if ($turi=~/\/(syllabus|aboutme|navmaps|smppg|grppg|bulletinboard)$/) {
1.53 www 203: $turi.='?register=1';
204: }
205:
206: $hash{'src_'.$rid}=$turi;
1.1 www 207:
1.22 www 208: if ($token->[2]->{'external'} eq 'true') {
1.1 www 209: $hash{'ext_'.$rid}='true:';
210: } else {
211: $hash{'ext_'.$rid}='false:';
212: }
213: if ($token->[2]->{'type'}) {
214: $hash{'type_'.$rid}=$token->[2]->{'type'};
1.2 www 215: if ($token->[2]->{'type'} eq 'start') {
216: $hash{'map_start_'.$uri}="$rid";
217: }
218: if ($token->[2]->{'type'} eq 'finish') {
219: $hash{'map_finish_'.$uri}="$rid";
220: }
1.1 www 221: } else {
222: $hash{'type_'.$rid}='normal';
223: }
224:
1.10 www 225: if (($turi=~/\.sequence$/) ||
1.1 www 226: ($turi=~/\.page$/)) {
1.2 www 227: $hash{'is_map_'.$rid}=1;
1.1 www 228: &loadmap($turi);
229: }
230:
231: } elsif ($token->[1] eq 'condition') {
232: # ------------------------------------------------------------------- Condition
233:
234: my $rid=$lpc.'.'.$token->[2]->{'id'};
235:
236: $hash{'kind_'.$rid}='cond';
1.2 www 237: $cond[$#cond+1]=$token->[2]->{'value'};
238: $hash{'condid_'.$rid}=$#cond;
1.1 www 239: if ($token->[2]->{'type'}) {
1.2 www 240: $cond[$#cond].=':'.$token->[2]->{'type'};
1.1 www 241: } else {
1.2 www 242: $cond[$#cond].=':normal';
1.1 www 243: }
244:
245: } elsif ($token->[1] eq 'link') {
246: # ----------------------------------------------------------------------- Links
247:
248: $linkpc++;
249: my $linkid=$lpc.'.'.$linkpc;
250:
251: my $goesto=$lpc.'.'.$token->[2]->{'to'};
252: my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
253: my $undercond=0;
254:
255: if ($token->[2]->{'condition'}) {
256: $undercond=$lpc.'.'.$token->[2]->{'condition'};
257: }
258:
259: $hash{'goesto_'.$linkid}=$goesto;
260: $hash{'comesfrom_'.$linkid}=$comesfrom;
261: $hash{'undercond_'.$linkid}=$undercond;
262:
263: if (defined($hash{'to_'.$comesfrom})) {
264: $hash{'to_'.$comesfrom}.=','.$linkid;
265: } else {
266: $hash{'to_'.$comesfrom}=''.$linkid;
267: }
268: if (defined($hash{'from_'.$goesto})) {
269: $hash{'from_'.$goesto}.=','.$linkid;
270: } else {
271: $hash{'from_'.$goesto}=''.$linkid;
272: }
1.18 www 273: } elsif ($token->[1] eq 'param') {
274: # ------------------------------------------------------------------- Parameter
275:
276: my $referid=$lpc.'.'.$token->[2]->{'to'};
1.63 albertel 277: my $name=$token->[2]->{'name'};
278: my $part;
279: if ($name=~/^parameter_(.*)_/) {
280: $part=$1;
281: } else {
282: $part=0;
283: }
284: $name=~s/^.*_([^_]*)$/$1/;
1.18 www 285: my $newparam=
1.114 www 286: &escape($token->[2]->{'type'}).':'.
287: &escape($part.'.'.$name).'='.
288: &escape($token->[2]->{'value'});
1.18 www 289: if (defined($hash{'param_'.$referid})) {
290: $hash{'param_'.$referid}.='&'.$newparam;
291: } else {
292: $hash{'param_'.$referid}=''.$newparam;
293: }
1.75 www 294: if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
1.28 www 295: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
296: }
1.75 www 297: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) {
1.29 www 298: $randompick{$referid}=$token->[2]->{'value'};
299: }
1.75 www 300: if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) {
1.51 www 301: $randompick{$referid}=$token->[2]->{'value'};
302: }
1.75 www 303: if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
1.74 www 304: if ($token->[2]->{'value'}=~/^yes$/i) {
305: $encurl{$referid}=1;
306: }
1.73 www 307: }
1.75 www 308: if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
1.74 www 309: if ($token->[2]->{'value'}=~/^yes$/i) {
310: $hiddenurl{$referid}=1;
311: }
1.73 www 312: }
1.1 www 313: }
1.85 albertel 314:
1.1 www 315: }
316: }
317:
318: } else {
1.114 www 319: $errtext.=&mt('<br />Map not loaded: The file <tt>[_1]</tt> does not exist.',$fn);
1.1 www 320: }
321: }
322:
1.3 www 323: # --------------------------------------------------------- Simplify expression
324:
325: sub simplify {
1.85 albertel 326: my $expression=shift;
1.101 albertel 327: # (0&1) = 1
1.105 albertel 328: $expression=~s/\(0\&([_\.\d]+)\)/$1/g;
1.3 www 329: # (8)=8
1.105 albertel 330: $expression=~s/\(([_\.\d]+)\)/$1/g;
1.3 www 331: # 8&8=8
1.105 albertel 332: $expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
1.3 www 333: # 8|8=8
1.105 albertel 334: $expression=~s/([^_\.\d])([_\.\d]+)\|\2([^_\.\d])/$1$2$3/g;
1.3 www 335: # (5&3)&4=5&3&4
1.105 albertel 336: $expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
1.3 www 337: # (((5&3)|(4&6)))=((5&3)|(4&6))
1.105 albertel 338: $expression=~
339: s/\((\(\([_\.\d]+(?:\&[_\.\d]+)*\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+\))\)/$1/g;
1.3 www 340: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
1.85 albertel 341: $expression=~
1.105 albertel 342: s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
1.85 albertel 343: return $expression;
1.3 www 344: }
345:
1.2 www 346: # -------------------------------------------------------- Build condition hash
347:
348: sub traceroute {
1.77 www 349: my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
1.81 albertel 350: my $newsofar=$sofar=simplify($sofar);
1.2 www 351: unless ($beenhere=~/\&$rid\&/) {
1.85 albertel 352: $beenhere.=$rid.'&';
353: my ($mapid,$resid)=split(/\./,$rid);
354: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
355: my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
1.91 albertel 356:
1.90 albertel 357: if ($hdnflag || lc($hidden) eq 'yes') {
358: $hiddenurl{$rid}=1;
1.91 albertel 359: }
360: if (!$hdnflag && lc($hidden) eq 'no') {
1.90 albertel 361: delete($hiddenurl{$rid});
362: }
1.91 albertel 363:
1.85 albertel 364: my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
365: if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
1.116 www 366: if (($retfrid eq '') && ($hash{'src_'.$rid})
1.85 albertel 367: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
1.116 www 368: $retfrid=$rid;
1.85 albertel 369: }
370: if (defined($hash{'conditions_'.$rid})) {
371: $hash{'conditions_'.$rid}=simplify(
1.103 albertel 372: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.85 albertel 373: } else {
374: $hash{'conditions_'.$rid}=$sofar;
375: }
1.107 albertel 376:
377: # if the expression is just the 0th condition keep it
378: # otherwise leave a pointer to this condition expression
379: $newsofar = ($sofar eq '0') ? $sofar : '_'.$rid;
380:
1.85 albertel 381: if (defined($hash{'is_map_'.$rid})) {
382: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
383: $sofar=$newsofar=
384: &traceroute($sofar,
385: $hash{'map_start_'.$hash{'src_'.$rid}},'&',
386: $encflag || $encurl{$rid},
387: $hdnflag || $hiddenurl{$rid});
388: }
389: }
390: if (defined($hash{'to_'.$rid})) {
1.106 albertel 391: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
1.2 www 392: my $further=$sofar;
1.106 albertel 393: if ($hash{'undercond_'.$id}) {
394: if (defined($hash{'condid_'.$hash{'undercond_'.$id}})) {
1.105 albertel 395: $further=simplify('('.'_'.$rid.')&('.
1.106 albertel 396: $hash{'condid_'.$hash{'undercond_'.$id}}.')');
1.85 albertel 397: } else {
1.114 www 398: $errtext.=&mt('<br />Undefined condition ID: [_1]',$hash{'undercond_'.$id});
1.85 albertel 399: }
1.2 www 400: }
1.106 albertel 401: $newsofar=&traceroute($further,$hash{'goesto_'.$id},$beenhere,
1.81 albertel 402: $encflag,$hdnflag);
1.85 albertel 403: }
404: }
1.2 www 405: }
1.81 albertel 406: return $newsofar;
1.2 www 407: }
1.1 www 408:
1.19 www 409: # ------------------------------ Cascading conditions, quick access, parameters
1.4 www 410:
411: sub accinit {
412: my ($uri,$short,$fn)=@_;
413: my %acchash=();
414: my %captured=();
415: my $condcounter=0;
1.5 www 416: $acchash{'acc.cond.'.$short.'.0'}=0;
1.104 albertel 417: foreach my $key (keys(%hash)) {
418: if ($key=~/^conditions/) {
419: my $expr=$hash{$key};
1.109 albertel 420: # try to find and factor out common sub-expressions
1.105 albertel 421: foreach my $sub ($expr=~m/(\(\([_\.\d]+(?:\&[_\.\d]+)+\)(?:\|\([_\.\d]+(?:\&[_\.\d]+)+\))+\))/g) {
1.104 albertel 422: my $orig=$sub;
1.109 albertel 423:
424: my ($factor) = ($sub=~/\(\(([_\.\d]+\&(:?[_\.\d]+\&)*)(?:[_\.\d]+\&*)+\)(?:\|\(\1(?:[_\.\d]+\&*)+\))+\)/);
425: next if (!defined($factor));
426:
427: $sub=~s/\Q$factor\E//g;
1.85 albertel 428: $sub=~s/^\(/\($factor\(/;
429: $sub.=')';
430: $sub=simplify($sub);
1.109 albertel 431: $expr=~s/\Q$orig\E/$sub/;
1.85 albertel 432: }
1.104 albertel 433: $hash{$key}=$expr;
1.85 albertel 434: unless (defined($captured{$expr})) {
435: $condcounter++;
436: $captured{$expr}=$condcounter;
437: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
438: }
1.104 albertel 439: } elsif ($key=~/^param_(\d+)\.(\d+)/) {
1.86 albertel 440: my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
441: $hash{'src_'.$1.'.'.$2});
1.104 albertel 442: foreach my $param (split(/\&/,$hash{$key})) {
443: my ($typename,$value)=split(/\=/,$param);
1.85 albertel 444: my ($type,$name)=split(/\:/,$typename);
1.114 www 445: $parmhash{$prefix.'.'.&unescape($name)}=
446: &unescape($value);
447: $parmhash{$prefix.'.'.&unescape($name).'.type'}=
448: &unescape($type);
1.85 albertel 449: }
450: }
1.26 harris41 451: }
1.104 albertel 452: foreach my $key (keys(%hash)) {
453: if ($key=~/^ids/) {
454: foreach my $resid (split(/\,/,$hash{$key})) {
1.85 albertel 455: my $uri=$hash{'src_'.$resid};
1.100 albertel 456: my ($uripath,$urifile) =
457: &Apache::lonnet::split_uri_for_cond($uri);
1.85 albertel 458: if ($uripath) {
459: my $uricond='0';
460: if (defined($hash{'conditions_'.$resid})) {
461: $uricond=$captured{$hash{'conditions_'.$resid}};
462: }
463: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
464: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
465: /(\&\Q$urifile\E\:[^\&]*)/) {
466: my $replace=$1;
467: my $regexp=$replace;
468: #$regexp=~s/\|/\\\|/g;
1.105 albertel 469: $acchash{'acc.res.'.$short.'.'.$uripath} =~
1.104 albertel 470: s/\Q$regexp\E/$replace\|$uricond/;
1.85 albertel 471: } else {
472: $acchash{'acc.res.'.$short.'.'.$uripath}.=
473: $urifile.':'.$uricond.'&';
474: }
475: } else {
476: $acchash{'acc.res.'.$short.'.'.$uripath}=
477: '&'.$urifile.':'.$uricond.'&';
478: }
479: }
480: }
481: }
1.26 harris41 482: }
1.24 www 483: $acchash{'acc.res.'.$short.'.'}='&:0&';
1.8 www 484: my $courseuri=$uri;
485: $courseuri=~s/^\/res\///;
1.19 www 486: &Apache::lonnet::delenv('(acc\.|httpref\.)');
1.79 albertel 487: &Apache::lonnet::appenv(%acchash);
1.4 www 488: }
489:
1.73 www 490: # ---------------- Selectively delete from randompick maps and hidden url parms
1.29 www 491:
1.73 www 492: sub hiddenurls {
1.31 www 493: my $randomoutentry='';
1.29 www 494: foreach my $rid (keys %randompick) {
495: my $rndpick=$randompick{$rid};
496: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
497: # ------------------------------------------- put existing resources into array
498: my @currentrids=();
1.106 albertel 499: foreach my $key (sort(keys(%hash))) {
500: if ($key=~/^src_($mpc\.\d+)/) {
1.29 www 501: if ($hash{'src_'.$1}) { push @currentrids, $1; }
502: }
503: }
1.50 albertel 504: # rids are number.number and we want to numercially sort on
505: # the second number
506: @currentrids=sort {
507: my (undef,$aid)=split(/\./,$a);
508: my (undef,$bid)=split(/\./,$b);
509: $aid <=> $bid;
510: } @currentrids;
1.29 www 511: next if ($#currentrids<$rndpick);
512: # -------------------------------- randomly eliminate the ones that should stay
1.50 albertel 513: my (undef,$id)=split(/\./,$rid);
1.51 www 514: if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1.50 albertel 515: my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
1.58 albertel 516: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.50 albertel 517: my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
518: for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
519: #&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1.29 www 520: # -------------------------------------------------------- delete the leftovers
521: for (my $k=0; $k<=$#currentrids; $k++) {
522: if ($currentrids[$k]) {
523: $hash{'randomout_'.$currentrids[$k]}=1;
1.32 www 524: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
525: $randomoutentry.='&'.
1.86 albertel 526: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
527: $resid,
528: $hash{'src_'.$currentrids[$k]}
529: ).'&';
1.29 www 530: }
531: }
1.31 www 532: }
1.73 www 533: # ------------------------------ take care of explicitly hidden urls or folders
534: foreach my $rid (keys %hiddenurl) {
535: $hash{'randomout_'.$rid}=1;
536: my ($mapid,$resid)=split(/\./,$rid);
537: $randomoutentry.='&'.
1.86 albertel 538: &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
539: $hash{'src_'.$rid}).'&';
1.73 www 540: }
541: # --------------------------------------- append randomout entry to environment
1.31 www 542: if ($randomoutentry) {
543: &Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
1.29 www 544: }
545: }
546:
1.1 www 547: # ---------------------------------------------------- Read map and all submaps
548:
549: sub readmap {
1.85 albertel 550: my $short=shift;
551: $short=~s/^\///;
1.108 albertel 552: my %cenv=&Apache::lonnet::coursedescription($short,{'freshen_cache'=>1});
1.85 albertel 553: my $fn=$cenv{'fn'};
554: my $uri;
555: $short=~s/\//\_/g;
556: unless ($uri=$cenv{'url'}) {
557: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
558: "Could not load course $short.</font>");
1.114 www 559: return ('',&mt('No course data available.'));;
1.85 albertel 560: }
561: @cond=('true:normal');
1.96 albertel 562:
563: open(LOCKFILE,">$fn.db.lock");
564: my $lock=0;
565: if (flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
566: $lock=1;
567: unlink($fn.'.db');
568: unlink($fn.'_symb.db');
569: unlink($fn.'.state');
570: unlink($fn.'parms.db');
571: }
1.85 albertel 572: undef %randompick;
573: undef %hiddenurl;
574: undef %encurl;
1.116 www 575: $retfrid='';
1.96 albertel 576: if ($lock && (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
1.85 albertel 577: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
578: %hash=();
579: %parmhash=();
580: $errtext='';
581: $pc=0;
582: &processversionfile(%cenv);
583: my $furi=&Apache::lonnet::clutter($uri);
584: $hash{'src_0.0'}=&versiontrack($furi);
585: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
586: $hash{'ids_'.$furi}='0.0';
587: $hash{'is_map_0.0'}=1;
588: loadmap($uri);
589: if (defined($hash{'map_start_'.$uri})) {
590: &Apache::lonnet::appenv("request.course.id" => $short,
591: "request.course.fn" => $fn,
592: "request.course.uri" => $uri);
1.116 www 593: $env{'request.course.id'}=$short;
1.85 albertel 594: &traceroute('0',$hash{'map_start_'.$uri},'&');
595: &accinit($uri,$short,$fn);
596: &hiddenurls();
597: }
1.62 www 598: # ------------------------------------------------------- Put versions into src
1.106 albertel 599: foreach my $key (keys(%hash)) {
1.110 albertel 600: if ($key=~/^src_/) {
1.106 albertel 601: $hash{$key}=&putinversion($hash{$key});
1.110 albertel 602: } elsif ($key =~ /^(map_(?:start|finish|pc)_)(.*)/) {
603: my ($type, $url) = ($1,$2);
604: my $value = $hash{$key};
605: $hash{$type.&putinversion($url)}=$value;
1.85 albertel 606: }
1.61 www 607: }
1.74 www 608: # ---------------------------------------------------------------- Encrypt URLs
1.106 albertel 609: foreach my $id (keys(%encurl)) {
610: # $hash{'src_'.$id}=&Apache::lonenc::encrypted($hash{'src_'.$id});
611: $hash{'encrypted_'.$id}=1;
1.85 albertel 612: }
1.74 www 613: # ----------------------------------------------- Close hashes to finally store
614: # --------------------------------- Routine must pass this point, no early outs
1.116 www 615: $hash{'first_rid'}=$retfrid;
616: my ($mapid,$resid)=split(/\./,$retfrid);
617: $hash{'first_mapurl'}=$hash{'map_id_'.$mapid};
618: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$retfrid});
619: $retfurl=&add_get_param($hash{'src_'.$retfrid},{ 'symb' => $symb });
620: if ($hash{'encrypted_'.$retfrid}) {
621: $retfurl=&Apache::lonenc::encrypted($retfurl,(&Apache::lonnet::allowed('adv') ne 'F'));
622: }
1.94 albertel 623: $hash{'first_url'}=$retfurl;
1.85 albertel 624: unless ((untie(%hash)) && (untie(%parmhash))) {
625: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
626: "Could not untie coursemap $fn for $uri.</font>");
627: }
628: # ---------------------------------------------------- Store away initial state
629: {
630: my $cfh;
1.88 raeburn 631: if (open($cfh,">$fn.state")) {
1.85 albertel 632: print $cfh join("\n",@cond);
633: } else {
634: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
635: "Could not write statemap $fn for $uri.</font>");
636: }
1.96 albertel 637: }
638: flock(LOCKFILE,LOCK_UN);
639: close(LOCKFILE);
1.85 albertel 640: } else {
1.87 albertel 641: # if we are here it is likely because we are already trying to
642: # initialize the course in another child, busy wait trying to
643: # tie the hashes for the next 90 seconds, if we succeed forward
644: # them on to navmaps, if we fail, throw up the Could not init
645: # course screen
1.96 albertel 646: if ($lock) {
647: # Got the lock but not the DB files
648: flock(LOCKFILE,LOCK_UN);
649: }
1.87 albertel 650: untie(%hash);
651: untie(%parmhash);
1.85 albertel 652: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
653: "Could not tie coursemap $fn for $uri.</font>");
1.87 albertel 654: my $i=0;
655: while($i<90) {
656: $i++;
657: sleep(1);
1.96 albertel 658: if (flock(LOCKFILE,LOCK_EX|LOCK_NB) &&
659: (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640))) {
1.87 albertel 660: if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
661: $retfurl='/adm/navmaps';
662: &Apache::lonnet::appenv("request.course.id" => $short,
663: "request.course.fn" => $fn,
664: "request.course.uri" => $uri);
665: untie(%hash);
666: untie(%parmhash);
667: last;
668: }
669: }
670: untie(%hash);
671: untie(%parmhash);
672: }
1.96 albertel 673: flock(LOCKFILE,LOCK_UN);
674: close(LOCKFILE);
1.1 www 675: }
1.89 albertel 676: &Apache::lonmsg::author_res_msg($env{'request.course.uri'},$errtext);
1.46 www 677: # ------------------------------------------------- Check for critical messages
678:
1.89 albertel 679: my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
680: $env{'user.name'});
1.46 www 681: if ($what[0]) {
682: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
683: $retfurl='/adm/email?critical=display';
684: }
685: }
1.85 albertel 686: return ($retfurl,$errtext);
1.1 www 687: }
1.15 www 688:
689: # ------------------------------------------------------- Evaluate state string
690:
691: sub evalstate {
1.89 albertel 692: my $fn=$env{'request.course.fn'}.'.state';
1.80 albertel 693: my $state='';
1.15 www 694: if (-e $fn) {
1.80 albertel 695: my @conditions=();
696: {
1.115 raeburn 697: open(my $fh,"<$fn");
1.80 albertel 698: @conditions=<$fh>;
1.115 raeburn 699: close($fh);
1.80 albertel 700: }
701: my $safeeval = new Safe;
702: my $safehole = new Safe::Hole;
703: $safeeval->permit("entereval");
704: $safeeval->permit(":base_math");
705: $safeeval->deny(":base_io");
706: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
707: foreach my $line (@conditions) {
708: chomp($line);
709: my ($condition,$weight)=split(/\:/,$line);
710: if ($safeeval->reval($condition)) {
711: if ($weight eq 'force') {
712: $state.='3';
713: } else {
714: $state.='2';
715: }
716: } else {
717: if ($weight eq 'stop') {
718: $state.='0';
719: } else {
720: $state.='1';
721: }
722: }
723: }
1.15 www 724: }
1.89 albertel 725: &Apache::lonnet::appenv('user.state.'.$env{'request.course.id'} => $state);
1.15 www 726: return $state;
727: }
728:
1.1 www 729: 1;
730: __END__
731:
1.26 harris41 732: =head1 NAME
733:
734: Apache::lonuserstate - Construct and maintain state and binary representation
735: of course for user
736:
737: =head1 SYNOPSIS
738:
739: Invoked by lonroles.pm.
740:
741: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
742:
743: =head1 INTRODUCTION
744:
745: This module constructs and maintains state and binary representation
746: of course for user.
747:
748: This is part of the LearningOnline Network with CAPA project
749: described at http://www.lon-capa.org.
750:
751: =head1 HANDLER SUBROUTINE
752:
753: There is no handler subroutine.
754:
755: =head1 OTHER SUBROUTINES
756:
757: =over 4
758:
759: =item *
760:
761: loadmap() : Loads map from disk
762:
763: =item *
764:
765: simplify() : Simplify expression
766:
767: =item *
768:
769: traceroute() : Build condition hash
770:
771: =item *
772:
773: accinit() : Cascading conditions, quick access, parameters
1.1 www 774:
1.26 harris41 775: =item *
1.1 www 776:
1.26 harris41 777: readmap() : Read map and all submaps
1.1 www 778:
1.26 harris41 779: =item *
1.1 www 780:
1.26 harris41 781: evalstate() : Evaluate state string
1.1 www 782:
1.26 harris41 783: =back
1.1 www 784:
1.26 harris41 785: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>