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

1
  • hook_vote_alter() is what you're looking for. Commented Jul 18, 2012 at 5:25

1 Answer 1

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.
3
  • 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']);. Commented Aug 7, 2012 at 13:13
  • 1
    Any way to do this with Rules and Flags?
    – user12342
    Commented Apr 7, 2013 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> Commented Oct 27, 2013 at 16:18

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