Index: webform_report.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform_report/webform_report.module,v
retrieving revision 1.2.2.66
diff -u -r1.2.2.66 webform_report.module
--- webform_report.module	22 Mar 2010 16:45:06 -0000	1.2.2.66
+++ webform_report.module	24 Mar 2010 16:24:11 -0000
@@ -101,6 +101,12 @@
       'access' => user_access('access webform reports'),
       'description' => t('View and edit Webform reports.'),
       'type' => MENU_NORMAL_ITEM);
+    $items[] = array('path' => 'node/' . arg(1) . '/csv',
+      'title' => t('Report CSV Export'),
+      'callback' => 'webform_report_csv',
+      'callback arguments' => array(arg(1)),
+      'access' => user_access('access webform reports'),
+      'type' => MENU_CALLBACK);
   }
   return $items;
 }
@@ -548,9 +554,10 @@
  *
  * @param data is a database query result set
  * @param node is the current node object
+ * @param formatcsv if TRUE format the output as a CSV file
  * @return a string of text or a themed table
  */
-function _webform_report_get_body_content($data, $node) {
+function _webform_report_get_body_content($data, $node, $formatcsv = FALSE) {
 
   if (db_num_rows($data) > 0) {
     $fields = array(); // webform field names
@@ -606,7 +613,9 @@
         }
         $values[$row->sid][$row->cid] = array('data' => filter_xss($row->data));
       } else {
-          $values[$row->sid][$row->cid] = array('data' => '&nbsp;'); // prevents the table cell from being omitted
+          if (!$formatcsv) {
+            $values[$row->sid][$row->cid] = array('data' => '&nbsp;'); // prevents the table cell from being omitted
+          }
       }
 
       // override the report's sort column with the table sort column, if applicable
@@ -699,12 +708,17 @@
       $values = _webform_report_filter_values($values, $node);
     }
 
-    $values = _webform_report_add_data_links($fields, $values);
-
-    // display number of rows after description
-    $output .= " (" . count($values) . " " . t('results') . ")</p>";
-
-    $output .= _webform_report_pager($fields, $values, $node);
+    if ($formatcsv) {
+      // format as csv
+      $output = _webform_report_output_csv($fields, $values);
+    }
+    else {
+      $values = _webform_report_add_data_links($fields, $values);
+      // display number of rows after description
+      $output .= ' (' . count($values) . ' ' . t('results') . ') ' . 
+                   l(t('Download as CSV'), 'node/' . arg(1) . '/csv') . '</p>';
+      $output .= _webform_report_pager($fields, $values, $node);
+    }
   }
   else { // no submitted data
     $output = t('Note: There are no submissions for the selected webform.</b> Either the form
@@ -871,3 +885,62 @@
 
   return $output;
 }
+
+/**
+ * Output a webform report in CSV format
+ */
+function webform_report_csv($nid) {
+
+  $node = node_load($nid);
+  $data = _webform_report_get_data($node);  
+  $output = _webform_report_get_body_content($data, $node, TRUE);
+      
+  $fname = 'wfr_export.csv';
+  header('Content-Type: text/plain');
+  header('Content-Length: ' . strlen($output));
+  header('Content-Disposition: attachment; filename="' . $fname . '"');
+  echo $output;
+}
+
+/**
+ * Format webform report data as a CSV
+ *
+ * @return CSV output
+ */
+function _webform_report_output_csv($fields, $values) {
+
+  $output = '';
+  $tmp = array();
+  foreach($fields as $cell) {
+    $tmp[] .= _webform_report_format_csv_column($cell['data']);
+  }
+  $output .= implode(',', $tmp) . "\r\n";
+
+  foreach($values as $row) {
+    $tmp = array();
+    foreach($row as $cell) {
+      $tmp[] = _webform_report_format_csv_column($cell['data']);
+    }
+    $output .= implode(',', $tmp) . "\r\n";
+  }
+
+  return $output;
+}
+
+/**
+ * Format a CSV column value
+ *
+ * @return CSV column
+ */
+function _webform_report_format_csv_column($value) {
+
+    // if value contains a comma, it should be delimited by quotes
+    if (strpos($value, ',')) {
+        // if value contains quotes, double the quotes
+        if (strpos($value, '"'))
+            return '"' . str_replace('"', '""', $value) . '"';
+        else
+            return '"' . $value . '"';
+    }
+    return $value;
+}

