File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.76: download - view: text, annotated - select for diffs
Fri Apr 23 23:01:35 2004 UTC (20 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Somewhat amazed at how easy this was should with these changes
   - uploaded .html/.page (all ssi type docs) now
       - get parsed
       - can be printed
       - get default parameters
       - are no longer wrapper ed

- .problems are still somewhat issue oriented though (need real metadata support for uploaded)

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

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