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