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

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 12 Sep 2003 19:00:54 -0000


matthew		Fri Sep 12 15:00:54 2003 EDT

  Modified files:              
    /loncom/interface/spreadsheet	assesscalc.pm studentcalc.pm 
  Log:
  Cache student performance data (only make 1 call to get_current_state)
  Cache export rows and write them only when asked to or when the computations
  are done.
  
  
Index: loncom/interface/spreadsheet/assesscalc.pm
diff -u loncom/interface/spreadsheet/assesscalc.pm:1.21 loncom/interface/spreadsheet/assesscalc.pm:1.22
--- loncom/interface/spreadsheet/assesscalc.pm:1.21	Wed Sep 10 14:33:35 2003
+++ loncom/interface/spreadsheet/assesscalc.pm	Fri Sep 12 15:00:54 2003
@@ -1,5 +1,5 @@
 #
-# $Id: assesscalc.pm,v 1.21 2003/09/10 18:33:35 matthew Exp $
+# $Id: assesscalc.pm,v 1.22 2003/09/12 19:00:54 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -90,6 +90,7 @@
 ########################################################
 
 my %Exportrows;
+my %newExportrows;
 
 my $current_name;
 my $current_domain;
@@ -99,6 +100,7 @@
 my %nice_parameter_name;
 
 my %useropt;
+my %userdata;
 my %courseopt;
 
 ########################################################
@@ -110,21 +112,40 @@
 
 =item &clear_package()
 
-Reset all package variables.  
+Reset all package variables and clean up caches.
 
 =cut
 
 ########################################################
 ########################################################
 sub clear_package {
+    if (defined($current_name) &&
+        defined($current_domain) &&
+        defined($current_course) &&
+        $current_course eq $ENV{'request.course.id'} &&
+        %newExportrows) {
+        &save_cached_export_rows($current_name,$current_domain);
+    }
     undef(%Exportrows);
+    undef(%newExportrows);
     undef($current_name);
     undef($current_domain);
     undef($current_course);
     undef(%useropt);
+    undef(%userdata);
     undef(%courseopt);
 }
 
+sub save_cached_export_rows {
+    my ($sname,$sdomain) = @_;
+    my $start = Time::HiRes::time;
+    my $result = &Apache::lonnet::put
+        ('nohist_calculatedsheets_'.$ENV{'request.course.id'},
+         $newExportrows{$sname.':'.$sdomain},
+         $sdomain,$sname);
+    delete($newExportrows{$sname.':'.$sdomain});
+}
+
 sub initialize {
     &clear_package();
 }
@@ -145,6 +166,7 @@
     $current_name   = $sname;
     $current_domain = $sdomain;
     undef(%useropt);
+    undef(%userdata);
     if ($current_course ne $ENV{'request.course.id'}) {
         $current_course = $ENV{'request.course.id'};
         undef(%courseopt);
@@ -153,6 +175,7 @@
     &load_parameter_caches();
 }
 
+
 ########################################################
 ########################################################
 
@@ -194,6 +217,12 @@
         }
         $useropt{'loadtime'} = time;
     }
+    if (! %userdata) {
+        %userdata = &Apache::loncoursedata::get_current_state($current_name,
+                                                              $current_domain);
+        $userdata{'loadtime'} = time;
+    }
+    return;
 }
 
 ########################################################
@@ -207,7 +236,7 @@
 
 ########################################################
 ########################################################
-sub ensure_current_parameter_caches {
+sub ensure_current_caches {
     my $self = shift;
     ##
     ## Check for a modified parameters
@@ -216,6 +245,8 @@
         $current_course ne $ENV{'request.course.id'} ) {
         $current_course = $ENV{'request.course.id'};
         undef(%courseopt); 
+        undef(%useropt);
+        undef(%userdata);
     }
     ##
     ## Check for new user
@@ -225,6 +256,7 @@
         $current_domain = $self->{'domain'};
         $current_name   = $self->{'name'};
         undef(%useropt);
+        undef(%userdata);
     }
     &load_parameter_caches();
 }
@@ -587,13 +619,21 @@
     return;
 }
 
+sub get_problem_state {
+    my $self = shift;
+    my %student_parameters;
+    if (exists($userdata{$self->{'symb'}}) && 
+        ref($userdata{$self->{'symb'}}) eq 'HASH') {
+        %student_parameters = %{$userdata{$self->{'symb'}}};
+    }
+    return %student_parameters;
+}
 
 sub compute {
     my $self = shift;
     my ($r) = @_;
     my $connection = $r->connection();
     if ($connection->aborted()) { $self->cleanup(); return; }
-#    $self->logthis('computing');
     $self->initialize_safe_space();
     #########################################
     #########################################
@@ -622,7 +662,7 @@
     #
     # Get the values of the metadata fields
     if ($connection->aborted()) { $self->cleanup(); return; }
-    $self->ensure_current_parameter_caches();
+    $self->ensure_current_caches();
     if ($connection->aborted()) { $self->cleanup(); return; }
     %parameters = $self->get_parameter_values(keys(%parameters));
     if ($connection->aborted()) { $self->cleanup(); return; }
@@ -633,11 +673,7 @@
     }
     #
     # Get the students performance data
-    my %student_parameters = 
-        &Apache::loncoursedata::get_current_state($self->{'name'},
-                                                  $self->{'domain'},
-                                                  $self->{'symb'},
-                                                  $self->{'cid'});
+    my %student_parameters = $self->get_problem_state();
     while (my ($parm,$value) = each(%student_parameters)) {
         $parm =~ s/^resource\./stores_/;
         $parm =~ s/\./_/g;
@@ -840,15 +876,12 @@
     }
     my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
     my $timekey = $key.'.time';
-    my $newstore= join('___;___',@{$Exportrows{$symb}->{$self->{'filename'}}});
+    my $newstore= join('___;___',
+                       map {s/[^[:print:]]//g;$_;} # strip out unprintable
+                                @{$Exportrows{$symb}->{$self->{'filename'}}});
     $newstore = $self->{'filename'}.'___=___'.$newstore;
-    my $result = &Apache::lonnet::put
-        ('nohist_calculatedsheets_'.$ENV{'request.course.id'},
-         { $key     => $newstore,
-           $timekey => $Exportrows{$symb}->{'time'} },
-         $self->{'domain'},
-         $self->{'name'});
-
+    $newExportrows{$student}->{$key} = $newstore;
+    $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
     return;
 }
 
Index: loncom/interface/spreadsheet/studentcalc.pm
diff -u loncom/interface/spreadsheet/studentcalc.pm:1.19 loncom/interface/spreadsheet/studentcalc.pm:1.20
--- loncom/interface/spreadsheet/studentcalc.pm:1.19	Mon Sep  8 14:25:26 2003
+++ loncom/interface/spreadsheet/studentcalc.pm	Fri Sep 12 15:00:54 2003
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.19 2003/09/08 18:25:26 matthew Exp $
+# $Id: studentcalc.pm,v 1.20 2003/09/12 19:00:54 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -589,6 +589,8 @@
 #############################################
 sub save_export_data {
     my $self = shift;
+    &Apache::assesscalc::save_cached_export_rows($self->{'name'},
+                                                 $self->{'domain'});
     return if ($self->temporary());
     my $student = $self->{'name'}.':'.$self->{'domain'};
     return if (! exists($Exportrows{$student}));