File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.66: download - view: text, annotated - select for diffs
Sat Nov 1 16:37:22 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Do not explicitly request most recent versions of files.

Two subroutines will need caching:

* lonnet::fixversion
& lonnet::getversion

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

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