You are here

function webform_get_submissions in Webform 6.3

Same name and namespace in other branches
  1. 5.2 webform_submissions.inc \webform_get_submissions()
  2. 6.2 webform_submissions.inc \webform_get_submissions()
  3. 7.4 includes/webform.submissions.inc \webform_get_submissions()
  4. 7.3 includes/webform.submissions.inc \webform_get_submissions()

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.

6 calls to webform_get_submissions()
webform_component_delete in includes/webform.components.inc
webform_get_submission in includes/webform.submissions.inc
Fetch a specified submission for a webform node.
webform_results_clear in includes/webform.report.inc
Delete all submissions for a node.
webform_results_export in includes/webform.report.inc
Generate a Excel-readable CSV file containing all submissions for a Webform.
webform_results_submissions in includes/webform.report.inc
Retrieve lists of submissions for a given webform.

... See full list

File

includes/webform.submissions.inc, line 583
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'])) {
    if (is_array($filters['sid'])) {
      $sids = $filters['sid'];
    }
    else {
      $sids = array(
        $filters['sid'],
      );
    }
  }
  else {
    $arguments = array_values($filters);
    $where = array();
    foreach ($filters as $column => $value) {
      $where[] = $column . ' = ' . (is_numeric($value) ? '%d' : "'%s'");
    }
    if (isset($filters['u.uid']) && $filters['u.uid'] === 0) {
      if (!empty($_SESSION['webform_submission'])) {
        $anonymous_sids = array_keys($_SESSION['webform_submission']);
        $where[] = 'sid IN (' . db_placeholders($anonymous_sids) . ')';
        $arguments = array_merge($arguments, $anonymous_sids);
      }
      else {
        $where[] = 'sid = 0';
      }
    }
    $where_clause = implode(' AND ', $where);
    $pager_query = 'SELECT sid FROM {webform_submissions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE ' . $where_clause;
    if (is_array($header)) {
      $pager_query .= tablesort_sql($header);
    }
    else {
      $pager_query .= ' ORDER BY sid ASC';
    }
    if ($pager_count) {
      $result = pager_query($pager_query, $pager_count, 0, NULL, $arguments);
    }
    else {
      $result = db_query($pager_query, $arguments);
    }
    $sids = array();
    while ($row = db_fetch_object($result)) {
      $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 = 'SELECT s.*, sd.cid, sd.no, sd.data, u.name, u.mail, u.status ' . 'FROM {webform_submitted_data} sd ' . 'LEFT JOIN {webform_submissions} s ON s.sid = sd.sid ' . 'LEFT JOIN {users} u ON u.uid = s.uid ' . 'WHERE sd.sid IN (' . db_placeholders($sids) . ') ' . (isset($filters['nid']) ? 'AND sd.nid = %d ' : '') . 'ORDER BY sd.sid ASC, sd.cid ASC, sd.no ASC';
  $args = $sids;
  if (isset($filters['nid'])) {
    $args[] = $filters['nid'];
  }
  $result = db_query($query, $args);

  // Convert the queried rows into submissions.
  $previous = array();
  while ($row = db_fetch_object($result)) {
    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;
}