function rate_get_results in Rate 6.2
Same name and namespace in other branches
- 7 rate.module \rate_get_results()
Get results for a voting widget.
Parameters
string $content_type "node" or "comment":
int $content_id Node id (nid) or comment id (cid):
int $widget_id Widget id:
Return value
array
1 call to rate_get_results()
- rate_generate_widget in ./
rate.module - Generate a widget.
File
- ./
rate.module, line 396 - Rate module
Code
function rate_get_results($content_type, $content_id, $widget_id) {
global $user;
$widgets = variable_get(RATE_VAR_WIDGETS, array());
$widget = $widgets[$widget_id];
$criteria = array(
'content_type' => $content_type,
'content_id' => $content_id,
'tag' => $widget->tag,
);
$results = votingapi_select_results($criteria);
$output = array(
'count' => 0,
);
if ($widget->value_type != 'option') {
// Set a default. Does not apply to options.
$output['rating'] = 0;
}
if ($widget->value_type == 'option') {
$output['options'] = array();
foreach ($widget->options as $option) {
$output['options'][$option[0]] = 0;
}
}
foreach ($results as $result) {
if ($widget->value_type == 'percent' && $result['function'] == 'average') {
$output['rating'] = $result['value'];
}
elseif ($widget->value_type == 'points' && $result['function'] == 'sum') {
$output['rating'] = $result['value'];
}
elseif ($result['function'] == 'count') {
$output['count'] = $result['value'];
}
elseif (preg_match('/^option\\-([\\-0-9]+)$/', $result['function'], $match)) {
$output['options'][$match[1]] = $result['value'];
$output['count'] += $result['value'];
}
}
// Check if this is a thumbs up / down voting, if so, calculate thumbs up / down percentages
if ($widget->value_type == 'points' && count($widget->options) == 2) {
// Votes down have a -1 value, votes up +1, $output['rating'] are the summed votes
$output['down'] = (int) ($output['count'] - $output['rating']) / 2;
$output['up'] = $output['count'] - $output['down'];
if ($output['count'] == 0) {
// If no one voted its 50-50
$output['up_percent'] = 50;
}
else {
$output['up_percent'] = (int) ($output['up'] * 100 / $output['count']);
}
$output['down_percent'] = 100 - $output['up_percent'];
}
$criteria += array(
'uid' => $user->uid,
'value_type' => $widget->value_type,
);
if (!$user->uid) {
$criteria += array(
'vote_source' => $_SERVER['REMOTE_ADDR'],
);
}
if ($user_vote = votingapi_select_votes($criteria)) {
// Check the anonymous window. We should not display this vote when its anonymous and casted too long ago.
$anonymous_window = variable_get('votingapi_anonymous_window', 86400);
if ($user->uid || $user_vote[0]['timestamp'] > time() - $anonymous_window || $anonymous_window == -1) {
$output['user_vote'] = $user_vote[0]['value'];
if ($widget->value_type == 'option') {
foreach ($widget->options as $option) {
if ($option[0] == $user_vote[0]['value']) {
$output['user_vote'] = $option[1];
}
}
}
}
}
return $output;
}