You are here

function _webform_analysis_select in Webform 6.3

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

Implements _webform_analysis_component().

File

components/select.inc, line 639
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';
  $placeholders = count($options) ? array_fill(0, count($options), "'%s'") : array();
  $query = 'SELECT data, count(data) as datacount ' . ' FROM {webform_submitted_data} ' . ' WHERE nid = %d ' . ' AND cid = %d ' . " AND data != ''" . $sid_filter . ($placeholders ? ' AND data ' . $option_operator . ' (' . implode(',', $placeholders) . ')' : '') . ' GROUP BY data ';
  $count_query = 'SELECT count(*) as datacount ' . ' FROM {webform_submitted_data} ' . ' WHERE nid = %d ' . ' AND cid = %d ' . " AND data != ''" . $sid_filter;
  $result = db_query($query, array_merge(array(
    $component['nid'],
    $component['cid'],
  ), $sids, array_keys($options)));
  $rows = array();
  $normal_count = 0;
  while ($data = db_fetch_array($result)) {
    $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 = db_result(db_query($count_query, array_merge(array(
        $component['nid'],
        $component['cid'],
      ), $sids)));
      $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;
}