--- loncom/interface/spreadsheet/Spreadsheet.pm 2003/09/16 15:36:32 1.27 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2003/11/21 21:51:28 1.31 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.27 2003/09/16 15:36:32 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.31 2003/11/21 21:51:28 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -59,6 +59,7 @@ use HTML::Entities(); use HTML::TokeParser; use Spreadsheet::WriteExcel; use Time::HiRes; +use Apache::lonlocal; ## ## Package Variables @@ -677,7 +678,7 @@ sub calc { return $lastcalc.': Maximum calculation depth exceeded'; } } - return ''; + return 'okay'; } # ------------------------------------------- End of "Inside of the safe space" @@ -1138,9 +1139,49 @@ 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 @@ -1149,16 +1190,24 @@ sub calcsheet { sub display { my $self = shift; my ($r) = @_; - $self->compute($r); my $outputmode = 'html'; - if ($ENV{'form.output_format'} =~ /^(html|excel|csv)$/) { - $outputmode = $ENV{'form.output_format'}; + foreach ($self->output_options()) { + if ($ENV{'form.output_format'} eq $_->{'value'}) { + $outputmode = $_->{'value'}; + last; + } } if ($outputmode eq 'html') { + $self->compute($r); $self->outsheet_html($r); + } elsif ($outputmode eq 'htmlclasslist') { + # No computation neccessary... This is kludgy + $self->outsheet_htmlclasslist($r); } elsif ($outputmode eq 'excel') { + $self->compute($r); $self->outsheet_excel($r); } elsif ($outputmode eq 'csv') { + $self->compute($r); $self->outsheet_csv($r); } $self->cleanup(); @@ -1168,6 +1217,18 @@ sub display { ############################################ ## HTML output routines ## ############################################ +sub html_report_error { + my $self = shift(); + my $Str = ''; + if ($self->badcalc()) { + $Str = '

'. + &mt('An error occurred while calculating this spreadsheet'). + "

\n". + '
'.$self->calcerror()."
\n"; + } + return $Str; +} + sub html_export_row { my $self = shift(); my ($color) = @_; @@ -1282,12 +1343,25 @@ sub html_header { my $self = shift; return '' if (! $ENV{'request.role.adv'}); return "\n". - ''."\n". - '\n". + ''."\n". + '\n". "
Output Format
'.&output_selector()."
'.&mt('Output Format').'
'.$self->output_selector()."
\n"; } +## +## Default output types are HTML, Excel, and CSV +sub output_options { + my $self = shift(); + return ({value => 'html', + description => 'HTML'}, + {value => 'excel', + description => 'Excel'}, + {value => 'csv', + description => 'Comma Separated Values'},); +} + sub output_selector { + my $self = shift(); my $output_selector = '\n"; return $output_selector; @@ -1336,9 +1407,9 @@ sub create_excel_spreadsheet { my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename); if (! defined($workbook)) { $r->log_error("Error creating excel spreadsheet $filename: $!"); - $r->print("Problems creating new Excel file. ". + $r->print(&mt("Problems creating new Excel file. ". "This error has been logged. ". - "Please alert your LON-CAPA administrator"); + "Please alert your LON-CAPA administrator")); return undef; } # @@ -1352,11 +1423,21 @@ sub create_excel_spreadsheet { return ($workbook,$filename); } +# +# This routine is just a stub +sub outsheet_htmlclasslist { + my $self = shift; + my ($r) = @_; + $r->print('

'.&mt("This output is not supported").'

'); + $r->rflush(); + return; +} + sub outsheet_excel { my $self = shift; my ($r) = @_; my $connection = $r->connection(); - $r->print("

Preparing Excel Spreadsheet

"); + $r->print("

".&mt('Preparing Excel Spreadsheet')."

"); # # Create excel worksheet my ($workbook,$filename) = $self->create_excel_spreadsheet($r); @@ -1408,9 +1489,9 @@ sub outsheet_csv { my $file; unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) { $r->log_error("Couldn't open $filename for output $!"); - $r->print("Problems occured in writing the csv file. ". + $r->print(&mt("Problems occured in writing the csv file. ". "This error has been logged. ". - "Please alert your LON-CAPA administrator."); + "Please alert your LON-CAPA administrator.")); $r->print("
\n".$csvdata."
\n"); return 0; } @@ -1426,7 +1507,7 @@ sub outsheet_csv { # Close the csv file close($file); $r->print('

'. - 'Your CSV spreadsheet.'."\n"); + ''.&mt('Your CSV spreadsheet.').''."\n"); # return 1; } @@ -1617,6 +1698,9 @@ sub set_row_numbers { ## sub exportrow { my $self = shift; + if (exists($self->{'badcalc'}) && $self->{'badcalc'}) { + return (); + } my @exportarray; foreach my $column (@UC_Columns) { push(@exportarray,$self->value($column.'0')); @@ -1752,9 +1836,9 @@ sub othersheets { $self->{'cdom'}, $self->{'cnum'}); my ($tmp) = keys(%results); if ($tmp =~ /^(con_lost|error|no_such_host)/i ) { - @alternatives = ('Default'); + @alternatives = (&mt('Default')); } else { - @alternatives = ('Default', sort (keys(%results))); + @alternatives = (&mt('Default'), sort (keys(%results))); } return @alternatives; }