Once a vote has been cast, you cannot tell if the user has previous voted, and/or what was the value of their previous vote.
For me, I need to do a comparison on the previous value. I want to do this with Voting Rules.
Case:
User has a currency of 10 votes. They can spend them on any votes of a specific node type. They can vote up to 5 times on an entity, using Vote Up/Down (and this patch #1869790: Allow multiple votes).
Solution:
Add a field to the users (decimal/float type) with a default value of 10.
Add the patch below to VotingAPI module
Install Vote Up/Down module
Add Default Up/Down widget to specific node type.
Add #1869790: Allow multiple votes patch to Vote Up/Down
Setup a rules for when the user casts a vote on specific node type
- Calculate the difference between the current and previous vote
- Add/Subtract value from the user field
This works for other solutions
- Display the previous vote in a view
- Tell if the user has voted before (previous vote will be NULL if they haven't)
- For modules like Rate, using Voting Rules, if the user voted Red before, they cannot vote Green, only Blue.
- For modules like fivestar and Voting Rules, you can tell the user change the "5/5" to "1/5" and the they have decrease their rating by 80 percent.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | voting-rules-expose-previous-value-1880462-4.patch | 1013 bytes | melissavdh |
| #3 | add_previous_vote-1880462-3.patch | 3.65 KB | istryker |
| #2 | add-previous-vote-1880462-2.patch | 3.27 KB | istryker |
| #1 | add-previous-vote-1880462-1.patch | 3.83 KB | istryker |
Comments
Comment #1
istryker commentedAttached it the patch.
Patch includes:
- Database upgrade hook
- Views integration
Minor Side-effect (with no solution):
After updating the database, all previous vote values are NULL. Its as if no one has voted twice or more before.
Comment #2
istryker commentedLooks like I added additional code to the patch. New patch removes this
Comment #3
istryker commentedWith patch #18 from #1269614: Add pre-save or pre-set hook before data is store to database you can add the previous value to be evaluated by implementing
hook_votingapi_preset_votes_alter().When you pass it by reference and set the previous value, Patch #2 breaks. You cannot select the correct vote to delete from the database, as the previous value now set. Attached is a patch that fixes this.
Simple example
Example that I am using in Voting Rules #1880550: Need: Pre vote or Pre cast Event
Comment #4
melissavdh commentedI have used the patch in #3 and it successfully defines a new column in the votingapi_vote table and populates it as expected. In order to expose the new previous_value field to Rules I added it to hook_rules_data_type_info() in voting_rules.module. Now I have defined a rule that says "IF vote:previous_value field is empty then award userpoints" (i.e. award the points if the user has not voted on that node before already).
Patch attached.
Comment #5
istryker commented@melissavdh I have created the identical patch already in the Voting Rules issue queue. I should have referenced it before. #1882086: Add Rule for Previous vote value
Thank you for you comment and explanation though. It does have its place here. I believe its safe to change the status to rtbc
Comment #6
adamtong commentedwill this patch in #4 committed in the module?
Comment #7
torotil commentedI'm not in favour of adding a previous_value column to the schema. I think there are better ways to approach your use-cases. As mentioned in #3 there is already a patch that might do what you want without the additional column.
Comment #8
istryker commentedI have rethought this. If we do an additional query in the set function, before the preset hook, and grab the "current" vote and add it to the $vote variable, then we should be good.
I will create a new issue.
FYI - The 3 patches I have for Voting Rules will have to be rewritten too.
Leaving this issue closed (won't fix).
Comment #9
socialnicheguru commentedthis issue is a little confusing.
the patch in #3 is the one to be applied to votingapi
the patch in https://www.drupal.org/node/1882086 should be applied to voting_rules to take advantage of #3
the patch in #4 is superceded by the patch here at https://www.drupal.org/node/1882086 as stated in 5
but why is this won't fix?