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.48 2002/12/12 18:00:24 www 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: # (Server for RAT Maps
29: #
30: # (Edit Handler for RAT Maps
31: # (TeX Content Handler
32: #
33: # YEAR=2000
34: # 05/29/00,05/30 Gerd Kortemeyer)
35: # 7/1 Gerd Kortemeyer)
36: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
37: #
38: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
39: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
40: # 11/1,11/2,11/14,11/16,11/22,12/28,
41: # YEAR=2001
42: # 07/05/01,08/30,08/31 Gerd Kortemeyer
43: # 12/16 Scott Harrison
44: #
45: ###
46:
47: package Apache::lonuserstate;
48:
49: # ------------------------------------------------- modules used by this module
50: use strict;
51: use Apache::Constants qw(:common :http);
52: use Apache::File;
53: use HTML::TokeParser;
54: use Apache::lonnet();
55: use Apache::loncommon();
56: use GDBM_File;
57: use Apache::lonmsg;
58: use Safe;
59: use Safe::Hole;
60: use Opcode;
61:
62: # ---------------------------------------------------- Globals for this package
63:
64: my $pc; # Package counter
65: my %hash; # The big tied hash
66: my %parmhash;# The hash with the parameters
67: my @cond; # Array with all of the conditions
68: my $errtext; # variable with all errors
69: my $retfurl; # variable with the very first URL in the course
70: my %randompick; # randomly picked resources
71: my %actualversion; # version of resource as loaded now
72: my %setversion; # forced version of resource
73: my %lastversion; # version when CC came in last
74: my $versionmode; # how versioning is handled in this course
75:
76: # --------------------------------------------------------- Loads map from disk
77:
78: sub loadmap {
79: my $uri=shift;
80: if ($hash{'map_pc_'.$uri}) { return OK; }
81:
82: $pc++;
83: my $lpc=$pc;
84: $hash{'map_pc_'.$uri}=$lpc;
85: $hash{'map_id_'.$lpc}=$uri;
86:
87: # Determine and check filename
88: my $fn=&Apache::lonnet::filelocation('',$uri);
89:
90: my $ispage=($fn=~/\.page$/);
91:
92: unless (($fn=~/\.sequence$/) ||
93: ($fn=~/\.page$/)) {
94: $errtext.="Invalid map: $fn\n";
95: return OK;
96: }
97:
98: my $instr=&Apache::lonnet::getfile($fn);
99:
100: unless ($instr == -1) {
101:
102: # Successfully got file, parse it
103:
104: my $parser = HTML::TokeParser->new(\$instr);
105: my $token;
106:
107: my $linkpc=0;
108:
109: $fn=~/\.(\w+)$/;
110:
111: $hash{'map_type_'.$lpc}=$1;
112:
113: while ($token = $parser->get_token) {
114: if ($token->[0] eq 'S') {
115: if ($token->[1] eq 'resource') {
116: # -------------------------------------------------------------------- Resource
117:
118: my $rid=$lpc.'.'.$token->[2]->{'id'};
119:
120: $hash{'kind_'.$rid}='res';
121: $hash{'title_'.$rid}=$token->[2]->{'title'};
122: my $turi=$token->[2]->{'src'};
123: $Apache::lonnet::titlecache{
124: &Apache::lonnet::symbclean(
125: &Apache::lonnet::declutter($uri).'___'.
126: $token->[2]->{'id'}.'___'.
127: &Apache::lonnet::declutter($turi))}=
128: $token->[2]->{'title'};
129: unless ($ispage) {
130: $turi=~/\.(\w+)$/;
131: my $embstyle=&Apache::loncommon::fileembstyle($1);
132: if ($token->[2]->{'external'} eq 'true') { # external
133: $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
134: } elsif ($turi=~/^\/*uploaded\//) { # uploaded
135: if (($embstyle eq 'img') || ($embstyle eq 'emb')
136: || ($embstyle eq 'ssi')) {
137: $turi='/adm/wrapper'.$turi;
138: } elsif ($turi!~/\.(sequence|page)$/) {
139: $turi='/adm/coursedocs/showdoc'.$turi;
140: }
141: } else { # normal internal resource
142: if (($embstyle eq 'img') || ($embstyle eq 'emb')
143: || ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/)) {
144: $turi='/adm/wrapper'.$turi;
145: }
146: }
147: }
148: $hash{'src_'.$rid}=$turi;
149:
150: if (defined($hash{'ids_'.$turi})) {
151: $hash{'ids_'.$turi}.=','.$rid;
152: } else {
153: $hash{'ids_'.$turi}=''.$rid;
154: }
155:
156: if ($token->[2]->{'external'} eq 'true') {
157: $hash{'ext_'.$rid}='true:';
158: } else {
159: $hash{'ext_'.$rid}='false:';
160: }
161: if ($token->[2]->{'type'}) {
162: $hash{'type_'.$rid}=$token->[2]->{'type'};
163: if ($token->[2]->{'type'} eq 'start') {
164: $hash{'map_start_'.$uri}="$rid";
165: }
166: if ($token->[2]->{'type'} eq 'finish') {
167: $hash{'map_finish_'.$uri}="$rid";
168: }
169: } else {
170: $hash{'type_'.$rid}='normal';
171: }
172:
173: if (($turi=~/\.sequence$/) ||
174: ($turi=~/\.page$/)) {
175: $hash{'is_map_'.$rid}=1;
176: &loadmap($turi);
177: }
178:
179: } elsif ($token->[1] eq 'condition') {
180: # ------------------------------------------------------------------- Condition
181:
182: my $rid=$lpc.'.'.$token->[2]->{'id'};
183:
184: $hash{'kind_'.$rid}='cond';
185: $cond[$#cond+1]=$token->[2]->{'value'};
186: $hash{'condid_'.$rid}=$#cond;
187: if ($token->[2]->{'type'}) {
188: $cond[$#cond].=':'.$token->[2]->{'type'};
189: } else {
190: $cond[$#cond].=':normal';
191: }
192:
193: } elsif ($token->[1] eq 'link') {
194: # ----------------------------------------------------------------------- Links
195:
196: $linkpc++;
197: my $linkid=$lpc.'.'.$linkpc;
198:
199: my $goesto=$lpc.'.'.$token->[2]->{'to'};
200: my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
201: my $undercond=0;
202:
203: if ($token->[2]->{'condition'}) {
204: $undercond=$lpc.'.'.$token->[2]->{'condition'};
205: }
206:
207: $hash{'goesto_'.$linkid}=$goesto;
208: $hash{'comesfrom_'.$linkid}=$comesfrom;
209: $hash{'undercond_'.$linkid}=$undercond;
210:
211: if (defined($hash{'to_'.$comesfrom})) {
212: $hash{'to_'.$comesfrom}.=','.$linkid;
213: } else {
214: $hash{'to_'.$comesfrom}=''.$linkid;
215: }
216: if (defined($hash{'from_'.$goesto})) {
217: $hash{'from_'.$goesto}.=','.$linkid;
218: } else {
219: $hash{'from_'.$goesto}=''.$linkid;
220: }
221: } elsif ($token->[1] eq 'param') {
222: # ------------------------------------------------------------------- Parameter
223:
224: my $referid=$lpc.'.'.$token->[2]->{'to'};
225: my $part=$token->[2]->{'part'};
226: unless ($part) { $part=0; }
227: my $newparam=
228: &Apache::lonnet::escape($token->[2]->{'type'}).':'.
229: &Apache::lonnet::escape($part.'.'.
230: $token->[2]->{'name'}).'='.
231: &Apache::lonnet::escape($token->[2]->{'value'});
232: if (defined($hash{'param_'.$referid})) {
233: $hash{'param_'.$referid}.='&'.$newparam;
234: } else {
235: $hash{'param_'.$referid}=''.$newparam;
236: }
237: if ($token->[2]->{'name'} eq 'parameter_mapalias') {
238: $hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
239: }
240: if ($token->[2]->{'name'} eq 'parameter_randompick') {
241: $randompick{$referid}=$token->[2]->{'value'};
242: }
243: }
244:
245: }
246: }
247:
248: } else {
249: $errtext.='Map not loaded: The file does not exist. ';
250: }
251: }
252:
253: # --------------------------------------------------------- Simplify expression
254:
255: sub simplify {
256: my $expression=shift;
257: # (8)=8
258: $expression=~s/\((\d+)\)/$1/g;
259: # 8&8=8
260: $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
261: # 8|8=8
262: $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
263: # (5&3)&4=5&3&4
264: $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
265: # (((5&3)|(4&6)))=((5&3)|(4&6))
266: $expression=~
267: s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
268: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
269: $expression=~
270: s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
271: return $expression;
272: }
273:
274: # -------------------------------------------------------- Build condition hash
275:
276: sub traceroute {
277: my ($sofar,$rid,$beenhere)=@_;
278: $sofar=simplify($sofar);
279: unless ($beenhere=~/\&$rid\&/) {
280: $beenhere.=$rid.'&';
281: if (($retfurl eq '') && ($hash{'src_'.$rid})
282: && ($hash{'src_'.$rid}!~/\.sequence$/)) {
283: my ($mapid,$resid)=split(/\./,$rid);
284: $retfurl=$hash{'src_'.$rid}.
285: (($hash{'src_'.$rid}=~/\?/)?'&':'?').'symb='.
286: &Apache::lonnet::symbclean(
287: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
288: '___'.$resid.'___'.
289: &Apache::lonnet::declutter($hash{'src_'.$rid}));
290: }
291: if (defined($hash{'conditions_'.$rid})) {
292: $hash{'conditions_'.$rid}=simplify(
293: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
294: } else {
295: $hash{'conditions_'.$rid}=$sofar;
296: }
297: if (defined($hash{'is_map_'.$rid})) {
298: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
299: &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
300: if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
301: $sofar=
302: $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
303: }
304: }
305: }
306: if (defined($hash{'to_'.$rid})) {
307: foreach (split(/\,/,$hash{'to_'.$rid})) {
308: my $further=$sofar;
309: if ($hash{'undercond_'.$_}) {
310: if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
311: $further=simplify('('.$further.')&('.
312: $hash{'condid_'.$hash{'undercond_'.$_}}.')');
313: } else {
314: $errtext.='Undefined condition ID: '
315: .$hash{'undercond_'.$_}.'. ';
316: }
317: }
318: &traceroute($further,$hash{'goesto_'.$_},$beenhere);
319: }
320: }
321: }
322: }
323:
324: # ------------------------------ Cascading conditions, quick access, parameters
325:
326: sub accinit {
327: my ($uri,$short,$fn)=@_;
328: my %acchash=();
329: my %captured=();
330: my $condcounter=0;
331: $acchash{'acc.cond.'.$short.'.0'}=0;
332: foreach (keys %hash) {
333: if ($_=~/^conditions/) {
334: my $expr=$hash{$_};
335: foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
336: my $sub=$_;
337: my $orig=$_;
338: $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
339: my $factor=$1;
340: $sub=~s/$factor//g;
341: $sub=~s/^\(/\($factor\(/;
342: $sub.=')';
343: $sub=simplify($sub);
344: $orig=~s/(\W)/\\$1/g;
345: $expr=~s/$orig/$sub/;
346: }
347: $hash{$_}=$expr;
348: unless (defined($captured{$expr})) {
349: $condcounter++;
350: $captured{$expr}=$condcounter;
351: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
352: }
353: } elsif ($_=~/^param_(\d+)\.(\d+)/) {
354: my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
355: '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
356: foreach (split(/\&/,$hash{$_})) {
357: my ($typename,$value)=split(/\=/,$_);
358: my ($type,$name)=split(/\:/,$typename);
359: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
360: &Apache::lonnet::unescape($value);
361: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
362: &Apache::lonnet::unescape($type);
363: }
364: }
365: }
366: foreach (keys %hash) {
367: if ($_=~/^ids/) {
368: foreach (split(/\,/,$hash{$_})) {
369: my $resid=$_;
370: my $uri=$hash{'src_'.$resid};
371: $uri=~s/^\/adm\/wrapper//;
372: my @uriparts=split(/\//,$uri);
373: my $urifile=$uriparts[$#uriparts];
374: $#uriparts--;
375: my $uripath=join('/',@uriparts);
376: $uripath=~s/^\/res\///;
377: if ($uripath) {
378: my $uricond='0';
379: if (defined($hash{'conditions_'.$resid})) {
380: $uricond=$captured{$hash{'conditions_'.$resid}};
381: }
382: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
383: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
384: /(\&$urifile\:[^\&]*)/) {
385: my $replace=$1;
386: my $regexp=$replace;
387: $regexp=~s/\|/\\\|/g;
388: $acchash{'acc.res.'.$short.'.'.$uripath}
389: =~s/$regexp/$replace\|$uricond/;
390: } else {
391: $acchash{'acc.res.'.$short.'.'.$uripath}.=
392: $urifile.':'.$uricond.'&';
393: }
394: } else {
395: $acchash{'acc.res.'.$short.'.'.$uripath}=
396: '&'.$urifile.':'.$uricond.'&';
397: }
398: }
399: }
400: }
401: }
402: $acchash{'acc.res.'.$short.'.'}='&:0&';
403: my $courseuri=$uri;
404: $courseuri=~s/^\/res\///;
405: &Apache::lonnet::delenv('(acc\.|httpref\.)');
406: &Apache::lonnet::appenv(%acchash,
407: "request.course.id" => $short,
408: "request.course.fn" => $fn,
409: "request.course.uri" => $courseuri);
410: }
411:
412: # ------------------------------------- Selectively delete from randompick maps
413:
414: sub pickrandom {
415: my $randomoutentry='';
416: foreach my $rid (keys %randompick) {
417: my $rndpick=$randompick{$rid};
418: my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
419: # ------------------------------------------- put existing resources into array
420: my @currentrids=();
421: foreach (keys %hash) {
422: if ($_=~/^src_($mpc\.\d+)/) {
423: if ($hash{'src_'.$1}) { push @currentrids, $1; }
424: }
425: }
426: next if ($#currentrids<$rndpick);
427: # -------------------------------- randomly eliminate the ones that should stay
428: srand(&Apache::lonnet::rndseed($rid)); # use rid instead of symb
429: for (my $i=1;$i<=$rndpick;$i++) {
430: while (1) {
431: my $randomidx=int(rand($#currentrids+1));
432: if ($currentrids[$randomidx]) {
433: $currentrids[$randomidx]='';
434: last;
435: }
436: }
437: }
438: # -------------------------------------------------------- delete the leftovers
439: for (my $k=0; $k<=$#currentrids; $k++) {
440: if ($currentrids[$k]) {
441: $hash{'randomout_'.$currentrids[$k]}=1;
442: my ($mapid,$resid)=split(/\./,$currentrids[$k]);
443: $randomoutentry.='&'.
444: &Apache::lonnet::symbclean(
445: &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
446: '___'.$resid.'___'.
447: &Apache::lonnet::declutter($hash{'src_'.$currentrids[$k]})
448: ).'&';
449: }
450: }
451: }
452: if ($randomoutentry) {
453: &Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
454: }
455: }
456:
457: # ---------------------------------------------------- Read map and all submaps
458:
459: sub readmap {
460: my $short=shift;
461: $short=~s/^\///;
462: my %cenv=&Apache::lonnet::coursedescription($short);
463: my $fn=$cenv{'fn'};
464: my $uri;
465: $short=~s/\//\_/g;
466: unless ($uri=$cenv{'url'}) {
467: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
468: "Could not load course $short.</font>");
469: return 'No course data available.';
470: }
471: @cond=('true:normal');
472: unlink($fn.'.db');
473: unlink($fn.'_symb.db');
474: unlink($fn.'.state');
475: unlink($fn.'parms.db');
476: undef %randompick;
477: $retfurl='';
478: if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
479: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
480: %hash=();
481: %parmhash=();
482: $errtext='';
483: $pc=0;
484: my $furi=&Apache::lonnet::clutter($uri);
485: $hash{'src_0.0'}=$furi;
486: $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
487: $hash{'ids_'.$furi}='0.0';
488: $hash{'is_map_0.0'}=1;
489: loadmap($uri);
490: if (defined($hash{'map_start_'.$uri})) {
491: &traceroute('0',$hash{'map_start_'.$uri},'&');
492: &accinit($uri,$short,$fn);
493: &pickrandom();
494: }
495: # ------------------------------------------------------------ Version tracking
496: # if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
497: # &Apache::lonnet::logthis('Will be version tracking');
498: # }
499: unless ((untie(%hash)) && (untie(%parmhash))) {
500: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
501: "Could not untie coursemap $fn for $uri.</font>");
502: }
503: {
504: my $cfh;
505: if ($cfh=Apache::File->new(">$fn.state")) {
506: print $cfh join("\n",@cond);
507: } else {
508: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
509: "Could not write statemap $fn for $uri.</font>");
510: }
511: }
512: } else {
513: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
514: "Could not tie coursemap $fn for $uri.</font>");
515: }
516: &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
517: # ------------------------------------------------- Check for critical messages
518:
519: my @what=&Apache::lonnet::dump('critical',$ENV{'user.domain'},
520: $ENV{'user.name'});
521: if ($what[0]) {
522: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
523: $retfurl='/adm/email?critical=display';
524: }
525: }
526: return ($retfurl,$errtext);
527: }
528:
529: # ------------------------------------------------------- Evaluate state string
530:
531: sub evalstate {
532:
533: my $fn=$ENV{'request.course.fn'}.'.state';
534: my $state='2';
535: if (-e $fn) {
536: my @conditions=();
537: {
538: my $fh=Apache::File->new($fn);
539: @conditions=<$fh>;
540: }
541: my $safeeval = new Safe;
542: my $safehole = new Safe::Hole;
543: $safeeval->permit("entereval");
544: $safeeval->permit(":base_math");
545: $safeeval->deny(":base_io");
546: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
547: foreach (@conditions) {
548: my $line=$_;
549: chomp($line);
550: my ($condition,$weight)=split(/\:/,$_);
551: if ($safeeval->reval($condition)) {
552: if ($weight eq 'force') {
553: $state.='3';
554: } else {
555: $state.='2';
556: }
557: } else {
558: if ($weight eq 'stop') {
559: $state.='0';
560: } else {
561: $state.='1';
562: }
563: }
564: }
565: }
566: &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
567: return $state;
568: }
569:
570: 1;
571: __END__
572:
573: =head1 NAME
574:
575: Apache::lonuserstate - Construct and maintain state and binary representation
576: of course for user
577:
578: =head1 SYNOPSIS
579:
580: Invoked by lonroles.pm.
581:
582: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
583:
584: =head1 INTRODUCTION
585:
586: This module constructs and maintains state and binary representation
587: of course for user.
588:
589: This is part of the LearningOnline Network with CAPA project
590: described at http://www.lon-capa.org.
591:
592: =head1 HANDLER SUBROUTINE
593:
594: There is no handler subroutine.
595:
596: =head1 OTHER SUBROUTINES
597:
598: =over 4
599:
600: =item *
601:
602: loadmap() : Loads map from disk
603:
604: =item *
605:
606: simplify() : Simplify expression
607:
608: =item *
609:
610: traceroute() : Build condition hash
611:
612: =item *
613:
614: accinit() : Cascading conditions, quick access, parameters
615:
616: =item *
617:
618: readmap() : Read map and all submaps
619:
620: =item *
621:
622: evalstate() : Evaluate state string
623:
624: =back
625:
626: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>