You are here

function _webform_analysis_grid in Webform 7.4

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

Implements _webform_analysis_component().

File

components/grid.inc, line 511
Webform module grid component.

Code

function _webform_analysis_grid($component, $sids = array(), $single = FALSE, $join = NULL) {

  // Generate the list of options and questions.
  $node = node_load($component['nid']);
  $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  $questions = _webform_select_replace_tokens($questions, $node);
  $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  $options = _webform_select_replace_tokens($options, $node);

  // Generate a lookup table of results.
  $query = db_select('webform_submitted_data', 'wsd')
    ->fields('wsd', array(
    'no',
    'data',
  ))
    ->condition('wsd.nid', $component['nid'])
    ->condition('wsd.cid', $component['cid'])
    ->condition('wsd.data', '', '<>')
    ->groupBy('wsd.no')
    ->groupBy('wsd.data');
  $query
    ->addExpression('COUNT(wsd.sid)', 'datacount');
  if (count($sids)) {
    $query
      ->condition('wsd.sid', $sids, 'IN');
  }
  if ($join) {
    $query
      ->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  }
  $result = $query
    ->execute();
  $counts = array();
  foreach ($result as $data) {
    $counts[$data->no][$data->data] = $data->datacount;
  }

  // Create an entire table to be put into the returned row.
  $rows = array();
  $header = array(
    '',
  );

  // Add options as a header row.
  foreach ($options as $option) {
    $header[] = webform_filter_xss($option);
  }

  // Add questions as each row.
  foreach ($questions as $qkey => $question) {
    $row = array(
      webform_filter_xss($question),
    );
    foreach ($options as $okey => $option) {
      $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
    }
    $rows[] = $row;
  }

  // Return return the table unless there are no internal questions in the grid.
  if ($rows) {
    return array(
      'table_header' => $header,
      'table_rows' => $rows,
    );
  }
}