File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.92: download - view: text, annotated - select for diffs
Wed Jun 8 18:49:38 2005 UTC (18 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_1_99_0, HEAD
Bug #882: ID management, get old idx back

    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.92 2005/06/08 18:49:38 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 HTML::TokeParser;
   36: use Apache::lonnet;
   37: use Apache::loncommon();
   38: use GDBM_File;
   39: use Apache::lonmsg;
   40: use Safe;
   41: use Safe::Hole;
   42: use Opcode;
   43: use Apache::lonenc;
   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: my %encurl; # URLs in this folder are supposed to be encrypted
   56: my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
   57: 
   58: # ----------------------------------- Remove version from URL and store in hash
   59: 
   60: sub versiontrack {
   61:     my $uri=shift;
   62:     if ($uri=~/\.(\d+)\.\w+$/) {
   63: 	my $version=$1;
   64: 	$uri=~s/\.\d+\.(\w+)$/\.$1/;
   65:         unless ($hash{'version_'.$uri}) {
   66: 	    $hash{'version_'.$uri}=$version;
   67: 	}
   68:     }
   69:     return $uri;
   70: }
   71: 
   72: # -------------------------------------------------------------- Put in version
   73: 
   74: sub putinversion {
   75:     my $uri=shift;
   76:     if ($hash{'version_'.$uri}) {
   77: 	my $version=$hash{'version_'.$uri};
   78: 	if ($version eq 'mostrecent') { return $uri; }
   79: 	if ($version eq &Apache::lonnet::getversion(
   80: 			&Apache::lonnet::filelocation('',$uri))) 
   81: 	             { return $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:                     if ($token->[2]->{'type'} eq 'zombie') { next; }
  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: 		    my $title=$token->[2]->{'title'};
  153: 		    $title=~s/\&colon\;/\:/gs;
  154: #		    my $symb=&Apache::lonnet::encode_symb($uri,
  155: #							  $token->[2]->{'id'},
  156: #							  $turi);
  157: #		    &Apache::lonnet::do_cache_new('title',$symb,$title);
  158:                     unless ($ispage) {
  159:                         $turi=~/\.(\w+)$/;
  160:                         my $embstyle=&Apache::loncommon::fileembstyle($1);
  161:                         if ($token->[2]->{'external'} eq 'true') { # external
  162:                             $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
  163:                         } elsif ($turi=~/^\/*uploaded\//) { # uploaded
  164: 			    if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
  165:                                 $turi='/adm/wrapper'.$turi;
  166: 			    } elsif ($embstyle eq 'ssi') {
  167: 				#do nothing with these
  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 ($turi=~/\/(syllabus|aboutme|navmaps|smppg|bulletinboard)$/) {
  190: 			$turi.='?register=1';
  191: 		    }
  192: 
  193:                     $hash{'src_'.$rid}=$turi;
  194: 
  195:                     if ($token->[2]->{'external'} eq 'true') {
  196:                         $hash{'ext_'.$rid}='true:';
  197:                     } else {
  198:                         $hash{'ext_'.$rid}='false:';
  199:                     }
  200:                     if ($token->[2]->{'type'}) {
  201: 			$hash{'type_'.$rid}=$token->[2]->{'type'};
  202:                         if ($token->[2]->{'type'} eq 'start') {
  203: 			    $hash{'map_start_'.$uri}="$rid";
  204:                         }
  205:                         if ($token->[2]->{'type'} eq 'finish') {
  206: 			    $hash{'map_finish_'.$uri}="$rid";
  207:                         }
  208:                     }  else {
  209:                         $hash{'type_'.$rid}='normal';
  210:                     }
  211: 
  212:                     if (($turi=~/\.sequence$/) ||
  213:                         ($turi=~/\.page$/)) {
  214:                         $hash{'is_map_'.$rid}=1;
  215:                         &loadmap($turi);
  216:                     } 
  217:                     
  218:                 } elsif ($token->[1] eq 'condition') {
  219: # ------------------------------------------------------------------- Condition
  220: 
  221:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  222: 
  223:                     $hash{'kind_'.$rid}='cond';
  224:                     $cond[$#cond+1]=$token->[2]->{'value'};
  225:                     $hash{'condid_'.$rid}=$#cond;
  226:                     if ($token->[2]->{'type'}) {
  227:                         $cond[$#cond].=':'.$token->[2]->{'type'};
  228:                     }  else {
  229:                         $cond[$#cond].=':normal';
  230:                     }
  231: 
  232:                 } elsif ($token->[1] eq 'link') {
  233: # ----------------------------------------------------------------------- Links
  234: 
  235:                     $linkpc++;
  236:                     my $linkid=$lpc.'.'.$linkpc;
  237: 
  238:                     my $goesto=$lpc.'.'.$token->[2]->{'to'};
  239:                     my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
  240:                     my $undercond=0;
  241: 
  242:                     if ($token->[2]->{'condition'}) {
  243: 			$undercond=$lpc.'.'.$token->[2]->{'condition'};
  244:                     }
  245: 
  246:                     $hash{'goesto_'.$linkid}=$goesto;
  247:                     $hash{'comesfrom_'.$linkid}=$comesfrom;
  248:                     $hash{'undercond_'.$linkid}=$undercond;
  249: 
  250:                     if (defined($hash{'to_'.$comesfrom})) {
  251:                         $hash{'to_'.$comesfrom}.=','.$linkid;
  252:                     } else {
  253:                         $hash{'to_'.$comesfrom}=''.$linkid;
  254:                     }
  255:                     if (defined($hash{'from_'.$goesto})) {
  256:                         $hash{'from_'.$goesto}.=','.$linkid;
  257:                     } else {
  258:                         $hash{'from_'.$goesto}=''.$linkid;
  259:                     }
  260:                 } elsif ($token->[1] eq 'param') {
  261: # ------------------------------------------------------------------- Parameter
  262: 
  263:                     my $referid=$lpc.'.'.$token->[2]->{'to'};
  264: 		    my $name=$token->[2]->{'name'};
  265: 		    my $part;
  266: 		    if ($name=~/^parameter_(.*)_/) {
  267: 			$part=$1;
  268: 		    } else {
  269: 			$part=0;
  270: 		    }
  271: 		    $name=~s/^.*_([^_]*)$/$1/;
  272:                     my $newparam=
  273: 			&Apache::lonnet::escape($token->[2]->{'type'}).':'.
  274: 			&Apache::lonnet::escape($part.'.'.$name).'='.
  275: 			&Apache::lonnet::escape($token->[2]->{'value'});
  276:                     if (defined($hash{'param_'.$referid})) {
  277:                         $hash{'param_'.$referid}.='&'.$newparam;
  278:                     } else {
  279:                         $hash{'param_'.$referid}=''.$newparam;
  280:                     }
  281:                     if ($token->[2]->{'name'}=~/^parameter_(0_)*mapalias$/) {
  282: 			$hash{'mapalias_'.$token->[2]->{'value'}}=$referid;
  283:                     }
  284:                     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompick$/) {
  285: 			$randompick{$referid}=$token->[2]->{'value'};
  286:                     }
  287:                     if ($token->[2]->{'name'}=~/^parameter_(0_)*randompickseed$/) {
  288: 			$randompick{$referid}=$token->[2]->{'value'};
  289:                     }
  290:                     if ($token->[2]->{'name'}=~/^parameter_(0_)*encrypturl$/) {
  291: 			if ($token->[2]->{'value'}=~/^yes$/i) {
  292: 			    $encurl{$referid}=1;
  293: 			}
  294:                     }
  295:                     if ($token->[2]->{'name'}=~/^parameter_(0_)*hiddenresource$/) {
  296: 			if ($token->[2]->{'value'}=~/^yes$/i) {
  297: 			    $hiddenurl{$referid}=1;
  298: 			}
  299:                     }
  300:                 } 
  301: 		
  302:             }
  303:         }
  304: 
  305:     } else {
  306:         $errtext.='Map not loaded: The file ('.$fn.') does not exist. ';
  307:     }
  308: }
  309: 
  310: # --------------------------------------------------------- Simplify expression
  311: 
  312: sub simplify {
  313:     my $expression=shift;
  314: # (8)=8
  315:     $expression=~s/\((\d+)\)/$1/g;
  316: # 8&8=8
  317:     $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
  318: # 8|8=8
  319:     $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
  320: # (5&3)&4=5&3&4
  321:     $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
  322: # (((5&3)|(4&6)))=((5&3)|(4&6))
  323:     $expression=~
  324: 	s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
  325: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  326:     $expression=~
  327: 	s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
  328:     return $expression;
  329: }
  330: 
  331: # -------------------------------------------------------- Build condition hash
  332: 
  333: sub traceroute {
  334:     my ($sofar,$rid,$beenhere,$encflag,$hdnflag)=@_;
  335:     my $newsofar=$sofar=simplify($sofar);
  336:     unless ($beenhere=~/\&$rid\&/) {
  337: 	$beenhere.=$rid.'&';  
  338: 	my ($mapid,$resid)=split(/\./,$rid);
  339: 	my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
  340: 	my $hidden=&Apache::lonnet::EXT('resource.0.hiddenresource',$symb);
  341: 
  342: 	if ($hdnflag || lc($hidden) eq 'yes') {
  343: 	    $hiddenurl{$rid}=1;
  344: 	}
  345: 	if (!$hdnflag && lc($hidden) eq 'no') {
  346: 	    delete($hiddenurl{$rid});
  347: 	}
  348: 
  349: 	my $encrypt=&Apache::lonnet::EXT('resource.0.encrypturl',$symb);
  350: 	if ($encflag || lc($encrypt) eq 'yes') { $encurl{$rid}=1; }
  351: 	if (($retfurl eq '') && ($hash{'src_'.$rid})
  352: 	    && ($hash{'src_'.$rid}!~/\.sequence$/)) {
  353: 	    $retfurl=$hash{'src_'.$rid}.(($hash{'src_'.$rid}=~/\?/)?'&':'?').
  354: 		'symb='.$symb;
  355: 	}
  356: 	if (defined($hash{'conditions_'.$rid})) {
  357: 	    $hash{'conditions_'.$rid}=simplify(
  358:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  359: 	} else {
  360: 	    $hash{'conditions_'.$rid}=$sofar;
  361: 	}
  362: 	$newsofar=$hash{'conditions_'.$rid};
  363: 	if (defined($hash{'is_map_'.$rid})) {
  364: 	    if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  365: 		$sofar=$newsofar=
  366: 		    &traceroute($sofar,
  367: 				$hash{'map_start_'.$hash{'src_'.$rid}},'&',
  368: 				$encflag || $encurl{$rid},
  369: 				$hdnflag || $hiddenurl{$rid});
  370: 	    }
  371: 	}
  372: 	if (defined($hash{'to_'.$rid})) {
  373: 	    foreach (split(/\,/,$hash{'to_'.$rid})) {
  374: 		my $further=$sofar;
  375:                 if ($hash{'undercond_'.$_}) {
  376: 		    if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  377: 			$further=simplify('('.$further.')&('.
  378: 					  $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  379: 		    } else {
  380: 			$errtext.='Undefined condition ID: '
  381: 			    .$hash{'undercond_'.$_}.'. ';
  382: 		    }
  383:                 }
  384:                 $newsofar=&traceroute($further,$hash{'goesto_'.$_},$beenhere,
  385: 				      $encflag,$hdnflag);
  386: 	    }
  387: 	}
  388:     }
  389:     return $newsofar;
  390: }
  391: 
  392: # ------------------------------ Cascading conditions, quick access, parameters
  393: 
  394: sub accinit {
  395:     my ($uri,$short,$fn)=@_;
  396:     my %acchash=();
  397:     my %captured=();
  398:     my $condcounter=0;
  399:     $acchash{'acc.cond.'.$short.'.0'}=0;
  400:     foreach (keys %hash) {
  401: 	if ($_=~/^conditions/) {
  402: 	    my $expr=$hash{$_};
  403: 	    foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
  404: 		my $sub=$_;
  405: 		my $orig=$_;
  406: 		$sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  407: 		my $factor=$1;
  408: 		$sub=~s/$factor//g;
  409: 		$sub=~s/^\(/\($factor\(/;
  410: 		$sub.=')';
  411: 		$sub=simplify($sub);
  412: 		$orig=~s/(\W)/\\$1/g;
  413: 		$expr=~s/$orig/$sub/;
  414: 	    }
  415: 	    $hash{$_}=$expr;
  416: 	    unless (defined($captured{$expr})) {
  417: 		$condcounter++;
  418: 		$captured{$expr}=$condcounter;
  419: 		$acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  420: 	    } 
  421: 	} elsif ($_=~/^param_(\d+)\.(\d+)/) {
  422: 	    my $prefix=&Apache::lonnet::encode_symb($hash{'map_id_'.$1},$2,
  423: 						    $hash{'src_'.$1.'.'.$2});
  424: 	    foreach (split(/\&/,$hash{$_})) {
  425: 		my ($typename,$value)=split(/\=/,$_);
  426: 		my ($type,$name)=split(/\:/,$typename);
  427: 		$parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
  428: 		    &Apache::lonnet::unescape($value);
  429: 		$parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
  430: 		    &Apache::lonnet::unescape($type);
  431: 	    }
  432: 	}
  433:     }
  434:     foreach (keys %hash) {
  435: 	if ($_=~/^ids/) {
  436: 	    foreach (split(/\,/,$hash{$_})) {
  437: 		my $resid=$_;
  438: 		my $uri=$hash{'src_'.$resid};
  439: 		$uri=~s/^\/adm\/wrapper//;
  440: 		$uri=&Apache::lonnet::declutter($uri);
  441: 		my @uriparts=split(/\//,$uri);
  442: 		my $urifile=$uriparts[$#uriparts];
  443: 		$#uriparts--;
  444: 		my $uripath=join('/',@uriparts);
  445: 		if ($uripath) {
  446: 		    my $uricond='0';
  447: 		    if (defined($hash{'conditions_'.$resid})) {
  448: 			$uricond=$captured{$hash{'conditions_'.$resid}};
  449: 		    }
  450: 		    if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  451: 			if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  452: 			    /(\&\Q$urifile\E\:[^\&]*)/) {
  453: 			    my $replace=$1;
  454: 			    my $regexp=$replace;
  455: 			    #$regexp=~s/\|/\\\|/g;
  456: 			    $acchash{'acc.res.'.$short.'.'.$uripath}
  457: 			    =~s/\Q$regexp\E/$replace\|$uricond/;
  458: 			} else {
  459: 			    $acchash{'acc.res.'.$short.'.'.$uripath}.=
  460: 				$urifile.':'.$uricond.'&';
  461: 			}
  462: 		    } else {
  463: 			$acchash{'acc.res.'.$short.'.'.$uripath}=
  464: 			    '&'.$urifile.':'.$uricond.'&';
  465: 		    }
  466: 		} 
  467: 	    }
  468: 	}
  469:     }
  470:     $acchash{'acc.res.'.$short.'.'}='&:0&';
  471:     my $courseuri=$uri;
  472:     $courseuri=~s/^\/res\///;
  473:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  474:     &Apache::lonnet::appenv(%acchash);
  475: }
  476: 
  477: # ---------------- Selectively delete from randompick maps and hidden url parms
  478: 
  479: sub hiddenurls {
  480:     my $randomoutentry='';
  481:     foreach my $rid (keys %randompick) {
  482:         my $rndpick=$randompick{$rid};
  483:         my $mpc=$hash{'map_pc_'.$hash{'src_'.$rid}};
  484: # ------------------------------------------- put existing resources into array
  485:         my @currentrids=();
  486:         foreach (sort(keys(%hash))) {
  487: 	    if ($_=~/^src_($mpc\.\d+)/) {
  488: 		if ($hash{'src_'.$1}) { push @currentrids, $1; }
  489:             }
  490:         }
  491: 	# rids are number.number and we want to numercially sort on 
  492:         # the second number
  493: 	@currentrids=sort {
  494: 	    my (undef,$aid)=split(/\./,$a);
  495: 	    my (undef,$bid)=split(/\./,$b);
  496: 	    $aid <=> $bid;
  497: 	} @currentrids;
  498:         next if ($#currentrids<$rndpick);
  499: # -------------------------------- randomly eliminate the ones that should stay
  500: 	my (undef,$id)=split(/\./,$rid);
  501:         if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
  502: 	my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
  503: 	&Apache::lonnet::setup_random_from_rndseed($rndseed);
  504: 	my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
  505:         for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
  506: 	#&Apache::lonnet::logthis("$id,$rndseed,".join(':',@whichids));
  507: # -------------------------------------------------------- delete the leftovers
  508:         for (my $k=0; $k<=$#currentrids; $k++) {
  509:             if ($currentrids[$k]) {
  510: 		$hash{'randomout_'.$currentrids[$k]}=1;
  511:                 my ($mapid,$resid)=split(/\./,$currentrids[$k]);
  512:                 $randomoutentry.='&'.
  513: 		    &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
  514: 						 $resid,
  515: 						 $hash{'src_'.$currentrids[$k]}
  516: 						 ).'&';
  517:             }
  518:         }
  519:     }
  520: # ------------------------------ take care of explicitly hidden urls or folders
  521:     foreach my $rid (keys %hiddenurl) {
  522: 	$hash{'randomout_'.$rid}=1;
  523: 	my ($mapid,$resid)=split(/\./,$rid);
  524: 	$randomoutentry.='&'.
  525: 	    &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,
  526: 					 $hash{'src_'.$rid}).'&';
  527:     }
  528: # --------------------------------------- append randomout entry to environment
  529:     if ($randomoutentry) {
  530: 	&Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
  531:     }
  532: }
  533: 
  534: # ---------------------------------------------------- Read map and all submaps
  535: 
  536: sub readmap {
  537:     my $short=shift;
  538:     $short=~s/^\///;
  539:     my %cenv=&Apache::lonnet::coursedescription($short);
  540:     my $fn=$cenv{'fn'};
  541:     my $uri;
  542:     $short=~s/\//\_/g;
  543:     unless ($uri=$cenv{'url'}) { 
  544: 	&Apache::lonnet::logthis("<font color=blue>WARNING: ".
  545: 				 "Could not load course $short.</font>"); 
  546: 	return 'No course data available.';
  547:     }
  548:     @cond=('true:normal');
  549:     #unlink($fn.'.db');
  550:     #unlink($fn.'_symb.db');
  551:     unlink($fn.'.state');
  552:     unlink($fn.'parms.db');
  553:     undef %randompick;
  554:     undef %hiddenurl;
  555:     undef %encurl;
  556:     $retfurl='';
  557:     if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
  558: 	(tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
  559: 	%hash=();
  560: 	%parmhash=();
  561: 	$errtext='';
  562: 	$pc=0;
  563: 	&processversionfile(%cenv);
  564: 	my $furi=&Apache::lonnet::clutter($uri);
  565: 	$hash{'src_0.0'}=&versiontrack($furi);
  566: 	$hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
  567: 	$hash{'ids_'.$furi}='0.0';
  568: 	$hash{'is_map_0.0'}=1;
  569: 	loadmap($uri);
  570: 	if (defined($hash{'map_start_'.$uri})) {
  571: 	    &Apache::lonnet::appenv("request.course.id"  => $short,
  572: 				    "request.course.fn"  => $fn,
  573: 				    "request.course.uri" => $uri);
  574: 	    &traceroute('0',$hash{'map_start_'.$uri},'&');
  575: 	    &accinit($uri,$short,$fn);
  576: 	    &hiddenurls();
  577: 	}
  578: # ------------------------------------------------------- Put versions into src
  579: 	foreach (keys %hash) {
  580: 	    if ($_=~/^src\_/) {
  581: 		$hash{$_}=&putinversion($hash{$_});
  582: 	    }
  583: 	}
  584: # ---------------------------------------------------------------- Encrypt URLs
  585: 	foreach (keys %encurl) {
  586: #	    $hash{'src_'.$_}=&Apache::lonenc::encrypted($hash{'src_'.$_});
  587: 	    $hash{'encrypted_'.$_}=1;
  588: 	}
  589: # ----------------------------------------------- Close hashes to finally store
  590: # --------------------------------- Routine must pass this point, no early outs
  591: 	unless ((untie(%hash)) && (untie(%parmhash))) {
  592: 	    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  593: 				     "Could not untie coursemap $fn for $uri.</font>"); 
  594: 	}
  595: # ---------------------------------------------------- Store away initial state
  596: 	{
  597: 	    my $cfh;
  598: 	    if (open($cfh,">$fn.state")) {
  599: 		print $cfh join("\n",@cond);
  600: 	    } else {
  601: 		&Apache::lonnet::logthis("<font color=blue>WARNING: ".
  602: 					 "Could not write statemap $fn for $uri.</font>"); 
  603: 	    }
  604: 	}  
  605:     } else {
  606: 	# if we are here it is likely because we are already trying to 
  607: 	# initialize the course in another child, busy wait trying to 
  608: 	# tie the hashes for the next 90 seconds, if we succeed forward 
  609: 	# them on to navmaps, if we fail, throw up the Could not init 
  610: 	# course screen
  611: 	untie(%hash);
  612: 	untie(%parmhash);
  613: 	&Apache::lonnet::logthis("<font color=blue>WARNING: ".
  614: 				 "Could not tie coursemap $fn for $uri.</font>"); 
  615: 	my $i=0;
  616: 	while($i<90) {
  617: 	    $i++;
  618: 	    sleep(1);
  619: 	    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640))) {
  620: 		if (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_READER(),0640)) {
  621: 		    $retfurl='/adm/navmaps';
  622: 		    &Apache::lonnet::appenv("request.course.id"  => $short,
  623: 					    "request.course.fn"  => $fn,
  624: 					    "request.course.uri" => $uri);
  625: 		    untie(%hash);
  626: 		    untie(%parmhash);
  627: 		    last;
  628: 		}
  629: 	    }
  630: 	    untie(%hash);
  631: 	    untie(%parmhash);
  632: 	}
  633:     }
  634:     &Apache::lonmsg::author_res_msg($env{'request.course.uri'},$errtext);
  635: # ------------------------------------------------- Check for critical messages
  636: 
  637:     my @what=&Apache::lonnet::dump('critical',$env{'user.domain'},
  638: 				   $env{'user.name'});
  639:     if ($what[0]) {
  640: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  641: 	    $retfurl='/adm/email?critical=display';
  642:         }
  643:     }
  644:     return ($retfurl,$errtext);
  645: }
  646: 
  647: # ------------------------------------------------------- Evaluate state string
  648: 
  649: sub evalstate {
  650:     my $fn=$env{'request.course.fn'}.'.state';
  651:     my $state='';
  652:     if (-e $fn) {
  653: 	my @conditions=();
  654: 	{
  655: 	    my $fh=Apache::File->new($fn);
  656: 	    @conditions=<$fh>;
  657: 	}  
  658: 	my $safeeval = new Safe;
  659: 	my $safehole = new Safe::Hole;
  660: 	$safeeval->permit("entereval");
  661: 	$safeeval->permit(":base_math");
  662: 	$safeeval->deny(":base_io");
  663: 	$safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  664: 	foreach my $line (@conditions) {
  665: 	    chomp($line);
  666: 	    my ($condition,$weight)=split(/\:/,$line);
  667: 	    if ($safeeval->reval($condition)) {
  668: 		if ($weight eq 'force') {
  669: 		    $state.='3';
  670: 		} else {
  671: 		    $state.='2';
  672: 		}
  673: 	    } else {
  674: 		if ($weight eq 'stop') {
  675: 		    $state.='0';
  676: 		} else {
  677: 		    $state.='1';
  678: 		}
  679: 	    }
  680: 	}
  681:     }
  682:     &Apache::lonnet::appenv('user.state.'.$env{'request.course.id'} => $state);
  683:     return $state;
  684: }
  685: 
  686: 1;
  687: __END__
  688: 
  689: =head1 NAME
  690: 
  691: Apache::lonuserstate - Construct and maintain state and binary representation
  692: of course for user
  693: 
  694: =head1 SYNOPSIS
  695: 
  696: Invoked by lonroles.pm.
  697: 
  698: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  699: 
  700: =head1 INTRODUCTION
  701: 
  702: This module constructs and maintains state and binary representation
  703: of course for user.
  704: 
  705: This is part of the LearningOnline Network with CAPA project
  706: described at http://www.lon-capa.org.
  707: 
  708: =head1 HANDLER SUBROUTINE
  709: 
  710: There is no handler subroutine.
  711: 
  712: =head1 OTHER SUBROUTINES
  713: 
  714: =over 4
  715: 
  716: =item *
  717: 
  718: loadmap() : Loads map from disk
  719: 
  720: =item *
  721: 
  722: simplify() : Simplify expression
  723: 
  724: =item *
  725: 
  726: traceroute() : Build condition hash
  727: 
  728: =item *
  729: 
  730: accinit() : Cascading conditions, quick access, parameters
  731: 
  732: =item *
  733: 
  734: readmap() : Read map and all submaps
  735: 
  736: =item *
  737: 
  738: evalstate() : Evaluate state string
  739: 
  740: =back
  741: 
  742: =cut

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