--- loncom/interface/spreadsheet/Spreadsheet.pm 2005/05/13 15:59:07 1.45 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2005/08/24 18:14:47 1.53 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.45 2005/05/13 15:59:07 albertel Exp $ +# $Id: Spreadsheet.pm,v 1.53 2005/08/24 18:14:47 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -106,13 +106,13 @@ sub new { cid => $env{'request.course.id'}, cnum => $env{'course.'.$env{'request.course.id'}.'.num'}, cdom => $env{'course.'.$env{'request.course.id'}.'.domain'}, - chome => $env{'course.'.$env{'request.course.id'}.'.home'}, coursedesc => $env{'course.'.$env{'request.course.id'}.'.description'}, coursefilename => $env{'request.course.fn'}, # # Flags temporary => 0, # true if this sheet has been modified but not saved - new_rows => 0, # true if this sheet has new rows + new_rows => 0, # true if this sheet has new rows + loaded => 0, # true if the formulas have been loaded # # blackout is used to determine if any data needs to be hidden from the # student. @@ -126,18 +126,9 @@ sub new { othersheets => [], }; # - $self->{'uhome'} = &Apache::lonnet::homeserver($name,$domain); - # bless($self,$class); - # - # Load in the spreadsheet definition $self->filename($filename); - if (exists($env{'form.workcopy'}) && - $self->{'type'} eq $env{'form.workcopy'}) { - $self->load_tmp(); - } else { - $self->load(); - } + # return $self; } @@ -330,6 +321,10 @@ sub initialize_safe_space { $safehole->wrap(\&mask,$safeeval,'&mask'); $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&logthis'); $safeeval->share('$@'); + # Holds the (computed, final) values for the sheet + # This is only written to by &calc, the spreadsheet computation routine. + # It is read by many functions + $safeeval->share('%sheet_values'); my $code=<<'ENDDEFS'; # ---------------------------------------------------- Inside of the safe space # @@ -339,12 +334,9 @@ sub initialize_safe_space { # c: preloaded constants (A-column) # rl: row label # os: other spreadsheets (for student spreadsheet only) -undef %sheet_values; # Holds the (computed, final) values for the sheet - # This is only written to by &calc, the spreadsheet computation routine. - # It is read by many functions -undef %t; # Holds the values of the spreadsheet temporarily. Set in &sett, - # which does the translation of strings like C5 into the value in C5. - # Used in &calc - %t holds the values that are actually eval'd. +undef %t; # Holds the forumlas of the spreadsheet to be computed. Set in + # &sett, which does the translation of strings like C5 into the value + # in C5. Used in &calc - %t holds the values that are actually eval'd. undef %f; # Holds the formulas for each cell. This is the users # (spreadsheet authors) data for each cell. undef %c; # Holds the constants for a sheet. In the assessment @@ -365,12 +357,10 @@ $filename = ''; # # user data $name = ''; -$uhome = ''; $domain = ''; # # course data $csec = ''; -$chome= ''; $cnum = ''; $cdom = ''; $cid = ''; @@ -729,7 +719,6 @@ sub get_values { } sub calc { - %sheet_values = %t; my $notfinished = 1; my $lastcalc = ''; my $depth = 0; @@ -770,7 +759,7 @@ ENDDEFS # itself my $initstring = ''; foreach (qw/name domain type symb cid csec coursefilename - cnum cdom chome uhome/) { + cnum cdom/) { $initstring.= qq{\$$_="$self->{$_}";}; } $initstring.=qq{\$usection="$usection";}; @@ -869,6 +858,7 @@ sub expandnamed { sub sett { my $self = shift; my %t=(); + undef(%Apache::Spreadsheet::sheet_values); # # Deal with the template row foreach my $col ($self->template_cells()) { @@ -901,8 +891,8 @@ sub sett { } elsif ( $col =~ /^[A-Z]$/ ) { if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell}) && $self->{'constants'}->{$cell} ne '') { - my $data = $self->{'constants'}->{$cell}; - $t{$cell} = $data; + $Apache::Spreadsheet::sheet_values{$cell}= + eval($self->{'constants'}->{$cell}); } } else { # $row > 1 and $col =~ /[a-z] $t{$cell}=$formula; @@ -921,11 +911,11 @@ sub sett { sub sync_safe_space { my $self = shift; # Inside the safe space 'formulas' has a diabolical alter-ego named 'f'. - %{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}}; + #%{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}}; # 'constants' leads a peaceful hidden life of 'c'. %{$self->{'safe'}->varglob('c')}=%{$self->{'constants'}}; # 'othersheets' hides as 'os', a disguise few can penetrate. - @{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}}; + #@{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}}; } ## @@ -980,6 +970,16 @@ sub formulas { $self->{'template_cells'} = []; return; } else { + if (!$self->{'loaded'}) { + $self->{'loaded'}=1; + # Load in the spreadsheet definition + if (exists($env{'form.workcopy'}) && + $self->{'type'} eq $env{'form.workcopy'}) { + $self->load_tmp(); + } else { + $self->load(); + } + } return %{$self->{'formulas'}}; } } @@ -1607,7 +1607,6 @@ sub load { my $stype = $self->{'type'}; my $cnum = $self->{'cnum'}; my $cdom = $self->{'cdom'}; - my $chome = $self->{'chome'}; # my $filename = $self->filename(); my $cachekey = join('_',($cnum,$cdom,$stype,$filename)); @@ -1616,7 +1615,11 @@ sub load { my ($formulas); if (exists($spreadsheets{$cachekey})) { $formulas = $spreadsheets{$cachekey}->{'formulas'}; - } else { + $self->formulas($formulas); + $self->{'row_source'}=$spreadsheets{$cachekey}->{'row_source'}; + $self->{'row_numbers'}=$spreadsheets{$cachekey}->{'row_numbers'}; + $self->{'maxrow'}=$spreadsheets{$cachekey}->{'maxrow'}; + } else { # Not cached, need to read if (! defined($filename)) { $formulas = $self->load_system_default_sheet(); @@ -1650,12 +1653,23 @@ sub load { } } $filename=$self->filename(); # filename may have changed - $cachekey = join('_',($cnum,$cdom,$stype,$filename)); - %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas}; + $cachekey = join('_',($cnum,$cdom,$stype,$filename)); + if (ref($formulas) eq 'HASH') { + %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas}; + } + $self->formulas($formulas); + $self->set_row_sources(); + $self->set_row_numbers(); + if (ref($self->{'row_source'})) { + %{$spreadsheets{$cachekey}->{'row_source'}} = + %{$self->{'row_source'}}; + } + if (ref($self->{'row_numbers'})) { + %{$spreadsheets{$cachekey}->{'row_numbers'}} = + %{$self->{'row_numbers'}}; + } + $spreadsheets{$cachekey}->{'maxrow'} = $self->{'maxrow'}; } - $self->formulas($formulas); - $self->set_row_sources(); - $self->set_row_numbers(); } sub set_row_sources { @@ -1704,7 +1718,6 @@ sub save { my $stype = $self->{'type'}; my $cnum = $self->{'cnum'}; my $cdom = $self->{'cdom'}; - my $chome = $self->{'chome'}; my $filename = $self->{'filename'}; my $cachekey = join('_',($cnum,$cdom,$stype,$filename)); # Cache new sheet