--- loncom/interface/spreadsheet/Spreadsheet.pm 2005/03/03 17:52:36 1.39 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2005/05/12 22:42:57 1.44 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.39 2005/03/03 17:52:36 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.44 2005/05/12 22:42:57 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -90,10 +90,10 @@ sub new { $usymb = $usymb->symb; } if (! defined($name) || $name eq '') { - $name = $ENV{'user.name'}; + $name = $env{'user.name'}; } if (! defined($domain) || $domain eq '') { - $domain = $ENV{'user.domain'}; + $domain = $env{'user.domain'}; } # my $self = { @@ -103,12 +103,12 @@ sub new { symb => $usymb, errorlog => '', maxrow => 0, - 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'}, + 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 @@ -132,8 +132,8 @@ sub new { # # Load in the spreadsheet definition $self->filename($filename); - if (exists($ENV{'form.workcopy'}) && - $self->{'type'} eq $ENV{'form.workcopy'}) { + if (exists($env{'form.workcopy'}) && + $self->{'type'} eq $env{'form.workcopy'}) { $self->load_tmp(); } else { $self->load(); @@ -160,8 +160,8 @@ sub filename { $newfilename !~ /\w/ || $newfilename eq '') { my $key = 'course.'.$self->{'cid'}.'.spreadsheet_default_'. $self->{'type'}; - if (exists($ENV{$key}) && $ENV{$key} ne '') { - $newfilename = $ENV{$key}; + if (exists($env{$key}) && $env{$key} ne '') { + $newfilename = $env{$key}; } else { $newfilename = 'default_'.$self->{'type'}; } @@ -259,10 +259,10 @@ sub initialize_spreadsheet_package { sub load_spreadsheet_expirationdates { undef %expiredates; - my $cid=$ENV{'request.course.id'}; + my $cid=$env{'request.course.id'}; my @tmp = &Apache::lonnet::dump('nohist_expirationdates', - $ENV{'course.'.$cid.'.domain'}, - $ENV{'course.'.$cid.'.num'}); + $env{'course.'.$cid.'.domain'}, + $env{'course.'.$cid.'.num'}); if (lc($tmp[0]) !~ /^error/){ %expiredates = @tmp; } @@ -318,7 +318,7 @@ sub initialize_safe_space { my $self = shift; my $usection = &Apache::lonnet::getsection($self->{'domain'}, $self->{'name'}, - $ENV{'request.course.id'}); + $env{'request.course.id'}); if (! defined($safeeval)) { $safeeval = new Safe(shift); my $safehole = new Safe::Hole; @@ -328,6 +328,7 @@ sub initialize_safe_space { $safeeval->deny(":base_io"); $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&Apache::lonnet::EXT'); $safehole->wrap(\&mask,$safeeval,'&mask'); + $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&logthis'); $safeeval->share('$@'); my $code=<<'ENDDEFS'; # ---------------------------------------------------- Inside of the safe space @@ -412,8 +413,8 @@ returns the number of items in the range #------------------------------------------------------- sub NUM { - my $mask=&mask(@_); - my $num= $#{@{grep(eval("/$mask/"),keys(%sheet_values))}}+1; + my $values=&get_values(@_); + my $num= scalar(@$values); return $num; } @@ -428,10 +429,10 @@ sub NUM { #------------------------------------------------------- sub BIN { my ($low,$high,$lower,$upper)=@_; - my $mask=&mask($lower,$upper); + my $values=&get_values($lower,$upper); my $num=0; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) { + foreach (@$values) { + if (($_>=$low) && ($_<=$high)) { $num++; } } @@ -450,10 +451,10 @@ returns the sum of items in the range. #------------------------------------------------------- sub SUM { - my $mask=&mask(@_); + my $values=&get_values(@_); my $sum=0; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $sum+=$sheet_values{$_}; + foreach (@$values) { + $sum+=$_; } return $sum; } @@ -470,11 +471,11 @@ compute the average of the items in the #------------------------------------------------------- sub MEAN { - my $mask=&mask(@_); + my $values=&get_values(@_); my $sum=0; my $num=0; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $sum+=$sheet_values{$_}; + foreach (@$values) { + $sum+=$_; $num++; } if ($num) { @@ -496,17 +497,17 @@ compute the standard deviation of the it #------------------------------------------------------- sub STDDEV { - my $mask=&mask(@_); + my $values=&get_values(@_); my $sum=0; my $num=0; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $sum+=$sheet_values{$_}; + foreach (@$values) { + $sum+=$_; $num++; } unless ($num>1) { return undef; } my $mean=$sum/$num; $sum=0; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $sum+=($sheet_values{$_}-$mean)**2; + foreach (@$values) { + $sum+=($_-$mean)**2; } return sqrt($sum/($num-1)); } @@ -523,10 +524,10 @@ compute the product of the items in the #------------------------------------------------------- sub PROD { - my $mask=&mask(@_); + my $values=&get_values(@_); my $prod=1; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $prod*=$sheet_values{$_}; + foreach (@$values) { + $prod*=$_; } return $prod; } @@ -543,12 +544,11 @@ compute the maximum of the items in the #------------------------------------------------------- sub MAX { - my $mask=&mask(@_); + my $values=&get_values(@_); my $max='-'; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - unless ($max) { $max=$sheet_values{$_}; } - if (($sheet_values{$_}>$max) || ($max eq '-')) { - $max=$sheet_values{$_}; + foreach (@$values) { + if (($_>$max) || ($max eq '-')) { + $max=$_; } } return $max; @@ -566,12 +566,11 @@ compute the minimum of the items in the #------------------------------------------------------- sub MIN { - my $mask=&mask(@_); + my $values=&get_values(@_); my $min='-'; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - unless ($max) { $max=$sheet_values{$_}; } - if (($sheet_values{$_}<$min) || ($min eq '-')) { - $min=$sheet_values{$_}; + foreach (@$values) { + if (($_<$min) || ($min eq '-')) { + $min=$_; } } return $min; @@ -591,12 +590,8 @@ compute the sum of the largest 'num' ite #------------------------------------------------------- sub SUMMAX { my ($num,$lower,$upper)=@_; - my $mask=&mask($lower,$upper); - my @inside=(); - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - push (@inside,$sheet_values{$_}); - } - @inside=sort(@inside); + my $values=&get_values($lower,$upper); + my @inside=sort {$a <=> $b} (@$values); my $sum=0; my $i; for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) { $sum+=$inside[$i]; @@ -618,12 +613,8 @@ compute the sum of the smallest 'num' it #------------------------------------------------------- sub SUMMIN { my ($num,$lower,$upper)=@_; - my $mask=&mask($lower,$upper); - my @inside=(); - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - $inside[$#inside+1]=$sheet_values{$_}; - } - @inside=sort(@inside); + my $values=&get_values($lower,$upper); + my @inside=sort {$a <=> $b} (@$values); my $sum=0; my $i; for ($i=0;(($i<$num) && ($i<=$#inside));$i++) { $sum+=$inside[$i]; @@ -679,6 +670,31 @@ sub MAXPARM { return $max; } +sub get_values { + my ($lower,$upper)=@_; + my $mask=&mask(@_); + my @values; + foreach (grep eval("/$mask/"),keys(%sheet_values)) { + push(@values,$sheet_values{$_}); + } + return \@values; + if (0) { + # perhaps creating a list of possible cells and looking if they exist + # would be faster somtimes? + &logthis("mask is ".$mask); + my @alpha; + if (($la eq '*') || ($ua eq '*')) { + @alpha=('A'..'z'); + } else { + if ($la gt $ua) { + my $tmp = $ua; + $ua = $la; + $la = $ua; + } + $alpha=($la..$ua); + } + } +} sub calc { %sheet_values = %t; @@ -937,7 +953,8 @@ sub sett { $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g; $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge; } elsif ( $col =~ /^[A-Z]$/ ) { - if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell})) { + if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell}) + && $self->{'constants'}->{$cell} ne '') { my $data = $self->{'constants'}->{$cell}; $t{$cell} = $data; } @@ -1222,7 +1239,7 @@ sub display { my ($r) = @_; my $outputmode = 'html'; foreach ($self->output_options()) { - if ($ENV{'form.output_format'} eq $_->{'value'}) { + if ($env{'form.output_format'} eq $_->{'value'}) { $outputmode = $_->{'value'}; last; } @@ -1266,7 +1283,7 @@ sub html_export_row { my $self = shift(); my ($color) = @_; $color = '#CCCCFF' if (! defined($color)); - my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); + my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'}); my $row_html; my @rowdata = $self->get_row(0); foreach my $cell (@rowdata) { @@ -1283,7 +1300,7 @@ sub html_export_row { sub html_template_row { my $self = shift(); - my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); + my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'}); my ($num_uneditable,$importcolor) = @_; my $row_html; my @rowdata = $self->get_template_row(); @@ -1350,7 +1367,7 @@ sub html_uneditable_cell { sub html_row { my $self = shift(); my ($num_uneditable,$row,$exportcolor,$importcolor) = @_; - my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); + my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'}); my @rowdata = $self->get_row($row); my $num_cols_output = 0; my $row_html; @@ -1374,7 +1391,7 @@ sub html_row { sub html_header { my $self = shift; - return '' if (! $ENV{'request.role.adv'}); + return '' if (! $env{'request.role.adv'}); return "\n". ''."\n". '\n". @@ -1399,10 +1416,10 @@ sub output_selector { my $self = shift(); my $output_selector = '
'.&mt('Output Format').'
'.$self->output_selector()."