--- loncom/interface/spreadsheet/Spreadsheet.pm 2003/06/18 17:35:14 1.14 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2003/12/05 22:24:20 1.21.2.1 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.14 2003/06/18 17:35:14 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.21.2.1 2003/12/05 22:24:20 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -248,18 +248,27 @@ sub load_spreadsheet_expirationdates { sub check_expiration_time { my $self = shift; my ($time)=@_; - my ($key1,$key2,$key3,$key4); + my ($key1,$key2,$key3,$key4,$key5); + # Description of keys + # + # key1: all sheets of this type have expired + # key2: all sheets of this type for this student + # key3: all sheets of this type in this map for this student + # key4: this assessment sheet for this student + # key5: this assessment sheet for all students $key1 = '::'.$self->{'type'}.':'; $key2 = $self->{'name'}.':'.$self->{'domain'}.':'.$self->{'type'}.':'; $key3 = $key2.$self->{'container'} if (defined($self->{'container'})); - $key4 = $key2.$self->{'usymb'} if (defined($self->{'usymb'})); - foreach my $key ($key1,$key2,$key3,$key4) { + $key4 = $key2.$self->{'symb'} if (defined($self->{'symb'})); + $key5 = $key1.$self->{'symb'} if (defined($self->{'symb'})); + my $returnvalue = 1; # default to okay + foreach my $key ($key1,$key2,$key3,$key4,$key5) { next if (! defined($key)); - if (exists($expiredates{$key}) &&$expiredates{$key} > $time) { - return 0; + if (exists($expiredates{$key}) && $expiredates{$key} > $time) { + $returnvalue = 0; # need to recompute } } - return 1; + return $returnvalue; } ###################################################### @@ -650,7 +659,7 @@ sub calc { return $lastcalc.': Maximum calculation depth exceeded'; } } - return ''; + return 'okay'; } # ------------------------------------------- End of "Inside of the safe space" @@ -785,8 +794,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/) { @@ -1112,9 +1121,51 @@ sub calcsheet { # $self->logthis($self->get_errorlog()); %{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')}; # $self->logthis($self->get_errorlog()); + if ($result ne 'okay') { + $self->set_calcerror($result); + } return $result; } + +sub set_badcalc { + my $self = shift(); + $self->{'badcalc'} =1; + return; +} + +sub badcalc { + my $self = shift; + if (exists($self->{'badcalc'}) && $self->{'badcalc'}) { + return 1; + } else { + return 0; + } +} + +sub set_calcerror { + my $self = shift; + if (@_) { + $self->set_badcalc(); + if (exists($self->{'calcerror'})) { + $self->{'calcerror'}.="\n".$_[0]; + } else { + $self->{'calcerror'}.=$_[0]; + } + } +} + +sub calcerror { + my $self = shift; + if ($self->badcalc()) { + if (exists($self->{'calcerror'})) { + return $self->{'calcerror'}; + } + } + return; +} + + ########################################################### ## ## Output Helpers @@ -1141,15 +1192,29 @@ sub display { ############################################ ## HTML output routines ## ############################################ +sub html_report_error { + my $self = shift(); + my $Str = ''; + if ($self->badcalc()) { + $Str = '

'. + 'An error occurred while calculating this spreadsheet'. + "

\n". + '
'.$self->calcerror()."
\n"; + } + return $Str; +} + 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 $row_html; my @rowdata = $self->get_row(0); foreach my $cell (@rowdata) { if ($cell->{'name'} =~ /^[A-Z]/) { - $row_html .= ''. - &html_editable_cell($cell,'#CCCCFF',$allowed).''; + $row_html .= ''. + &html_editable_cell($cell,$color,$allowed).''; } else { $row_html .= ''. &html_editable_cell($cell,'#DDCCFF',$allowed).''; @@ -1161,14 +1226,14 @@ sub html_export_row { sub html_template_row { my $self = shift(); my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); - my ($num_uneditable) = @_; + my ($num_uneditable,$importcolor) = @_; my $row_html; my @rowdata = $self->get_template_row(); my $count = 0; for (my $i = 0; $i<=$#rowdata; $i++) { my $cell = $rowdata[$i]; if ($i < $num_uneditable) { - $row_html .= ''. + $row_html .= ''. &html_uneditable_cell($cell,'#FFDDDD',$allowed).''; } else { $row_html .= ''. @@ -1200,13 +1265,17 @@ sub html_editable_cell { $value = &HTML::Entities::encode($value) if ($value !~/ /); } return $value if (! $allowed); - # Make the formula safe for outputting - $formula =~ s/\'/\"/g; + # # The formula will be parsed by the browser twice before being - # displayed to the user for editing. - $formula = &HTML::Entities::encode(&HTML::Entities::encode($formula)); - # Escape newlines so they make it into the edit window - $formula =~ s/\n/\\n/gs; + # displayed to the user for editing. + # + # 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 !#$%-;=?~'); + # 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 $result .= "".$value.""; @@ -1222,14 +1291,19 @@ sub html_uneditable_cell { sub html_row { my $self = shift(); - my ($num_uneditable,$row) = @_; + my ($num_uneditable,$row,$exportcolor,$importcolor) = @_; my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); my @rowdata = $self->get_row($row); my $num_cols_output = 0; my $row_html; + my $color = $importcolor; + if ($row == 0) { + $color = $exportcolor; + } + $color = '#FFDDDD' if (! defined($color)); foreach my $cell (@rowdata) { if ($num_cols_output++ < $num_uneditable) { - $row_html .= ''; + $row_html .= ''; $row_html .= &html_uneditable_cell($cell,'#FFDDDD'); } else { $row_html .= ''; @@ -1345,6 +1419,7 @@ sub outsheet_excel { # # Close the excel file $workbook->close(); + $r->print($self->html_report_error()); # # Write a link to allow them to download it $r->print('
'. @@ -1385,6 +1460,7 @@ sub outsheet_csv { # # Close the csv file close($file); + $r->print($self->html_report_error()); $r->print('

'. 'Your CSV spreadsheet.'."\n"); # @@ -1531,9 +1607,6 @@ sub load { } else { $self->filename($newfilename); } - } elsif ($filename =~ /^default\.$self->{'type'}/) { - # if there is an Original_$stype, load it instead - $formulas = $self->load_system_default_sheet(); } else { # Load the spreadsheet definition file from the save file my %tmphash = &Apache::lonnet::dump($filename,$cdom,$cnum); @@ -1621,6 +1694,9 @@ sub save { } if ($self->is_default()) { &Apache::lonnet::expirespread('','',$self->{'type'},''); + if ($self->{'type'} eq 'assesscalc') { + &Apache::lonnet::expirespread('','','studentcalc',''); + } } return $reply; } @@ -1632,7 +1708,7 @@ sub save { sub save_tmp { my $self = shift; my $filename=$ENV{'user.name'}.'_'. - $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'. + $ENV{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'. $self->{'filename'}; $filename=~s/\W/\_/g; $filename=$Apache::lonnet::tmpdir.$filename.'.tmp'; @@ -1652,7 +1728,7 @@ sub save_tmp { sub load_tmp { my $self = shift; my $filename=$ENV{'user.name'}.'_'. - $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'. + $ENV{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'. $self->{'filename'}; $filename=~s/\W/\_/g; $filename=$Apache::lonnet::tmpdir.$filename.'.tmp';