Annotation of rat/lonuserstate.pm, revision 1.37

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

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