4

In Drupal 7 I have enabled the fivestar module to allow users to vote in their comments. The results are tallied and give an average vote. Cool stuff.

Problem is a person can just keep voting over and over in each new comment or reply.

How do I limited a person to just one vote per node, via the comments. In other words, once they have voted, either the stars disappear, or the persons other vote is the default and cannot be edited?

Thanks

  • hook_vote_alter() is what you're looking for. – Antonio Torres Jul 18 '12 at 5:25
2

You can easily do this using a custom module that contains code similar to the following one.

function YOURMODULENAME_print_rating($nid, $fivestar_widget) {
   $path = drupal_get_path('module', 'fivestar');
   drupal_add_js($path . '/js/fivestar.js');
   drupal_add_css($path . '/css/fivestar.css');
   $voting_message = '';
   $output = '';
   $is_login = user_is_logged_in();
   $rating = votingapi_select_single_result_value(array(
       'entity_id' => $nid,
       'entity_type' => 'node',
       'tag' => 'vote',
       'function' => 'average',
   ));
   if ($is_login) {
     if (isset($rating)) {
       $voting_message = "<div>You have already rated this.</div>";
       $output = theme('fivestar_static', array('rating' => $rating, 'stars' => 5, 'tag' => 'vote')) . $voting_message;
     }
     else {
       $output = render($fivestar_widget);
     }
   }
   else {
     $fivestar_links = l('Login', 'user/login') . ' or ' . l('Register', 'user/register');
     $voting_message = "<div>Only registered user can rate this content type.<br/>$fivestar_links to rate this content type.</div>";
     $output = theme('fivestar_static', array('rating' => $rating, 'stars' => 5, 'tag' => 'vote')) . $voting_message;
   }
   return $output;
 }

Use the below snippet in your node template file.

hide($content['field_fivestar_rating']);// This line will hide the stars which are coming from the fivestar module.
print YOURMODULENAME_print_rating($node->nid, $content['field_fivestar_rating']);// This will print the fivestar.
  • Thank you very much for your code. It works if you replace hide($content['field_fivestar_rating']); print YOURMODULENAME_print_rating($node->nid, $content['field_fivestar_rating']); with hide($content['field_fivestar']); print YOURMODULENAME_print_rating($node->nid, $content['field_fivestar']);. – Sundararaj Govindasamy Aug 7 '12 at 13:13
  • 1
    Any way to do this with Rules and Flags? – user12342 Apr 7 '13 at 3:51
  • @Jeroen: MAy you please check your node.tpl. I think somewhere you are printing fivestar rating field just remove that code and use <code>hide($content['field_fivestar_rating']);// This line will hide the stars which are coming from the fivestar module. print YOURMODULENAME_print_rating($node->nid, $content['field_fivestar_rating']);// This will print the fivestar.</code> – Mohammad Anwar Oct 27 '13 at 16:18

protected by kiamlaluno Sep 7 '12 at 2:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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