File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.60: download - view: text, annotated - select for diffs
Fri Sep 19 18:20:35 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- titlecache now uses the common cahcing infrastructure

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

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