[LON-CAPA-cvs] cvs: loncom /interface/spreadsheet assesscalc.pm

matthew lon-capa-cvs@mail.lon-capa.org
Wed, 10 Sep 2003 18:33:35 -0000


matthew		Wed Sep 10 14:33:35 2003 EDT

  Modified files:              
    /loncom/interface/spreadsheet	assesscalc.pm 
  Log:
  1. Be more vigilent about clearing user parameter caches (%useropt)
  2. Added 'loadtime' value to %useropt which prevents it from being requested
     again and again just because the student did not have any data...
     (how many times will I create and fix the same bug?)
  3. Break compute method down to facilitate testing.
  4. More fatal warning enduced robustness in a few if statements.
  
  
Index: loncom/interface/spreadsheet/assesscalc.pm
diff -u loncom/interface/spreadsheet/assesscalc.pm:1.20 loncom/interface/spreadsheet/assesscalc.pm:1.21
--- loncom/interface/spreadsheet/assesscalc.pm:1.20	Tue Sep  9 14:46:28 2003
+++ loncom/interface/spreadsheet/assesscalc.pm	Wed Sep 10 14:33:35 2003
@@ -1,5 +1,5 @@
 #
-# $Id: assesscalc.pm,v 1.20 2003/09/09 18:46:28 www Exp $
+# $Id: assesscalc.pm,v 1.21 2003/09/10 18:33:35 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -144,6 +144,7 @@
     my ($sname,$sdomain) = @_;
     $current_name   = $sname;
     $current_domain = $sdomain;
+    undef(%useropt);
     if ($current_course ne $ENV{'request.course.id'}) {
         $current_course = $ENV{'request.course.id'};
         undef(%courseopt);
@@ -172,7 +173,6 @@
         $current_course = $ENV{'request.course.id'};
         undef(%courseopt);
         if (! defined($current_name) || ! defined($current_domain)) {
-            &Apache::lonnet::logthis('bad call to setup_parameter_caches');
             return;
         }
         my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
@@ -192,6 +192,7 @@
             }
             $useropt{$userprefix.$name}=$value;
         }
+        $useropt{'loadtime'} = time;
     }
 }
 
@@ -522,6 +523,71 @@
     return;
 }
 
+##
+## Routines to support assesscalc::compute
+##
+sub get_parm_names {
+    my $self = shift;
+    my @Mandatory_parameters = @_;
+    my %parameters_and_names;
+    #
+    my ($symap,$syid,$srcf)=split(/___/,$self->{'symb'});
+    my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
+    foreach my $parm (@Mandatory_parameters,@Metadata) {
+        next if ($parm !~ /^(resource\.|stores|parameter)_/);
+        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;
+    }
+    return (%parameters_and_names);
+}
+
+sub get_parameter_values {
+    my $self = shift();
+    my @Parameters;
+    my ($parameters) = @_;
+    if (!ref($parameters)) {
+        @Parameters = @_;
+    } elsif (ref($parameters) eq 'ARRAY') {
+        @Parameters = @$parameters;
+    } elsif (ref($parameters) eq 'HASH') {
+        @Parameters = keys(%$parameters);
+    }
+    #
+    my %parameters;
+    #
+    my $filename = $self->{'coursefilename'}.'_parms.db';
+    if (tie(%parmhash,'GDBM_File',
+            $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
+        foreach my $parmname (@Parameters) {
+            my $value = $self->parmval($parmname);
+            $parameters{$parmname} =$value;
+        }
+        untie(%parmhash);
+    } else {
+        $self->logthis('unable to tie '.$filename);
+    }
+    return %parameters;
+}
+
+sub deal_with_export_row {
+    my $self = shift();
+    my @exportarray = @_;
+    $Exportrows{$self->{'symb'}}->{'time'} = time;
+    $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
+    #
+    # Save the export data
+    $self->save_export_data();
+    return;
+}
+
+
 sub compute {
     my $self = shift;
     my ($r) = @_;
@@ -548,18 +614,8 @@
     my %parameters;   # holds underscored parameters by name
     #
     # Get the metadata fields and determine their proper names
-    my ($symap,$syid,$srcf)=&Apache::lonnet::decode_symb($self->{'symb'});
-    my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
-    foreach my $parm (@Mandatory_parameters,@Metadata) {
-        next if ($parm !~ /^(resource\.|stores|parameter)_/);
-        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');
-        }
+    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;
     }
@@ -568,17 +624,7 @@
     if ($connection->aborted()) { $self->cleanup(); return; }
     $self->ensure_current_parameter_caches();
     if ($connection->aborted()) { $self->cleanup(); return; }
-    my $filename = $self->{'coursefilename'}.'_parms.db';
-    if (tie(%parmhash,'GDBM_File',
-            $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
-        foreach my $parmname (keys(%parameters)) {
-            my $value = $self->parmval($parmname);
-            $parameters{$parmname} =$value;
-        }
-        untie(%parmhash);
-    } else {
-        $self->logthis('unable to tie '.$filename);
-    }
+    %parameters = $self->get_parameter_values(keys(%parameters));
     if ($connection->aborted()) { $self->cleanup(); return; }
     #
     # Clean out unnecessary parameters
@@ -607,7 +653,10 @@
         while (my ($parm,$value) = each(%parameters)) {
             last if ($self->blackout());
             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
-            next if ($parameters{$1.'_answerdate'}<time);
+            if ($parameters{$1.'_answerdate'} eq '' ||
+                $parameters{$1.'_answerdate'} < time) {
+                next;
+            }
             if (lc($value) eq 'no') {
                 # We must blackout this sheet
                 $self->blackout(1);
@@ -631,11 +680,7 @@
     #
     # Store export row in cache
     my @exportarray = $self->exportrow();
-    $Exportrows{$self->{'symb'}}->{'time'} = time;
-    $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
-    #
-    # Save the export data
-    $self->save_export_data();
+    $self->deal_with_export_row(@exportarray);
     $self->save() if ($self->need_to_save());
     if ($connection->aborted()) { $self->cleanup(); return; }
     return;