You are here

function webform_results_export in Webform 6.3

Same name and namespace in other branches
  1. 7.4 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.

The CSV requires that the data be presented in a flat file. In order to maximize usability 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 ,X , , ,.., ,X ,X ,X ,..,X ,My comment 23 Feb 2005 ,1125,24 ,89 ,X ,X , ,.., ,X ,X ,X ,..,X ,Hello ................................................................................................................................. 27 Feb 2005 ,1035,56 ,212 ,X , , ,.., ,X ,X ,X ,..,X ,How is this?

1 call to webform_results_export()
webform_results_download_form_submit in includes/webform.report.inc
Validate handler for webform_results_download_form().

File

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

Code

function webform_results_export($node, $format = 'delimited', $options = array()) {
  global $user;
  module_load_include('inc', 'webform', 'includes/webform.export');
  module_load_include('inc', 'webform', 'includes/webform.components');
  $submission_information = array(
    'serial' => t('Serial'),
    'sid' => t('SID'),
    'time' => t('Time'),
    'draft' => t('Draft'),
    'ip_address' => t('IP Address'),
    'uid' => t('UID'),
    'username' => t('Username'),
  );
  if (empty($options)) {
    $options = array(
      'delimiter' => variable_get('webform_csv_delimiter', '\\t'),
      'components' => array_keys($submission_information) + array_keys(webform_component_list($node, 'csv', TRUE)),
      'components' => array_merge(array_keys($submission_information), array_keys(webform_component_list($node, 'csv', TRUE))),
      'select_display' => 'value',
      'select_format' => 'separate',
      'range_type' => 'all',
    );
  }
  else {
    foreach ($submission_information as $key => $label) {
      if (!in_array($key, $options['components'])) {
        unset($submission_information[$key]);
      }
    }
  }

  // Open a new Webform exporter object.
  $exporter = webform_export_create_handler($format, $options);
  $file_name = tempnam(variable_get('file_directory_temp', file_directory_temp()), 'webform');
  $handle = @fopen($file_name, 'w');

  // The @ suppresses errors.
  $exporter
    ->bof($handle);

  // Fill in the header for the submission information (if any).
  $header[2] = $header[1] = $header[0] = count($submission_information) ? array_fill(0, count($submission_information), '') : array();
  if (count($submission_information)) {
    $header[0][0] = $node->title;
    $header[1][0] = t('Submission Details');
    foreach (array_values($submission_information) as $column => $label) {
      $header[2][$column] = $label;
    }
  }

  // Compile header information for components.
  foreach ($options['components'] as $cid) {
    if (isset($node->webform['components'][$cid])) {
      $component = $node->webform['components'][$cid];

      // Let each component determine its headers.
      if (webform_component_feature($component['type'], 'csv')) {
        $component_header = (array) webform_component_invoke($component['type'], 'csv_headers', $component, $options);

        // Allow modules to modify the component CSV header.
        drupal_alter('webform_csv_header', $component_header, $component);

        // Merge component CSV header to overall CSV header
        $header[0] = array_merge($header[0], (array) $component_header[0]);
        $header[1] = array_merge($header[1], (array) $component_header[1]);
        $header[2] = array_merge($header[2], (array) $component_header[2]);
      }
    }
  }

  // Add headers to the file.
  foreach ($header as $row) {
    $exporter
      ->add_row($handle, $row);
  }

  // Get all the submissions for the node.
  $filters['nid'] = $node->nid;
  if (!empty($options['sids'])) {
    $filters['sid'] = $options['sids'];
  }
  $submissions = webform_get_submissions($filters);

  // Generate a row for each submission.
  $row_count = 0;
  $sid = 0;
  foreach ($submissions as $sid => $submission) {
    $row_count++;
    $row = array();
    if (isset($submission_information['serial'])) {
      $row[] = $row_count;
    }
    if (isset($submission_information['sid'])) {
      $row[] = $sid;
    }
    if (isset($submission_information['time'])) {
      $row[] = format_date($submission->submitted, 'small');
    }
    if (isset($submission_information['draft'])) {
      $row[] = $submission->is_draft;
    }
    if (isset($submission_information['ip_address'])) {
      $row[] = $submission->remote_addr;
    }
    if (isset($submission_information['uid'])) {
      $row[] = $submission->uid;
    }
    if (isset($submission_information['username'])) {
      $row[] = $submission->name;
    }
    foreach ($options['components'] as $cid) {
      if (isset($node->webform['components'][$cid])) {
        $component = $node->webform['components'][$cid];

        // Let each component add its data.
        $raw_data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
        if (webform_component_feature($component['type'], 'csv')) {
          $data = webform_component_invoke($component['type'], 'csv_data', $component, $options, $raw_data);

          // Allow modules to modify the CSV data.
          drupal_alter('webform_csv_data', $data, $component, $submission);
          if (is_array($data)) {
            $row = array_merge($row, array_values($data));
          }
          else {
            $row[] = isset($data) ? $data : '';
          }
        }
      }
    }

    // Write data from submissions.
    $data = $exporter
      ->add_row($handle, $row);
  }

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

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