[LON-CAPA-cvs] cvs: loncom /interface/spreadsheet Spreadsheet.pm assesscalc.pm classcalc.pm studentcalc.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 23 May 2003 21:03:29 -0000


matthew		Fri May 23 17:03:29 2003 EDT

  Modified files:              
    /loncom/interface/spreadsheet	Spreadsheet.pm assesscalc.pm 
                                 	classcalc.pm studentcalc.pm 
  Log:
  CSV output implemented.
  
  
Index: loncom/interface/spreadsheet/Spreadsheet.pm
diff -u loncom/interface/spreadsheet/Spreadsheet.pm:1.5 loncom/interface/spreadsheet/Spreadsheet.pm:1.6
--- loncom/interface/spreadsheet/Spreadsheet.pm:1.5	Fri May 23 15:36:04 2003
+++ loncom/interface/spreadsheet/Spreadsheet.pm	Fri May 23 17:03:29 2003
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.5 2003/05/23 19:36:04 matthew Exp $
+# $Id: Spreadsheet.pm,v 1.6 2003/05/23 21:03:29 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1339,6 +1339,65 @@
     # 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;
 }
 
Index: loncom/interface/spreadsheet/assesscalc.pm
diff -u loncom/interface/spreadsheet/assesscalc.pm:1.8 loncom/interface/spreadsheet/assesscalc.pm:1.9
--- loncom/interface/spreadsheet/assesscalc.pm:1.8	Fri May 23 15:36:04 2003
+++ loncom/interface/spreadsheet/assesscalc.pm	Fri May 23 17:03:29 2003
@@ -1,5 +1,5 @@
 #
-# $Id: assesscalc.pm,v 1.8 2003/05/23 19:36:04 matthew Exp $
+# $Id: assesscalc.pm,v 1.9 2003/05/23 21:03:29 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -446,10 +446,27 @@
     return $row_html;
 }
 
-sub outsheet_csv {
+sub csv_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
-    $r->print('<h1>csv output is not supported yet</h1>');
+    my ($filehandle) = @_;
+    #
+    # Write a header row
+    $self->csv_output_row($filehandle,undef,
+                          ('Parameter','Description','Value'));
+    #
+    # Write each row
+    foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
+        my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
+        my $description = '';
+        if (exists($nice_parameter_name{$parameter_name})) {
+            $description = $nice_parameter_name{$parameter_name};
+        }
+        $self->csv_output_row($filehandle,$rownum,
+                              $parameter_name,$description);
+    }
+    return;
 }
 
 sub excel_rows {
Index: loncom/interface/spreadsheet/classcalc.pm
diff -u loncom/interface/spreadsheet/classcalc.pm:1.3 loncom/interface/spreadsheet/classcalc.pm:1.4
--- loncom/interface/spreadsheet/classcalc.pm:1.3	Fri May 23 15:36:04 2003
+++ loncom/interface/spreadsheet/classcalc.pm	Fri May 23 17:03:29 2003
@@ -1,5 +1,5 @@
 #
-# $Id: classcalc.pm,v 1.3 2003/05/23 19:36:04 matthew Exp $
+# $Id: classcalc.pm,v 1.4 2003/05/23 21:03:29 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -192,13 +192,6 @@
     return;
 }
 
-sub outsheet_csv {
-    my $self = shift;
-    my ($r) = @_;
-    $r->print('<h1>csv output is not supported yet</h1>');
-}
-
-
 sub excel_rows {
     # writes the meat of the spreadsheet to an excel worksheet.  Called
     # by Spreadsheet::outsheet_excel;
@@ -229,6 +222,31 @@
     return;
 }
 
+sub csv_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
+    my $self = shift;
+    my ($filehandle) = @_;
+    #
+    # Write a header row
+
+    $self->csv_output_row($filehandle,undef,
+                          ('fullname','username','domain','section','status'));
+    #
+    # Write each students row
+    foreach my $student ($self->get_classlist()) {
+	my $rownum = $self->get_row_number_from_key
+	    ($student->{'username'}.':'.$student->{'domain'});
+        $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
+        my @studentdata = ($student->{'fullname'},
+                           $student->{'username'},
+                           $student->{'domain'},
+                           $student->{'section'},
+                           $student->{'status'});
+        $self->csv_output_row($filehandle,$rownum,@studentdata);
+    }
+    return;
+}
 
 sub outsheet_recursive_excel {
     my $self = shift;
Index: loncom/interface/spreadsheet/studentcalc.pm
diff -u loncom/interface/spreadsheet/studentcalc.pm:1.6 loncom/interface/spreadsheet/studentcalc.pm:1.7
--- loncom/interface/spreadsheet/studentcalc.pm:1.6	Fri May 23 15:36:04 2003
+++ loncom/interface/spreadsheet/studentcalc.pm	Fri May 23 17:03:29 2003
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.6 2003/05/23 19:36:04 matthew Exp $
+# $Id: studentcalc.pm,v 1.7 2003/05/23 21:03:29 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -312,10 +312,30 @@
     return;
 }
 
-sub outsheet_csv {
+sub csv_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
-    $r->print('<h1>csv output is not supported yet</h1>');
+    my ($filehandle) = @_;
+    #
+    # Write a header row
+    $self->csv_output_row($filehandle,undef,
+                          ('Container','Assessment title'));
+    #
+    # Write each assessments row
+    if (scalar(@Sequences)< 1) {
+        &initialize_sequence_cache();
+    }
+    foreach my $Sequence (@Sequences) {
+	next if ($Sequence->{'num_assess'} < 1);
+	foreach my $resource (@{$Sequence->{'contents'}}) {
+	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
+            my @assessdata = ($Sequence->{'title'},
+                              $resource->{'title'});
+            $self->csv_output_row($filehandle,$rownum,@assessdata);
+        }
+    }
+    return;
 }
 
 sub excel_rows {