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