You are here

function _webform_analysis_select in Webform 7.3

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

Implements _webform_analysis_component().

File

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

Code

function _webform_analysis_select($component, $sids = array(), $single = FALSE) {
  $options = _webform_select_options($component, TRUE);
  $show_other_results = $single;
  $sid_placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
  $sid_filter = count($sids) ? " AND sid IN (" . implode(",", $sid_placeholders) . ")" : "";
  $option_operator = $show_other_results ? 'NOT IN' : 'IN';
  $query = db_select('webform_submitted_data', 'wsd', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('wsd', array(
    'data',
  ))
    ->condition('nid', $component['nid'])
    ->condition('cid', $component['cid'])
    ->condition('data', '', '<>')
    ->condition('data', array_keys($options), $option_operator)
    ->groupBy('data');
  $query
    ->addExpression('COUNT(data)', 'datacount');
  if (count($sids)) {
    $query
      ->condition('sid', $sids, 'IN');
  }
  $count_query = db_select('webform_submitted_data', 'wsd', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->condition('nid', $component['nid'])
    ->condition('cid', $component['cid'])
    ->condition('data', '', '<>');
  $count_query
    ->addExpression('COUNT(*)', 'datacount');
  if (count($sids)) {
    $count_query
      ->condition('sid', $sids, 'IN');
  }
  $result = $query
    ->execute();
  $rows = array();
  $normal_count = 0;
  foreach ($result as $data) {
    $display_option = $single ? $data['data'] : $options[$data['data']];
    $rows[$data['data']] = array(
      _webform_filter_xss($display_option),
      $data['datacount'],
    );
    $normal_count += $data['datacount'];
  }
  if (!$show_other_results) {

    // 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];
    }

    // Add a row for any unknown or user-entered values.
    if ($component['extra']['other_option']) {
      $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 ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
      $ordered_rows[] = array(
        $display_option,
        $other_text,
      );
    }
    $rows = $ordered_rows;
  }
  return $rows;
}