vote_up_down.module in Vote Up/Down 5
Same filename and directory in other branches
vote_up_down is a module that adds a widget for +1/-1 votes on nodes. It depends upon Voting API. It's based upon "simplevote.module".
File
vote_up_down.moduleView source
<?php
/**
* @file
* vote_up_down is a module that adds a widget for +1/-1 votes on nodes.
* It depends upon Voting API.
* It's based upon "simplevote.module".
*/
/* if (module_exists('views')) { */
/* include_once('vote_up_down_views.inc'); */
/* } */
if (module_exists('actions')) {
include_once 'vote_up_down_actions.inc';
}
/**
* Implementation of hook_help().
*/
function vote_up_down_help($section) {
switch ($section) {
case 'admin/help#vote_up_down':
return t('<p>This module is used to add a up/down vote widget to selected node types, part of Vote up/down.</p>');
}
}
/**
* Implementation of hook_perm().
*/
function vote_up_down_perm() {
return array(
'view up-down vote',
'use up-down vote',
'access up-down vote statistic',
'administer up-down vote',
);
}
/**
* Implementation of hook_settings().
*/
function vote_up_down_admin_settings() {
$form['vote_node_types'] = array(
'#type' => 'fieldset',
'#title' => t('Node types'),
'#description' => t('Set the node types you want to activate voting on.'),
'#collapsible' => TRUE,
);
$form['vote_node_types']['vote_up_down_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Types'),
'#default_value' => variable_get('vote_up_down_node_types', array()),
'#options' => node_get_types('names'),
);
$form['vote_widget_settings_node'] = array(
'#type' => 'fieldset',
'#title' => t('Vote widget settings for nodes'),
'#description' => t('For more control over the voting widget placement it can be inserted directly in the theme, see the included template.php and node-storylink.tpl.php for an example. When this method is used turn off the widget display here.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['vote_widget_settings_node']['vote_up_down_widget_style_node'] = array(
'#type' => 'radios',
'#title' => t('Vote widget style'),
'#default_value' => variable_get('vote_up_down_widget_style_node', 0),
'#options' => array(
0 => t('Default +1/-1 vote style'),
1 => t('Alternative +1 vote style'),
),
'#description' => t('If set to alternative style it\'s recomended to turn off the link display of vote points.'),
);
$form['vote_widget_settings_node']['vote_up_down_widget_node'] = array(
'#type' => 'select',
'#title' => t('Vote widget display'),
'#default_value' => variable_get('vote_up_down_widget_node', 3),
'#options' => array(
0 => t('Disabled'),
1 => t('Teaser view'),
2 => t('Full-page view'),
3 => t('Teasers and full-page view'),
),
'#description' => t('When to display the vote widget for nodes.'),
);
$form['vote_widget_settings_node']['vote_up_down_link_node'] = array(
'#type' => 'select',
'#title' => t('Link display of vote points'),
'#default_value' => variable_get('vote_up_down_link_node', 3),
'#options' => array(
0 => t('Disabled'),
1 => t('Teaser view'),
2 => t('Full-page view'),
3 => t('Teasers and full-page view'),
),
'#description' => t('When to display vote points for nodes.'),
);
$form['vote_widget_settings_comment'] = array(
'#type' => 'fieldset',
'#title' => t('Vote widget settings for comments'),
'#description' => t('For more control over the voting widget placement it can be inserted directly in the theme, see the included node-storylink.tpl.php for an example. When this metod is used turn off the widget display here.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['vote_widget_settings_comment']['vote_up_down_widget_style_comment'] = array(
'#type' => 'radios',
'#title' => t('Vote widget style'),
'#default_value' => variable_get('vote_up_down_widget_style_comment', 0),
'#options' => array(
0 => t('Default +1/-1 vote style'),
1 => t('Alternative +1 vote style'),
),
'#description' => t('If set to alternative style it\'s recomended to turn off the link display of vote points.'),
);
$form['vote_widget_settings_comment']['vote_up_down_widget_comment'] = array(
'#type' => 'radios',
'#title' => t('Vote widget display'),
'#default_value' => variable_get('vote_up_down_widget_comment', 1),
'#options' => array(
0 => t('Do not display widget'),
1 => t('Display widget'),
),
);
$form['vote_widget_settings_comment']['vote_up_down_link_comment'] = array(
'#type' => 'radios',
'#title' => t('Link display of vote points'),
'#default_value' => variable_get('vote_up_down_link_comment', 1),
'#options' => array(
0 => t('Do not display link'),
1 => t('Display link'),
),
);
$form['vote_widget_settings_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['vote_widget_settings_advanced']['vote_up_down_tag'] = array(
'#type' => 'textfield',
'#title' => t('Vote tag'),
'#default_value' => variable_get('vote_up_down_tag', 'vote'),
'#description' => t('All votes from the Vote up/down module will be tagged with this term. Using a unique value here may be useful when you deploy various voting modules that all use the Voting API. (Default: vote)'),
);
$form['vote_widget_settings_advanced']['vote_up_down_anonymous_vote'] = array(
'#type' => 'radios',
'#title' => t('Allow anonymous voting'),
'#default_value' => variable_get('vote_up_down_anonymous_vote', 0),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('If yes anonymous users may cast votes, restricted to one vote per day and IP address.'),
);
$form['vote_widget_settings_advanced']['vote_up_down_reset_vote'] = array(
'#type' => 'radios',
'#title' => t('Allow users to reset their votes'),
'#default_value' => variable_get('vote_up_down_reset_vote', 0),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('If yes a link will be displayd on nodes and commets that will reset the users votes on that node/comment.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_menu().
*/
function vote_up_down_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/voteupdown',
'title' => t('Vote up/down'),
'description' => t('Control the functions of Vote up/down.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'vote_up_down_admin_settings',
'access' => user_access('administer up-down vote'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'vote_up_down',
'title' => t('Vote on content'),
'callback' => 'vote_up_down_vote',
'access' => user_access('use up-down vote'),
'type' => MENU_CALLBACK,
);
}
else {
$items[] = array(
'path' => 'voteupdown',
'title' => t('Vote up/down'),
'description' => t('Vote up/down vote summaries.'),
'callback' => 'vote_up_down_page',
'access' => user_access('access up-down vote statistic'),
);
$items[] = array(
'path' => 'voteupdown/usersvotes',
'title' => t('Users by votes'),
'callback' => 'vote_up_down_page',
'access' => user_access('access up-down vote statistic'),
);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$vote_access = user_access('access up-down vote statistic') && in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
$items[] = array(
'path' => 'node/' . arg(1) . '/votesupdown',
'title' => t('Votes'),
'callback' => 'vote_up_down_tracker',
'access' => $vote_access,
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
}
if (arg(0) == 'user' && is_numeric(arg(1))) {
$items[] = array(
'path' => 'user/' . arg(1) . '/votesupdown',
'title' => t('Votes'),
'callback' => 'vote_up_down_user_votes',
'access' => user_access('access up-down vote statistic'),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
}
drupal_add_css(drupal_get_path('module', 'vote_up_down') . '/vote_up_down.css');
drupal_add_js(drupal_get_path('module', 'vote_up_down') . '/ajax_vote_up_down.js');
}
return $items;
}
/**
* Implementation of hook_nodeapi().
*/
function vote_up_down_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'view':
$node_type = in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
if ($node_type) {
$style = variable_get('vote_up_down_widget_style_node', 0) == 1 ? '_alt' : '';
if ($teaser && variable_get('vote_up_down_widget_node', 0) && variable_get('vote_up_down_widget_node', 0) != 2) {
$node->content['vote_up_down'] = array(
'#value' => theme("vote_up_down_widget{$style}", $node->nid, 'node'),
'#weight' => -10,
);
}
else {
if (!$teaser && variable_get('vote_up_down_widget_node', 0) > 1) {
$node->content['vote_up_down'] = array(
'#value' => theme("vote_up_down_widget{$style}", $node->nid, 'node'),
'#weight' => -10,
);
}
}
}
break;
}
}
/**
* Implementation of hook_comment().
*/
function vote_up_down_comment(&$comment, $op) {
switch ($op) {
case 'view':
if (variable_get('vote_up_down_widget_comment', 1)) {
$style = variable_get('vote_up_down_widget_style_comment', 0) == 1 ? '_alt' : '';
$comment->comment = theme("vote_up_down_widget{$style}", $comment->cid, 'comment') . $comment->comment;
}
break;
}
}
/**
* Implementation of hook_link().
*/
function vote_up_down_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
switch ($type) {
case 'node':
$node_type = in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
if ($node_type && user_access('view up-down vote')) {
if (variable_get('vote_up_down_reset_vote', 0) && user_access('use up-down vote')) {
$token = drupal_get_token("vote_up_down/{$type}/{$node->nid}/0");
$links['vote_up_down_reset'] = array(
'title' => t('Reset vote'),
'href' => "vote_up_down/{$type}/{$node->nid}/0",
'attributes' => array(
'title' => t('Reset your vote.'),
),
'query' => drupal_get_destination() . '&token=' . $token,
);
}
if ($teaser && variable_get('vote_up_down_link_node', 0) && variable_get('vote_up_down_link_node', 0) != 2) {
$links['vote_up_down_points'] = theme('vote_up_down_points', $node->nid, $type, TRUE);
}
else {
if (!$teaser && variable_get('vote_up_down_link_node', 0) > 1) {
$links['vote_up_down_points'] = theme('vote_up_down_points', $node->nid, $type, TRUE);
}
}
}
break;
case 'comment':
if (variable_get('vote_up_down_reset_vote', 0) && user_access('use up-down vote') && (variable_get('vote_up_down_widget_comment', 0) || variable_get('vote_up_down_link_comment', 0))) {
$token = drupal_get_token("vote_up_down/{$type}/{$node->cid}/0");
$links['vote_up_down_reset_c'] = array(
'title' => t('Reset vote'),
'href' => "vote_up_down/{$type}/{$node->cid}/0",
'attributes' => array(
'title' => t('Reset your vote.'),
),
'query' => drupal_get_destination() . '&token=' . $token,
);
}
if (variable_get('vote_up_down_link_comment', 0) && user_access('view up-down vote')) {
$links['vote_up_down_points_c'] = theme('vote_up_down_points', $node->cid, $type, TRUE);
}
break;
}
return $links;
}
/**
* Implementation of hook_userpoints().
*/
function vote_up_down_userpoints($op, $points = 0, $uid = 0, $event = NULL, $description = NULL) {
switch ($op) {
case 'setting':
$form['vote_updown_points'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Vote up/down points'),
);
$form['vote_updown_points']['userpoints_vote_up_down'] = array(
'#type' => 'textfield',
'#title' => t('Points for voting on a node (requires the Vote up/down module)'),
'#default_value' => variable_get('userpoints_vote_up_down', 0),
'#size' => 5,
'#maxlength' => 5,
);
$form['vote_updown_points']['userpoints_actions'] = array(
'#type' => 'textfield',
'#title' => t('Points for actions (requires the Actions module)'),
'#default_value' => variable_get('userpoints_actions', 0),
'#size' => 5,
'#maxlength' => 5,
);
return $form;
break;
}
}
/*
* Menu callback; Handles voteupdown and subpages.
*/
function vote_up_down_page() {
$op = arg(1);
switch ($op) {
case 'usersvotes':
$output = vote_up_down_users_votes();
break;
default:
$items = array();
$items[] = l(t('Users by votes'), 'voteupdown/usersvotes');
$output = theme('item_list', $items, t('Vote up/down statistics'));
}
return $output;
}
/*
* Users votes page for the vote_up_down data
*/
function vote_up_down_users_votes() {
$sql = "SELECT COUNT(v.value) AS number_votes, SUM(v.value) AS total_votes, v.uid, v.tag, u.uid, u.name FROM {votingapi_vote} v INNER JOIN {users} u on v.uid = u.uid WHERE v.tag = '%s' GROUP BY u.name";
$sql_cnt = "SELECT COUNT(DISTINCT(uid)) FROM {votingapi_vote}";
$header = array(
array(
'data' => t('User'),
'field' => 'u.name',
),
array(
'data' => t('Votes'),
'field' => 'number_votes',
'sort' => 'desc',
),
array(
'data' => t('Vote sum'),
'field' => 'total_votes',
),
);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 30, 0, $sql_cnt, variable_get('vote_up_down_tag', 'vote'));
while ($vote = db_fetch_object($result)) {
$rows[] = array(
theme('username', $vote),
$vote->number_votes,
$vote->total_votes,
);
}
drupal_set_title(t('Users by votes'));
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 30, 0);
return $output;
}
/*
* Menu callback; display all votes for a node.
*/
function vote_up_down_tracker() {
if ($node = node_load(arg(1))) {
$header = array(
array(
'data' => t('User'),
),
array(
'data' => t('Vote'),
),
array(
'data' => t('Date'),
),
);
$votes = votingapi_get_content_votes('node', $node->nid);
foreach ($votes as $vote) {
$account = user_load(array(
'uid' => $vote[0]->uid,
'status' => 1,
));
$rows[] = array(
theme('username', $account),
$vote[0]->value,
array(
'data' => format_date($vote[0]->timestamp, 'small'),
'class' => 'nowrap',
),
);
}
drupal_set_title(check_plain($node->title));
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 30);
return $output;
}
else {
drupal_not_found();
}
}
/*
* Menu callback; display all votes for a user.
*/
function vote_up_down_user_votes() {
if ($account = user_load(array(
'uid' => arg(1),
'status' => 1,
))) {
if ($account->status || user_access('administer users')) {
$header = array(
array(
'data' => t('Node'),
),
array(
'data' => t('Vote'),
),
array(
'data' => t('Date'),
),
);
$sql = db_rewrite_sql("SELECT n.nid, n.title, v.value, v.timestamp FROM {node} n LEFT JOIN {votingapi_vote} v ON n.nid = v.content_id WHERE v.uid = %d AND v.tag = '%s' AND v.content_type = 'node' AND n.status = 1 ORDER BY v.timestamp DESC");
$result = pager_query($sql, 25, 0, NULL, $account->uid, variable_get('vote_up_down_tag', 'vote'));
while ($node = db_fetch_object($result)) {
$rows[] = array(
l($node->title, 'node/' . $node->nid),
$node->value,
t('!time ago', array(
'!time' => format_interval(time() - $node->timestamp),
)),
);
}
drupal_set_title(check_plain($account->name));
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 25);
return $output;
}
else {
drupal_access_denied();
}
}
else {
drupal_not_found();
}
}
/**
* An voting get handler with AJAX support
*/
function vote_up_down_vote($type, $cid, $value, $ajax = FALSE, $alt = FALSE) {
if (is_numeric($cid) && is_numeric($value) && isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], "vote_up_down/{$type}/{$cid}/{$value}")) {
// Sanity-check the incoming values.
if ($value > 0) {
$vote->value = 1;
}
else {
if ($value < 0) {
$vote->value = -1;
}
else {
$vote->value = 0;
}
}
$vote->value_type = 'points';
$vote->tag = variable_get('vote_up_down_tag', 'vote');
// Userpoints integration.
if (module_exists('userpoints')) {
global $user;
if ($type == 'node') {
$node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.uid FROM {node} n WHERE n.nid = %d'), $cid));
}
else {
$node = db_fetch_object(db_query(db_rewrite_sql('SELECT c.uid FROM {comments} c WHERE c.cid = %d', 'c', 'cid'), $cid));
}
$user_vote = votingapi_get_user_votes($type, $cid, $user->uid);
if ($user->uid && !$user_vote && $node->uid != $user->uid && $vote->value != 0) {
userpoints_userpointsapi('points', variable_get('userpoints_vote_up_down', 0), $user->uid, 'Vote up/down points');
}
}
// Do the voting via voting api.
if ($uid = _vote_up_down_get_uid()) {
if ($vote->value == 0) {
votingapi_unset_vote($type, $cid, $uid);
}
else {
votingapi_set_vote($type, $cid, $vote, $uid);
}
}
if ($ajax) {
$style = $alt == TRUE ? '_alt' : '';
print theme("vote_up_down_points{$style}", $cid, $type);
exit;
}
else {
drupal_goto(drupal_get_destination());
}
}
}
function theme_vote_up_down_widget($cid, $type) {
global $user;
if (user_access('view up-down vote')) {
$output = '<div class="vote-up-down-widget">';
if (user_access('use up-down vote') && ($user->uid || variable_get('vote_up_down_anonymous_vote', 0))) {
$user_vote = votingapi_get_user_votes($type, $cid, _vote_up_down_get_uid());
if ($user_vote[0]->value > 0) {
$class = 'vote-up-act';
$class2 = 'vote-down-inact';
}
else {
if ($user_vote[0]->value < 0) {
$class = 'vote-up-inact';
$class2 = 'vote-down-act';
}
else {
$class = 'vote-up-inact';
$class2 = 'vote-down-inact';
}
}
$token = drupal_get_token("vote_up_down/{$type}/{$cid}/1");
$output .= '<span id="vote_up_' . $cid . '" class="' . $class . '" title="' . url("vote_up_down/{$type}/{$cid}/1/1", array(
'query' => 'token=' . $token,
)) . '">' . l('', "vote_up_down/{$type}/{$cid}/1", array(
'class' => $class,
'title' => t('Vote up'),
), drupal_get_destination() . '&token=' . $token, NULL, FALSE, TRUE) . '</span>';
$token = drupal_get_token("vote_up_down/{$type}/{$cid}/-1");
$output .= '<span id="vote_down_' . $cid . '" class="' . $class2 . '" title="' . url("vote_up_down/{$type}/{$cid}/-1/1", array(
'query' => 'token=' . $token,
)) . '">' . l('', "vote_up_down/{$type}/{$cid}/-1", array(
'class' => $class2,
'title' => t('Vote down'),
), drupal_get_destination() . '&token=' . $token, NULL, FALSE, TRUE) . '</span>';
}
else {
$output .= '<span class="up-inact" title="' . t('You must login to vote.') . '"></span>';
$output .= '<span class="down-inact" title="' . t('You must login to vote.') . '"></span>';
}
$output .= '</div>';
return $output;
}
}
function theme_vote_up_down_widget_alt($cid, $type) {
global $user;
if (user_access('view up-down vote')) {
$output = '<div class="vote-up-down-widget-alt">';
$output .= theme('vote_up_down_points_alt', $cid, $type);
if (user_access('use up-down vote') && ($user->uid || variable_get('vote_up_down_anonymous_vote', 0))) {
$user_vote = votingapi_get_user_votes($type, $cid, _vote_up_down_get_uid());
if ($user_vote[0]->value > 0) {
$class = 'vote-up-act';
}
else {
$class = 'vote-up-inact';
}
$token = drupal_get_token("vote_up_down/{$type}/{$cid}/1");
$output .= '<span id="vote_up_' . $cid . '" class="' . $class . '" title="' . url("vote_up_down/{$type}/{$cid}/1/1/1", array(
'query' => 'token=' . $token,
)) . '">' . l('', "vote_up_down/{$type}/{$cid}/1", array(
'class' => $class,
'title' => t('Vote'),
), drupal_get_destination() . '&token=' . $token, NULL, FALSE, TRUE) . '</span>';
}
else {
$output .= '<span class="up-inact" title="' . t('You must login to vote.') . '"></span>';
}
$output .= '</div>';
return $output;
}
}
function theme_vote_up_down_points($cid, $type, $nodelink = FALSE) {
$vote_result = votingapi_get_voting_result($type, $cid, 'points', variable_get('vote_up_down_tag', 'vote'), 'sum');
if ($nodelink) {
if ($vote_result) {
$output = array(
'title' => '<span id="vote_points_' . $cid . '" class="vote-points">' . format_plural($vote_result->value, '1 point', '@count points') . '</span>',
'html' => TRUE,
);
}
else {
$output = array(
'title' => '<span id="vote_points_' . $cid . '" class="vote-points">' . t('0 points') . '</span>',
'html' => TRUE,
);
}
}
else {
if ($vote_result) {
$output = '<span id="vote_points_' . $cid . '" class="vote-points">' . format_plural($vote_result->value, '1 point', '@count points') . '</span>';
}
else {
$output = '<span id="vote_points_' . $cid . '" class="vote-points">' . t('0 points') . '</span>';
}
}
return $output;
}
function theme_vote_up_down_points_alt($cid, $type) {
$vote_result = votingapi_get_voting_result($type, $cid, 'points', variable_get('vote_up_down_tag', 'vote'), 'sum');
if ($vote_result) {
$output = '<div id="vote_points_' . $cid . '" class="vote-points">' . $vote_result->value;
}
else {
$output = '<div id="vote_points_' . $cid . '" class="vote-points">0';
}
$output .= '<div class="vote-points-label">' . format_plural($vote_result->value, 'point', 'points') . '</div></div>';
return $output;
}
/**
* Get a uid for anonymous vs. registered voting
*/
function _vote_up_down_get_uid() {
global $user;
if ($user->uid) {
$uid = $user->uid;
}
else {
if (variable_get('vote_up_down_anonymous_vote', 0)) {
// Fake uid for anonymous users.
// If the IP is valid turn it into a integer and add the number of the current day.
// The current day is what limit anonymous voting to one vote per day and IP address.
$hostname = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
if ($long = ip2long($hostname)) {
$uid = abs($long) + date('z');
}
}
else {
$uid = NULL;
}
}
return $uid;
}
Functions
Name | Description |
---|---|
theme_vote_up_down_points | |
theme_vote_up_down_points_alt | |
theme_vote_up_down_widget | |
theme_vote_up_down_widget_alt | |
vote_up_down_admin_settings | Implementation of hook_settings(). |
vote_up_down_comment | Implementation of hook_comment(). |
vote_up_down_help | Implementation of hook_help(). |
vote_up_down_link | Implementation of hook_link(). |
vote_up_down_menu | Implementation of hook_menu(). |
vote_up_down_nodeapi | Implementation of hook_nodeapi(). |
vote_up_down_page | |
vote_up_down_perm | Implementation of hook_perm(). |
vote_up_down_tracker | |
vote_up_down_userpoints | Implementation of hook_userpoints(). |
vote_up_down_users_votes | |
vote_up_down_user_votes | |
vote_up_down_vote | An voting get handler with AJAX support |
_vote_up_down_get_uid | Get a uid for anonymous vs. registered voting |