Motivation

I am writing a VoteResultFunction plugin for the module like and dislike that calculates the total of likes substracting the number of dislikes. This module defines two vote types for liking and disliking so I access data from other votes types when calculating. The problem comes when the last vote from an entity is removed so all the calculations are deleted and since there is no more votes to calculate the result for that entity remains deleted in the votingapi_results table.

Steps to reproduce

Recalculate votes for an entity with no votes.

Proposed resolution

Tally votes even for empty sets

Remaining tasks

Code it.

API changes

VoteResultFunction plugins can receive empty arrays.

Data model changes

When deleting the last vote of an entity the results will be calculated so for example the VoteResultFunction shipped with the module will store zeroes.

Issue fork votingapi-3202103

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rodrigoaguilera created an issue. See original summary.

rodrigoaguilera’s picture

Status: Active » Needs review

Apart from removing the check for an empty array I removed a chunk of logic that was checking for a $vote_type that was never changing as the query already fetches only one type of vote.

Fixed also a bit in the test that was creating a vote of an invalid type.

Please review.

rodrigoaguilera’s picture

For better context this is what I am trying to achieve on the other module
https://www.drupal.org/project/like_and_dislike/issues/3195799#comment-1...

tr’s picture

Version: 8.x-3.x-dev » 4.0.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests

MR needs to be rebased against 4.0.x. But it looks like a reasonable improvement on the code. The only thing I think needs to be added is a test to make sure the result functions work when there are no votes - your patch fixes that for the Average function, but if we add new functions in the future or if some dependent module adds some result functions, then we want to have this test to make sure those functions don't have the same problem.