You are here

function webform_get_submissions_query in Webform 7.4

Returns an unexecuted webform_submissions query on for the arguments.

This is an internal routine and not intended for use by other modules.

Parameters

$filters: An array of filters to apply to this query. Usually in the format array('nid' => $nid, 'uid' => $uid). A single integer may also be passed in, which will be equivalent to specifying a $nid filter. 'sid' may also be included, either as a single sid or an array of sid's.

$header: If the results of this fetch will be used in a sortable table, pass the array header of the table.

$pager_count: Optional. The number of submissions to include in the results.

Return value

QueryExtendableInterface|SelectQueryInterface The query object.

1 call to webform_get_submissions_query()
webform_get_submissions in includes/webform.submissions.inc
Return all the submissions for a particular node.

File

includes/webform.submissions.inc, line 781
Submission handling functions.

Code

function webform_get_submissions_query($filters = array(), $header = NULL, $pager_count = 0) {
  if (!is_array($filters)) {
    $filters = array(
      'ws.nid' => $filters,
    );
  }

  // Qualify all filters with a table alias. ws.* is assumed, except for u.uid.
  foreach ($filters as $column => $value) {
    if (strpos($column, '.') === FALSE) {
      $filters[($column == 'uid' ? 'u.' : 'ws.') . $column] = $value;
      unset($filters[$column]);
    }
  }

  // If the sid is specified, but there are none, force the query to fail
  // rather than query on an empty array.
  if (isset($filters['ws.sid']) && empty($filters['ws.sid'])) {
    $filters['ws.sid'] = 0;
  }

  // Build the list of submissions and load their basic information.
  $pager_query = db_select('webform_submissions', 'ws')
    ->distinct()
    ->addTag('webform_get_submissions_sids')
    ->fields('ws');

  // Add each filter.
  foreach ($filters as $column => $value) {
    $pager_query
      ->condition($column, $value);
  }

  // Join to the users table to include user name in results.
  $pager_query
    ->leftJoin('users', 'u', 'u.uid = ws.uid');
  $pager_query
    ->fields('u', array(
    'name',
  ));
  if (isset($filters['u.uid']) && $filters['u.uid'] === 0) {
    if (!empty($_SESSION['webform_submission'])) {
      $anonymous_sids = array_keys($_SESSION['webform_submission']);
      $pager_query
        ->condition('ws.sid', $anonymous_sids, 'IN');
    }
    else {
      $pager_query
        ->condition('ws.sid', 0);
    }
  }
  if (is_array($header)) {
    $metadata_columns = array();
    foreach ($header as $header_item) {
      $metadata_columns[] = $header_item['data'];
    }
    $sort = drupal_get_query_parameters();

    // Sort by submitted data column if order is set but not in
    // $metadata_columns.
    if (isset($sort['order']) && !in_array($sort['order'], $metadata_columns, TRUE)) {

      // Default if sort is unset or invalid.
      if (!isset($sort['sort']) || !in_array($sort['sort'], array(
        'asc',
        'desc',
      ), TRUE)) {
        $sort['sort'] = '';
      }
      $pager_query
        ->leftJoin('webform_component', 'wc', 'ws.nid = wc.nid AND wc.name = :form_key', array(
        'form_key' => $sort['order'],
      ));
      $pager_query
        ->leftJoin('webform_submitted_data', 'wsd', 'wc.nid = wsd.nid AND ws.sid = wsd.sid AND wc.cid = wsd.cid');
      $pager_query
        ->orderBy('wsd.data', $sort['sort']);
      $pager_query
        ->orderBy('ws.sid', 'ASC');
    }
    else {

      // Extending the query instantiates a new query object.
      $pager_query = $pager_query
        ->extend('TableSort');
      $pager_query
        ->orderByHeader($header);
    }
  }
  else {
    $pager_query
      ->orderBy('ws.sid', 'ASC');
  }
  if ($pager_count) {

    // Extending the query instantiates a new query object.
    $pager_query = $pager_query
      ->extend('PagerDefault');
    $pager_query
      ->limit($pager_count);
  }
  return $pager_query;
}