You are here

function _webform_analysis_rows_grid in Webform 5

Same name and namespace in other branches
  1. 5.2 components/grid.inc \_webform_analysis_rows_grid()
  2. 6.2 components/grid.inc \_webform_analysis_rows_grid()

Calculate and returns statistics about results for this component from all submission to this webform. The output of this function will be displayed under the "results" tab then "analysis".

Parameters

$component: An array of information describing the component, directly correlating to the webform_component database schema.

Return value

An array of data rows, each containing a statistic for this component's submissions.

File

components/grid.inc, line 227

Code

function _webform_analysis_rows_grid($component) {

  // Generate the list of options.
  $rows = explode("\n", _webform_filtervalues($component['extra']['options']));
  $options = array();
  foreach ($rows as $row) {
    $row = trim($row);
    if (preg_match('/^([^"|]+)\\|(.*)$/', $row, $matches)) {
      $options[$matches[1]] = $matches[2];
    }
    else {
      $options[$row] = $row;
    }
  }

  // Generate the list of questions.
  $rows = explode("\n", _webform_filtervalues($component['extra']['questions']));
  $questions = array();
  $cid = 0;
  foreach ($rows as $row) {
    $row = trim($row);
    if (preg_match('/^([^"|]+)\\|(.*)$/', $row, $matches)) {
      $questions[$matches[1]] = $matches[2];
    }
    else {
      $questions[$cid++] = $row;
    }
  }

  // Generate a lookup table of results.
  $query = 'SELECT no, data, count(data) as datacount ' . ' FROM {webform_submitted_data} ' . ' WHERE nid = %d ' . ' AND cid = %d ' . " AND data != '0' AND data != '' " . ' GROUP BY no, data';
  $result = db_query($query, $component['nid'], $component['cid']);
  $counts = array();
  while ($data = db_fetch_object($result)) {
    $counts[$data->no][$data->data] = $data->datacount;
  }

  // Create an entire table to be put into the returned row.
  $rows = array();
  $header = array(
    '',
  ) + $options;
  foreach ($questions as $qkey => $question) {
    $row = array(
      $question,
    );
    foreach ($options as $okey => $option) {
      $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
    }
    $rows[] = $row;
  }
  $output = theme('table', $header, $rows);
  return array(
    array(
      array(
        'data' => $output,
        'colspan' => 2,
      ),
    ),
  );
}