File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.73: download - view: text, annotated - select for diffs
Fri Apr 23 15:23:35 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
Open up the wonderful can of worms of hidden and encrypted URLs and folders.

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

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