--- loncom/interface/spreadsheet/assesscalc.pm 2005/04/07 06:56:23 1.39
+++ loncom/interface/spreadsheet/assesscalc.pm 2006/12/21 02:54:19 1.57
@@ -1,5 +1,5 @@
#
-# $Id: assesscalc.pm,v 1.39 2005/04/07 06:56:23 albertel Exp $
+# $Id: assesscalc.pm,v 1.57 2006/12/21 02:54:19 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -174,7 +174,7 @@ sub initialize {
########################################################
########################################################
sub initialize_package {
- my ($sname,$sdomain,$in_navmap) = @_;
+ my ($sname,$sdomain,$in_navmap,$calling_sheet) = @_;
$current_name = $sname;
$current_domain = $sdomain;
$navmap = $in_navmap;
@@ -185,7 +185,7 @@ sub initialize_package {
undef(%courseopt);
}
&load_cached_export_rows();
- &load_parameter_caches();
+ &load_parameter_caches($calling_sheet);
&Apache::loncoursedata::clear_internal_caches();
}
@@ -202,6 +202,7 @@ sub initialize_package {
########################################################
########################################################
sub load_parameter_caches {
+ my ($calling_sheet) = @_;
my $userprefix = $current_name.':'.$current_domain.'_';
$userprefix =~ s/:/_/g;
#
@@ -234,6 +235,10 @@ sub load_parameter_caches {
if (! %userdata) {
%userdata = &Apache::loncoursedata::get_current_state($current_name,
$current_domain);
+ my ($tmp) = %userdata;
+ if ($tmp =~ /^error:(.*)/) {
+ $calling_sheet->set_calcerror($1);
+ }
$userdata{'loadtime'} = time;
}
return;
@@ -272,7 +277,7 @@ sub ensure_current_caches {
undef(%useropt);
undef(%userdata);
}
- &load_parameter_caches();
+ &load_parameter_caches($self);
}
##################################################
@@ -290,7 +295,8 @@ Returns: The value of a parameter, or ''
This function cascades through the possible levels searching for a value for
a parameter. The levels are checked in the following order:
-user, course (at section level and course level), map, and lonnet::metadata.
+user, course (at group, section level and course level), map, and
+lonnet::metadata.
This function uses %parmhash, which must be tied prior to calling it.
This function also requires %courseopt and %useropt to be initialized for
this user and course.
@@ -301,16 +307,20 @@ this user and course.
##################################################
sub parmval {
my $self = shift;
- my ($what,$symb,$uname,$udom,$csec,$recurse)=@_;
- $uname = $self->{'name'} if (! defined($uname));
- $udom = $self->{'domain'} if (! defined($udom));
- $csec = $self->{'section'} if (! defined($csec));
- $symb = $self->{'symb'} if (! defined($symb));
+ my ($what,$symb,$uname,$udom,$csec,$recurse,$mapname,$id,$fn,$groups)=@_;
+ $uname = $self->{'name'} if (! defined($uname));
+ $udom = $self->{'domain'} if (! defined($udom));
+ $csec = $self->{'section'} if (! defined($csec));
+ $groups = $self->{'groups'} if (! defined($groups));
+ $symb = $self->{'symb'} if (! defined($symb));
#
my $result='';
#
# This should be a
- my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
+ if (!defined($mapname) || !defined($id) || !defined($fn)) {
+ ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
+ $mapname = &Apache::lonnet::deversion($mapname);
+ }
# Cascading lookup scheme
my $rwhat=$what;
$what =~ s/^parameter\_//;
@@ -332,12 +342,23 @@ sub parmval {
my $ucourselevel = $usercourseprefix.'.'.$what;
my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
- # check user
+ # check user
if (defined($uname)) {
return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
return $useropt{$ucourselevel} if (defined($useropt{$ucourselevel}));
}
+ # check groups
+ if (defined($groups) && ref($groups) eq 'ARRAY') {
+ foreach my $group (@{$groups}) {
+ foreach my $level ($symbparm,$mapparm,$what) {
+ my $item = $courseprefix.'.['.$group.'].'.$level;
+ if (defined($courseopt{$item})) {
+ return $courseopt{$item};
+ }
+ }
+ }
+ }
# check section
if (defined($csec)) {
return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
@@ -367,7 +388,8 @@ sub parmval {
if ($part eq '') { $part='0'; }
my $newwhat=$rwhat;
$newwhat=~s/\Q$space\E/$part/;
- my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1);
+ my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1,
+ $mapname,$id,$fn,$groups);
if (defined($partgeneral)) { return $partgeneral; }
}
if ($recurse) { return undef; }
@@ -512,8 +534,8 @@ sub assess_html_row {
my @rowdata = $self->get_row($row);
my $num_cols_output = 0;
my $row_html;
- if (exists($nice_parameter_name{$parameter_name})) {
- my $name = $nice_parameter_name{$parameter_name};
+ my $name=$self->get_parm_name($parameter_name);
+ if ($name ne '') {
$name =~ s/ /\ /g;
$row_html .= '
'.$name.' '.$parameter_name.' | ';
} else {
@@ -525,7 +547,7 @@ sub assess_html_row {
$row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
'#FFDDDD');
} else {
- $row_html .= '';
+ $row_html .= ' | ';
$row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
'#E0FFDD',1);
}
@@ -548,10 +570,7 @@ sub csv_rows {
# Write each row
foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
- my $description = '';
- if (exists($nice_parameter_name{$parameter_name})) {
- $description = $nice_parameter_name{$parameter_name};
- }
+ my $description = $self->get_parm_name($parameter_name);
$self->csv_output_row($filehandle,$rownum,
$parameter_name,$description);
}
@@ -575,10 +594,7 @@ sub excel_rows {
# Write each row
foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
- my $description = '';
- if (exists($nice_parameter_name{$parameter_name})) {
- $description = $nice_parameter_name{$parameter_name};
- }
+ my $description = $self->get_parm_name($parameter_name);
$self->excel_output_row($worksheet,$rownum,$rows_output++,
$parameter_name,$description);
}
@@ -588,10 +604,10 @@ sub excel_rows {
##
## Routines to support assesscalc::compute
##
-sub get_parm_names {
+sub get_parm {
my $self = shift;
my @Mandatory_parameters = @_;
- my %parameters_and_names;
+ my %parameters;
#
my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
@@ -600,14 +616,20 @@ sub get_parm_names {
my $cleaned_name = $parm;
$cleaned_name =~ s/^resource\./stores_/;
$cleaned_name =~ s/\./_/g;
- my $display = &Apache::lonnet::metadata($srcf,
- $cleaned_name.'.display');
- if (! $display) {
- $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
- }
- $parameters_and_names{$cleaned_name}=$display;
+ $parameters{$cleaned_name}=1;
+ }
+ return (keys(%parameters));
+}
+
+sub get_parm_name {
+ my $self = shift;
+ my $parm = shift;
+ my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
+ my $display = &Apache::lonnet::metadata($srcf,$parm.'.display');
+ if (! $display) {
+ $display .= &Apache::lonnet::metadata($srcf,$parm.'.name');
}
- return (%parameters_and_names);
+ return $display;
}
sub get_parameter_values {
@@ -627,8 +649,13 @@ sub get_parameter_values {
my $filename = $self->{'coursefilename'}.'_parms.db';
if (tie(%parmhash,'GDBM_File',
$self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
+ my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($self->{'symb'});
+ $mapname = &Apache::lonnet::deversion($mapname);
foreach my $parmname (@Parameters) {
- my $value = $self->parmval($parmname);
+ my $value = $self->parmval($parmname,$self->{'symb'},
+ $self->{'name'},$self->{'domain'},
+ $self->{'section'},undef,
+ $mapname,$id,$fn,$self->{'groups'});
$parameters{$parmname} =$value;
}
untie(%parmhash);
@@ -661,6 +688,7 @@ sub get_problem_state {
sub determine_parts {
my $self = shift;
+ my $check_hidden = shift;
if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
return;
}
@@ -671,9 +699,10 @@ sub determine_parts {
my $metadata = &Apache::lonnet::metadata($src,'packages');
foreach (split(',',$metadata)) {
my ($part) = (/^part_(.*)$/);
- if (defined($part) &&
- ! &Apache::loncommon::check_if_partid_hidden
- ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
+ if (!defined($part)) { next; }
+ if (!$check_hidden) { $Parts{$part}++; next; }
+ if (!&Apache::loncommon::check_if_partid_hidden
+ ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
) {
$Parts{$part}++;
}
@@ -694,22 +723,22 @@ sub parameter_part_is_valid {
return 1;
}
#
- my (undef,$part) =
- ($parameter =~ m/^(resource|stores|parameter)_([^_]+)_.*/);
- if (exists($self->{'Parts'}) &&
- exists($self->{'Parts'}->{$part}) &&
- $self->{'Parts'}->{$part} ) {
- return 1;
- } else {
- return 0;
+ my ($start,@pieces)=split('_',$parameter);
+ if ( $start !~ m/^(resource|stores|parameter)$/) { return 0; }
+ while (@pieces) {
+ pop(@pieces);
+ my $testpart=join('_',@pieces);
+ if (exists($self->{'Parts'}->{$testpart}) &&
+ $self->{'Parts'}->{$testpart} ) {
+ return 1;
+ }
}
+ return 0;
}
sub compute {
my $self = shift;
my ($r) = @_;
- my $connection = $r->connection();
- if ($connection->aborted()) { $self->cleanup(); return; }
$self->initialize_safe_space();
#########################################
#########################################
@@ -730,18 +759,11 @@ sub compute {
my %parameters; # holds underscored parameters by name
#
# Get the metadata fields and determine their proper names
- my %nice_parm_names = $self->get_parm_names(@Mandatory_parameters);
- while (my($cleaned_name,$display) = each(%nice_parm_names)) {
- $parameters{$cleaned_name}++;
- $nice_parameter_name{$cleaned_name} = $display;
- }
+ my @parameters=$self->get_parm(@Mandatory_parameters);
#
# Get the values of the metadata fields
- if ($connection->aborted()) { $self->cleanup(); return; }
$self->ensure_current_caches();
- if ($connection->aborted()) { $self->cleanup(); return; }
- %parameters = $self->get_parameter_values(keys(%parameters));
- if ($connection->aborted()) { $self->cleanup(); return; }
+ %parameters = $self->get_parameter_values(@parameters);
#
# Clean out unnecessary parameters
foreach (keys(%parameters)) {
@@ -749,19 +771,14 @@ sub compute {
}
#
# Get the students performance data
+ $self->determine_parts(($parameters{'parameter_0_hiddenparts'} ne ''));
my %student_parameters = $self->get_problem_state();
while (my ($parm,$value) = each(%student_parameters)) {
$parm =~ s/^resource\./stores_/;
$parm =~ s/\./_/g;
- $parameters{$parm} = $value;
- }
- #
- # Clean out any bad parameters
- $self->determine_parts();
- foreach my $param (keys(%parameters)) {
- if (! $self->parameter_part_is_valid($param)) {
- delete ($parameters{$param});
- }
+ # Clean out any bad parameters
+ next if (! $self->parameter_part_is_valid($parm));
+ $parameters{$parm} = $value;
}
#
# Set up the formulas and parameter values
@@ -783,7 +800,6 @@ sub compute {
}
}
}
- if ($connection->aborted()) { $self->cleanup(); return; }
#
# Move the parameters into the spreadsheet
while (my ($parm,$value) = each(%parameters)) {
@@ -795,16 +811,20 @@ sub compute {
$value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
$c{$parm} = $value;
}
+ foreach my $cell (grep(/^A/,keys(%f))) {
+ # Clean out any bad formulas
+ next if (exists($c{$f{$cell}}));
+ next if ($cell eq 'A0');
+ delete($f{$cell});
+ }
$self->formulas(\%f);
$self->constants(\%c);
- if ($connection->aborted()) { $self->cleanup(); return; }
$self->calcsheet();
#
# Store export row in cache
my @exportarray = $self->exportrow();
$self->deal_with_export_row(@exportarray);
$self->save() if ($self->need_to_save());
- if ($connection->aborted()) { $self->cleanup(); return; }
return;
}
@@ -814,6 +834,7 @@ sub compute {
sub sett {
my $self = shift;
my %t=();
+ undef(%Apache::Spreadsheet::sheet_values);
#
# Deal with the template row by copying the template formulas into each
# row.
@@ -839,8 +860,12 @@ sub sett {
while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
next if ($cell =~ /template_/);
if ($cell =~ /^A/ && $cell ne 'A0') {
- if ($formula !~ /^\!/) {
- $t{$cell}=$self->{'constants'}->{$formula};
+ if ($formula !~ /^\!/
+ && exists($self->{'constants'}->{$formula})
+ && $self->{'constants'}->{$formula} ne ''
+ ) {
+ $Apache::Spreadsheet::sheet_values{$cell}=
+ eval($self->{'constants'}->{$formula});
}
} else {
$t{$cell}=$formula;
@@ -919,7 +944,6 @@ spreadsheet only if necessary.
sub export_data {
my $self = shift;
my ($r) = @_;
- my $connection = $r->connection();
my $symb = $self->{'symb'};
if (! exists($env{'request.role.adv'}) || ! $env{'request.role.adv'} ||
! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb}) ||
@@ -930,7 +954,6 @@ sub export_data {
) {
$self->compute($r);
}
- if ($connection->aborted()) { $self->cleanup(); return; }
my @Data;
if ($self->badcalc()) {
@Data = ();
|