File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.71: download - view: text, annotated - select for diffs
Mon Feb 2 00:46:51 2004 UTC (20 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2381: External URLs with query string work now. Bighash's ids now do
not have query strings anymore.

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

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