--- loncom/lonmap.pm 2011/10/06 10:56:49 1.3
+++ loncom/lonmap.pm 2013/01/03 20:32:40 1.9
@@ -2,7 +2,7 @@
#
# Read maps into a 'big hash'.
#
-# $Id: lonmap.pm,v 1.3 2011/10/06 10:56:49 foxr Exp $
+# $Id: lonmap.pm,v 1.9 2013/01/03 20:32:40 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -40,6 +40,8 @@ use HTML::TokeParser;
use LONCAPA;
use Apache::lonnet;
+use Apache::lonlocal;
+
use Data::Dumper;
@@ -68,6 +70,7 @@ my $retfrid;
my $username; # User for whom the map is being read.
my $userdomain; # Domain the user lives in.
+my $short_name; # Course shortname.
my %mapalias_cache; # Keeps track of map aliases -> resources detects duplicates.
my %cenv; # Course environment.
@@ -95,10 +98,7 @@ my %cenv; # Course environment.
sub simplify {
my $expression=shift;
- my $prior = ''; # This is safe as a null expression is pretty optimal.
-
- while ($prior ne $expression) {
- $prior = $expression; # Stop when the substitutions below do nothing.
+
# (0&1) = 1
$expression=~s/\(0\&([_\.\d]+)\)/$1/g;
# (8)=8
@@ -115,7 +115,8 @@ sub simplify {
# ((5&3)|(4&6))|(1&2)=(5&3)|(4&6)|(1&2)
$expression=~
s/\((\([_\.\d]+(?:\&[_\.\d]+)*\))((?:\|\([_\.\d]+(?:\&[_\.\d]+)*\))+)\)\|(\([_\.\d]+(?:\&[_\.\d]+)*\))/\($1$2\|$3\)/g;
- }
+
+
return $expression;
}
@@ -294,7 +295,6 @@ sub process_versions {
#
sub versionerror {
my ($uri, $used, $unused) = @_;
- my ($uri,$usedversion,$unusedversion)=@_;
return '
'.
&mt('Version discrepancy: resource [_1] included in both version [_2] and version [_3]. Using version [_2].',
$uri,$used,$unused).'
';
@@ -373,6 +373,32 @@ sub append_version {
return $uri;
}
+#------------------------------------------------------------------------------
+#
+# Misc. utilities that don't fit into the other classifications.
+
+# Determine if the specified user has an 'advanced' role in a course.
+# Parameters:
+# cenv - reference to a course environment.
+# username - Name of the user we care about.
+# domain - Domain in which the user is defined.
+# Returns:
+# 0 - User does not have an advanced role in the course.
+# 1 - User does have an advanced role in the course.
+#
+sub has_advanced_role {
+ my ($username, $domain) = @_;
+
+ my %adv_roles = &Apache::lonnet::get_course_adv_roles($short_name);
+ my $merged_username = $username . ':' . $domain;
+ foreach my $user (values %adv_roles) {
+ if ($merged_username eq $user) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
#--------------------------------------------------------------------------------
# Post processing subs:
sub hiddenurls {
@@ -405,7 +431,6 @@ sub hiddenurls {
my (undef,$id)=split(/\./,$rid);
if ($randompickseed{$rid}) { $id=$randompickseed{$rid}; }
my $rndseed=&Apache::lonnet::rndseed($id, $courseid, $udom, $uname, \%cenv); # use id instead of symb
- &Apache::lonnet::logthis("lonmap random seed: $rndseed");
&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]]=''; }
@@ -775,6 +800,7 @@ sub parse_param {
# $lpc - Map nesting level (?)
# $ispage - True if this resource is encapsulated in a .page (assembled resourcde).
# $uri - URI of the enclosing resource.
+# $code - CODE for which resource is being parsed (CODEd assignments).
# $hash - Reference to the hash we are building.
#
# Returns:
@@ -797,7 +823,7 @@ sub parse_param {
#
sub parse_resource {
- my ($token,$lpc,$ispage,$uri, $hash) = @_;
+ my ($token,$lpc,$ispage,$uri,$code,$hash) = @_;
# I refuse to countenance code like this that has
# such a dirty side effect (and forcing this sub to be called within a loop).
@@ -944,7 +970,7 @@ sub parse_resource {
if (($turi=~/\.sequence$/) ||
($turi=~/\.page$/)) {
$hash->{'is_map_'.$rid}='1'; # String in lonuserstate.
- &read_map($turi,$rid, $hash);
+ &read_map($turi,$rid,$code,$hash);
}
return $token->[2]->{'id'};
}
@@ -1152,7 +1178,8 @@ sub parse_mapalias_param {
# $parent_rid - map number qualified id of the parent of the map being read.
# For the top level course map this is 0.0. For the first nested
# map 1.n where n is the id of the resource within the
-# top level map and so on.
+# top level map and so on.
+# $code - CODE for which map is being read (CODEd assignments).
# $hash - Reference to a hash that will become the big hash for the course
# This hash is modified as per the map description.
# Side-effects:
@@ -1162,7 +1189,7 @@ sub parse_mapalias_param {
#
#
sub read_map {
- my ($uri, $parent_rid, $hash) = @_;
+ my ($uri, $parent_rid, $code, $hash) = @_;
# Check for duplication: A map may only be included once.
@@ -1208,6 +1235,7 @@ sub read_map {
my $ispage = ($filename =~/\.page$/);
unless ($ispage || ($filename =~ /\.sequence$/)) {
+ &Apache::lonnet::logthis("invalid: $filename : $uri");
throw Error::Simple(&mt("
Invalid map: [_1]", $filename));
}
@@ -1262,7 +1290,7 @@ sub read_map {
# Resource
if ($token->[1] eq 'resource') {
- my $resource_id = &parse_resource($token,$lmap_no,$ispage,$uri, $hash);
+ my $resource_id = &parse_resource($token,$lmap_no,$ispage,$uri,$code,$hash);
if (defined $resource_id) {
push(@map_ids, $resource_id);
}
@@ -1286,7 +1314,7 @@ sub read_map {
#
if ($randomize) {
- if (!$env{'request.role.adv'}) {
+ if (!&has_advanced_role($username, $userdomain) || $code) {
my $seed;
# In the advanced role, the map's random seed
@@ -1308,7 +1336,11 @@ sub read_map {
}
- my $rndseed=&Apache::lonnet::rndseed($seed, $username, $userdomain);
+ my $rndseed=&Apache::lonnet::rndseed($seed, '',
+ $userdomain, $username,
+ \%cenv);
+
+
&Apache::lonnet::setup_random_from_rndseed($rndseed);
# Take the set of map ids we have decoded and permute them to a
@@ -1316,10 +1348,8 @@ sub read_map {
# 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);
}
-
-
my $from = shift(@map_ids);
my $from_rid = $lmap_no.'.'.$from;
$hash->{'map_start_'.$uri} = $from_rid;
@@ -1341,6 +1371,7 @@ sub read_map {
$hash->{'type_'.$from_rid}='finish';
}
+
# The last parsing pass parses the tags that associate a name
# with resource ids.
@@ -1367,6 +1398,7 @@ sub read_map {
# $cdom - Domain in which the course is evaluated.
# $uname - Name of the user for whom the course is being read
# $udom - Name of the domain of the user for whom the course is being read.
+# $code - CODE for which course is being read (CODEd assignments)
# $target_hash- Reference to the target hash into which all of this is read.
# Note tht some of the hash entries we need to build require knowledge of the
# course URI.. these are expected to be filled in by the caller.
@@ -1375,11 +1407,11 @@ sub read_map {
#
#
sub loadmap {
- my ($cnum, $cdom, $uname, $udom, $target_hash) = @_;
+ my ($cnum, $cdom, $uname, $udom, $code, $target_hash) = @_;
- # Clear the auxillary hashes and the cond array.
+ # Clear the auxiliary hashes and the cond array.
%randompick = ();
@@ -1387,7 +1419,7 @@ sub loadmap {
%encurl = ();
%hiddenurl = ();
%parmhash = ();
- @cond = ();
+ @cond = ('true:normal'); # Initial value for cond 0.
$retfrid = '';
$username = '';
$userdomain = '';
@@ -1400,30 +1432,29 @@ sub loadmap {
$username = $uname;
$userdomain = $udom;
- my $short_name = $cdom .'/' . $cnum;
+ $short_name = $cdom .'/' . $cnum;
my $retfurl;
try {
# Get the information we need about the course.
- # Return without filling in anything if we can't get any info:
-
- %cenv = &Apache::lonnet::coursedescription($short_name,
- {'freshen_cache' => 1,
- 'user' => $uname});
-
- unless ($cenv{'url'}) {
- &Apache::lonnet::logthis("lonmap::loadmap failed: $cnum/$cdom - did not get url");
- return;
- }
- &Apache::lonnet::logthis("Course environment: \n" . Dumper(\%cenv));
-
- $course_id = $cdom . '_' . $cnum; # Long course id.
-
- # Load the version information into the hash
-
-
+ # Return without filling in anything if we can't get any info:
+
+ %cenv = &Apache::lonnet::coursedescription($short_name,
+ {'freshen_cache' => 1,
+ 'user' => $uname});
+
+ unless ($cenv{'url'}) {
+ &Apache::lonnet::logthis("lonmap::loadmap failed: $cnum/$cdom - did not get url");
+ return;
+ }
+
+ $course_id = $cdom . '_' . $cnum; # Long course id.
+
+ # Load the version information into the hash
+
+
&process_versions(\%cenv, $target_hash);
@@ -1447,7 +1478,8 @@ sub loadmap {
$target_hash->{'context.userdom'} = $userdomain;
$target_hash->{'context.courseid'} = $course_id;
- &read_map($course_uri, '0.0', $target_hash);
+
+ &read_map($course_uri, '0.0', $code, $target_hash);
#