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: #
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,10/2,10/11,10/30,10/31,
15: # 11/1,11/2,11/14,11/16,11/22,12/28 Gerd Kortemeyer
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;
25: use Apache::lonmsg;
26: use Safe;
27: use Safe::Hole;
28: use Opcode;
29:
30: # ---------------------------------------------------- Globals for this package
31:
32: my $pc; # Package counter
33: my %hash; # The big tied hash
34: my %parmhash;# The hash with the parameters
35: my @cond; # Array with all of the conditions
36: my $errtext; # variable with all errors
37: my $retfurl; # variable with the very first URL in the course
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:
52: unless (($fn=~/\.sequence$/) ||
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'};
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: }
122: } else {
123: $hash{'type_'.$rid}='normal';
124: }
125:
126: if (($turi=~/\.sequence$/) ||
127: ($turi=~/\.page$/)) {
128: $hash{'is_map_'.$rid}=1;
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';
138: $cond[$#cond+1]=$token->[2]->{'value'};
139: $hash{'condid_'.$rid}=$#cond;
140: if ($token->[2]->{'type'}) {
141: $cond[$#cond].=':'.$token->[2]->{'type'};
142: } else {
143: $cond[$#cond].=':normal';
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: }
174: } elsif ($token->[1] eq 'param') {
175: # ------------------------------------------------------------------- Parameter
176:
177: my $referid=$lpc.'.'.$token->[2]->{'to'};
178: my $part=$token->[2]->{'part'};
179: unless ($part) { $part=0; }
180: my $newparam=
181: &Apache::lonnet::escape($token->[2]->{'type'}).':'.
182: &Apache::lonnet::escape($part.'.'.
183: $token->[2]->{'name'}).'='.
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:
191: }
192:
193: }
194: }
195:
196: } else {
197: $errtext.='Map not loaded: The file does not exist. ';
198: }
199: }
200:
201: # --------------------------------------------------------- Simplify expression
202:
203: sub simplify {
204: my $expression=shift;
205: # (8)=8
206: $expression=~s/\((\d+)\)/$1/g;
207: # 8&8=8
208: $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
209: # 8|8=8
210: $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
211: # (5&3)&4=5&3&4
212: $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
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:
222: # -------------------------------------------------------- Build condition hash
223:
224: sub traceroute {
225: my ($sofar,$rid,$beenhere)=@_;
226: $sofar=simplify($sofar);
227: unless ($beenhere=~/\&$rid\&/) {
228: $beenhere.=$rid.'&';
229: if ($retfurl eq '') {
230: $retfurl=$hash{'src_'.$rid};
231: }
232: if (defined($hash{'conditions_'.$rid})) {
233: $hash{'conditions_'.$rid}=simplify(
234: '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
235: } else {
236: $hash{'conditions_'.$rid}=$sofar;
237: }
238: if (defined($hash{'is_map_'.$rid})) {
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: }
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_'.$_}})) {
252: $further=simplify('('.$further.')&('.
253: $hash{'condid_'.$hash{'undercond_'.$_}}.')');
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: }
264:
265: # ------------------------------ Cascading conditions, quick access, parameters
266:
267: sub accinit {
268: my ($uri,$short,$fn)=@_;
269: my %acchash=();
270: my %captured=();
271: my $condcounter=0;
272: $acchash{'acc.cond.'.$short.'.0'}=0;
273: map {
274: if ($_=~/^conditions/) {
275: my $expr=$hash{$_};
276: map {
277: my $sub=$_;
278: my $orig=$_;
279: $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
280: my $factor=$1;
281: $sub=~s/$factor//g;
282: $sub=~s/^\(/\($factor\(/;
283: $sub.=')';
284: $sub=simplify($sub);
285: $orig=~s/(\W)/\\$1/g;
286: $expr=~s/$orig/$sub/;
287: } ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g);
288: $hash{$_}=$expr;
289: unless (defined($captured{$expr})) {
290: $condcounter++;
291: $captured{$expr}=$condcounter;
292: $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
293: }
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: }
306: } keys %hash;
307: map {
308: if ($_=~/^ids/) {
309: map {
310: my $resid=$_;
311: my $uri=$hash{'src_'.$resid};
312: my @uriparts=split(/\//,$uri);
313: my $urifile=$uriparts[$#uriparts];
314: $#uriparts--;
315: my $uripath=join('/',@uriparts);
316: $uripath=~s/^\/res\///;
317: my $uricond='0';
318: if (defined($hash{'conditions_'.$resid})) {
319: $uricond=$captured{$hash{'conditions_'.$resid}};
320: }
321: if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
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: }
331: } else {
332: $acchash{'acc.res.'.$short.'.'.$uripath}=
333: '&'.$urifile.':'.$uricond.'&';
334: }
335: } split(/\,/,$hash{$_});
336: }
337: } keys %hash;
338: my $courseuri=$uri;
339: $courseuri=~s/^\/res\///;
340: &Apache::lonnet::delenv('(acc\.|httpref\.)');
341: &Apache::lonnet::appenv(%acchash,
342: "request.course.id" => $short,
343: "request.course.fn" => $fn,
344: "request.course.uri" => $courseuri);
345: }
346:
347: # ---------------------------------------------------- Read map and all submaps
348:
349: sub readmap {
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: }
361: @cond=('true:normal');
362: unlink($fn.'.db');
363: unlink($fn.'_symb.db');
364: unlink($fn.'.state');
365: unlink($fn.'parms.db');
366: $retfurl='';
367: if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) &&
368: (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT,0640))) {
369: %hash=();
370: %parmhash=();
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);
377: }
378: unless ((untie(%hash)) && (untie(%parmhash))) {
379: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
380: "Could not untie coursemap $fn for $uri.</font>");
381: }
382: {
383: my $cfh;
384: if ($cfh=Apache::File->new(">$fn.state")) {
385: print $cfh join("\n",@cond);
386: } else {
387: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
388: "Could not write statemap $fn for $uri.</font>");
389: }
390: }
391: } else {
392: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
393: "Could not tie coursemap $fn for $uri.</font>");
394: }
395: &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
396: return ($retfurl,$errtext);
397: }
398:
399: # ------------------------------------------------------- Evaluate state string
400:
401: sub evalstate {
402:
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: }
411: my $safeeval = new Safe;
412: my $safehole = new Safe::Hole;
413: $safeeval->permit("entereval");
414: $safeeval->permit(":base_math");
415: $safeeval->deny(":base_io");
416: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
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:
440: 1;
441: __END__
442:
443:
444:
445:
446:
447:
448:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>