Hey all,

I am trying to make user on my site only vote 1 times a day period for a certain tag. The modules im using are Voting api and Rate. I have 2 Rate Widgets but only need this configuration for one which has a tag of vote. Can anyone help me with this? I tried to use the Voting api settings didnt work because it allows users to vote on many nodes once. I need it to be once a day period. I also tried the vote rules module and got confused.

Comments

decibel.places’s picture

Project: Rate » Voting API
Version: 6.x-1.0 » 6.x-2.x-dev
Component: User interface » Miscellaneous
Status: Closed (fixed) » Active

Since you are using the Rate module custom voting widget modify the rate-widget.tpl.php file to set the class on all widget buttons to 'voted' for a 24 hour period after one vote is cast:

<?php
// $Id: rate-widget.tpl.php,v 1.5 2010/09/27 19:39:14 mauritsl Exp $

/**
 * @file
 * Rate widget theme
 *
 * This is the default template for rate widgets. See section 3 of the README
 * file for information on theming widgets.
 */

/**
 * Check db for ip and timestamp to allow only 1 vote per 24 hours
 * mod added by decibel.places on 1/9/2011 rgoya@yahoo.com http://netsperience.org
 */
$curip = ip_address();
$result = db_query("SELECT timestamp FROM {votingapi_vote} WHERE vote_source = '$curip' ORDER BY vote_id DESC LIMIT 1");
$fetch = db_fetch_object($result);
$timeago = $fetch->timestamp;
$timenow = time();
$timespan = $timenow - $timeago;
$readytovote = 0;
if ($timespan > 86400) $readytovote = 1;

// Print all rate buttons
foreach ($links as $link) {
  if ($results['user_vote'] == $link['value'] || !$readytovote) {
    // user has voted
    $class = 'voted';
  }

you could also query for the uid, to allow authenticated users from the same IP to vote once, and uid0 is anonymous

decibel.places’s picture

Project: Voting API » Rate
Version: 6.x-2.x-dev » 6.x-1.0
Component: Miscellaneous » User interface
Status: Active » Closed (fixed)

moving this issue to Rate module issue queue

JoshW’s picture

Project: Voting API » Rate
Version: 6.x-2.x-dev » 6.x-1.0
Component: Miscellaneous » User interface
Status: Active » Closed (fixed)

im still having issues with this. it doesnt seem to be resetting at midnight and when you vote it says you have voted but the image is still a link and you can still vote. I think its changing the class for the vote button but still actually works.

JoshW’s picture

also how would you make it work for authenticated users as well?

JoshW’s picture

Status: Closed (fixed) » Active

reopening because its not fixed.

vm’s picture

Version: 6.x-1.0 » 6.x-1.x-dev
Component: User interface » Code
Category: support » feature
Priority: Critical » Normal

this is a feature request which goes against -dev and shouldn't be marked critical. If it was an existing feature that didn't work as described different story.

JoshW’s picture

full code is

<?php
// $Id: rate-widget.tpl.php,v 1.5 2010/09/27 19:39:14 mauritsl Exp $

/**
 * @file
 * Rate widget theme
 *
 * This is the default template for rate widgets. See section 3 of the README
 * file for information on theming widgets.
 */

/**
 * Check db for ip and timestamp to allow only 1 vote per 24 hours
 * mod added by decibel.places on 1/9/2011 rgoya@yahoo.com http://netsperience.org
 */
$curip = ip_address();
$result = db_query("SELECT timestamp FROM {votingapi_vote} WHERE vote_source = '$curip' ORDER BY vote_id DESC LIMIT 1");
$fetch = db_fetch_object($result);
$timeago = $fetch->timestamp;
$timenow = time();
$timespan = $timenow - $timeago;
$readytovote = 0;
if ($timespan > 86400) $readytovote = 1;

// Print all rate buttons
foreach ($links as $link) {
  if ($results['user_vote'] == $link['value'] || !$readytovote) {
    // user has voted
    $class = 'voted';
  }
  else {
    // user has not voted
    $class = 'not-voted';
  }
  print theme('rate_button', $link['text'], $link['href'], $class);
}

print t('Total votes: !count', array('!count' => $results['count'])); ?>

scott815’s picture

I am trying to do the same and tried the code you gave and wanted to know if you it works in current or only works in Dev. If I can help in getting this work please let me know.

Scott

neoliminal’s picture

If you want to restrict individual logged in users, do so here.

// $Id: rate-widget.tpl.php,v 1.5 2010/09/27 19:39:14 mauritsl Exp $

/**
* @file
* Rate widget theme
*
* This is the default template for rate widgets. See section 3 of the README
* file for information on theming widgets.
*/

/**
* Check db for ip and timestamp to allow only 1 vote per registered user per 24 hours
*
* user check added by neoliminal on 6/23/2011
*
* mod added by decibel.places on 1/9/2011 <a href="mailto:rgoya@yahoo.com" rel="nofollow">rgoya@yahoo.com</a> <a href="http://netsperience.org
* " title="http://netsperience.org
* " rel="nofollow">http://netsperience.org
* </a> 
*
*
*/

$cur_uid = $user->uid;
$result = db_query("SELECT timestamp FROM {votingapi_vote} WHERE uid = '$cur_uid' ORDER BY vote_id DESC LIMIT 1");
$fetch = db_fetch_object($result);
$timeago = $fetch->timestamp;
$timenow = time();
$timespan = $timenow - $timeago;
$readytovote = 0;
if ($timespan > 86400) $readytovote = 1;

// Print all rate buttons
foreach ($links as $link) {
  if ($cur_uid == 0) {
  // Anonymous users may not vote. 
  // Add possibly login option here.
  break 2; 
  }
  elseif ($results['user_vote'] == $link['value'] || !$readytovote) {
    // user has voted
    $class = 'voted';
  } else {
    // user has not voted
    $class = 'not-voted';
  }
  print theme('rate_button', $link['text'], $link['href'], $class);
}

print t('Total votes: !count', array('!count' => $results['count'])); 
emergencyofstate’s picture

-edit-

Found my solution here: http://drupal.org/node/1224150
----

Hi guys. Are there any modifications I can make to the above code to make it work with D7?

I believe db_fetch_object is depreciated but I'm not skilled enough to re-code for 7. Help is appreciated.

tinny’s picture

This may help: http://drupal.org/update/modules/6/7#dbtng

In d7, you use db_query() and then use a foreach loop

$result = db_query("SELECT timestamp FROM {votingapi_vote} WHERE uid = :curuid ORDER BY vote_id DESC LIMIT 1", array(':curuid' => $cur_uid));
foreach ($result as $record) {
    $timeago = $record->timestamp;
    // etc
}

Im not sure you need the foreach loop though if you only have 1 record...

lamp5’s picture

Issue summary: View changes
Status: Active » Closed (outdated)