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