File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.78: download - view: text, annotated - select for diffs
Tue May 11 06:28:23 2004 UTC (19 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, HEAD
- leave the /uploaded URLs of ssi able documents alone

    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.78 2004/05/11 06:28:23 albertel 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:                                 $turi='/adm/wrapper'.$turi;
  165: 			    } elsif ($embstyle eq 'ssi') {
  166: 				#do nothing with these
  167: 			    } elsif ($turi!~/\.(sequence|page)$/) {
  168: 				$turi='/adm/coursedocs/showdoc'.$turi;
  169:                             }
  170:                         } elsif ($turi=~/\S/) { # normal non-empty internal resource
  171: 			    my $mapdir=$uri;
  172: 			    $mapdir=~s/[^\/]+$//;
  173: 			    $turi=&Apache::lonnet::hreflocation($mapdir,$turi);
  174: 			    if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
  175: 				$turi='/adm/wrapper'.$turi;
  176: 			    }
  177:                         }
  178: 		    }
  179: # Store reverse lookup, remove query string
  180: 		    my $idsuri=$turi;
  181: 		    $idsuri=~s/\?.+$//;
  182:                     if (defined($hash{'ids_'.$idsuri})) {
  183:                         $hash{'ids_'.$idsuri}.=','.$rid;
  184:                     } else {
  185:                         $hash{'ids_'.$idsuri}=''.$rid;
  186:                     }
  187:                
  188:                     if
  189: 	        ($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 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:     $sofar=simplify($sofar);
  336:     unless ($beenhere=~/\&$rid\&/) {
  337:        $beenhere.=$rid.'&';  
  338:        if ($hdnflag) {
  339: 	   $hiddenurl{$rid}=1;
  340:        }
  341:        if ($encflag) {
  342: 	   $encurl{$rid}=1;
  343:        }
  344:        if (($retfurl eq '') && ($hash{'src_'.$rid})
  345:         && ($hash{'src_'.$rid}!~/\.sequence$/)) {
  346:            my ($mapid,$resid)=split(/\./,$rid);
  347:            $retfurl=$hash{'src_'.$rid}.
  348:            (($hash{'src_'.$rid}=~/\?/)?'&':'?').'symb='.
  349:            &Apache::lonnet::symbclean(
  350:                            &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  351:                            '___'.$resid.'___'.
  352:                            &Apache::lonnet::declutter($hash{'src_'.$rid}));
  353:        }
  354:        if (defined($hash{'conditions_'.$rid})) {
  355: 	   $hash{'conditions_'.$rid}=simplify(
  356:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  357:        } else {
  358:            $hash{'conditions_'.$rid}=$sofar;
  359:        }
  360:        if (defined($hash{'is_map_'.$rid})) {
  361:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  362: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&',
  363: 			   $encflag || $encurl{$rid},
  364: 			   $hdnflag || $hiddenurl{$rid});
  365:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
  366: 		   $sofar=
  367:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
  368:                }
  369:            }
  370:        }
  371:        if (defined($hash{'to_'.$rid})) {
  372:           foreach (split(/\,/,$hash{'to_'.$rid})) {
  373: 		my $further=$sofar;
  374:                 if ($hash{'undercond_'.$_}) {
  375: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  376:   		       $further=simplify('('.$further.')&('.
  377:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  378: 		   } else {
  379:                        $errtext.='Undefined condition ID: '
  380:                                  .$hash{'undercond_'.$_}.'. ';
  381:                    }
  382:                 }
  383:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere,$encflag,$hdnflag);
  384:           }
  385:        }
  386:     }
  387: }
  388: 
  389: # ------------------------------ Cascading conditions, quick access, parameters
  390: 
  391: sub accinit {
  392:     my ($uri,$short,$fn)=@_;
  393:     my %acchash=();
  394:     my %captured=();
  395:     my $condcounter=0;
  396:     $acchash{'acc.cond.'.$short.'.0'}=0;
  397:     foreach (keys %hash) {
  398:        if ($_=~/^conditions/) {
  399: 	  my $expr=$hash{$_};
  400:          foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
  401:              my $sub=$_;
  402:              my $orig=$_;
  403:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  404:              my $factor=$1;
  405:              $sub=~s/$factor//g;
  406:              $sub=~s/^\(/\($factor\(/;
  407: 	     $sub.=')';
  408:              $sub=simplify($sub);
  409:              $orig=~s/(\W)/\\$1/g;
  410:  	     $expr=~s/$orig/$sub/;
  411: 	  }
  412:           $hash{$_}=$expr;
  413:           unless (defined($captured{$expr})) {
  414: 	      $condcounter++;
  415:               $captured{$expr}=$condcounter;
  416:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  417:           } 
  418:        } elsif ($_=~/^param_(\d+)\.(\d+)/) {
  419:           my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
  420:       '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
  421:           foreach (split(/\&/,$hash{$_})) {
  422: 	     my ($typename,$value)=split(/\=/,$_);
  423:              my ($type,$name)=split(/\:/,$typename);
  424:              $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
  425:                                    &Apache::lonnet::unescape($value);
  426: 	     $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
  427:                                    &Apache::lonnet::unescape($type);
  428:           }
  429:        }
  430:     }
  431:     foreach (keys %hash) {
  432: 	if ($_=~/^ids/) {
  433: 	  foreach (split(/\,/,$hash{$_})) {
  434: 	    my $resid=$_;
  435:             my $uri=$hash{'src_'.$resid};
  436:             $uri=~s/^\/adm\/wrapper//;
  437:             $uri=&Apache::lonnet::declutter($uri);
  438:             my @uriparts=split(/\//,$uri);
  439:             my $urifile=$uriparts[$#uriparts];
  440:             $#uriparts--;
  441:             my $uripath=join('/',@uriparts);
  442:            if ($uripath) {
  443:             my $uricond='0';
  444:             if (defined($hash{'conditions_'.$resid})) {
  445:  		$uricond=$captured{$hash{'conditions_'.$resid}};
  446:             }
  447:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  448:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  449:                    /(\&\Q$urifile\E\:[^\&]*)/) {
  450: 		    my $replace=$1;
  451:                     my $regexp=$replace;
  452:                     #$regexp=~s/\|/\\\|/g;
  453:                     $acchash{'acc.res.'.$short.'.'.$uripath}
  454:                      =~s/\Q$regexp\E/$replace\|$uricond/;
  455:                 } else {
  456: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
  457:                      $urifile.':'.$uricond.'&';
  458: 	        }
  459:             } else {
  460:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
  461:                  '&'.$urifile.':'.$uricond.'&';
  462:             }
  463:            } 
  464:          }
  465:       }
  466:     }
  467:     $acchash{'acc.res.'.$short.'.'}='&:0&';
  468:     my $courseuri=$uri;
  469:     $courseuri=~s/^\/res\///;
  470:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  471:     &Apache::lonnet::appenv(%acchash,
  472:                             "request.course.id"  => $short,
  473:                             "request.course.fn"  => $fn,
  474:                             "request.course.uri" => $courseuri); 
  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::symbclean(
  514: 		    &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  515:                     '___'.$resid.'___'.
  516: 		    &Apache::lonnet::declutter($hash{'src_'.$currentrids[$k]})
  517:                  ).'&';
  518:             }
  519:         }
  520:     }
  521: # ------------------------------ take care of explicitly hidden urls or folders
  522:     foreach my $rid (keys %hiddenurl) {
  523: 	$hash{'randomout_'.$rid}=1;
  524: 	my ($mapid,$resid)=split(/\./,$rid);
  525: 	$randomoutentry.='&'.
  526: 	    &Apache::lonnet::symbclean(
  527: 		         &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  528: 				       '___'.$resid.'___'.
  529: 		    &Apache::lonnet::declutter($hash{'src_'.$rid})
  530: 				       ).'&';
  531:     }
  532: # --------------------------------------- append randomout entry to environment
  533:     if ($randomoutentry) {
  534: 	&Apache::lonnet::appenv('acc.randomout' => $randomoutentry);
  535:     }
  536: }
  537: 
  538: # ---------------------------------------------------- Read map and all submaps
  539: 
  540: sub readmap {
  541:    my $short=shift;
  542:    $short=~s/^\///;
  543:    my %cenv=&Apache::lonnet::coursedescription($short);
  544:    my $fn=$cenv{'fn'};
  545:    my $uri;
  546:    $short=~s/\//\_/g;
  547:    unless ($uri=$cenv{'url'}) { 
  548:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  549:                        "Could not load course $short.</font>"); 
  550:       return 'No course data available.';
  551:    }
  552:    @cond=('true:normal');
  553:    unlink($fn.'.db');
  554:    unlink($fn.'_symb.db');
  555:    unlink($fn.'.state');
  556:    unlink($fn.'parms.db');
  557:    undef %randompick;
  558:    undef %hiddenurl;
  559:    undef %encurl;
  560:    $retfurl='';
  561:    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT(),0640)) &&
  562:        (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT(),0640))) {
  563:     %hash=();
  564:     %parmhash=();
  565:     $errtext='';
  566:     $pc=0;
  567:     &processversionfile(%cenv);
  568:     my $furi=&Apache::lonnet::clutter($uri);
  569:     $hash{'src_0.0'}=&versiontrack($furi);
  570:     $hash{'title_0.0'}=&Apache::lonnet::metadata($uri,'title');
  571:     $hash{'ids_'.$furi}='0.0';
  572:     $hash{'is_map_0.0'}=1;
  573:     loadmap($uri);
  574:     if (defined($hash{'map_start_'.$uri})) {
  575:         &traceroute('0',$hash{'map_start_'.$uri},'&');
  576:         &accinit($uri,$short,$fn);
  577:         &hiddenurls();
  578:     }
  579: # ------------------------------------------------------- Put versions into src
  580:     foreach (keys %hash) {
  581: 	if ($_=~/^src\_/) {
  582: 	    $hash{$_}=&putinversion($hash{$_});
  583: 	}
  584:     }
  585: # ---------------------------------------------------------------- Encrypt URLs
  586:     foreach (keys %encurl) {
  587: 	$hash{'src_'.$_}=&Apache::lonenc::encrypted($hash{'src_'.$_});
  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 ($cfh=Apache::File->new(">$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:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  607:                        "Could not tie coursemap $fn for $uri.</font>"); 
  608:    }
  609:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
  610: # ------------------------------------------------- Check for critical messages
  611: 
  612:     my @what=&Apache::lonnet::dump('critical',$ENV{'user.domain'},
  613:                                               $ENV{'user.name'});
  614:     if ($what[0]) {
  615: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  616: 	    $retfurl='/adm/email?critical=display';
  617:         }
  618:     }
  619:    return ($retfurl,$errtext);
  620: }
  621: 
  622: # ------------------------------------------------------- Evaluate state string
  623: 
  624: sub evalstate {
  625: 
  626:     my $fn=$ENV{'request.course.fn'}.'.state';
  627:     my $state='2';
  628:     if (-e $fn) {
  629:        my @conditions=();
  630:        {
  631:         my $fh=Apache::File->new($fn);
  632:         @conditions=<$fh>;
  633:        }  
  634:        my $safeeval = new Safe;
  635:        my $safehole = new Safe::Hole;
  636:        $safeeval->permit("entereval");
  637:        $safeeval->permit(":base_math");
  638:        $safeeval->deny(":base_io");
  639:        $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  640:        foreach (@conditions) {
  641: 	   my $line=$_;
  642:            chomp($line);
  643: 	   my ($condition,$weight)=split(/\:/,$_);
  644:            if ($safeeval->reval($condition)) {
  645: 	       if ($weight eq 'force') {
  646: 		   $state.='3';
  647:                } else {
  648:                    $state.='2';
  649:                }
  650:            } else {
  651:                if ($weight eq 'stop') {
  652: 		   $state.='0';
  653:                } else {
  654:                    $state.='1';
  655:                }
  656:            }
  657:        }
  658:     }
  659:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
  660:     return $state;
  661: }
  662: 
  663: 1;
  664: __END__
  665: 
  666: =head1 NAME
  667: 
  668: Apache::lonuserstate - Construct and maintain state and binary representation
  669: of course for user
  670: 
  671: =head1 SYNOPSIS
  672: 
  673: Invoked by lonroles.pm.
  674: 
  675: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  676: 
  677: =head1 INTRODUCTION
  678: 
  679: This module constructs and maintains state and binary representation
  680: of course for user.
  681: 
  682: This is part of the LearningOnline Network with CAPA project
  683: described at http://www.lon-capa.org.
  684: 
  685: =head1 HANDLER SUBROUTINE
  686: 
  687: There is no handler subroutine.
  688: 
  689: =head1 OTHER SUBROUTINES
  690: 
  691: =over 4
  692: 
  693: =item *
  694: 
  695: loadmap() : Loads map from disk
  696: 
  697: =item *
  698: 
  699: simplify() : Simplify expression
  700: 
  701: =item *
  702: 
  703: traceroute() : Build condition hash
  704: 
  705: =item *
  706: 
  707: accinit() : Cascading conditions, quick access, parameters
  708: 
  709: =item *
  710: 
  711: readmap() : Read map and all submaps
  712: 
  713: =item *
  714: 
  715: evalstate() : Evaluate state string
  716: 
  717: =back
  718: 
  719: =cut

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