You are here

function webform_results_batch_rows in Webform 7.4

Batch API callback; Write the rows of the export to the export file.

1 string reference to 'webform_results_batch_rows'
webform_results_export_batch in includes/webform.report.inc
Return a Batch API array of commands that will generate an export.

File

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

Code

function webform_results_batch_rows($node, $format = 'delimited', $options = array(), &$context = NULL) {
  module_load_include('inc', 'webform', 'includes/webform.export');

  // Initialize the sandbox if this is the first execution of the batch
  // operation.
  if (!isset($context['sandbox']['batch_number'])) {
    $context['sandbox']['batch_number'] = 0;
    $context['sandbox']['sid_count'] = webform_download_sids_count($node->nid, $options['range']);
    $context['sandbox']['batch_max'] = max(1, ceil($context['sandbox']['sid_count'] / $options['range']['batch_size']));
    $context['sandbox']['serial'] = 0;
    $context['sandbox']['last_sid'] = 0;
  }

  // Retrieve the submissions for this batch process.
  $options['range']['batch_number'] = $context['sandbox']['batch_number'];
  $query = webform_download_sids_query($node->nid, $options['range']);

  // Join to the users table to include user name in results, as required by
  // webform_results_download_rows_process.
  $query
    ->leftJoin('users', 'u', 'u.uid = ws.uid');
  $query
    ->fields('u', array(
    'name',
  ));
  $query
    ->fields('ws');
  if (!empty($options['sids'])) {
    $query
      ->condition('ws.sid', $options['sids'], 'IN');
  }
  $submissions = webform_get_submissions_load($query);
  $rows = webform_results_download_rows_process($node, $options, $context['sandbox']['serial'], $submissions);

  // Write these submissions to the file.
  $exporter = webform_export_create_handler($format, $options);
  $handle = fopen($options['file_name'], 'a');
  if (!$handle) {
    return;
  }
  foreach ($rows as $row) {
    $exporter
      ->add_row($handle, $row, $context['results']['row_count']);
    $context['results']['row_count']++;
  }
  $context['sandbox']['serial'] += count($submissions);
  $context['sandbox']['last_sid'] = end($submissions) ? key($submissions) : NULL;
  $context['sandbox']['batch_number']++;
  @fclose($handle);

  // Display status message.
  $context['message'] = t('Exported @count of @total submissions...', array(
    '@count' => $context['sandbox']['serial'],
    '@total' => $context['sandbox']['sid_count'],
  ));
  $context['finished'] = $context['sandbox']['batch_number'] < $context['sandbox']['batch_max'] ? $context['sandbox']['batch_number'] / $context['sandbox']['batch_max'] : 1.0;
  $context['results']['last_sid'] = $context['sandbox']['last_sid'];
}