1

I'm using Drupal 7 with the FiveStar module to let users vote on a certain node type. When the user votes, I have a workflow rule that grants them some points to reward them for casting their vote.

The problem I'm running into is that if the user votes more than once on a given node, they are granted points again, each time they vote. There is currently no filter to check that they have already voted before awarding the points.

I could use some help building a workflow rule filter that checks to make sure the user has not already voted on the particular node.

Conceptually I would like to query the votingapi table for any records with the given node ID and given user ID and pass only if zero records are returned. But not sure how to do this in Drupal. Trying to avoid hard-coding a query too.

Thanks BH

2

No need to hardcode a query. The voting api module provides some helper function for this.

The function you need is votingapi_select_votes($criteria = array(), $limit = 0)

The $criteria parameter takes a decent amount of options.

Here are the options:

$criteria['vote_id']  (If this is set, all other keys are skipped)
$criteria['entity_id']
$criteria['entity_type']
$criteria['value_type']
$criteria['tag']
$criteria['uid']
$criteria['vote_source']
$criteria['timestamp']   (If this is set, records with timestamps
                          GREATER THAN the set value will be selected.)

In your case you really want to pass in the 'entity_id', 'entity_type' and 'uid' to check if the user has voted in that particular content item before awarding him points.

This is the same way we prevent a user from voting multiple times in the same item with Fivestar.

Hope this helps.

  • I'm struggling getting it to work - to get node ID and user ID can I use $nid and $uid ? Those are the only things I can see failing at this point – Brad Hein Jun 28 '13 at 13:44
0

Kind of a weird work around, but you could have a taxonomy vocabulary that adds a term every time a node of whatever type of content is being voted on is created, then when a user votes for a node, it adds the title of the node (or nid?) to a hidden field on their account that lists taxonomy terms from that vocabulary you created.

Then add a condition to your rule that says, if they have this nid or title in their hidden taxonomy field, don't give them points, or subtract the added points, however you want to work it.

It's definitely not ideal, but if you have knowledge of Rules then this is an easy work around for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.