0

The anonymous visitors and registered users on my website can rate nodes through a comment with a Fivestar field. I display the average rating on the node's page. While testing this mechanism, I found out that the average isn't calculated correctly when multiple anonymous users vote with a comment. I suspect that only the last vote of a certain uid is taken into account: since all anonymous users have a uid equal to 0 - even if their name in the comment is different - they are considered as the same person.

In order to solve this problem, I did some research to how Fivestar calculates averages. All comments and corresponding votes of anonymous users are stored seperately in the database, so I can certainly use all the available information. I think that I also located the function in fivestar.module (lines 45-59) that calculates the average:

function fivestar_microdata_suggestions() {
  $mappings = array();

  // Add the review mapping for Schema.org
  $mappings['fields']['fivestar']['schema.org'] = array(
    '#itemprop' => array('aggregateRating'),
    '#is_item' => TRUE,
    '#itemtype' => array('http://schema.org/AggregateRating'),
    'average_rating' => array(
      '#itemprop' => array('ratingValue'),
    ),
    'rating_count' => array(
      '#itemprop' => array('ratingCount'),
    ),
  );

  return $mappings;
}

My situation is as follows: I use the average rating of a node in Rules and Views too, so I'm looking for an easy solution. Surely, I expect that I can calculate the correct average with Views or Computed Field, but implementing the usage of this average elsewhere could require quite a lot of adaptations.

Therefore, I would first like to ask you whether you have faced this problem as well. If so, how did you solve it? Is it possible to somehow "overwrite" the way Fivestar calculates averages programatically? I'm not familiar with how one uses schema.org or what information that website provides...

0

According to me, the insufficient support for anonymous voting is due to an underdeveloped Voting API (see my comment on this issue on drupal.org). Since I am not a professional programmer, I am not able to solve this matter on my own, so I looked for a workaround, using the Rules, Views and Computed Field modules. These are the main steps to solve the problem:

  1. Create a comment View with all the comments on a node which have a non-zero voting field. Compute the average vote with aggregation.
  2. Add a new computed field for nodes, which stores the result of the previously created View. The safest option is to add the code in a custom module (see the module's documentation).
  3. In order to update the computed field from step 2, it is necessary to save the corresponding node again. To do that, I created a simple Rule which launches after a comment is edited or deleted.
  4. Additionaly, if you want users to be allowed to vote only once, you can create a node Flag Reacted and Rules which (un)flag a node when (a comment is deleted) it receives a comment. Next, use HOOK_form_comment_form_alter() in your custom module to hide the voting field in the comment form when the current user has already flagged the actual node with Reacted.

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.