--- loncom/interface/spreadsheet/Spreadsheet.pm 2003/07/16 16:48:51 1.19 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2003/09/16 15:36:32 1.27 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.19 2003/07/16 16:48:51 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.27 2003/09/16 15:36:32 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -48,6 +48,8 @@ Spreadsheet package Apache::Spreadsheet; use strict; +#use warnings FATAL=>'all'; +#no warnings 'uninitialized'; use Apache::Constants qw(:common :http); use Apache::lonnet; use Safe; @@ -90,7 +92,7 @@ sub new { type => $stype, symb => $usymb, errorlog => '', - maxrow => '', + maxrow => 0, cid => $ENV{'request.course.id'}, cnum => $ENV{'course.'.$ENV{'request.course.id'}.'.num'}, cdom => $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}, @@ -229,6 +231,16 @@ sub initialize { # the descendents of the spreadsheet class. } +sub clear_package { + # This method is here to remind you that it will be overridden by + # the descendents of the spreadsheet class. +} + +sub cleanup { + my $self = shift(); + $self->clear_package(); +} + sub initialize_spreadsheet_package { &load_spreadsheet_expirationdates(); &clear_spreadsheet_definition_cache(); @@ -248,6 +260,7 @@ sub load_spreadsheet_expirationdates { sub check_expiration_time { my $self = shift; my ($time)=@_; + return 0 if (! defined($time)); my ($key1,$key2,$key3,$key4,$key5); # Description of keys # @@ -286,18 +299,23 @@ Returns the safe space required by a Spr =cut ###################################################### +{ + + my $safeeval; + sub initialize_safe_space { - my $self = shift; - my $safeeval = new Safe(shift); - my $safehole = new Safe::Hole; - $safeeval->permit("entereval"); - $safeeval->permit(":base_math"); - $safeeval->permit("sort"); - $safeeval->deny(":base_io"); - $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT'); - $safehole->wrap(\&mask,$safeeval,'&mask'); - $safeeval->share('$@'); - my $code=<<'ENDDEFS'; + my $self = shift; + if (! defined($safeeval)) { + $safeeval = new Safe(shift); + my $safehole = new Safe::Hole; + $safeeval->permit("entereval"); + $safeeval->permit(":base_math"); + $safeeval->permit("sort"); + $safeeval->deny(":base_io"); + $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT'); + $safehole->wrap(\&mask,$safeeval,'&mask'); + $safeeval->share('$@'); + my $code=<<'ENDDEFS'; # ---------------------------------------------------- Inside of the safe space # # f: formulas @@ -362,7 +380,7 @@ returns the number of items in the range #------------------------------------------------------- sub NUM { my $mask=&mask(@_); - my $num= $#{@{grep(/$mask/,keys(%sheet_values))}}+1; + my $num= $#{@{grep(eval("/$mask/"),keys(%sheet_values))}}+1; return $num; } @@ -379,7 +397,7 @@ sub BIN { my ($low,$high,$lower,$upper)=@_; my $mask=&mask($lower,$upper); my $num=0; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) { $num++; } @@ -401,7 +419,7 @@ returns the sum of items in the range. sub SUM { my $mask=&mask(@_); my $sum=0; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $sum+=$sheet_values{$_}; } return $sum; @@ -422,7 +440,7 @@ sub MEAN { my $mask=&mask(@_); my $sum=0; my $num=0; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $sum+=$sheet_values{$_}; $num++; } @@ -447,14 +465,14 @@ compute the standard deviation of the it sub STDDEV { my $mask=&mask(@_); my $sum=0; my $num=0; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $sum+=$sheet_values{$_}; $num++; } unless ($num>1) { return undef; } my $mean=$sum/$num; $sum=0; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $sum+=($sheet_values{$_}-$mean)**2; } return sqrt($sum/($num-1)); @@ -474,7 +492,7 @@ compute the product of the items in the sub PROD { my $mask=&mask(@_); my $prod=1; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $prod*=$sheet_values{$_}; } return $prod; @@ -494,7 +512,7 @@ compute the maximum of the items in the sub MAX { my $mask=&mask(@_); my $max='-'; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { unless ($max) { $max=$sheet_values{$_}; } if (($sheet_values{$_}>$max) || ($max eq '-')) { $max=$sheet_values{$_}; @@ -517,7 +535,7 @@ compute the minimum of the items in the sub MIN { my $mask=&mask(@_); my $min='-'; - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { unless ($max) { $max=$sheet_values{$_}; } if (($sheet_values{$_}<$min) || ($min eq '-')) { $min=$sheet_values{$_}; @@ -542,7 +560,7 @@ sub SUMMAX { my ($num,$lower,$upper)=@_; my $mask=&mask($lower,$upper); my @inside=(); - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { push (@inside,$sheet_values{$_}); } @inside=sort(@inside); @@ -569,7 +587,7 @@ sub SUMMIN { my ($num,$lower,$upper)=@_; my $mask=&mask($lower,$upper); my @inside=(); - foreach (grep /$mask/,keys(%sheet_values)) { + foreach (grep eval("/$mask/"),keys(%sheet_values)) { $inside[$#inside+1]=$sheet_values{$_}; } @inside=sort(@inside); @@ -664,7 +682,8 @@ sub calc { # ------------------------------------------- End of "Inside of the safe space" ENDDEFS - $safeeval->reval($code); + $safeeval->reval($code); + } $self->{'safe'} = $safeeval; $self->{'root'} = $self->{'safe'}->root(); # @@ -678,6 +697,9 @@ ENDDEFS $self->{'safe'}->reval($initstring); return $self; } + +} + ###################################################### =pod @@ -691,6 +713,17 @@ ENDDEFS ###################################################### +=pod + +=item &mask($lower,$upper) + +Inputs: $lower and $upper, cell names ("X12" or "a150") or globs ("X*"). + +Returns: Regular expression matching spreadsheet cells that are within +the rectangle defined by $lower and $upper. Due to the nature of the +regular expression this result must be used inside an eval(). + +=cut ###################################################### { @@ -705,78 +738,62 @@ sub mask { } $upper = $lower if (! defined($upper)); # - my ($la,$ld) = ($lower=~/([A-Za-z]|\*)(\d+|\*)/); - my ($ua,$ud) = ($upper=~/([A-Za-z]|\*)(\d+|\*)/); + my ($la,$ld) = ($lower=~/([A-z]|\*)(\d+|\*)/); + my ($ua,$ud) = ($upper=~/([A-z]|\*)(\d+|\*)/); # my $alpha=''; my $num=''; # + # Do not put parenthases around $alpha. + # $num depends on the value in $1. if (($la eq '*') || ($ua eq '*')) { - $alpha='[A-Za-z]'; + $alpha='[A-z]'; } else { - if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) || - ($la=~/[a-z]/) && ($ua=~/[a-z]/)) { - $alpha='['.$la.'-'.$ua.']'; - } else { - $alpha='['.$la.'-Za-'.$ua.']'; - } - } - if (($ld eq '*') || ($ud eq '*')) { - $num='\d+'; + if ($la gt $ua) { + my $tmp = $ua; + $ua = $la; + $la = $ua; + } + $alpha=qq/[$la-$ua]/; + } + if ($ld ne '*' && $ud ne '*') { + # Make sure $ld <= $ud + if ($ld > $ud) { + my $tmp = $ud; + $ud = $ld; + $ld = $tmp; + } + # Here we make a regular expression using some advanced regexp + # abilities. + # (\d+) will match the digits of the cell name and dump them in + # to $1 + # (?(?{ ... code ...} pattern_if_true | pattern_if_false)) will + # choose pattern_if_true if { ... code ... } is true and + # pattern_if_false if { ... code ... } is false. + # In this case, pattern_if_true is empty. pattern_if_false is + # 'donotmatch' and will not match our cells because none of + # them end with donotmatch. + # Unfortunately, the use of this type of regular expression + # requires that each match be wrapped in an eval(). Search for + # $mask in this module for examples + $num = '(\d+)(?(?{$1>= '.$ld.' && $1<='.$ud.'})|donotmatch)'; } else { - if (length($ld)!=length($ud)) { - $num.='('; - foreach ($ld=~m/\d/g) { - $num.='['.$_.'-9]'; - } - if (length($ud)-length($ld)>1) { - $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}'; - } - $num.='|'; - foreach ($ud=~m/\d/g) { - $num.='[0-'.$_.']'; - } - $num.=')'; - } else { - my @lda=($ld=~m/\d/g); - my @uda=($ud=~m/\d/g); - my $i; - my $j=0; - my $notdone=1; - for ($i=0;($i<=$#lda)&&($notdone);$i++) { - if ($lda[$i]==$uda[$i]) { - $num.=$lda[$i]; - $j=$i; - } else { - $notdone=0; - } - } - if ($j<$#lda-1) { - $num.='('.$lda[$j+1]; - for ($i=$j+2;$i<=$#lda;$i++) { - $num.='['.$lda[$i].'-9]'; - } - if ($uda[$j+1]-$lda[$j+1]>1) { - $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'. - ($#lda-$j-1).'}'; - } - $num.='|'.$uda[$j+1]; - for ($i=$j+2;$i<=$#uda;$i++) { - $num.='[0-'.$uda[$i].']'; - } - $num.=')'; - } else { - if ($lda[-1]!=$uda[-1]) { - $num.='['.$lda[-1].'-'.$uda[-1].']'; - } - } - } + $num = '(\d+)'; } - my $expression ='^'.$alpha.$num."\$"; + my $expression = '^'.$alpha.$num.'$'; $memoizer{$key} = $expression; return $expression; } +# +# Debugging routine +sub dump_memoized_values { + while (my ($key,$value) = each(%memoizer)) { + &Apache::lonnet::logthis('memoizer: '.$key.' = '.$value); + } + return; +} + } ## @@ -794,8 +811,8 @@ sub expandnamed { my @vars=split(/\W+/,$formula); my %values=(); foreach my $varname ( @vars ) { - if ($varname=~/\D/) { - $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge; + if ($varname=~/^(parameter|stores|timestamp)/) { + $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge; $varname=~s/$var/\([\\w:\\- ]\+\)/g; foreach (keys(%{$self->{'constants'}})) { if ($_=~/$varname/) { @@ -1144,6 +1161,7 @@ sub display { } elsif ($outputmode eq 'csv') { $self->outsheet_csv($r); } + $self->cleanup(); return; } @@ -1217,7 +1235,9 @@ sub html_editable_cell { # # The encoding string "^A-blah" is placed in []'s inside a regexp, so # we specify the characters we want left alone by putting a '^' in front. - $formula = &HTML::Entities::encode($formula,"^A-z0-9 !#\$%-;=?~"); + $formula = &HTML::Entities::encode($formula,'^A-z0-9 !#$%-;=?~'); + # HTML::Entities::encode does not catch everything - we need '\' encoded + $formula =~ s/\\/&\#092/g; # Escape it again - this time the only encodable character is '&' $formula =~ s/\&/\&/g; # Glue everything together @@ -1335,6 +1355,7 @@ sub create_excel_spreadsheet { sub outsheet_excel { my $self = shift; my ($r) = @_; + my $connection = $r->connection(); $r->print("

Preparing Excel Spreadsheet

"); # # Create excel worksheet @@ -1358,7 +1379,7 @@ sub outsheet_excel { $self->excel_output_row($worksheet,0,$rows_output++,'Summary'); $rows_output++; # skip a line # - $self->excel_rows($worksheet,$cols_output,$rows_output); + $self->excel_rows($connection,$worksheet,$cols_output,$rows_output); # # # Close the excel file @@ -1376,6 +1397,7 @@ sub outsheet_excel { sub outsheet_csv { my $self = shift; my ($r) = @_; + my $connection = $r->connection(); my $csvdata = ''; my @Values; # @@ -1399,7 +1421,7 @@ sub outsheet_csv { } # # Output the body of the spreadsheet - $self->csv_rows($file); + $self->csv_rows($connection,$file); # # Close the csv file close($file); @@ -1573,7 +1595,7 @@ sub load { sub set_row_sources { my $self = shift; while (my ($cell,$value) = each(%{$self->{'formulas'}})) { - next if ($cell !~ /^A(\d+)/ && $1 > 0); + next if ($cell !~ /^A(\d+)/ || $1 < 1); my $row = $1; $self->{'row_source'}->{$row} = $value; } @@ -1635,8 +1657,10 @@ sub save { return $reply if ($reply ne 'ok'); } if ($self->is_default()) { - &Apache::lonnet::expirespread('','',$self->{'type'},''); - if ($self->{'type'} eq 'assesscalc') { + if ($self->{'type'} eq 'studentcalc') { + &Apache::lonnet::expirespread('','','studentcalc',''); + } elsif ($self->{'type'} eq 'assesscalc') { + &Apache::lonnet::expirespread('','','assesscalc',''); &Apache::lonnet::expirespread('','','studentcalc',''); } }