--- loncom/interface/Attic/lonspreadsheet.pm 2002/11/04 22:35:45 1.132
+++ loncom/interface/Attic/lonspreadsheet.pm 2002/11/06 20:00:13 1.134
@@ -1,5 +1,5 @@
#
-# $Id: lonspreadsheet.pm,v 1.132 2002/11/04 22:35:45 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.134 2002/11/06 20:00:13 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -62,6 +62,8 @@ use GDBM_File;
use HTML::TokeParser;
use Apache::lonhtmlcommon;
use Apache::loncoursedata;
+use Apache::File();
+use Spreadsheet::WriteExcel;
#
# Caches for coursewide information
#
@@ -1149,11 +1151,6 @@ sub sort_indicies {
sub html_editable_cell {
my ($cell,$bgcolor) = @_;
my $result;
-# if (defined($cell)) {
-# &Apache::lonnet::logthis("cell ".$cell->{'name'}.
-# " = ".$cell->{'value'}.
-# " : ".$cell->{'formula'});
-# }
my ($name,$formula,$value);
if (defined($cell)) {
$name = $cell->{'name'};
@@ -1168,7 +1165,9 @@ sub html_editable_cell {
$value = 'undefined value';
}
}
- #
+ if ($value =~ /^\s*$/ ) {
+ $value = '#';
+ }
$result .= ''.$value.'';
return $result;
@@ -1231,7 +1230,7 @@ END
# Print out template row
####################################
my ($rowlabel,@rowdata) = &get_row($sheet,'-');
- my $row_html = '
'.&format_rowlabel($rowlabel).'
';
+ my $row_html = '
'.&format_html_rowlabel($rowlabel).'
';
my $num_cols_output = 0;
foreach my $cell (@rowdata) {
if ($num_cols_output++ < $num_uneditable) {
@@ -1250,7 +1249,7 @@ END
####################################
my ($rowlabel,@rowdata) = &get_row($sheet,'0');
my $rowcount = 0;
- $row_html = '
'.&format_rowlabel($rowlabel).'
';
+ $row_html = '
'.&format_html_rowlabel($rowlabel).'
';
$num_cols_output = 0;
foreach my $cell (@rowdata) {
if ($num_cols_output++ < 26) {
@@ -1281,11 +1280,11 @@ END
'';
#
if ($sheet->{'sheettype'} eq 'classcalc') {
- $row_html.='
'.&format_rowlabel($rowlabel).'
';
+ $row_html.='
'.&format_html_rowlabel($rowlabel).'
';
# Output links for each student?
- # Nope, that is already done for us in format_rowlabel (for now)
+ # Nope, that is already done for us in format_html_rowlabel (for now)
} elsif ($sheet->{'sheettype'} eq 'studentcalc') {
- $row_html.='
';
}
#
my $shown_cells = 0;
@@ -1354,10 +1353,105 @@ END
sub outsheet_csv {
my ($sheet,$r) = @_;
+ my $csvdata = '';
+ my @Values;
+ ####################################
+ # Prepare to output rows
+ ####################################
+ my @Rows = &sort_indicies($sheet);
+ #
+ # Loop through the rows and output them one at a time
+ my $rows_output=0;
+ foreach my $rownum (@Rows) {
+ my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
+ push (@Values,&format_csv_rowlabel($rowlabel));
+ foreach my $cell (@rowdata) {
+ push (@Values,'"'.$cell->{'value'}.'"');
+ }
+ $csvdata.= join(',',@Values)."\n";
+ @Values = ();
+ }
+ #
+ # Write the CSV data to a file and serve up a link
+ #
+ my $filename = '/prtspool/'.
+ $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+ time.'_'.rand(1000000000).'.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. ".
+ "This error has been logged. ".
+ "Please alert your LON-CAPA administrator.");
+ $r->print("
'.
+ 'Your CSV spreadsheet.'."\n");
+ #
+ return 1;
}
sub outsheet_excel {
my ($sheet,$r) = @_;
+ my $filename = '/prtspool/'.
+ $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+ time.'_'.rand(1000000000).'.xls';
+ &Apache::lonnet::logthis("spreadsheet:filename = ".$filename);
+ 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. ".
+ "This error has been logged. ".
+ "Please alert your LON-CAPA administrator");
+ return 0;
+ }
+ #
+ # The spreadsheet stores temporary data in files, then put them
+ # together. If needed we should be able to disable this (memory only).
+ # The temporary directory must be specified before calling 'addworksheet'.
+ # File::Temp is used to determine the temporary directory.
+ $workbook->set_tempdir('/home/httpd/perl/tmp');
+ #
+ # Determine the name to give the worksheet
+ my $sheetname;
+ if ($sheet->{'sheettype'} eq 'classcalc') {
+ $sheetname = 'Main';
+ } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
+ $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'};
+ } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
+ $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'}.' assessment';
+ }
+ my $worksheet = $workbook->addworksheet($sheetname);
+ ####################################
+ # Prepare to output rows
+ ####################################
+ my @Rows = &sort_indicies($sheet);
+ #
+ # Loop through the rows and output them one at a time
+ my $rows_output=0;
+ foreach my $rownum (@Rows) {
+ my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
+ my $cols_output = 0;
+ my $label = &format_excel_rowlabel($rowlabel);
+ $worksheet->write($rows_output,$cols_output++,$label);
+ if (ref($label)) {
+ $cols_output = (scalar(@$label));
+ }
+ foreach my $cell (@rowdata) {
+ $worksheet->write($rows_output,$cols_output++,
+ $cell->{'value'});
+ }
+ $rows_output++;
+ }
+ #
+ $workbook->close();
+ # Okay, the spreadsheet is taken care of, so give the user a link.
+ $r->print('