File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.65: download - view: text, annotated - select for diffs
Wed Oct 29 21:50:41 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #531: Nail down versions of resources.

Needless to say that this now causes all kinds of little nasty follow-up bugs
with ambiguous resources, etc., so do not use yet.

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

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