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