webform_get_submissions

  1. drupal
    1. 6 webform.submissions.inc function
    2. 7 webform.submissions.inc function
Drupal 7 webform_get_submissions($filters = array(), $header = NULL, $pager_count = 0)

Return all the submissions for a particular node.

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.

$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.

7 calls to webform_get_submissions()

File

sites/all/modules/webform/includes/webform.submissions.inc, line 599
This file is loaded when handling submissions, either submitting new, editing, or viewing. It also contains all CRUD functions for submissions.

Code

function webform_get_submissions($filters = array(), $header = NULL, $pager_count = 0) {
  $submissions = array();

  if (!is_array($filters)) {
    $filters = array('nid' => $filters);
  }

  // UID filters need to be against a specific table.
  if (isset($filters['uid'])) {
    $filters['u.uid'] = $filters['uid'];
    unset($filters['uid']);
  }

  // No need to find SIDs if it was given to us.
  if (isset($filters['sid'])) {
    $sids = array($filters['sid']);
  }
  // Build the list of SIDs that need to be retrieved.
  else {
    $pager_query = db_select('webform_submissions', 'ws')->fields('ws', array('sid'));
    foreach ($filters as $column => $value) {
      $pager_query->condition($column, $value);
    }

    if (isset($filters['u.uid']) || !empty($header)) {
      // Join to the users table for sorting by user name.
      $pager_query->leftJoin('users', 'u', 'u.uid = ws.uid');
    }

    if (isset($filters['u.uid']) && $filters['u.uid'] === 0) {
      if (!empty($_SESSION['webform_submission'])) {
        $anonymous_sids = array_keys($_SESSION['webform_submission']);
        $pager_query->condition('sid', $anonymous_sids, 'IN');
      }
      else {
        $pager_query->condition('sid', 0);
      }
    }

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

    if ($pager_count) {
      // Extending the query instatiates a new query object.
      $pager_query = $pager_query->extend('PagerDefault');
      $pager_query->limit($pager_count);
    }
    $result = $pager_query->execute();

    $sids = array();
    foreach ($result as $row) {
      $sids[] = $row->sid;
      $submissions[$row->sid] = FALSE;
    }
  }

  // If there are no submissions being retrieved, return an empty array.
  if (empty($sids)) {
    return $submissions;
  }

  // Query the required submission data.
  $query = db_select('webform_submitted_data', 'sd');
  $query->leftJoin('webform_submissions', 's', 's.sid = sd.sid');
  $query->leftJoin('users', 'u', 'u.uid = s.uid');
  $query
    ->fields('s')
    ->fields('sd', array('cid', 'no', 'data'))
    ->fields('u', array('name'))
    ->condition('sd.sid', $sids, 'IN')
    ->orderBy('sd.sid', 'ASC')
    ->orderBy('sd.cid', 'ASC')
    ->orderBy('sd.no', 'ASC');

  // By adding the NID to this query we allow MySQL to use the primary key on
  // in webform_submitted_data for sorting (nid_sid_cid_no).
  if (isset($filters['nid'])) {
    $query->condition('sd.nid', $filters['nid']);
  }

  $result = $query->execute();

  // Convert the queried rows into submissions.
  $previous = 0;
  foreach ($result as $row) {
    if ($row->sid != $previous) {
      $submissions[$row->sid] = new stdClass();
      $submissions[$row->sid]->sid = $row->sid;
      $submissions[$row->sid]->nid = $row->nid;
      $submissions[$row->sid]->submitted = $row->submitted;
      $submissions[$row->sid]->remote_addr = $row->remote_addr;
      $submissions[$row->sid]->uid = $row->uid;
      $submissions[$row->sid]->name = $row->name;
      $submissions[$row->sid]->is_draft = $row->is_draft;
      $submissions[$row->sid]->data = array();
    }
    // CID may be NULL if this submission does not actually contain any data.
    if ($row->cid) {
      $submissions[$row->sid]->data[$row->cid]['value'][$row->no] = $row->data;
    }
    $previous = $row->sid;
  }

  foreach (module_implements('webform_submission_load') as $module) {
    $function = $module . '_webform_submission_load';
    $function($submissions);
  }

  return $submissions;
}