File:  [LON-CAPA] / rat / lonuserstate.pm
Revision 1.26: download - view: text, annotated - select for diffs
Mon Dec 17 00:57:59 2001 UTC (22 years, 4 months ago) by harris41
Branches: MAIN
CVS tags: stable_2002_spring, stable_2002_april, HEAD
adding in POD documentation; changing void context map statements to
foreach statements; removing references to Apache::lonnet::fileembstyle
and using Apache::loncommon::fileembstyle -Scott Harrison

    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.26 2001/12/17 00:57:59 harris41 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: # (Server for RAT Maps
   29: #
   30: # (Edit Handler for RAT Maps
   31: # (TeX Content Handler
   32: #
   33: # YEAR=2000
   34: # 05/29/00,05/30 Gerd Kortemeyer)
   35: # 7/1 Gerd Kortemeyer)
   36: # 7/1,7/3,7/4,7/7,7/8,7/10 Gerd Kortemeyer)
   37: #
   38: # 7/15,7/17,7/18,8/1,8/2,8/4,8/5,8/21,8/22,8/23,8/30,
   39: # 9/2,9/4,9/29,9/30,10/2,10/11,10/30,10/31,
   40: # 11/1,11/2,11/14,11/16,11/22,12/28,
   41: # YEAR=2001
   42: # 07/05/01,08/30,08/31 Gerd Kortemeyer
   43: # 12/16 Scott Harrison
   44: #
   45: ###
   46: 
   47: package Apache::lonuserstate;
   48: 
   49: # ------------------------------------------------- modules used by this module
   50: use strict;
   51: use Apache::Constants qw(:common :http);
   52: use Apache::File;
   53: use HTML::TokeParser;
   54: use Apache::lonnet();
   55: use Apache::loncommon();
   56: use GDBM_File;
   57: use Apache::lonmsg;
   58: use Safe;
   59: use Safe::Hole;
   60: use Opcode;
   61: 
   62: # ---------------------------------------------------- Globals for this package
   63: 
   64: my $pc;      # Package counter
   65: my %hash;    # The big tied hash
   66: my %parmhash;# The hash with the parameters
   67: my @cond;    # Array with all of the conditions
   68: my $errtext; # variable with all errors
   69: my $retfurl; # variable with the very first URL in the course
   70: 
   71: # --------------------------------------------------------- Loads map from disk
   72: 
   73: sub loadmap { 
   74:     my $uri=shift;
   75:     if ($hash{'map_pc_'.$uri}) { return OK; }
   76: 
   77:     $pc++;
   78:     my $lpc=$pc;
   79:     $hash{'map_pc_'.$uri}=$lpc;
   80:     $hash{'map_id_'.$lpc}=$uri;
   81: 
   82:     my $fn='/home/httpd/html'.$uri;
   83: 
   84:     unless (($fn=~/\.sequence$/) ||
   85:             ($fn=~/\.page$/)) { 
   86:        $errtext.="Invalid map: $fn\n";
   87:        return OK; 
   88:     }
   89: 
   90:     my $ispage=($fn=~/\.page$/);
   91: 
   92:     unless (-e $fn) {
   93: 	my $returned=Apache::lonnet::repcopy($fn);
   94:         unless ($returned eq OK) {
   95:            $errtext.="Could not import: $fn - ";
   96:            if ($returned eq HTTP_SERVICE_UNAVAILABLE) {
   97: 	      $errtext.="Server unavailable\n";
   98:            }
   99:            if ($returned eq HTTP_NOT_FOUND) {
  100: 	      $errtext.="File not found\n";
  101:            }
  102:            if ($returned eq FORBIDDEN) {
  103: 	      $errtext.="Access forbidden\n";
  104:            }
  105:            return OK;
  106:        }
  107:     }
  108: 
  109:     if (-e $fn) {
  110:         my @content;
  111:         {
  112: 	    my $fh=Apache::File->new($fn);
  113:             @content=<$fh>;
  114:         }
  115:         my $instr=join('',@content);
  116:         my $parser = HTML::TokeParser->new(\$instr);
  117:         my $token;
  118: 
  119:         my $linkpc=0;
  120: 
  121:         $fn=~/\.(\w+)$/;
  122: 
  123:         $hash{'map_type_'.$lpc}=$1;
  124: 
  125:         while ($token = $parser->get_token) {
  126: 	    if ($token->[0] eq 'S') {
  127:                 if ($token->[1] eq 'resource') {
  128: # -------------------------------------------------------------------- Resource
  129: 
  130:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  131: 
  132:                     $hash{'kind_'.$rid}='res';
  133:                     $hash{'title_'.$rid}=$token->[2]->{'title'};
  134:                     my $turi=$token->[2]->{'src'};
  135:                     unless ($ispage) {
  136:                         $turi=~/\.(\w+)$/;
  137:                         my $embstyle=&Apache::loncommon::fileembstyle($1);
  138:                         if ($token->[2]->{'external'} eq 'true') {
  139:                             $turi=~s/^http\:\/\//\/adm\/wrapper\/ext\//;
  140:                         } else {
  141:                            my $embstyle=&Apache::loncommon::fileembstyle($1);
  142:                            if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
  143: 			       $turi='/adm/wrapper'.$turi;
  144:                            }
  145:                         }
  146: 		    }
  147:                     $hash{'src_'.$rid}=$turi;
  148: 
  149:                     if (defined($hash{'ids_'.$turi})) {
  150:                         $hash{'ids_'.$turi}.=','.$rid;
  151:                     } else {
  152:                         $hash{'ids_'.$turi}=''.$rid;
  153:                     }
  154: 
  155:                     if ($token->[2]->{'external'} eq 'true') {
  156:                         $hash{'ext_'.$rid}='true:';
  157:                     } else {
  158:                         $hash{'ext_'.$rid}='false:';
  159:                     }
  160:                     if ($token->[2]->{'type'}) {
  161: 			$hash{'type_'.$rid}=$token->[2]->{'type'};
  162:                         if ($token->[2]->{'type'} eq 'start') {
  163: 			    $hash{'map_start_'.$uri}="$rid";
  164:                         }
  165:                         if ($token->[2]->{'type'} eq 'finish') {
  166: 			    $hash{'map_finish_'.$uri}="$rid";
  167:                         }
  168:                     }  else {
  169:                         $hash{'type_'.$rid}='normal';
  170:                     }
  171: 
  172:                     if (($turi=~/\.sequence$/) ||
  173:                         ($turi=~/\.page$/)) {
  174:                         $hash{'is_map_'.$rid}=1;
  175:                         &loadmap($turi);
  176:                     } 
  177:                     
  178:                 } elsif ($token->[1] eq 'condition') {
  179: # ------------------------------------------------------------------- Condition
  180: 
  181:                     my $rid=$lpc.'.'.$token->[2]->{'id'};
  182: 
  183:                     $hash{'kind_'.$rid}='cond';
  184:                     $cond[$#cond+1]=$token->[2]->{'value'};
  185:                     $hash{'condid_'.$rid}=$#cond;
  186:                     if ($token->[2]->{'type'}) {
  187:                         $cond[$#cond].=':'.$token->[2]->{'type'};
  188:                     }  else {
  189:                         $cond[$#cond].=':normal';
  190:                     }
  191: 
  192:                 } elsif ($token->[1] eq 'link') {
  193: # ----------------------------------------------------------------------- Links
  194: 
  195:                     $linkpc++;
  196:                     my $linkid=$lpc.'.'.$linkpc;
  197: 
  198:                     my $goesto=$lpc.'.'.$token->[2]->{'to'};
  199:                     my $comesfrom=$lpc.'.'.$token->[2]->{'from'};
  200:                     my $undercond=0;
  201: 
  202:                     if ($token->[2]->{'condition'}) {
  203: 			$undercond=$lpc.'.'.$token->[2]->{'condition'};
  204:                     }
  205: 
  206:                     $hash{'goesto_'.$linkid}=$goesto;
  207:                     $hash{'comesfrom_'.$linkid}=$comesfrom;
  208:                     $hash{'undercond_'.$linkid}=$undercond;
  209: 
  210:                     if (defined($hash{'to_'.$comesfrom})) {
  211:                         $hash{'to_'.$comesfrom}.=','.$linkid;
  212:                     } else {
  213:                         $hash{'to_'.$comesfrom}=''.$linkid;
  214:                     }
  215:                     if (defined($hash{'from_'.$goesto})) {
  216:                         $hash{'from_'.$goesto}.=','.$linkid;
  217:                     } else {
  218:                         $hash{'from_'.$goesto}=''.$linkid;
  219:                     }
  220:                 } elsif ($token->[1] eq 'param') {
  221: # ------------------------------------------------------------------- Parameter
  222: 
  223:                     my $referid=$lpc.'.'.$token->[2]->{'to'};
  224:                     my $part=$token->[2]->{'part'};
  225:                     unless ($part) { $part=0; }
  226:                     my $newparam=
  227: 			&Apache::lonnet::escape($token->[2]->{'type'}).':'.
  228: 			&Apache::lonnet::escape($part.'.'.
  229:                          $token->[2]->{'name'}).'='.
  230: 			&Apache::lonnet::escape($token->[2]->{'value'});
  231:                     if (defined($hash{'param_'.$referid})) {
  232:                         $hash{'param_'.$referid}.='&'.$newparam;
  233:                     } else {
  234:                         $hash{'param_'.$referid}=''.$newparam;
  235:                     }
  236: 
  237:                 } 
  238: 
  239:             }
  240:         }
  241: 
  242:     } else {
  243:         $errtext.='Map not loaded: The file does not exist. ';
  244:     }
  245: }
  246: 
  247: # --------------------------------------------------------- Simplify expression
  248: 
  249: sub simplify {
  250:    my $expression=shift;
  251: # (8)=8
  252:    $expression=~s/\((\d+)\)/$1/g;
  253: # 8&8=8
  254:    $expression=~s/(\D)(\d+)\&\2(\D)/$1$2$3/g;
  255: # 8|8=8
  256:    $expression=~s/(\D)(\d+)\|\2(\D)/$1$2$3/g;
  257: # (5&3)&4=5&3&4
  258:    $expression=~s/\((\d+)((?:\&\d+)+)\)\&(\d+\D)/$1$2\&$3/g;
  259: # (((5&3)|(4&6)))=((5&3)|(4&6))
  260:    $expression=~
  261:        s/\((\(\(\d+(?:\&\d+)*\)(?:\|\(\d+(?:\&\d+)*\))+\))\)/$1/g;
  262: # ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
  263:    $expression=~
  264:        s/\((\(\d+(?:\&\d+)*\))((?:\|\(\d+(?:\&\d+)*\))+)\)\|(\(\d+(?:\&\d+)*\))/\($1$2\|$3\)/g;
  265:    return $expression;
  266: }
  267: 
  268: # -------------------------------------------------------- Build condition hash
  269: 
  270: sub traceroute {
  271:     my ($sofar,$rid,$beenhere)=@_;
  272:     $sofar=simplify($sofar);
  273:     unless ($beenhere=~/\&$rid\&/) {
  274:        $beenhere.=$rid.'&';  
  275:        if ($retfurl eq '') {
  276:            $retfurl=$hash{'src_'.$rid};
  277:        }
  278:        if (defined($hash{'conditions_'.$rid})) {
  279: 	   $hash{'conditions_'.$rid}=simplify(
  280:            '('.$hash{'conditions_'.$rid}.')|('.$sofar.')');
  281:        } else {
  282:            $hash{'conditions_'.$rid}=$sofar;
  283:        }
  284:        if (defined($hash{'is_map_'.$rid})) {
  285:            if (defined($hash{'map_start_'.$hash{'src_'.$rid}})) {
  286: 	       &traceroute($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},'&');
  287:                if (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) {
  288: 		   $sofar=
  289:                   $hash{'conditions_'.$hash{'map_finish_'.$hash{'src_'.$rid}}};
  290:                }
  291:            }
  292:        }
  293:        if (defined($hash{'to_'.$rid})) {
  294:           foreach (split(/\,/,$hash{'to_'.$rid})) {
  295: 		my $further=$sofar;
  296:                 if ($hash{'undercond_'.$_}) {
  297: 		   if (defined($hash{'condid_'.$hash{'undercond_'.$_}})) {
  298:   		       $further=simplify('('.$further.')&('.
  299:                               $hash{'condid_'.$hash{'undercond_'.$_}}.')');
  300: 		   } else {
  301:                        $errtext.='Undefined condition ID: '
  302:                                  .$hash{'undercond_'.$_}.'. ';
  303:                    }
  304:                 }
  305:                 &traceroute($further,$hash{'goesto_'.$_},$beenhere);
  306:           }
  307:        }
  308:     }
  309: }
  310: 
  311: # ------------------------------ Cascading conditions, quick access, parameters
  312: 
  313: sub accinit {
  314:     my ($uri,$short,$fn)=@_;
  315:     my %acchash=();
  316:     my %captured=();
  317:     my $condcounter=0;
  318:     $acchash{'acc.cond.'.$short.'.0'}=0;
  319:     foreach (keys %hash) {
  320:        if ($_=~/^conditions/) {
  321: 	  my $expr=$hash{$_};
  322:          foreach ($expr=~m/(\(\(\d+(?:\&\d+)+\)(?:\|\(\d+(?:\&\d+)+\))+\))/g) {
  323:              my $sub=$_;
  324:              my $orig=$_;
  325:       $sub=~/\(\((\d+\&(:?\d+\&)*)(?:\d+\&*)+\)(?:\|\(\1(?:\d+\&*)+\))+\)/;
  326:              my $factor=$1;
  327:              $sub=~s/$factor//g;
  328:              $sub=~s/^\(/\($factor\(/;
  329: 	     $sub.=')';
  330:              $sub=simplify($sub);
  331:              $orig=~s/(\W)/\\$1/g;
  332:  	     $expr=~s/$orig/$sub/;
  333: 	  }
  334:           $hash{$_}=$expr;
  335:           unless (defined($captured{$expr})) {
  336: 	      $condcounter++;
  337:               $captured{$expr}=$condcounter;
  338:               $acchash{'acc.cond.'.$short.'.'.$condcounter}=$expr;
  339:           } 
  340:        } elsif ($_=~/^param_(\d+)\.(\d+)/) {
  341:           my $prefix=&Apache::lonnet::declutter($hash{'map_id_'.$1}).
  342:       '___'.$2.'___'.&Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
  343:           foreach (split(/\&/,$hash{$_})) {
  344: 	     my ($typename,$value)=split(/\=/,$_);
  345:              my ($type,$name)=split(/\:/,$typename);
  346:              $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name)}=
  347:                                    &Apache::lonnet::unescape($value);
  348: 	     $parmhash{$prefix.'.'.&Apache::lonnet::unescape($name).'.type'}=
  349:                                    &Apache::lonnet::unescape($type);
  350:           }
  351:        }
  352:     }
  353:     foreach (keys %hash) {
  354: 	if ($_=~/^ids/) {
  355: 	  foreach (split(/\,/,$hash{$_})) {
  356: 	    my $resid=$_;
  357:             my $uri=$hash{'src_'.$resid};
  358:             $uri=~s/^\/adm\/wrapper//;
  359:             my @uriparts=split(/\//,$uri);
  360:             my $urifile=$uriparts[$#uriparts];
  361:             $#uriparts--;
  362:             my $uripath=join('/',@uriparts);
  363:             $uripath=~s/^\/res\///;
  364:            if ($uripath) {
  365:             my $uricond='0';
  366:             if (defined($hash{'conditions_'.$resid})) {
  367:  		$uricond=$captured{$hash{'conditions_'.$resid}};
  368:             }
  369:             if (defined($acchash{'acc.res.'.$short.'.'.$uripath})) {
  370:                 if ($acchash{'acc.res.'.$short.'.'.$uripath}=~
  371:                    /(\&$urifile\:[^\&]*)/) {
  372: 		    my $replace=$1;
  373:                     $acchash{'acc.res.'.$short.'.'.$uripath}
  374:                      =~s/$replace/$replace\|$uricond/;
  375:                 } else {
  376: 		   $acchash{'acc.res.'.$short.'.'.$uripath}.=
  377:                      $urifile.':'.$uricond.'&';
  378: 	        }
  379:             } else {
  380:                 $acchash{'acc.res.'.$short.'.'.$uripath}=
  381:                  '&'.$urifile.':'.$uricond.'&';
  382:             }
  383:            } 
  384:          }
  385:       }
  386:     }
  387:     $acchash{'acc.res.'.$short.'.'}='&:0&';
  388:     my $courseuri=$uri;
  389:     $courseuri=~s/^\/res\///;
  390:     &Apache::lonnet::delenv('(acc\.|httpref\.)');
  391:     &Apache::lonnet::appenv(%acchash,
  392:                             "request.course.id"  => $short,
  393:                             "request.course.fn"  => $fn,
  394:                             "request.course.uri" => $courseuri); 
  395: }
  396: 
  397: # ---------------------------------------------------- Read map and all submaps
  398: 
  399: sub readmap {
  400:    my $short=shift;
  401:    $short=~s/^\///;
  402:    my %cenv=&Apache::lonnet::coursedescription($short);
  403:    my $fn=$cenv{'fn'};
  404:    my $uri;
  405:    $short=~s/\//\_/g;
  406:    unless ($uri=$cenv{'url'}) { 
  407:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  408:                        "Could not load course $short.</font>"); 
  409:       return 'No course data available.';
  410:    }
  411:    @cond=('true:normal');
  412:    unlink($fn.'.db');
  413:    unlink($fn.'_symb.db');
  414:    unlink($fn.'.state');
  415:    unlink($fn.'parms.db');
  416:    $retfurl='';
  417:    if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) &&
  418:        (tie(%parmhash,'GDBM_File',$fn.'_parms.db',&GDBM_WRCREAT,0640))) {
  419:     %hash=();
  420:     %parmhash=();
  421:     $errtext='';
  422:     $pc=0;
  423:     loadmap($uri);
  424:     if (defined($hash{'map_start_'.$uri})) {
  425:         &traceroute('0',$hash{'map_start_'.$uri},'&');
  426:         &accinit($uri,$short,$fn);
  427:     }
  428:     unless ((untie(%hash)) && (untie(%parmhash))) {
  429:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  430:                        "Could not untie coursemap $fn for $uri.</font>"); 
  431:     }
  432:     {
  433:      my $cfh;
  434:      if ($cfh=Apache::File->new(">$fn.state")) {
  435:         print $cfh join("\n",@cond);
  436:      } else {
  437:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  438:                        "Could not write statemap $fn for $uri.</font>"); 
  439:      }
  440:     }  
  441:    } else {
  442:       &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  443:                        "Could not tie coursemap $fn for $uri.</font>"); 
  444:    }
  445:    &Apache::lonmsg::author_res_msg($ENV{'request.course.uri'},$errtext);
  446:    return ($retfurl,$errtext);
  447: }
  448: 
  449: # ------------------------------------------------------- Evaluate state string
  450: 
  451: sub evalstate {
  452: 
  453:     my $fn=$ENV{'request.course.fn'}.'.state';
  454:     my $state='2';
  455:     if (-e $fn) {
  456:        my @conditions=();
  457:        {
  458:         my $fh=Apache::File->new($fn);
  459:         @conditions=<$fh>;
  460:        }  
  461:        my $safeeval = new Safe;
  462:        my $safehole = new Safe::Hole;
  463:        $safeeval->permit("entereval");
  464:        $safeeval->permit(":base_math");
  465:        $safeeval->deny(":base_io");
  466:        $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  467:        foreach (@conditions) {
  468: 	   my $line=$_;
  469:            chomp($line);
  470: 	   my ($condition,$weight)=split(/\:/,$_);
  471:            if ($safeeval->reval($condition)) {
  472: 	       if ($weight eq 'force') {
  473: 		   $state.='3';
  474:                } else {
  475:                    $state.='2';
  476:                }
  477:            } else {
  478:                if ($weight eq 'stop') {
  479: 		   $state.='0';
  480:                } else {
  481:                    $state.='1';
  482:                }
  483:            }
  484:        }
  485:     }
  486:     &Apache::lonnet::appenv('user.state.'.$ENV{'request.course.id'} => $state);
  487:     return $state;
  488: }
  489: 
  490: 1;
  491: __END__
  492: 
  493: =head1 NAME
  494: 
  495: Apache::lonuserstate - Construct and maintain state and binary representation
  496: of course for user
  497: 
  498: =head1 SYNOPSIS
  499: 
  500: Invoked by lonroles.pm.
  501: 
  502: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  503: 
  504: =head1 INTRODUCTION
  505: 
  506: This module constructs and maintains state and binary representation
  507: of course for user.
  508: 
  509: This is part of the LearningOnline Network with CAPA project
  510: described at http://www.lon-capa.org.
  511: 
  512: =head1 HANDLER SUBROUTINE
  513: 
  514: There is no handler subroutine.
  515: 
  516: =head1 OTHER SUBROUTINES
  517: 
  518: =over 4
  519: 
  520: =item *
  521: 
  522: loadmap() : Loads map from disk
  523: 
  524: =item *
  525: 
  526: simplify() : Simplify expression
  527: 
  528: =item *
  529: 
  530: traceroute() : Build condition hash
  531: 
  532: =item *
  533: 
  534: accinit() : Cascading conditions, quick access, parameters
  535: 
  536: =item *
  537: 
  538: readmap() : Read map and all submaps
  539: 
  540: =item *
  541: 
  542: evalstate() : Evaluate state string
  543: 
  544: =back
  545: 
  546: =cut

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