You are here

function webform_download_sids_query in Webform 7.4

Given a set of range options, return an unexecuted select query.

The query will have no fields as they should be added by the caller as desired.

Parameters

int $nid: The node id of the webform.

array $range_options: Associate array of range options.

int $uid: The user id of the user whose last download information should be used, or the current user if NULL. This is unrelated to which user submitted the submissions.

Return value

QueryAlterableInterface The query object.

1 call to webform_download_sids_query()
webform_results_batch_rows in includes/webform.report.inc
Batch API callback; Write the rows of the export to the export file.

File

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

Code

function webform_download_sids_query($nid, array $range_options, $uid = NULL) {
  $query = db_select('webform_submissions', 'ws')
    ->condition('ws.nid', $nid)
    ->addTag('webform_download_sids');
  switch ($range_options['range_type']) {
    case 'all':

      // All Submissions.
      $query
        ->orderBy('ws.sid', 'ASC');
      break;
    case 'latest':

      // Last x Submissions.
      $start_sid = webform_download_latest_start_sid($nid, $range_options['latest'], $range_options['completion_type']);
      $query
        ->condition('ws.sid', $start_sid, '>=');
      break;
    case 'range':

      // Submissions Start-End.
      $query
        ->condition('ws.sid', $range_options['start'], '>=');
      if ($range_options['end']) {
        $query
          ->condition('ws.sid', $range_options['end'], '<=');
      }
      $query
        ->orderBy('ws.sid', 'ASC');
      break;
    case 'range_serial':

      // Submissions Start-End, using serial numbers.
      $query
        ->condition('ws.serial', $range_options['start'], '>=');
      if ($range_options['end']) {
        $query
          ->condition('ws.serial', $range_options['end'], '<=');
      }
      $query
        ->orderBy('ws.serial', 'ASC');
      break;
    case 'new':

      // All since last download. This is the same as 'range_date' except that
      // the start date is the date of the last download.
      $download_info = webform_download_last_download_info($nid, $uid);
      $start_date = (int) $download_info ? $download_info['requested'] : 0;
      $end_time = NULL;

    // Fall through.
    case 'range_date':

      // If the submission is completed, use the completed date, otherwise, the
      // submitted date.
      $date_field = 'COALESCE(NULLIF(ws.completed, 0), ws.submitted)';

      // This is required because SQLite uses dynamic typing.
      if (db_driver() === 'sqlite') {
        $date_field = 'CAST(' . $date_field . ' AS INTEGER)';
      }
      if (!isset($start_date)) {
        $format = webform_date_format('short');
        $start_date = DateTime::createFromFormat($format, $range_options['start_date']);
        $start_date
          ->setTime(0, 0, 0);
        $start_date = $start_date
          ->getTimestamp();
        $end_time = DateTime::createFromFormat($format, $range_options['end_date']);
      }
      $query
        ->where($date_field . ' >= :start_date', array(
        ':start_date' => $start_date,
      ));
      if ($end_time) {

        // Check for the full day's submissions.
        $end_time
          ->setTime(23, 59, 59);
        $end_time = $end_time
          ->getTimestamp();
        $query
          ->where($date_field . ' <= :end_time', array(
          ':end_time' => $end_time,
        ));
      }
      $query
        ->orderBy($date_field, 'ASC');
      break;
  }

  // Filter down to draft or finished submissions.
  if (!empty($range_options['completion_type']) && $range_options['completion_type'] !== 'all') {
    $query
      ->condition('is_draft', (int) ($range_options['completion_type'] === 'draft'));
  }
  if (isset($range_options['batch_number']) && !empty($range_options['batch_size'])) {
    $query
      ->range($range_options['batch_number'] * $range_options['batch_size'], $range_options['batch_size']);
  }
  drupal_alter('webform_download_sids_query', $query);
  return $query;
}