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

matthew lon-capa-cvs@mail.lon-capa.org
Tue, 22 Oct 2002 18:54:53 -0000


matthew		Tue Oct 22 14:54:53 2002 EDT

  Modified files:              
    /loncom/interface	lonspreadsheet.pm 
  Log:
  Moved calculation of the spreadsheet outside of the safe space, with calls
  going in.
  Changed calling structure of &setformulas slightly.
  Removed calls to study in safe space.
  
  
Index: loncom/interface/lonspreadsheet.pm
diff -u loncom/interface/lonspreadsheet.pm:1.122 loncom/interface/lonspreadsheet.pm:1.123
--- loncom/interface/lonspreadsheet.pm:1.122	Tue Oct 22 09:29:57 2002
+++ loncom/interface/lonspreadsheet.pm	Tue Oct 22 14:54:53 2002
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.122 2002/10/22 13:29:57 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.123 2002/10/22 18:54:53 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -754,7 +754,6 @@
 sub MINPARM {
     my ($expression) = @_;
     my $min = undef;
-    study($expression);
     foreach $parameter (keys(%c)) {
         next if ($parameter !~ /$expression/);
         if ((! defined($min)) || ($min > $c{$parameter})) {
@@ -777,7 +776,6 @@
 sub MAXPARM {
     my ($expression) = @_;
     my $max = undef;
-    study($expression);
     foreach $parameter (keys(%c)) {
         next if ($parameter !~ /$expression/);
         if ((! defined($min)) || ($max < $c{$parameter})) {
@@ -829,7 +827,6 @@
         my $returnvalue = '';
         my @matches = ();
         $#matches = -1;
-        study $expression;
         foreach $parameter (keys(%c)) {
             push @matches,$parameter if ($parameter =~ /$expression/);
         }
@@ -918,32 +915,6 @@
     $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
 }
 
-sub calc {
-    undef %sheet_values;
-    &sett();
-    my $notfinished=1;
-    my $lastcalc='';
-    my $depth=0;
-    while ($notfinished) {
-	$notfinished=0;
-        foreach (keys(%t)) {
-            my $old=$sheet_values{$_};
-            $sheet_values{$_}=eval $t{$_};
-	    if ($@) {
-		undef %sheet_values;
-                return $_.': '.$@;
-            }
-	    if ($sheet_values{$_} ne $old) { $notfinished=1; $lastcalc=$_; }
-        }
-        $depth++;
-        if ($depth>100) {
-	    undef %sheet_values;
-            return $lastcalc.': Maximum calculation depth exceeded';
-        }
-    }
-    return '';
-}
-
 # ------------------------------------------- End of "Inside of the safe space"
 ENDDEFS
     $safeeval->reval($code);
@@ -1033,9 +1004,36 @@
 }
 
 # ------------------------------------------------ Add or change formula values
+sub update_values {
+    my $sheet = shift;
+    %{$sheet->{'safe'}->varglob('sheet_values')}=%{$sheet->{'values'}};
+    return undef;
+}
+
+sub setvalues {
+    my $sheet=shift;
+    my ($values) = @_;
+    $values = {} if (! defined($values));
+    if (! ref($values)) {
+        my %tmp   = @_;
+        $values = \%tmp;
+    }
+    $sheet->{'values'} = $values;
+    %{$sheet->{'safe'}->varglob('sheet_values')}=%{$sheet->{'values'}};
+    return undef;
+}
+
 sub setformulas {
-    my ($sheet)=shift;
+    my $sheet=shift;
+    my ($formulas) = @_;
+    $formulas = {} if (! defined($formulas));
+    if (! ref($formulas)) {
+        my %tmp   = @_;
+        $formulas = \%tmp;
+    }
+    $sheet->{'f'} = $formulas;
     %{$sheet->{'safe'}->varglob('f')}=%{$sheet->{'f'}};
+    return undef;
 }
 
 # ------------------------------------------------ Add or change formula values
@@ -1068,9 +1066,38 @@
 # ------------------------------------------------------- Calculate spreadsheet
 sub calcsheet {
     my $sheet=shift;
-    my $result =  $sheet->{'safe'}->reval('&calc();');
-    %{$sheet->{'values'}} = %{$sheet->{'safe'}->varglob('sheet_values')};
-    return $result;
+    &setvalues($sheet,undef);
+    $sheet->{'safe'}->reval('&sett();');
+    my %t = %{$sheet->{'safe'}->varglob('t')};
+    my $notfinished=1;
+    my $lastcalc='';
+    my $depth=0;
+    while ($notfinished) {
+	$notfinished=0;
+        foreach (keys(%t)) {
+            my $old=$sheet->{'values'}->{$_};
+            $sheet->{'values'}->{$_}=$sheet->{'safe'}->reval($t{$_}.';');
+	    if ($@) {
+		&setvalues($sheet,undef);
+                return $_.': '.$@;
+            }
+	    if ($sheet->{'values'}->{$_} ne $old) { 
+                $notfinished=1; 
+                $lastcalc=$_;
+            }
+        }
+        if ($notfinished) {
+            %{$sheet->{'safe'}->varglob('sheet_values')} = 
+                %{$sheet->{'values'}};
+        }
+
+        $depth++;
+        if ($depth>100) {
+	    &setvalues($sheet,undef);
+            return $lastcalc.': Maximum calculation depth exceeded';
+        }
+    }
+    return ;
 }
 
 # ---------------------------------------------------------------- Get formulas
@@ -1354,8 +1381,7 @@
     my $fstring='';
     if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
         my %tmp = split(/___;___/,$fstring);
-        $sheet->{'f'} = \%tmp;
-        &setformulas($sheet);
+        &setformulas($sheet,\%tmp);
     } else {
         # Not cached, need to read
         my %f=();
@@ -1390,8 +1416,7 @@
         }
         # Cache and set
         $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);  
-        $sheet->{'f'}=\%f;
-        &setformulas($sheet);
+        &setformulas($sheet,\%f);
     }
 }
 
@@ -1537,8 +1562,7 @@
     } else {
        if ($nfield) { $fo{$nfield}=$nform; }
     }
-    $sheet->{'f'}=\%fo;
-    &setformulas($sheet);
+    &setformulas($sheet,\%fo);
 }
 
 ##################################################
@@ -1684,8 +1708,7 @@
         }
     }
     if ($changed) { 
-        $sheet->{'f'} = \%f;
-        &setformulas($sheet,%f); 
+        &setformulas($sheet,\%f); 
     }
     #
     $sheet->{'rowlabel'} = \%currentlist;
@@ -1826,8 +1849,7 @@
         }
     }
     if ($changed) { 
-        $sheet->{'f'} = \%f;
-        &setformulas($sheet); 
+        &setformulas($sheet,\%f); 
     }
     &setrowlabels($sheet);
     #
@@ -1879,8 +1901,7 @@
     }
     $cachedassess='';
     undef %cachedstores;
-    $sheet->{'f'} = \%f;
-    &setformulas($sheet);
+    &setformulas($sheet,\%f);
     &setconstants($sheet,\%c);
 }
 
@@ -1940,8 +1961,7 @@
             }
         }
     }
-    $sheet->{'f'}=\%f;
-    &setformulas($sheet);
+    &setformulas($sheet,\%f);
     &setconstants($sheet,\%c);
     $r->print('<script>popwin.close()</script>');
     $r->rflush();