You are here

function _webform_analysis_component in Webform 6.3

Same name and namespace in other branches
  1. 7.4 webform.api.php \_webform_analysis_component()
  2. 7.3 webform.api.php \_webform_analysis_component()

Calculate and returns statistics about results for this component.

This takes into account all submissions 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.

$sids: An optional array of submission IDs (sid). If supplied, the analysis will be limited to these sids.

$single: Boolean flag determining if the details about a single component are being shown. May be used to provided detailed information about a single component's analysis, such as showing "Other" options within a select list.

Return value

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

Related topics

File

./webform.api.php, line 805
Sample hooks demonstrating usage in Webform.

Code

function _webform_analysis_component($component, $sids = array(), $single = FALSE) {

  // Generate the list of options and questions.
  $options = _webform_component_options($component['extra']['options']);
  $questions = array_values(_webform_component_options($component['extra']['questions']));

  // Generate a lookup table of results.
  $sidfilter = count($sids) ? " AND sid in (" . db_placeholders($sids, 'int') . ")" : "";
  $query = 'SELECT no, data, count(data) as datacount ' . ' FROM {webform_submitted_data} ' . ' WHERE nid = %d ' . ' AND cid = %d ' . " AND data != '' " . $sidfilter . ' GROUP BY no, data';
  $result = db_query($query, array_merge(array(
    $component['nid'],
    $component['cid'],
  ), $sids));
  $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(
    '',
  );

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

  // Add questions as each row.
  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, array(
    'class' => 'webform-grid',
  ));
  return array(
    array(
      array(
        'data' => $output,
        'colspan' => 2,
      ),
    ),
  );
}