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