/** * 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". * * @param $component * An array of information describing the component, directly correlating to * the webform_component database schema. * @param $sids * An optional array of submission IDs (sid). If supplied, the analysis will * be limited to these sids. * @param $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 * An array of data rows, each containing a statistic for this component's * submissions. */ function _webform_analysis_component($component, $sids = array(), $single = FALSE) { // Generate the list of options and questions. $options = _webform_select_options_from_text($component['extra']['options'], TRUE); $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE); // Generate a lookup table of results. $query = db_select('webform_submitted_data', 'wsd')->fields('wsd', array('no', 'data'))->condition('nid', $component['nid'])->condition('cid', $component['cid'])->condition('data', '', '<>')->groupBy('no')->groupBy('data'); $query->addExpression('COUNT(sid)', 'datacount'); if (count($sids)) { $query->condition('sid', $sids, 'IN'); } $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[] = $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', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid')))); return array(array(array('data' => $output, 'colspan' => 2))); }
/** * 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". * * @param $component * An array of information describing the component, directly correlating to * the webform_component database schema. * @param $sids * An optional array of submission IDs (sid). If supplied, the analysis will * be limited to these sids. * @param $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. * @param $join * An optional SelectQuery object to be used to join with the submissions * table to restrict the submissions being analyzed. * @return * An array containing one or more of the following keys: * - table_rows: If this component has numeric data that can be represented in * a grid, return the values here. This array assumes a 2-dimensional * structure, with the first value being a label and subsequent values * containing a decimal or integer. * - table_header: If this component has more than a single set of values, * include a table header so each column can be labeled. * - other_data: If your component has non-numeric data to include, such as * a description or link, include that in the other_data array. Each item * may be a string or an array of values that matches the number of columns * in the table_header property. * At the very least, either table_rows or other_data should be provided. * Note that if you want your component's analysis to be available by default * without the user specifically enabling it, you must set * $component['extra']['analysis'] = TRUE in your * _webform_defaults_component() callback. * * @see _webform_defaults_component() */ function _webform_analysis_component($component, $sids = array(), $single = FALSE, $join = NULL) { // Generate the list of options and questions. $options = _webform_select_options_from_text($component['extra']['options'], TRUE); $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE); // Generate a lookup table of results. $query = db_select('webform_submitted_data', 'wsd')->fields('wsd', array('no', 'data'))->condition('nid', $component['nid'])->condition('cid', $component['cid'])->condition('data', '', '<>')->groupBy('no')->groupBy('data'); $query->addExpression('COUNT(sid)', 'datacount'); if (count($sids)) { $query->condition('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[] = $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; } $other = array(); $other[] = l(t('More information'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']); return array('table_header' => $header, 'table_rows' => $rows, 'other_data' => $other); }