--- rat/lonuserstate.pm 2011/08/09 09:15:50 1.140
+++ rat/lonuserstate.pm 2013/06/26 21:22:55 1.146
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Construct and maintain state and binary representation of course for user
#
-# $Id: lonuserstate.pm,v 1.140 2011/08/09 09:15:50 foxr Exp $
+# $Id: lonuserstate.pm,v 1.146 2013/06/26 21:22:55 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -59,6 +59,7 @@ my $retfurl; # first URL
my %randompick; # randomly picked resources
my %randompickseed; # optional seed for randomly picking resources
my %randomorder; # maps to order contents randomly
+my %randomizationcode; # code used to grade folder for bubblesheet exam
my %encurl; # URLs in this folder are supposed to be encrypted
my %hiddenurl; # this URL (or complete folder) is supposed to be hidden
@@ -140,10 +141,10 @@ sub processversionfile {
# Parameters:
# uri - URI of the map file.
# parent_rid - Resource id in the map of the parent resource (0.0 for the top level map)
-#
+# courseid - Course id for the course for which the map is being loaded
#
sub loadmap {
- my ($uri,$parent_rid)=@_;
+ my ($uri,$parent_rid,$courseid)=@_;
# Is the map already included?
@@ -236,15 +237,28 @@ sub loadmap {
# This is handled in the next chunk of code.
my @map_ids;
+ my $codechecked;
while (my $token = $parser->get_token) {
next if ($token->[0] ne 'S');
# Resource
if ($token->[1] eq 'resource') {
- my $resource_id = &parse_resource($token,$lpc,$ispage,$uri);
+ my $resource_id = &parse_resource($token,$lpc,$ispage,$uri,$courseid);
if (defined $resource_id) {
- push(@map_ids, $resource_id);
+ push(@map_ids, $resource_id);
+ unless ($codechecked) {
+ my $startsymb =
+ &Apache::lonnet::encode_symb($hash{'map_id_'.$lpc},$resource_id,
+ $hash{'src_'."$lpc.$resource_id"});
+ my $code =
+ &Apache::lonnet::EXT('resource.0.examcode',$startsymb,undef,undef,
+ undef,undef,$courseid);
+ if ($code) {
+ $randomizationcode{$parent_rid} = $code;
+ }
+ $codechecked = 1;
+ }
}
# Link
@@ -260,6 +274,7 @@ sub loadmap {
&parse_condition($token,$lpc);
}
}
+ undef($codechecked);
# Handle randomization and random selection
@@ -289,15 +304,28 @@ sub loadmap {
# TODO: Here for sure we need to pass along the username/domain
# so that we can impersonate users in lonprintout e.g.
+ my $setcode;
+ if (defined($randomizationcode{$parent_rid})) {
+ if ($env{'form.CODE'} eq '') {
+ $env{'form.CODE'} = $randomizationcode{$parent_rid};
+ $setcode = 1;
+ }
+ }
+
my $rndseed=&Apache::lonnet::rndseed($seed);
&Apache::lonnet::setup_random_from_rndseed($rndseed);
+ if ($setcode) {
+ undef($env{'form.CODE'});
+ undef($setcode);
+ }
+
# Take the set of map ids we have decoded and permute them to a
# random order based on the seed set above. All of this is
# processing the randomorder parameter if it is set, not
# randompick.
- @map_ids=&math::Random::random_permutation(@map_ids);
+ @map_ids=&Math::Random::random_permutation(@map_ids);
}
@@ -352,6 +380,7 @@ sub loadmap {
# $lpc - Map nesting level (?)
# $ispage - True if this resource is encapsulated in a .page (assembled resourcde).
# $uri - URI of the enclosing resource.
+# $courseid - Course id of the course containing the resource being parsed.
# Returns:
# Value of the id attribute of the tag.
#
@@ -372,7 +401,7 @@ sub loadmap {
sub parse_resource {
- my ($token,$lpc,$ispage,$uri) = @_;
+ my ($token,$lpc,$ispage,$uri,$courseid) = @_;
# I refuse to countenance code like this that has
# such a dirty side effect (and forcing this sub to be called within a loop).
@@ -511,7 +540,7 @@ sub parse_resource {
if (($turi=~/\.sequence$/) ||
($turi=~/\.page$/)) {
$hash{'is_map_'.$rid}=1;
- &loadmap($turi,$rid);
+ &loadmap($turi,$rid,$courseid);
}
return $token->[2]->{'id'};
}
@@ -801,7 +830,7 @@ sub simplify {
# 8&8=8
$expression=~s/([^_\.\d])([_\.\d]+)\&\2([^_\.\d])/$1$2$3/g;
# 8|8=8
- $expression=~s/([^_\.\d])([_\.\d]+)\|\2([^_\.\d])/$1$2$3/g;
+ $expression=~s/([^_\.\d])([_\.\d]+)(?:\|\2)+([^_\.\d])/$1$2$3/g;
# (5&3)&4=5&3&4
$expression=~s/\(([_\.\d]+)((?:\&[_\.\d]+)+)\)\&([_\.\d]+[^_\.\d])/$1$2\&$3/g;
# (((5&3)|(4&6)))=((5&3)|(4&6))
@@ -1052,7 +1081,18 @@ sub hiddenurls {
# -------------------------------- randomly eliminate the ones that should stay
my (undef,$id)=split(/\./,$rid);
if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
+ my $setcode;
+ if (defined($randomizationcode{$rid})) {
+ if ($env{'form.CODE'} eq '') {
+ $env{'form.CODE'} = $randomizationcode{$rid};
+ $setcode = 1;
+ }
+ }
my $rndseed=&Apache::lonnet::rndseed($id); # use id instead of symb
+ if ($setcode) {
+ undef($env{'form.CODE'});
+ undef($setcode);
+ }
&Apache::lonnet::setup_random_from_rndseed($rndseed);
my @whichids=&Math::Random::random_permuted_index($#currentrids+1);
for (my $i=1;$i<=$rndpick;$i++) { $currentrids[$whichids[$i]]=''; }
@@ -1108,9 +1148,8 @@ sub readmap {
#
# Most likely a permissions problem on the lockfile or its directory.
#
- $errtext.='
'.&mt('Map not loaded - Lock file could not be opened when reading map:').' '.$fn.'.';
$retfurl = '';
- return ($retfurl,$errtext);
+ return ($retfurl,'
'.&mt('Map not loaded - Lock file could not be opened when reading map:').' '.$fn.'.');
}
my $lock=0;
my $gotstate=0;
@@ -1126,6 +1165,7 @@ sub readmap {
undef %hiddenurl;
undef %encurl;
$retfrid='';
+ $errtext='';
my ($untiedhash,$untiedparmhash,$tiedhash,$tiedparmhash); # More state flags.
# if we got the lock, regenerate course regnerate empty files and tie them.
@@ -1220,7 +1260,9 @@ sub readmap {
#
&Apache::lonnet::appenv({"request.course.id" => $short,
"request.course.fn" => $fn,
- "request.course.uri" => $uri});
+ "request.course.uri" => $uri,
+ "request.course.tied" => time});
+
$untiedhash = untie(%hash);
$untiedparmhash = untie(%parmhash);
$gotstate = 1;
@@ -1265,6 +1307,7 @@ sub readmap {
undef %randompick;
undef %hiddenurl;
undef %encurl;
+ $errtext='';
$retfrid='';
#
# Once more through the routine of tying and loading and so on.
@@ -1367,7 +1410,7 @@ sub build_tmp_hashes {
# sub-maps.
- &loadmap($uri,'0.0');
+ &loadmap($uri,'0.0',$short);
# The code below only executes if there is a starting point for the map>
# Q/BUG??? If there is no start resource for the map should that be an error?
@@ -1376,7 +1419,8 @@ sub build_tmp_hashes {
if (defined($hash{'map_start_'.$uri})) {
&Apache::lonnet::appenv({"request.course.id" => $short,
"request.course.fn" => $fn,
- "request.course.uri" => $uri});
+ "request.course.uri" => $uri,
+ "request.course.tied" => time});
$env{'request.course.id'}=$short;
&traceroute('0',$hash{'map_start_'.$uri},'&');
&accinit($uri,$short,$fn);
@@ -1427,7 +1471,7 @@ sub unlink_tmpfiles {
my ($fn) = @_;
my $file_dir = dirname($fn);
- if ($fn eq LONCAPA::tempdir()) {
+ if ("$file_dir/" eq LONCAPA::tempdir()) {
my @files = qw (.db _symb.db .state _parms.db);
foreach my $file (@files) {
if (-e $fn.$file) {