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

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 29 May 2003 18:31:27 -0000


matthew		Thu May 29 14:31:27 2003 EDT

  Modified files:              
    /loncom/interface/spreadsheet	Spreadsheet.pm assesscalc.pm 
                                 	classcalc.pm studentcalc.pm 
  Log:
  Fixes bugs 1513 and 1516. Every time a new row is added to a spreadsheet
  the spreadsheet will be saved.  
  &set_row_numbers now actually keeps track of the maximum row found, which
  can be useful if you need to add a new row...
  Also fixed bug in caching of spreadsheets - was storing the 'join'd version
  of the formulas when I expected a hash reference.
  
  
Index: loncom/interface/spreadsheet/Spreadsheet.pm
diff -u loncom/interface/spreadsheet/Spreadsheet.pm:1.11 loncom/interface/spreadsheet/Spreadsheet.pm:1.12
--- loncom/interface/spreadsheet/Spreadsheet.pm:1.11	Thu May 29 09:39:38 2003
+++ loncom/interface/spreadsheet/Spreadsheet.pm	Thu May 29 14:31:27 2003
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.11 2003/05/29 13:39:38 matthew Exp $
+# $Id: Spreadsheet.pm,v 1.12 2003/05/29 18:31:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -97,7 +97,10 @@
         chome    => $ENV{'course.'.$ENV{'request.course.id'}.'.home'},
         coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
         coursefilename => $ENV{'request.course.fn'},
-        temporary => '',
+        #
+        # Flags
+        temporary => 0,  # true if this sheet has been modified but not saved
+        new_rows  => 0, # true if this sheet has new rows
         #
         # blackout is used to determine if any data needs to be hidden from the
         # student.
@@ -1555,6 +1558,16 @@
     return;
 }
 
+sub set_row_numbers {
+    my $self = shift;
+    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
+	next if ($cell !~ /^A(\d+)$/);
+        next if (! defined($value));
+	$self->{'row_numbers'}->{$value} = $1;
+        $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
+    }
+}
+
 ##
 ## exportrow is *not* used to get the export row from a computed sub-sheet.
 ##
@@ -1579,22 +1592,23 @@
         my $cnum  = $self->{'cnum'};
         my $cdom  = $self->{'cdom'};
         my $chome = $self->{'chome'};
-        my $fn    = $self->{'filename'};
+        my $filename    = $self->{'filename'};
+        my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
         # Cache new sheet
-        $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
+        %{$spreadsheets{$cachekey}}=%f;
         # Write sheet
         foreach (keys(%f)) {
             delete($f{$_}) if ($f{$_} eq 'import');
         }
-        my $reply = &Apache::lonnet::put($fn,\%f,$cdom,$cnum);
+        my $reply = &Apache::lonnet::put($filename,\%f,$cdom,$cnum);
         return $reply if ($reply ne 'ok');
         $reply = &Apache::lonnet::put($stype.'_spreadsheets',
-                       {$fn => $ENV{'user.name'}.'@'.$ENV{'user.domain'}},
+                     {$filename => $ENV{'user.name'}.'@'.$ENV{'user.domain'}},
                                       $cdom,$cnum);
         return $reply if ($reply ne 'ok');
         if ($makedef) { 
             $reply = &Apache::lonnet::put('environment',
-                                {'spreadsheet_default_'.$stype => $fn },
+                                {'spreadsheet_default_'.$stype => $filename },
                                           $cdom,$cnum);
             return $reply if ($reply ne 'ok');
         } 
@@ -1727,14 +1741,12 @@
     return @cols;
 }
 
-sub set_row_numbers {
+sub need_to_save {
     my $self = shift;
-    my %f=$self->formulas();
-    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
-	next if ($cell !~ /^A(\d+)$/);
-        next if (! defined($value));
-	$self->{'row_numbers'}->{$value} = $1;
+    if ($self->{'new_rows'} && ! $self->temporary()) {
+        return 1;
     }
+    return 0;
 }
 
 sub get_row_number_from_key {
@@ -1746,6 +1758,7 @@
         # may not be the key we need to save
 	$self->{'maxrow'}++;
 	$self->{'row_numbers'}->{$key} = $self->{'maxrow'};
+        $self->{'new_rows'} = 1;
     }
     return $self->{'row_numbers'}->{$key};
 }
Index: loncom/interface/spreadsheet/assesscalc.pm
diff -u loncom/interface/spreadsheet/assesscalc.pm:1.11 loncom/interface/spreadsheet/assesscalc.pm:1.12
--- loncom/interface/spreadsheet/assesscalc.pm:1.11	Thu May 29 09:39:38 2003
+++ loncom/interface/spreadsheet/assesscalc.pm	Thu May 29 14:31:27 2003
@@ -1,5 +1,5 @@
 #
-# $Id: assesscalc.pm,v 1.11 2003/05/29 13:39:38 matthew Exp $
+# $Id: assesscalc.pm,v 1.12 2003/05/29 18:31:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -612,6 +612,7 @@
     #
     # Save the export data
     $self->save_export_data();
+    $self->save() if ($self->need_to_save());
     return;
 }
 
Index: loncom/interface/spreadsheet/classcalc.pm
diff -u loncom/interface/spreadsheet/classcalc.pm:1.6 loncom/interface/spreadsheet/classcalc.pm:1.7
--- loncom/interface/spreadsheet/classcalc.pm:1.6	Thu May 29 09:39:38 2003
+++ loncom/interface/spreadsheet/classcalc.pm	Thu May 29 14:31:27 2003
@@ -1,5 +1,5 @@
 #
-# $Id: classcalc.pm,v 1.6 2003/05/29 13:39:38 matthew Exp $
+# $Id: classcalc.pm,v 1.7 2003/05/29 18:31:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -282,6 +282,7 @@
     $self->constants(\%c);
     $self->formulas(\%f);
     $self->calcsheet();
+    $self->save() if ($self->need_to_save());
 }
 
 1;
Index: loncom/interface/spreadsheet/studentcalc.pm
diff -u loncom/interface/spreadsheet/studentcalc.pm:1.8 loncom/interface/spreadsheet/studentcalc.pm:1.9
--- loncom/interface/spreadsheet/studentcalc.pm:1.8	Thu May 29 09:39:38 2003
+++ loncom/interface/spreadsheet/studentcalc.pm	Thu May 29 14:31:27 2003
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.8 2003/05/29 13:39:38 matthew Exp $
+# $Id: studentcalc.pm,v 1.9 2003/05/29 18:31:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -385,18 +385,6 @@
     my ($r) = @_;
 } 
 
-sub set_row_sources {
-    my $self = shift;
-    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
-        next if ($cell !~ /^A(\d+)/ && $1 > 0);
-        my $row = $1;
-        (undef,$value) = split('__&&&__',$value);
-        $value = 'Default' if (! defined($value));
-        $self->{'row_source'}->{$row} = $value;
-    }
-    return;
-}
-
 sub compute {
     my $self = shift;
     $self->logthis('computing');
@@ -474,6 +462,20 @@
     $Exportrows{$student}->{'data'} = \@exportarray;
     # save export row
     $self->save_export_data();
+    #
+    $self->save() if ($self->need_to_save());
+    return;
+}
+
+sub set_row_sources {
+    my $self = shift;
+    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
+        next if ($cell !~ /^A(\d+)/ && $1 > 0);
+        my $row = $1;
+        (undef,$value) = split('__&&&__',$value);
+        $value = 'Default' if (! defined($value));
+        $self->{'row_source'}->{$row} = $value;
+    }
     return;
 }
 
@@ -485,6 +487,7 @@
         next if ($row == 0);
         my ($symb,undef) = split('__&&&__',$formula);
         $self->{'row_numbers'}->{$symb} = $row;
+        $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
     }
 }