You are here

function webform_results_export in Webform 7.4

Same name and namespace in other branches
  1. 6.3 includes/webform.report.inc \webform_results_export()
  2. 7.3 includes/webform.report.inc \webform_results_export()

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

Return value

array|null The array of export info or null if the file could not be opened.

Deprecated

in webform:7.x-4.8 and is removed from webform:7.x-5.0. Use webform_results_export_batch().

See also

https://www.drupal.org/project/webform/issues/2465291

File

includes/webform.report.inc, line 943
This file includes helper functions for creating reports for webform.module.

Code

function webform_results_export($node, $format = 'delimited', $options = array()) {
  module_load_include('inc', 'webform', 'includes/webform.export');
  module_load_include('inc', 'webform', 'includes/webform.components');
  $defaults = webform_results_download_default_options($node, $format);
  $options += $defaults;
  $options['range'] += $defaults['range'];

  // Open a new Webform exporter object.
  $exporter = webform_export_create_handler($format, $options);
  $file_name = _webform_export_tempname();
  $handle = fopen($file_name, 'w');
  if (!$handle) {
    return;
  }

  // Add the beginning of file marker (little-endian usually for MS Excel).
  $exporter
    ->bof($handle);

  // Add headers to the file.
  $row_count = 0;
  $col_count = 0;
  $headers = webform_results_download_headers($node, $options);
  foreach ($headers as $row) {

    // Output header if header_keys is non-negative. -1 means no headers.
    if ($options['header_keys'] >= 0) {
      $exporter
        ->add_row($handle, $row, $row_count);
      $row_count++;
    }
    $col_count = count($row) > $col_count ? count($row) : $col_count;
  }

  // Write data from submissions. $last_is is non_NULL to trigger returning it
  // by reference.
  $last_sid = TRUE;
  $rows = webform_results_download_rows($node, $options, 0, $last_sid);
  foreach ($rows as $row) {
    $exporter
      ->add_row($handle, $row, $row_count);
    $row_count++;
  }

  // Add the closing bytes.
  $exporter
    ->eof($handle, $row_count, $col_count);

  // Close the file.
  @fclose($handle);
  $export_info['format'] = $format;
  $export_info['options'] = $options;
  $export_info['file_name'] = $file_name;
  $export_info['row_count'] = $row_count;
  $export_info['col_count'] = $col_count;
  $export_info['last_sid'] = $last_sid;
  $export_info['exporter'] = $exporter;
  return $export_info;
}