function _webform_analysis_number in Webform 6.3
Same name and namespace in other branches
- 7.4 components/number.inc \_webform_analysis_number()
- 7.3 components/number.inc \_webform_analysis_number()
Implements _webform_analysis_component().
File
- components/
number.inc, line 401 - Webform module number component.
Code
function _webform_analysis_number($component, $sids = array(), $single = FALSE) {
$advanced_stats = $single;
$placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
$sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
$query = 'SELECT data ' . ' FROM {webform_submitted_data} ' . ' WHERE nid = %d ' . ' AND cid = %d ' . $sidfilter;
$population = array();
$submissions = 0;
$non_zero = 0;
$non_empty = 0;
$sum = 0;
$result = db_query($query, array_merge(array(
$component['nid'],
$component['cid'],
), $sids));
while ($data = db_fetch_array($result)) {
$value = trim($data['data']);
$number = (double) $value;
$non_empty += (int) ($value !== '');
$non_zero += (int) ($number != 0.0);
$sum += $number;
$population[] = $number;
$submissions++;
}
sort($population, SORT_NUMERIC);
// Average and population count.
if ($component['extra']['excludezero']) {
$average = $non_zero ? $sum / $non_zero : 0;
$average_title = t('Average !mu excluding zeros/blanks', array(
'!mu' => $advanced_stats ? '(μ)' : '',
));
// Sample (sub-set of total population).
$population_count = $non_zero - 1;
$sigma = 'sd';
$description = t('sample');
}
else {
$average = $submissions ? $sum / $submissions : 0;
$average_title = t('Average !mu including zeros/blanks', array(
'!mu' => $advanced_stats ? '(μ)' : '',
));
// Population.
$population_count = $submissions;
$sigma = 'σ';
$description = t('population');
}
// Formatting.
$average = _webform_number_format($component, $average);
$sum = _webform_number_format($component, $sum);
$rows[0] = array(
t('Zero/blank'),
$submissions - $non_zero,
);
$rows[1] = array(
t('User entered value'),
$non_empty,
);
$rows[2] = array(
t('Sum') . ($advanced_stats ? ' (Σ)' : ''),
$sum,
);
$rows[3] = array(
$average_title,
$average,
);
if (!$advanced_stats && $sum != 0) {
$rows[4] = array(
'',
l(t('More stats »'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']),
);
}
// Normal distribution information.
if ($advanced_stats && $population_count && $sum != 0) {
// Standard deviation.
$stddev = 0;
foreach ($population as $value) {
// Obtain the total of squared variances.
$stddev += pow($value - $average, 2);
}
if ($population_count > 0) {
$stddev = sqrt($stddev / $population_count);
}
else {
$stddev = sqrt($stddev);
}
// Skip the rest of the distribution rows if standard deviation is 0.
if (empty($stddev)) {
return $rows;
}
// Build normal distribution table rows.
$count = array();
$percent = array();
$limit = array();
$index = 0;
$count[] = 0;
$limit[] = $average - $stddev * 4;
foreach ($population as $value) {
while ($value >= $limit[$index]) {
$percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
$limit[] = $limit[$index] + $stddev;
$index += 1;
if ($limit[$index] == $average) {
$limit[$index] = $limit[$index] + $stddev;
}
$count[$index] = 0;
}
$count[$index] += 1;
}
$percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
// Format normal distribution table output.
$stddev = _webform_number_format($component, $stddev);
$low = _webform_number_format($component, $population[0]);
$high = _webform_number_format($component, end($population));
foreach ($limit as $key => $value) {
$limit[$key] = _webform_number_format($component, $value);
}
// Column headings (override potential theme uppercase, e.g. Seven in D7).
$header = array(
t('Normal Distribution'),
array(
'data' => '-4' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '-3' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '-2' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '-1' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '+1' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '+2' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '+3' . $sigma,
'style' => 'text-transform: lowercase;',
),
array(
'data' => '+4' . $sigma,
'style' => 'text-transform: lowercase;',
),
);
// Insert row labels.
array_unshift($limit, t('Boundary'));
array_unshift($count, t('Count'));
array_unshift($percent, t('% of !description', array(
'!description' => $description,
)));
$output = theme('table', $header, array(
$limit,
$count,
$percent,
), array(
'class' => 'webform-grid',
));
$rows[4] = array(
t('Range'),
t('!low to !high', array(
'!low' => $low,
'!high' => $high,
)),
);
$rows[5] = array(
t('Standard deviation (!sigma)', array(
'!sigma' => $sigma,
)),
$stddev,
);
$rows[6] = array(
array(
'data' => $output,
'colspan' => 2,
),
);
}
return $rows;
}