--- loncom/interface/spreadsheet/Spreadsheet.pm	2003/05/23 14:52:51	1.4
+++ loncom/interface/spreadsheet/Spreadsheet.pm	2003/05/23 21:03:29	1.6
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.4 2003/05/23 14:52:51 matthew Exp $
+# $Id: Spreadsheet.pm,v 1.6 2003/05/23 21:03:29 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1110,6 +1110,24 @@ sub calcsheet {
 ## Output Helpers
 ##
 ###########################################################
+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'};
+    }
+    if ($outputmode eq 'html') {
+        $self->outsheet_html($r);
+    } elsif ($outputmode eq 'excel') {
+        $self->outsheet_excel($r);
+    } elsif ($outputmode eq 'csv') {
+        $self->outsheet_csv($r);
+    }
+    return;
+}
+
 ############################################
 ##         HTML output routines           ##
 ############################################
@@ -1212,6 +1230,55 @@ sub html_row {
     return $row_html;
 }
 
+sub html_header {
+    my $self = shift;
+    return '' if (! $ENV{'request.role.adv'});
+    return "<table>\n".
+        '<tr><th align="center">Output Format</th><tr>'."\n".
+        '<tr><td>'.&output_selector()."</td></tr>\n".
+        "</table>\n";
+}
+
+sub output_selector {
+    my $output_selector = '<select name="output_format" size="3">'."\n";
+    my $default = 'html';
+    if (exists($ENV{'form.output_format'})) {
+        $default = $ENV{'form.output_format'} 
+    } else {
+        $ENV{'form.output_format'} = $default;
+    }
+    foreach (['html','HTML'],
+             ['excel','Excel'],
+             ['csv','Comma Seperated Values']) {
+        my ($name,$description) = @{$_};
+        $output_selector.=qq{<option value="$name"};
+        if ($name eq $default) {
+            $output_selector .= ' selected';
+        }
+        $output_selector .= ">$description</option>\n";
+    }
+    $output_selector .= "</select>\n";
+    return $output_selector;
+}
+
+################################################
+##          Excel output routines             ##
+################################################
+sub excel_output_row {
+    my $self = shift;
+    my ($worksheet,$rownum,$rows_output,@prepend) = @_;
+    my $cols_output = 0;
+    #
+    my @rowdata = $self->get_row($rownum);
+    foreach my $cell (@prepend,@rowdata) {
+        my $value = $cell;
+        $value = $cell->{'value'} if (ref($value));
+        $value =~ s/\&nbsp;/ /gi;
+        $worksheet->write($rows_output,$cols_output++,$value);
+    }
+    return;
+}
+
 sub create_excel_spreadsheet {
     my $self = shift;
     my ($r) = @_;
@@ -1237,6 +1304,103 @@ sub create_excel_spreadsheet {
     return ($workbook,$filename);
 }
 
+sub outsheet_excel {
+    my $self = shift;
+    my ($r) = @_;
+    $r->print("<h2>Preparing Excel Spreadsheet</h2>");
+    #
+    # Create excel worksheet
+    my ($workbook,$filename) = $self->create_excel_spreadsheet($r);
+    return if (! defined($workbook));
+    #
+    # Create main worksheet
+    my $worksheet = $workbook->addworksheet('main');
+    my $rows_output = 0;
+    my $cols_output = 0;
+    #
+    # Write excel header
+    foreach my $value ($self->get_title()) {
+        $cols_output = 0;
+        $worksheet->write($rows_output++,$cols_output,$value);
+    }
+    $rows_output++;    # skip a line
+    #
+    # Write summary/export row
+    $cols_output = 0;
+    $self->excel_output_row($worksheet,0,$rows_output++,'Summary');
+    $rows_output++;    # skip a line
+    #
+    $self->excel_rows($worksheet,$cols_output,$rows_output);
+    #
+    #
+    # Close the excel file
+    $workbook->close();
+    #
+    # Write a link to allow them to download it
+    $r->print('<br />'.
+              '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
+    return;
+}
+
+#################################
+## CSV output routines         ##
+#################################
+sub outsheet_csv   {
+    my $self = shift;
+    my ($r) = @_;
+    my $csvdata = '';
+    my @Values;
+    #
+    # Open the csv file
+    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("<pre>\n".$csvdata."</pre>\n");
+        return 0;
+    }
+    #
+    # Output the title information
+    foreach my $value ($self->get_title()) {
+        print $file "'".&Apache::loncommon::csv_translate($value)."'\n";
+    }
+    #
+    # Output the body of the spreadsheet
+    $self->csv_rows($file);
+    #
+    # Close the csv file
+    close($file);
+    $r->print('<br /><br />'.
+              '<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n");
+    #
+    return 1;
+}
+
+sub csv_output_row {
+    my $self = shift;
+    my ($filehandle,$rownum,@prepend) = @_;
+    #
+    my @rowdata = ();
+    if (defined($rownum)) {
+        @rowdata = $self->get_row($rownum);
+    }
+    my @output = ();
+    foreach my $cell (@prepend,@rowdata) {
+        my $value = $cell;
+        $value = $cell->{'value'} if (ref($value));
+        $value =~ s/\&nbsp;/ /gi;
+        $value = "'".$value."'";
+        push (@output,$value);
+    }
+    print $filehandle join(',',@output )."\n";
+    return;
+}
+
 ############################################
 ##          XML output routines           ##
 ############################################