You are here

function _webform_results_download in Webform 5

Generate a Excel-readable CSV file containing all submissions for a webform.

The CSV requires that the data be presented in a flat file. In order to maximize useability to the Excel community and minimize subsequent stats or spreadsheet programming this program extracts data from the various records for a given session and presents them as a single file where each row represents a single record. The structure of the file is: Heading Line 1: Gives group overviews padded by empty cells to the next group. A group may be a question and corresponds to a component in the webform philosophy. Each group overview will have a fixed number of columns beneath it. Heading line 2: gives column headings Data line 1 ..... Data line 2 .....

An example of this format is given below. Note the columns have had spaces added so the columns line up. This is not the case with actual file where a column may be null. Note also, that multiple choice questions as produced by checkboxes or radio buttons have been presented as "yes" or "no" and the actual choice text is retained only in the header line 2. Data from text boxes and input fields are written out in the body of the table.

Submission Details, , , ,Question 1, , ,.., ,Question 2, , ,.., ,Question n timestamp ,time,SID,userid,Choice 1 ,Choice 2,Choice 3,..,Choice n,Choice 1 ,Choice 2,Choice 3,..,Choice n,Comment 21 Feb 2005 ,1835,23 ,34 ,Yes ,No ,No ,..,No ,Yes ,Yes ,Yes ,..,Yes ,My comment 23 Feb 2005 ,1125,24 ,89 ,Yes ,Yes ,No ,..,No ,Yes ,Yes ,Yes ,..,Yes ,Hello ............................................................................................................... 27 Feb 2005 ,1035,56 ,212 ,Yes ,No ,No ,..,No ,Yes ,No ,Yes ,..,Yes ,How is this?

1 call to _webform_results_download()
webform_results in ./webform.module
Menu callback for all content under admin/content/webform.

File

./webform_report.inc, line 191

Code

function _webform_results_download($nid) {
  include_once drupal_get_path('module', 'webform') . "/webform.inc";
  $node = node_load($nid);
  $file_name = tempnam(variable_get('file_directory_temp', FILE_DIRECTORY_TEMP), 'webform');
  $handle = @fopen($file_name, 'w');

  // The @ suppresses errors.
  $header[0] .= $node->title . ",,,,";
  $header[1] .= "Submission Details,,,,,";
  $header[2] .= "Serial,SID,Time,IP Address,UID,Username";

  // Compile header information.
  _webform_load_components();

  // Load all components.
  foreach ($node->webformcomponents as $cid => $component) {
    $csv_header_function = "_webform_csv_headers_" . $component['type'];
    if (function_exists($csv_header_function)) {

      // Let each component determine its headers.
      $component_header = $csv_header_function($component);
      $header[0] .= ',"' . str_replace(array(
        '"',
        '\\,',
      ), array(
        '""',
        '","',
      ), $component_header[0]) . '"';
      $header[1] .= ',"' . str_replace(array(
        '"',
        '\\,',
      ), array(
        '""',
        '","',
      ), $component_header[1]) . '"';
      $header[2] .= ',"' . str_replace(array(
        '"',
        '\\,',
      ), array(
        '""',
        '","',
      ), $component_header[2]) . '"';
    }
  }

  // Write header information.
  $file_record = $header[0] . "\n" . $header[1] . "\n" . $header[2] . "\n";
  @fwrite($handle, $file_record);

  // Get all the submissions for the node.
  $submissions = _webform_fetch_submissions($nid);

  // Generate a row for each submission.
  $rowcount = 0;
  foreach ($submissions as $sid => $submission) {
    $row = ++$rowcount . "," . $sid . ",\"" . format_date($submission->submitted, 'small') . "\",\"" . $submission->remote_addr . "\"," . $submission->uid . ",\"" . $submission->name . "\"";
    foreach ($node->webformcomponents as $cid => $component) {
      $csv_data_function = "_webform_csv_data_" . $component['type'];
      if (function_exists($csv_data_function)) {

        // Let each component add its data.
        $row .= ',"' . str_replace(array(
          '"',
          '\\,',
        ), array(
          '""',
          '","',
        ), $csv_data_function($submission->data[$cid], $component)) . '"';
      }
    }

    // Write data from submissions.
    @fwrite($handle, $row . "\n");
  }

  // Close the file.
  @fclose($handle);
  drupal_set_header("Content-type: text/csv; charset=utf-8");
  drupal_set_header("Content-Disposition: attachment; filename=" . preg_replace('/\\.$/', '', str_replace(' ', '_', $node->title)) . ".csv");
  @readfile($file_name);

  // The @ makes it silent.
  @unlink($file_name);

  // Clean up, the @ makes it silent.
  exit(0);
}