Allow authenticated users to vote multiple times with Rate module
Last updated on
30 April 2025
With Rate module you can use this solution (this code is part of my custom module - in my case it's name is regions_vote - i hope you get idea):
define('REGIONS_VOTE_LIMIT', 60); // 60 seconds for demonstration
/**
* Implements hook_rate_vote_alter()
*
* @param type $votes
* @param type $context
*/
function regions_vote_rate_vote_alter(&$votes, &$context) {
// We want save all votes
$context['criteria'] = FALSE;
// User vote allowed only if current time is higher than user
// latest vote time + limit vote time
$latest_vote = regions_vote_check_vote_allowed($votes);
if ($latest_vote['status'] === FALSE) {
$context['save'] = FALSE;
$_SESSION['regions_vote'] = $latest_vote;
}
}
/**
* Implements hook_rate_widget()
*
* @param type $op
* @param type $widget
* @param type $values
*/
function regions_vote_rate_widget($op, &$widget, $values = NULL) {
if ($op == 'view') {
$_SESSION['regions_vote'] = regions_vote_check_vote_allowed(array(
'content_type' => $values['content_type'],
'content_id' => $values['content_id'],
'tag' => REGIONS_VOTE_RATE_TAG,
));
}
}
/**
* Preprocess function for the regions_vote template.
*/
function regions_vote_preprocess_regions_vote_template_regions_vote(&$variables) {
extract($variables);
$variables['button'] = theme('rate_button', $links[0]['text'], $links[0]['href'], 'rate-regions-vote');
$info = array();
if ($mode == RATE_CLOSED) {
$info[] = 'Closed.';
}
if ($mode != RATE_COMPACT && $mode != RATE_COMPACT_DISABLED) {
if (isset($results['user_vote']) && $mode != RATE_CLOSED) {
if ($just_voted) {
$info[] = 'Thanks for your vote. You can add new vote after '. $_SESSION['regions_vote']['new_vote_allowed_rendered'];
}
else {
if ($_SESSION['regions_vote']['status'] === FALSE) {
$info[] = 'Thanks for your vote. You can add new vote after '. $_SESSION['regions_vote']['new_vote_allowed_rendered'];
}
else {
$info[] = 'Thanks for your vote. You could have added new vote for '. $_SESSION['regions_vote']['new_vote_allowed_rendered'];
}
}
}
else {
$info[] = $results['count'] .' votes.';
}
}
$variables['info'] = implode(' ', $info);
if ($just_voted === FALSE) {
unset($_SESSION['regions_vote']);
}
}
/**
* Check if user can vote now.
*
* @global type $user
* @param type $votes
* @return type
*/
function regions_vote_check_vote_allowed($votes) {
global $user;
// Current time.
$current_time = time();
// Only one vote per limit per user.
$limit_vote_time = REGIONS_VOTE_LIMIT;
// Get latest vote time for this content for current logged user.
$latest_vote_time = db_result(db_query(
"SELECT
timestamp
FROM {votingapi_vote}
WHERE
content_type = '%s' AND
content_id = %d AND
tag = '%s' AND
uid = %d
ORDER BY timestamp DESC
LIMIT 1",
$votes['content_type'],
$votes['content_id'],
$votes['tag'],
$user->uid
));
// User vote allowed only if current time is higher than user
// latest vote time + limit vote time
$new_vote_allowed = $latest_vote_time + $limit_vote_time;
$new_vote_allowed_in_seconds = $new_vote_allowed - $current_time;
// User can not vote new_vote_allowed_in_seconds seconds...
if ($current_time < $new_vote_allowed) {
return array(
'status' => FALSE,
'current_time' => $current_time,
'new_vote_allowed' => $new_vote_allowed,
'new_vote_allowed_in_seconds' => $new_vote_allowed_in_seconds,
'new_vote_allowed_rendered' => format_interval($new_vote_allowed_in_seconds),
);
}
// User can vote now...
return array(
'status' => TRUE,
'current_time' => $current_time,
'new_vote_allowed_in_seconds' => abs($new_vote_allowed_in_seconds),
'new_vote_allowed_rendered' => format_interval(abs($new_vote_allowed_in_seconds)),
);
}
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion