Annotation of rat/lonuserstate.pm, revision 1.19
1.1 www 1: # The LearningOnline Network with CAPA
2: # Construct and maintain state and binary representation of course for user
3: #
4: # (Server for RAT Maps
5: #
6: # (Edit Handler for RAT Maps
7: # (TeX Content Handler
8: #
9: # 05/29/00,05/30 Gerd Kortemeyer)
10: # 7/1 Gerd Kortemeyer)
11: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
12: #
1.9 www 13: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
1.19 ! www 14: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
! 15: # 11/1,11/2,11/14,11/16 Gerd Kortemeyer
1.1 www 16:
17: package Apache::lonuserstate;
18:
19: use strict;
20: use Apache::Constants qw(:common :http);
21: use Apache::File;
22: use HTML::TokeParser;
23: use Apache::lonnet();
24: use GDBM_File;
1.12 www 25: use Apache::lonmsg;
1.15 www 26: use Safe;
27: use Opcode;
28:
1.1 www 29: # ---------------------------------------------------- Globals for this package
30:
31: my $pc; # Package counter
32: my %hash; # The big tied hash
1.19 ! www 33: my %parmhash;# The hash with the parameters
1.1 www 34: my @cond; # Array with all of the conditions
35: my $errtext; # variable with all errors
36:
37: # --------------------------------------------------------- Loads map from disk
38:
39: sub loadmap {
40: my $uri=shift;
41: if ($hash{'map_pc_'.$uri}) { return OK; }
42:
43: $pc++;
44: my $lpc=$pc;
45: $hash{'map_pc_'.$uri}=$lpc;
46: $hash{'map_id_'.$lpc}=$uri;
47:
48: my $fn='/home/httpd/html'.$uri;
49:
1.10 www 50: unless (($fn=~/\.sequence$/) ||
1.1 www 51: ($fn=~/\.page$/)) {
52: $errtext.="Invalid map: $fn\n";
53: return OK;
54: }
55:
56: unless (-e $fn) {
57: my $returned=Apache::lonnet::repcopy($fn);
58: unless ($returned eq OK) {
59: $errtext.="Could not import: $fn - ";
60: if ($returned eq HTTP_SERVICE_UNAVAILABLE) {
61: $errtext.="Server unavailable\n";
62: }
63: if ($returned eq HTTP_NOT_FOUND) {
64: $errtext.="File not found\n";
65: }
66: if ($returned eq FORBIDDEN) {
67: $errtext.="Access forbidden\n";
68: }
69: return OK;
70: }
71: }
72:
73: if (-e $fn) {
74: my @content;
75: {
76: my $fh=Apache::File->new($fn);
77: @content=<$fh>;
78: }
79: my $instr=join('',@content);
80: my $parser = HTML::TokeParser->new(\$instr);
81: my $token;
82:
83: my $linkpc=0;
84:
85: $fn=~/\.(\w+)$/;
86:
87: $hash{'map_type_'.$lpc}=$1;
88:
89: while ($token = $parser->get_token) {
90: if ($token->[0] eq 'S') {
91: if ($token->[1] eq 'resource') {
92: # -------------------------------------------------------------------- Resource
93:
94: my $rid=$lpc.'.'.$token->[2]->{'id'};
95:
96: $hash{'kind_'.$rid}='res';
97: $hash{'title_'.$rid}=$token->[2]->{'title'};
98: my $turi=$token->[2]->{'src'};
99: $hash{'src_'.$rid}=$turi;
100:
101: if (defined($hash{'ids_'.$turi})) {
102: $hash{'ids_'.$turi}.=','.$rid;
103: } else {
104: $hash{'ids_'.$turi}=''.$rid;
105: }
106:
107: if ($token->[2]->{'src'}=~/\/\//) {
108: $hash{'ext_'.$rid}='true:';
109: } else {
110: $hash{'ext_'.$rid}='false:';
111: }
112: if ($token->[2]->{'type'}) {
113: $hash{'type_'.$rid}=$token->[2]->{'type'};
1.2 www 114: if ($token->[2]->{'type'} eq 'start') {
115: $hash{'map_start_'.$uri}="$rid";
116: }
117: if ($token->[2]->{'type'} eq 'finish') {
118: $hash{'map_finish_'.$uri}="$rid";
119: }
1.1 www 120: } else {
121: $hash{'type_'.$rid}='normal';
122: }
123:
1.10 www 124: if (($turi=~/\.sequence$/) ||
1.1 www 125: ($turi=~/\.page$/)) {
1.2 www 126: $hash{'is_map_'.$rid}=1;
1.1 www 127: &loadmap($turi);
128: }
129:
130: } elsif ($token->[1] eq 'condition') {
131: # ------------------------------------------------------------------- Condition
132:
133: my $rid=$lpc.'.'.$token->[2]->{'id'};
134:
135: $hash{'kind_'.$rid}='cond';
1.2 www 136: $cond[$#cond+1]=$token->[2]->{'value'};
137: $hash{'condid_'.$rid}=$#cond;
1.1 www 138: if ($token->[2]->{'type'}) {
1.2 www 139: $cond[$#cond].=':'.$token->[2]->{'type'};
1.1 www 140: } else {
1.2 www 141: $cond[$#cond].=':normal';
1.1 www 142: }
143:
144: } elsif ($token->[1] eq 'link') {
145: # ----------------------------------------------------------------------- Links
146:
147: $linkpc++;
148: my $linkid=$lpc.'.'.$linkpc;
149:
150: my $goesto=$lpc.'.'.$token->[2]->{'to'};
151: my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
152: my $undercond=0;
153:
154: if ($token->[2]->{'condition'}) {
155: $undercond=$lpc.'.'.$token->[2]->{'condition'};
156: }
157:
158: $hash{'goesto_'.$linkid}=$goesto;
159: $hash{'comesfrom_'.$linkid}=$comesfrom;
160: $hash{'undercond_'.$linkid}=$undercond;
161:
162: if (defined($hash{'to_'.$comesfrom})) {
163: $hash{'to_'.$comesfrom}.=','.$linkid;
164: } else {
165: $hash{'to_'.$comesfrom}=''.$linkid;
166: }
167: if (defined($hash{'from_'.$goesto})) {
168: $hash{'from_'.$goesto}.=','.$linkid;
169: } else {
170: $hash{'from_'.$goesto}=''.$linkid;
171: }
1.18 www 172: } elsif ($token->[1] eq 'param') {
173: # ------------------------------------------------------------------- Parameter
174:
175: my $referid=$lpc.'.'.$token->[2]->{'to'};
176: my $newparam=
177: &Apache::lonnet::escape($token->[2]->{'type'}).':'.
178: &Apache::lonnet::escape($token->[2]->{'name'}).'='.
179: &Apache::lonnet::escape($token->[2]->{'value'});
180: if (defined($hash{'param_'.$referid})) {
181: $hash{'param_'.$referid}.='&'.$newparam;
182: } else {
183: $hash{'param_'.$referid}=''.$newparam;
184: }
185:
1.1 www 186: }
187:
188: }
189: }
190:
191: } else {
192: $errtext.='Map not loaded: The file does not exist. ';
193: }
194: }
195:
1.3 www 196: # --------------------------------------------------------- Simplify expression
197:
198: sub simplify {
199: my $expression=shift;
200: # (8)=8
201: $expression=~s/\((\d+)\)/$1/g;
202: # 8&8=8
1.7 www 203: $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
1.3 www 204: # 8|8=8
1.7 www 205: $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
1.3 www 206: # (5&3)&4=5&3&4
1.7 www 207: $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
1.3 www 208: # (((5&3)|(4&6)))=((5&3)|(4&6))
209: $expression=~
210: s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
211: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
212: $expression=~
213: s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
214: return $expression;
215: }
216:
1.2 www 217: # -------------------------------------------------------- Build condition hash
218:
219: sub traceroute {
1.3 www 220: my ($sofar,$rid,$beenhere)=@_;
221: $sofar=simplify($sofar);
1.2 www 222: unless ($beenhere=~/\&$rid\&/) {
223: $beenhere.=$rid.'&';
224: if (defined($hash{'conditions_'.$rid})) {
1.3 www 225: $hash{'conditions_'.$rid}=simplify(
226: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.2 www 227: } else {
228: $hash{'conditions_'.$rid}=$sofar;
229: }
230: if (defined($hash{'is_map_'.$rid})) {
1.3 www 231: if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
232: &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
233: if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
234: $sofar=
235: $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
236: }
1.2 www 237: }
238: }
239: if (defined($hash{'to_'.$rid})) {
240: map {
241: my $further=$sofar;
242: if ($hash{'undercond_'.$_}) {
243: if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
1.3 www 244: $further=simplify('('.$further.')&('.
245: $hash{'condid_'.$hash{'undercond_'.$_}}.')');
1.2 www 246: } else {
247: $errtext.='Undefined condition ID: '
248: .$hash{'undercond_'.$_}.'. ';
249: }
250: }
251: &traceroute($further,$hash{'goesto_'.$_},$beenhere);
252: } split(/\,/,$hash{'to_'.$rid});
253: }
254: }
255: }
1.1 www 256:
1.19 ! www 257: # ------------------------------ Cascading conditions, quick access, parameters
1.4 www 258:
259: sub accinit {
260: my ($uri,$short,$fn)=@_;
261: my %acchash=();
262: my %captured=();
263: my $condcounter=0;
1.5 www 264: $acchash{'acc.cond.'.$short.'.0'}=0;
1.4 www 265: map {
266: if ($_=~/^conditions/) {
267: my $expr=$hash{$_};
268: map {
269: my $sub=$_;
270: my $orig=$_;
1.13 www 271: $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
1.4 www 272: my $factor=$1;
1.7 www 273: $sub=~s/$factor//g;
274: $sub=~s/^\(/\($factor\(/;
1.4 www 275: $sub.=')';
276: $sub=simplify($sub);
277: $orig=~s/(\W)/\\$1/g;
1.7 www 278: $expr=~s/$orig/$sub/;
1.4 www 279: } ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g);
280: $hash{$_}=$expr;
281: unless (defined($captured{$expr})) {
282: $condcounter++;
283: $captured{$expr}=$condcounter;
1.5 www 284: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
1.4 www 285: }
1.19 ! www 286: } elsif ($_=~/^param_(\d+)\.(\d+)/) {
! 287: my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
! 288: '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
! 289: map {
! 290: my ($typename,$value)=split(/\=/,$_);
! 291: my ($type,$name)=split(/\:/,$typename);
! 292: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
! 293: &Apache::lonnet::unescape($value);
! 294: $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
! 295: &Apache::lonnet::unescape($type);
! 296: } split(/\&/,$hash{$_});
! 297: }
1.4 www 298: } keys %hash;
299: map {
300: if ($_=~/^ids/) {
1.13 www 301: map {
302: my $resid=$_;
1.4 www 303: my $uri=$hash{'src_'.$resid};
304: my @uriparts=split(/\//,$uri);
305: my $urifile=$uriparts[$#uriparts];
306: $#uriparts--;
307: my $uripath=join('/',@uriparts);
1.8 www 308: $uripath=~s/^\/res\///;
1.13 www 309: my $uricond='0';
1.4 www 310: if (defined($hash{'conditions_'.$resid})) {
1.13 www 311: $uricond=$captured{$hash{'conditions_'.$resid}};
1.4 www 312: }
1.5 www 313: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
1.13 www 314: if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
315: /(\&$urifile\:[^\&]*)/) {
316: my $replace=$1;
317: $acchash{'acc.res.'.$short.'.'.$uripath}
318: =~s/$replace/$replace\|$uricond/;
319: } else {
320: $acchash{'acc.res.'.$short.'.'.$uripath}.=
321: $urifile.':'.$uricond.'&';
322: }
1.4 www 323: } else {
1.13 www 324: $acchash{'acc.res.'.$short.'.'.$uripath}=
325: '&'.$urifile.':'.$uricond.'&';
326: }
327: } split(/\,/,$hash{$_});
328: }
1.4 www 329: } keys %hash;
1.8 www 330: my $courseuri=$uri;
331: $courseuri=~s/^\/res\///;
1.19 ! www 332: &Apache::lonnet::delenv('(acc\.|httpref\.)');
1.4 www 333: &Apache::lonnet::appenv(%acchash,
1.9 www 334: "request.course.id" => $short,
1.8 www 335: "request.course.fn" => $fn,
336: "request.course.uri" => $courseuri);
1.4 www 337: }
338:
1.1 www 339: # ---------------------------------------------------- Read map and all submaps
340:
341: sub readmap {
1.9 www 342: my $short=shift;
343: $short=~s/^\///;
344: my %cenv=&Apache::lonnet::coursedescription($short);
345: my $fn=$cenv{'fn'};
346: my $uri;
347: $short=~s/\//\_/g;
348: unless ($uri=$cenv{'url'}) {
349: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
350: "Could not load course $short.</font>");
351: return 'No course data available.';
352: }
1.3 www 353: @cond=('true:normal');
1.11 www 354: unlink($fn.'.db');
355: unlink($fn.'_symb.db');
356: unlink($fn.'.state');
1.19 ! www 357: unlink($fn.'parms.db');
! 358: if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) &&
! 359: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT,0640))) {
1.4 www 360: %hash=();
1.19 ! www 361: %parmhash=();
1.4 www 362: $errtext='';
363: $pc=0;
364: loadmap($uri);
365: if (defined($hash{'map_start_'.$uri})) {
366: &traceroute('0',$hash{'map_start_'.$uri},'&');
367: &accinit($uri,$short,$fn);
1.2 www 368: }
1.19 ! www 369: unless ((untie(%hash)) && (untie(%parmhash))) {
1.4 www 370: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
371: "Could not untie coursemap $fn for $uri.</font>");
1.1 www 372: }
1.4 www 373: {
374: my $cfh;
375: if ($cfh=Apache::File->new(">$fn.state")) {
376: print $cfh join("\n",@cond);
377: } else {
1.6 www 378: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4 www 379: "Could not write statemap $fn for $uri.</font>");
380: }
381: }
382: } else {
1.6 www 383: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4 www 384: "Could not tie coursemap $fn for $uri.</font>");
385: }
1.12 www 386: &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
1.4 www 387: return $errtext;
1.1 www 388: }
1.15 www 389:
390: # ------------------------------------------------------- Evaluate state string
391:
392: sub evalstate {
393: my $safeeval = new Safe;
394: my $fn=$ENV{'request.course.fn'}.'.state';
395: my $state='2';
396: if (-e $fn) {
397: my @conditions=();
398: {
399: my $fh=Apache::File->new($fn);
400: @conditions=<$fh>;
401: }
402: $safeeval->permit("entereval");
403: $safeeval->permit(":base_math");
404: $safeeval->deny(":base_io");
1.17 www 405: $safeeval->share_from('Apache::lonnet',['&EXT']);
1.15 www 406: map {
407: my $line=$_;
408: chomp($line);
409: my ($condition,$weight)=split(/\:/,$_);
410: if ($safeeval->reval($condition)) {
411: if ($weight eq 'force') {
412: $state.='3';
413: } else {
414: $state.='2';
415: }
416: } else {
417: if ($weight eq 'stop') {
418: $state.='0';
419: } else {
420: $state.='1';
421: }
422: }
423: } @conditions;
424: }
425: &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
426: return $state;
427: }
428:
1.1 www 429: 1;
430: __END__
431:
432:
433:
434:
435:
436:
437:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>