[LON-CAPA-cvs] cvs: loncom /interface lonspreadsheet.pm
matthew
lon-capa-cvs@mail.lon-capa.org
Tue, 22 Oct 2002 19:47:13 -0000
matthew Tue Oct 22 15:47:13 2002 EDT
Modified files:
/loncom/interface lonspreadsheet.pm
Log:
Undo of previous changes. Moving the calculation out of the safe space
caused it to take twice as long.
Index: loncom/interface/lonspreadsheet.pm
diff -u loncom/interface/lonspreadsheet.pm:1.123 loncom/interface/lonspreadsheet.pm:1.124
--- loncom/interface/lonspreadsheet.pm:1.123 Tue Oct 22 14:54:53 2002
+++ loncom/interface/lonspreadsheet.pm Tue Oct 22 15:47:13 2002
@@ -1,5 +1,5 @@
#
-# $Id: lonspreadsheet.pm,v 1.123 2002/10/22 18:54:53 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.124 2002/10/22 19:47:13 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -754,6 +754,7 @@
sub MINPARM {
my ($expression) = @_;
my $min = undef;
+ study($expression);
foreach $parameter (keys(%c)) {
next if ($parameter !~ /$expression/);
if ((! defined($min)) || ($min > $c{$parameter})) {
@@ -776,6 +777,7 @@
sub MAXPARM {
my ($expression) = @_;
my $max = undef;
+ study($expression);
foreach $parameter (keys(%c)) {
next if ($parameter !~ /$expression/);
if ((! defined($min)) || ($max < $c{$parameter})) {
@@ -827,6 +829,7 @@
my $returnvalue = '';
my @matches = ();
$#matches = -1;
+ study $expression;
foreach $parameter (keys(%c)) {
push @matches,$parameter if ($parameter =~ /$expression/);
}
@@ -915,6 +918,32 @@
$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);
@@ -1004,36 +1033,9 @@
}
# ------------------------------------------------ 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 ($formulas) = @_;
- $formulas = {} if (! defined($formulas));
- if (! ref($formulas)) {
- my %tmp = @_;
- $formulas = \%tmp;
- }
- $sheet->{'f'} = $formulas;
+ my ($sheet)=shift;
%{$sheet->{'safe'}->varglob('f')}=%{$sheet->{'f'}};
- return undef;
}
# ------------------------------------------------ Add or change formula values
@@ -1066,38 +1068,9 @@
# ------------------------------------------------------- Calculate spreadsheet
sub calcsheet {
my $sheet=shift;
- &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 ;
+ my $result = $sheet->{'safe'}->reval('&calc();');
+ %{$sheet->{'values'}} = %{$sheet->{'safe'}->varglob('sheet_values')};
+ return $result;
}
# ---------------------------------------------------------------- Get formulas
@@ -1381,7 +1354,8 @@
my $fstring='';
if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
my %tmp = split(/___;___/,$fstring);
- &setformulas($sheet,\%tmp);
+ $sheet->{'f'} = \%tmp;
+ &setformulas($sheet);
} else {
# Not cached, need to read
my %f=();
@@ -1416,7 +1390,8 @@
}
# Cache and set
$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
- &setformulas($sheet,\%f);
+ $sheet->{'f'}=\%f;
+ &setformulas($sheet);
}
}
@@ -1562,7 +1537,8 @@
} else {
if ($nfield) { $fo{$nfield}=$nform; }
}
- &setformulas($sheet,\%fo);
+ $sheet->{'f'}=\%fo;
+ &setformulas($sheet);
}
##################################################
@@ -1708,7 +1684,8 @@
}
}
if ($changed) {
- &setformulas($sheet,\%f);
+ $sheet->{'f'} = \%f;
+ &setformulas($sheet,%f);
}
#
$sheet->{'rowlabel'} = \%currentlist;
@@ -1849,7 +1826,8 @@
}
}
if ($changed) {
- &setformulas($sheet,\%f);
+ $sheet->{'f'} = \%f;
+ &setformulas($sheet);
}
&setrowlabels($sheet);
#
@@ -1901,7 +1879,8 @@
}
$cachedassess='';
undef %cachedstores;
- &setformulas($sheet,\%f);
+ $sheet->{'f'} = \%f;
+ &setformulas($sheet);
&setconstants($sheet,\%c);
}
@@ -1961,7 +1940,8 @@
}
}
}
- &setformulas($sheet,\%f);
+ $sheet->{'f'}=\%f;
+ &setformulas($sheet);
&setconstants($sheet,\%c);
$r->print('<script>popwin.close()</script>');
$r->rflush();