You are here

function _webform_analysis_select in Webform 7.4

Same name and namespace in other branches
  1. 6.3 components/select.inc \_webform_analysis_select()
  2. 7.3 components/select.inc \_webform_analysis_select()

Implements _webform_analysis_component().

File

components/select.inc, line 648
Webform module multiple select component.

Code

function _webform_analysis_select($component, $sids = array(), $single = FALSE, $join = NULL) {
  $options = _webform_select_options($component, TRUE);

  // Create a generic query for the component.
  $query = db_select('webform_submitted_data', 'wsd', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->condition('wsd.nid', $component['nid'])
    ->condition('wsd.cid', $component['cid'])
    ->condition('wsd.data', '', '<>');
  if ($sids) {
    $query
      ->condition('wsd.sid', $sids, 'IN');
  }
  if ($join) {
    $query
      ->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  }

  // Clone the query for later use, if needed.
  if ($component['extra']['other_option']) {
    $count_query = clone $query;
    if ($single) {
      $other_query = clone $query;
    }
  }
  $rows = array();
  $other = array();
  $normal_count = 0;
  if ($options) {

    // Gather the normal results first (not "other" options).
    $query
      ->addExpression('COUNT(wsd.data)', 'datacount');
    $result = $query
      ->condition('wsd.data', array_keys($options), 'IN')
      ->fields('wsd', array(
      'data',
    ))
      ->groupBy('wsd.data')
      ->execute();
    foreach ($result as $data) {
      $display_option = isset($options[$data['data']]) ? $options[$data['data']] : $data['data'];
      $rows[$data['data']] = array(
        webform_filter_xss($display_option),
        $data['datacount'],
      );
      $normal_count += $data['datacount'];
    }

    // Order the results according to the normal options array.
    $ordered_rows = array();
    foreach (array_intersect_key($options, $rows) as $key => $label) {
      $ordered_rows[] = $rows[$key];
    }
    $rows = $ordered_rows;
  }

  // Add a row for displaying the total unknown or user-entered values.
  if ($component['extra']['other_option']) {
    $count_query
      ->addExpression('COUNT(*)', 'datacount');
    $full_count = $count_query
      ->execute()
      ->fetchField();
    $other_count = $full_count - $normal_count;
    $display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
    $other_text = $other_count && !$single ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
    $rows[] = array(
      $display_option,
      $other_text,
    );

    // If showing all results, execute the "other" query and append their rows.
    if ($single) {
      $other_query
        ->addExpression('COUNT(wsd.data)', 'datacount');
      $other_query
        ->fields('wsd', array(
        'data',
      ))
        ->groupBy('wsd.data');
      if ($options) {
        $other_query
          ->condition('wsd.data', array_keys($options), 'NOT IN');
      }
      $other_result = $other_query
        ->execute();
      foreach ($other_result as $data) {
        $other[] = array(
          check_plain($data['data']),
          $data['datacount'],
        );
      }
      if ($other) {
        array_unshift($other, '<strong>' . t('Other responses') . '</strong>');
      }
    }
  }
  return array(
    'table_rows' => $rows,
    'other_data' => $other,
  );
}