Annotation of rat/lonuserstate.pm, revision 1.51

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Construct and maintain state and binary representation of course for user
                      3: #
1.51    ! www         4: # $Id: lonuserstate.pm,v 1.50 2003/02/21 23:01:48 albertel Exp $
1.25      www         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: #
1.26      harris41   33: # YEAR=2000
1.1       www        34: # 05/29/00,05/30 Gerd Kortemeyer)
                     35: # 7/1 Gerd Kortemeyer)
                     36: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
                     37: #
1.9       www        38: # 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        39: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
1.22      www        40: # 11/1,11/2,11/14,11/16,11/22,12/28,
1.26      harris41   41: # YEAR=2001
1.24      www        42: # 07/05/01,08/30,08/31 Gerd Kortemeyer
1.26      harris41   43: #
                     44: ###
1.1       www        45: 
                     46: package Apache::lonuserstate;
                     47: 
1.26      harris41   48: # ------------------------------------------------- modules used by this module
1.1       www        49: use strict;
                     50: use Apache::Constants qw(:common :http);
                     51: use Apache::File;
                     52: use HTML::TokeParser;
                     53: use Apache::lonnet();
1.26      harris41   54: use Apache::loncommon();
1.1       www        55: use GDBM_File;
1.12      www        56: use Apache::lonmsg;
1.15      www        57: use Safe;
1.21      www        58: use Safe::Hole;
1.15      www        59: use Opcode;
                     60: 
1.1       www        61: # ---------------------------------------------------- Globals for this package
                     62: 
                     63: my $pc;      # Package counter
                     64: my %hash;    # The big tied hash
1.19      www        65: my %parmhash;# The hash with the parameters
1.1       www        66: my @cond;    # Array with all of the conditions
                     67: my $errtext; # variable with all errors
1.21      www        68: my $retfurl; # variable with the very first URL in the course
1.29      www        69: my %randompick; # randomly picked resources
1.51    ! www        70: my %randompickseed; # optional seed for randomly picking resources
1.45      www        71: my %actualversion; # version of resource as loaded now
                     72: my %setversion; # forced version of resource
                     73: my %lastversion; # version when CC came in last
                     74: my $versionmode; # how versioning is handled in this course
                     75: 
1.1       www        76: # --------------------------------------------------------- Loads map from disk
                     77: 
                     78: sub loadmap { 
                     79:     my $uri=shift;
                     80:     if ($hash{'map_pc_'.$uri}) { return OK; }
                     81: 
                     82:     $pc++;
                     83:     my $lpc=$pc;
                     84:     $hash{'map_pc_'.$uri}=$lpc;
                     85:     $hash{'map_id_'.$lpc}=$uri;
                     86: 
1.37      www        87: # Determine and check filename
                     88:     my $fn=&Apache::lonnet::filelocation('',$uri);
                     89: 
                     90:     my $ispage=($fn=~/\.page$/);
1.1       www        91: 
1.10      www        92:     unless (($fn=~/\.sequence$/) ||
1.1       www        93:             ($fn=~/\.page$/)) { 
                     94:        $errtext.="Invalid map: $fn\n";
                     95:        return OK; 
                     96:     }
                     97: 
1.37      www        98:     my $instr=&Apache::lonnet::getfile($fn);
                     99: 
                    100:     unless ($instr == -1) {
1.22      www       101: 
1.37      www       102: # Successfully got file, parse it
1.1       www       103: 
                    104:         my $parser = HTML::TokeParser->new(\$instr);
                    105:         my $token;
                    106: 
                    107:         my $linkpc=0;
                    108: 
                    109:         $fn=~/\.(\w+)$/;
                    110: 
                    111:         $hash{'map_type_'.$lpc}=$1;
                    112: 
                    113:         while ($token = $parser->get_token) {
                    114: 	    if ($token->[0] eq 'S') {
                    115:                 if ($token->[1] eq 'resource') {
                    116: # -------------------------------------------------------------------- Resource
                    117: 
                    118:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
                    119: 
                    120:                     $hash{'kind_'.$rid}='res';
                    121:                     $hash{'title_'.$rid}=$token->[2]->{'title'};
                    122:                     my $turi=$token->[2]->{'src'};
1.47      www       123:                     $Apache::lonnet::titlecache{
                    124: 		     &Apache::lonnet::symbclean(
                    125:                       &Apache::lonnet::declutter($uri).'___'.
                    126:                       $token->[2]->{'id'}.'___'.
                    127: 		      &Apache::lonnet::declutter($turi))}=
                    128:                           $token->[2]->{'title'};
1.22      www       129:                     unless ($ispage) {
                    130:                         $turi=~/\.(\w+)$/;
1.26      harris41  131:                         my $embstyle=&Apache::loncommon::fileembstyle($1);
1.40      www       132:                         if ($token->[2]->{'external'} eq 'true') { # external
1.22      www       133:                             $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
1.40      www       134:                         } elsif ($turi=~/^\/*uploaded\//) { # uploaded
                    135: 			    if (($embstyle eq 'img') || ($embstyle eq 'emb')
                    136:                              || ($embstyle eq 'ssi')) {
                    137:                                 $turi='/adm/wrapper'.$turi;
1.41      www       138:                             } elsif ($turi!~/\.(sequence|page)$/) {
1.42      www       139: 				$turi='/adm/coursedocs/showdoc'.$turi;
1.40      www       140:                             }
                    141:                         } else { # normal internal resource
1.39      www       142:                            if (($embstyle eq 'img') || ($embstyle eq 'emb')
1.43      www       143:              || ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/)) {
1.22      www       144: 			       $turi='/adm/wrapper'.$turi;
                    145:                            }
                    146:                         }
                    147: 		    }
1.1       www       148:                     $hash{'src_'.$rid}=$turi;
                    149: 
                    150:                     if (defined($hash{'ids_'.$turi})) {
                    151:                         $hash{'ids_'.$turi}.=','.$rid;
                    152:                     } else {
                    153:                         $hash{'ids_'.$turi}=''.$rid;
                    154:                     }
                    155: 
1.22      www       156:                     if ($token->[2]->{'external'} eq 'true') {
1.1       www       157:                         $hash{'ext_'.$rid}='true:';
                    158:                     } else {
                    159:                         $hash{'ext_'.$rid}='false:';
                    160:                     }
                    161:                     if ($token->[2]->{'type'}) {
                    162: 			$hash{'type_'.$rid}=$token->[2]->{'type'};
1.2       www       163:                         if ($token->[2]->{'type'} eq 'start') {
                    164: 			    $hash{'map_start_'.$uri}="$rid";
                    165:                         }
                    166:                         if ($token->[2]->{'type'} eq 'finish') {
                    167: 			    $hash{'map_finish_'.$uri}="$rid";
                    168:                         }
1.1       www       169:                     }  else {
                    170:                         $hash{'type_'.$rid}='normal';
                    171:                     }
                    172: 
1.10      www       173:                     if (($turi=~/\.sequence$/) ||
1.1       www       174:                         ($turi=~/\.page$/)) {
1.2       www       175:                         $hash{'is_map_'.$rid}=1;
1.1       www       176:                         &loadmap($turi);
                    177:                     } 
                    178:                     
                    179:                 } elsif ($token->[1] eq 'condition') {
                    180: # ------------------------------------------------------------------- Condition
                    181: 
                    182:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
                    183: 
                    184:                     $hash{'kind_'.$rid}='cond';
1.2       www       185:                     $cond[$#cond+1]=$token->[2]->{'value'};
                    186:                     $hash{'condid_'.$rid}=$#cond;
1.1       www       187:                     if ($token->[2]->{'type'}) {
1.2       www       188:                         $cond[$#cond].=':'.$token->[2]->{'type'};
1.1       www       189:                     }  else {
1.2       www       190:                         $cond[$#cond].=':normal';
1.1       www       191:                     }
                    192: 
                    193:                 } elsif ($token->[1] eq 'link') {
                    194: # ----------------------------------------------------------------------- Links
                    195: 
                    196:                     $linkpc++;
                    197:                     my $linkid=$lpc.'.'.$linkpc;
                    198: 
                    199:                     my $goesto=$lpc.'.'.$token->[2]->{'to'};
                    200:                     my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
                    201:                     my $undercond=0;
                    202: 
                    203:                     if ($token->[2]->{'condition'}) {
                    204: 			$undercond=$lpc.'.'.$token->[2]->{'condition'};
                    205:                     }
                    206: 
                    207:                     $hash{'goesto_'.$linkid}=$goesto;
                    208:                     $hash{'comesfrom_'.$linkid}=$comesfrom;
                    209:                     $hash{'undercond_'.$linkid}=$undercond;
                    210: 
                    211:                     if (defined($hash{'to_'.$comesfrom})) {
                    212:                         $hash{'to_'.$comesfrom}.=','.$linkid;
                    213:                     } else {
                    214:                         $hash{'to_'.$comesfrom}=''.$linkid;
                    215:                     }
                    216:                     if (defined($hash{'from_'.$goesto})) {
                    217:                         $hash{'from_'.$goesto}.=','.$linkid;
                    218:                     } else {
                    219:                         $hash{'from_'.$goesto}=''.$linkid;
                    220:                     }
1.18      www       221:                 } elsif ($token->[1] eq 'param') {
                    222: # ------------------------------------------------------------------- Parameter
                    223: 
                    224:                     my $referid=$lpc.'.'.$token->[2]->{'to'};
1.20      www       225:                     my $part=$token->[2]->{'part'};
                    226:                     unless ($part) { $part=0; }
1.18      www       227:                     my $newparam=
                    228: 			&Apache::lonnet::escape($token->[2]->{'type'}).':'.
1.20      www       229: 			&Apache::lonnet::escape($part.'.'.
                    230:                          $token->[2]->{'name'}).'='.
1.18      www       231: 			&Apache::lonnet::escape($token->[2]->{'value'});
                    232:                     if (defined($hash{'param_'.$referid})) {
                    233:                         $hash{'param_'.$referid}.='&'.$newparam;
                    234:                     } else {
                    235:                         $hash{'param_'.$referid}=''.$newparam;
                    236:                     }
1.29      www       237:                     if ($token->[2]->{'name'} eq 'parameter_mapalias') {
1.28      www       238: 			$hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
                    239:                     }
1.29      www       240:                     if ($token->[2]->{'name'} eq 'parameter_randompick') {
                    241: 			$randompick{$referid}=$token->[2]->{'value'};
                    242:                     }
1.51    ! www       243:                     if ($token->[2]->{'name'} eq 'parameter_randompickseed') {
        !           244: 			$randompick{$referid}=$token->[2]->{'value'};
        !           245:                     }
1.1       www       246:                 } 
                    247: 
                    248:             }
                    249:         }
                    250: 
                    251:     } else {
                    252:         $errtext.='Map not loaded: The file does not exist. ';
                    253:     }
                    254: }
                    255: 
1.3       www       256: # --------------------------------------------------------- Simplify expression
                    257: 
                    258: sub simplify {
                    259:    my $expression=shift;
                    260: # (8)=8
                    261:    $expression=~s/\((\d+)\)/$1/g;
                    262: # 8&8=8
1.7       www       263:    $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
1.3       www       264: # 8|8=8
1.7       www       265:    $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
1.3       www       266: # (5&3)&4=5&3&4
1.7       www       267:    $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
1.3       www       268: # (((5&3)|(4&6)))=((5&3)|(4&6))
                    269:    $expression=~
                    270:        s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
                    271: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
                    272:    $expression=~
                    273:        s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
                    274:    return $expression;
                    275: }
                    276: 
1.2       www       277: # -------------------------------------------------------- Build condition hash
                    278: 
                    279: sub traceroute {
1.3       www       280:     my ($sofar,$rid,$beenhere)=@_;
                    281:     $sofar=simplify($sofar);
1.2       www       282:     unless ($beenhere=~/\&$rid\&/) {
                    283:        $beenhere.=$rid.'&';  
1.48      www       284:        if (($retfurl eq '') && ($hash{'src_'.$rid})
                    285:         && ($hash{'src_'.$rid}!~/\.sequence$/)) {
1.29      www       286:            my ($mapid,$resid)=split(/\./,$rid);
1.35      www       287:            $retfurl=$hash{'src_'.$rid}.
                    288:            (($hash{'src_'.$rid}=~/\?/)?'&':'?').'symb='.
1.29      www       289:            &Apache::lonnet::symbclean(
                    290:                            &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
                    291:                            '___'.$resid.'___'.
                    292:                            &Apache::lonnet::declutter($hash{'src_'.$rid}));
1.21      www       293:        }
1.2       www       294:        if (defined($hash{'conditions_'.$rid})) {
1.3       www       295: 	   $hash{'conditions_'.$rid}=simplify(
                    296:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
1.2       www       297:        } else {
                    298:            $hash{'conditions_'.$rid}=$sofar;
                    299:        }
                    300:        if (defined($hash{'is_map_'.$rid})) {
1.3       www       301:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
                    302: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
                    303:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
                    304: 		   $sofar=
                    305:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
                    306:                }
1.2       www       307:            }
                    308:        }
                    309:        if (defined($hash{'to_'.$rid})) {
1.26      harris41  310:           foreach (split(/\,/,$hash{'to_'.$rid})) {
1.2       www       311: 		my $further=$sofar;
                    312:                 if ($hash{'undercond_'.$_}) {
                    313: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
1.3       www       314:   		       $further=simplify('('.$further.')&('.
                    315:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
1.2       www       316: 		   } else {
                    317:                        $errtext.='Undefined condition ID: '
                    318:                                  .$hash{'undercond_'.$_}.'. ';
                    319:                    }
                    320:                 }
                    321:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere);
1.26      harris41  322:           }
1.2       www       323:        }
                    324:     }
                    325: }
1.1       www       326: 
1.19      www       327: # ------------------------------ Cascading conditions, quick access, parameters
1.4       www       328: 
                    329: sub accinit {
                    330:     my ($uri,$short,$fn)=@_;
                    331:     my %acchash=();
                    332:     my %captured=();
                    333:     my $condcounter=0;
1.5       www       334:     $acchash{'acc.cond.'.$short.'.0'}=0;
1.26      harris41  335:     foreach (keys %hash) {
1.4       www       336:        if ($_=~/^conditions/) {
                    337: 	  my $expr=$hash{$_};
1.26      harris41  338:          foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
1.4       www       339:              my $sub=$_;
                    340:              my $orig=$_;
1.13      www       341:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
1.4       www       342:              my $factor=$1;
1.7       www       343:              $sub=~s/$factor//g;
                    344:              $sub=~s/^\(/\($factor\(/;
1.4       www       345: 	     $sub.=')';
                    346:              $sub=simplify($sub);
                    347:              $orig=~s/(\W)/\\$1/g;
1.7       www       348:  	     $expr=~s/$orig/$sub/;
1.26      harris41  349: 	  }
1.4       www       350:           $hash{$_}=$expr;
                    351:           unless (defined($captured{$expr})) {
                    352: 	      $condcounter++;
                    353:               $captured{$expr}=$condcounter;
1.5       www       354:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
1.4       www       355:           } 
1.19      www       356:        } elsif ($_=~/^param_(\d+)\.(\d+)/) {
                    357:           my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
                    358:       '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
1.26      harris41  359:           foreach (split(/\&/,$hash{$_})) {
1.19      www       360: 	     my ($typename,$value)=split(/\=/,$_);
                    361:              my ($type,$name)=split(/\:/,$typename);
                    362:              $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
                    363:                                    &Apache::lonnet::unescape($value);
                    364: 	     $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
                    365:                                    &Apache::lonnet::unescape($type);
1.26      harris41  366:           }
1.19      www       367:        }
1.26      harris41  368:     }
                    369:     foreach (keys %hash) {
1.4       www       370: 	if ($_=~/^ids/) {
1.26      harris41  371: 	  foreach (split(/\,/,$hash{$_})) {
1.13      www       372: 	    my $resid=$_;
1.4       www       373:             my $uri=$hash{'src_'.$resid};
1.22      www       374:             $uri=~s/^\/adm\/wrapper//;
1.4       www       375:             my @uriparts=split(/\//,$uri);
                    376:             my $urifile=$uriparts[$#uriparts];
                    377:             $#uriparts--;
                    378:             my $uripath=join('/',@uriparts);
1.8       www       379:             $uripath=~s/^\/res\///;
1.23      www       380:            if ($uripath) {
1.13      www       381:             my $uricond='0';
1.4       www       382:             if (defined($hash{'conditions_'.$resid})) {
1.13      www       383:  		$uricond=$captured{$hash{'conditions_'.$resid}};
1.4       www       384:             }
1.5       www       385:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
1.13      www       386:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
                    387:                    /(\&$urifile\:[^\&]*)/) {
                    388: 		    my $replace=$1;
1.27      www       389:                     my $regexp=$replace;
                    390:                     $regexp=~s/\|/\\\|/g;
1.13      www       391:                     $acchash{'acc.res.'.$short.'.'.$uripath}
1.27      www       392:                      =~s/$regexp/$replace\|$uricond/;
1.13      www       393:                 } else {
                    394: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
                    395:                      $urifile.':'.$uricond.'&';
                    396: 	        }
1.4       www       397:             } else {
1.13      www       398:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
                    399:                  '&'.$urifile.':'.$uricond.'&';
1.23      www       400:             }
                    401:            } 
1.26      harris41  402:          }
1.13      www       403:       }
1.26      harris41  404:     }
1.24      www       405:     $acchash{'acc.res.'.$short.'.'}='&:0&';
1.8       www       406:     my $courseuri=$uri;
                    407:     $courseuri=~s/^\/res\///;
1.19      www       408:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
1.4       www       409:     &Apache::lonnet::appenv(%acchash,
1.9       www       410:                             "request.course.id"  => $short,
1.8       www       411:                             "request.course.fn"  => $fn,
                    412:                             "request.course.uri" => $courseuri); 
1.4       www       413: }
                    414: 
1.29      www       415: # ------------------------------------- Selectively delete from randompick maps
                    416: 
                    417: sub pickrandom {
1.31      www       418:     my $randomoutentry='';
1.29      www       419:     foreach my $rid (keys %randompick) {
                    420:         my $rndpick=$randompick{$rid};
                    421:         my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
                    422: # ------------------------------------------- put existing resources into array
                    423:         my @currentrids=();
1.50      albertel  424:         foreach (sort(keys(%hash))) {
1.29      www       425: 	    if ($_=~/^src_($mpc\.\d+)/) {
                    426: 		if ($hash{'src_'.$1}) { push @currentrids, $1; }
                    427:             }
                    428:         }
1.50      albertel  429: 	# rids are number.number and we want to numercially sort on 
                    430:         # the second number
                    431: 	@currentrids=sort {
                    432: 	    my (undef,$aid)=split(/\./,$a);
                    433: 	    my (undef,$bid)=split(/\./,$b);
                    434: 	    $aid <=> $bid;
                    435: 	} @currentrids;
1.29      www       436:         next if ($#currentrids<$rndpick);
                    437: # -------------------------------- randomly eliminate the ones that should stay
1.50      albertel  438: 	my (undef,$id)=split(/\./,$rid);
1.51    ! www       439:         if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
1.50      albertel  440: 	my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
                    441: 	&Math::Random::random_set_seed_from_phrase($rndseed);
                    442: 	my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
                    443:         for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
                    444: 	#&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
1.29      www       445: # -------------------------------------------------------- delete the leftovers
                    446:         for (my $k=0; $k<=$#currentrids; $k++) {
                    447:             if ($currentrids[$k]) {
                    448: 		$hash{'randomout_'.$currentrids[$k]}=1;
1.32      www       449:                 my ($mapid,$resid)=split(/\./,$currentrids[$k]);
                    450:                 $randomoutentry.='&'.
                    451:                  &Apache::lonnet::symbclean(
                    452: 		    &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
                    453:                     '___'.$resid.'___'.
                    454: 		    &Apache::lonnet::declutter($hash{'src_'.$currentrids[$k]})
                    455:                  ).'&';
1.29      www       456:             }
                    457:         }
1.31      www       458:     }
                    459:     if ($randomoutentry) {
                    460: 	&Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
1.29      www       461:     }
                    462: }
                    463: 
1.1       www       464: # ---------------------------------------------------- Read map and all submaps
                    465: 
                    466: sub readmap {
1.9       www       467:    my $short=shift;
                    468:    $short=~s/^\///;
                    469:    my %cenv=&Apache::lonnet::coursedescription($short);
                    470:    my $fn=$cenv{'fn'};
                    471:    my $uri;
                    472:    $short=~s/\//\_/g;
                    473:    unless ($uri=$cenv{'url'}) { 
                    474:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    475:                        "Could not load course $short.</font>"); 
                    476:       return 'No course data available.';
                    477:    }
1.3       www       478:    @cond=('true:normal');
1.11      www       479:    unlink($fn.'.db');
                    480:    unlink($fn.'_symb.db');
                    481:    unlink($fn.'.state');
1.19      www       482:    unlink($fn.'parms.db');
1.29      www       483:    undef %randompick;
1.21      www       484:    $retfurl='';
1.36      albertel  485:    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
                    486:        (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
1.4       www       487:     %hash=();
1.19      www       488:     %parmhash=();
1.4       www       489:     $errtext='';
                    490:     $pc=0;
1.38      www       491:     my $furi=&Apache::lonnet::clutter($uri);
1.30      www       492:     $hash{'src_0.0'}=$furi;
                    493:     $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
                    494:     $hash{'ids_'.$furi}='0.0';
                    495:     $hash{'is_map_0.0'}=1;
1.4       www       496:     loadmap($uri);
                    497:     if (defined($hash{'map_start_'.$uri})) {
                    498:         &traceroute('0',$hash{'map_start_'.$uri},'&');
                    499:         &accinit($uri,$short,$fn);
1.29      www       500:         &pickrandom();
1.45      www       501:     }
                    502: # ------------------------------------------------------------ Version tracking
1.48      www       503: #    if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                    504: #	&Apache::lonnet::logthis('Will be version tracking');
                    505: #    }
1.19      www       506:     unless ((untie(%hash)) && (untie(%parmhash))) {
1.4       www       507:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    508:                        "Could not untie coursemap $fn for $uri.</font>"); 
1.1       www       509:     }
1.4       www       510:     {
                    511:      my $cfh;
                    512:      if ($cfh=Apache::File->new(">$fn.state")) {
                    513:         print $cfh join("\n",@cond);
                    514:      } else {
1.6       www       515:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4       www       516:                        "Could not write statemap $fn for $uri.</font>"); 
                    517:      }
                    518:     }  
                    519:    } else {
1.6       www       520:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.4       www       521:                        "Could not tie coursemap $fn for $uri.</font>"); 
                    522:    }
1.12      www       523:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
1.46      www       524: # ------------------------------------------------- Check for critical messages
                    525: 
                    526:     my @what=&Apache::lonnet::dump('critical',$ENV{'user.domain'},
                    527:                                               $ENV{'user.name'});
                    528:     if ($what[0]) {
                    529: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
                    530: 	    $retfurl='/adm/email?critical=display';
                    531:         }
                    532:     }
1.21      www       533:    return ($retfurl,$errtext);
1.1       www       534: }
1.15      www       535: 
                    536: # ------------------------------------------------------- Evaluate state string
                    537: 
                    538: sub evalstate {
1.21      www       539: 
1.15      www       540:     my $fn=$ENV{'request.course.fn'}.'.state';
                    541:     my $state='2';
                    542:     if (-e $fn) {
                    543:        my @conditions=();
                    544:        {
                    545:         my $fh=Apache::File->new($fn);
                    546:         @conditions=<$fh>;
                    547:        }  
1.21      www       548:        my $safeeval = new Safe;
                    549:        my $safehole = new Safe::Hole;
1.15      www       550:        $safeeval->permit("entereval");
                    551:        $safeeval->permit(":base_math");
                    552:        $safeeval->deny(":base_io");
1.21      www       553:        $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.26      harris41  554:        foreach (@conditions) {
1.15      www       555: 	   my $line=$_;
                    556:            chomp($line);
                    557: 	   my ($condition,$weight)=split(/\:/,$_);
                    558:            if ($safeeval->reval($condition)) {
                    559: 	       if ($weight eq 'force') {
                    560: 		   $state.='3';
                    561:                } else {
                    562:                    $state.='2';
                    563:                }
                    564:            } else {
                    565:                if ($weight eq 'stop') {
                    566: 		   $state.='0';
                    567:                } else {
                    568:                    $state.='1';
                    569:                }
                    570:            }
1.26      harris41  571:        }
1.15      www       572:     }
                    573:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
                    574:     return $state;
                    575: }
                    576: 
1.1       www       577: 1;
                    578: __END__
                    579: 
1.26      harris41  580: =head1 NAME
                    581: 
                    582: Apache::lonuserstate - Construct and maintain state and binary representation
                    583: of course for user
                    584: 
                    585: =head1 SYNOPSIS
                    586: 
                    587: Invoked by lonroles.pm.
                    588: 
                    589: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                    590: 
                    591: =head1 INTRODUCTION
                    592: 
                    593: This module constructs and maintains state and binary representation
                    594: of course for user.
                    595: 
                    596: This is part of the LearningOnline Network with CAPA project
                    597: described at http://www.lon-capa.org.
                    598: 
                    599: =head1 HANDLER SUBROUTINE
                    600: 
                    601: There is no handler subroutine.
                    602: 
                    603: =head1 OTHER SUBROUTINES
                    604: 
                    605: =over 4
                    606: 
                    607: =item *
                    608: 
                    609: loadmap() : Loads map from disk
                    610: 
                    611: =item *
                    612: 
                    613: simplify() : Simplify expression
                    614: 
                    615: =item *
                    616: 
                    617: traceroute() : Build condition hash
                    618: 
                    619: =item *
                    620: 
                    621: accinit() : Cascading conditions, quick access, parameters
1.1       www       622: 
1.26      harris41  623: =item *
1.1       www       624: 
1.26      harris41  625: readmap() : Read map and all submaps
1.1       www       626: 
1.26      harris41  627: =item *
1.1       www       628: 
1.26      harris41  629: evalstate() : Evaluate state string
1.1       www       630: 
1.26      harris41  631: =back
1.1       www       632: 
1.26      harris41  633: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>