Problem/Motivation

I am working on a site using the Voting API module and the Like & Dislike module. I would like to allow anonymous users to vote.

This module saves a vote_source (by default the IP address) for each vote, so this should be possible. The problem is that when a module (here Like & Dislike) asks how many times the current user has voted by calling VoteStorage::getUserVotes() the vote_source is ignored. If the current user is anonymous, and a different anonymous user has voted, it looks as though the currrent user has already voted.

Proposed resolution

Add an optional parameter to VoteStorage::getUserVotes() specifying the vote source. If the parameter is omitted, and the user is anonymous, then use the default callback for the vote type.

Whenever the user is anonymous, count only votes for the matching vote_source, whether passed in or from the default.

Make corresponding changes to the VoteStorage::deleteUserVotes() method.

Remaining tasks

  • Write a patch.
  • Review the patch.
  • Update the documentation.

User interface changes

None.

API changes

This changes the public API of the VoteStorage class. It adds an optional parameter to the getUserVotes() and deleteUserVotes() methods, and it changes the behavior of these method when the user is anonymous (whether or not the optional parameter is supplied). IMHO, the change in behavior is a bug fix.

Modules that use the Voting API and manage the vote source themselves (using Vote::setSource() before Vote::save()) will have to be updated to use the new parameter. Modules that use the default behavior will not have to change, but they will get different results when calling VoteStorage::getUserVotes() for anonymous users.

Data model changes

None. There is already a vote_source field in the database table.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

benjifisher created an issue. See original summary.

benjifisher’s picture

Status: Active » Needs review
FileSize
4.15 KB

The attached patch does what I suggested in the issue description. It also makes the corresponding update to the deleteUserVotes() method. In fact, in the spirit of DRY, this patch replaces most of the body of that function with a call to getUserVotes().

benjifisher’s picture

Issue summary: View changes
Tom.W’s picture

@benjifisher - As we were discussing this yesterday and you asked for my input - Could you write an accompanying test which proves that this change works as intended? That would definitely make the patch easier to review

benjifisher’s picture

@Tom.W You might have changed the status to NW.

I have added two new patches. The "tests-only" patch should lead to a failing test if you apply it. (It looks as though automated testing is disabled for this module.) It also amounts to an interdiff. The other patch includes both the new test and the changes from #2.

jeroen.b’s picture

I can confirm that this patch fixes the issue in like_and_dislike module.
Thanks for the patch @benjifisher!

benjifisher’s picture

@jeroen.b Can you mark the issue RTBC? Your comment suggests that you have Tested, but maybe you have not Reviewed the code changes.

jeroen.b’s picture

Status: Needs review » Reviewed & tested by the community

Did a code review now. Looks good.

BarisW’s picture

Thanks for the patch. I'm still having issues with VotingAPI Widgets, but I'm afraid that's related to how that module checks if voting should be enabled: #2896994: Anonymous voting doesn't work

  • pifagor committed 9d409b6 on 8.x-3.x
    Issue #2935545 by kala4ek: Deprecated constant REQUEST_TIME. Issue #...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Done

pifagor’s picture

Status: Fixed » Closed (fixed)