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