File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.72: download - view: text, annotated - select for diffs
Mon Apr 5 18:25:08 2004 UTC (20 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Do not prepend /adm/wrapper if URI for uploaded item is for a page.
This allows "non-res" pages to be uploaded via DOCS, e.g., pages created from processing of an IMS package.

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

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