Hi,

My users can submit Recipes (a custom content type). I want to create a contest to let my users vote for their unique favorite recipe.

To vote, users (anonymous users) have to go to the page of the recipe they want to vote for and clic on "I vote for this one" to vote for it. If they go to another recipe page, they won't be able to vote for it because they would have already voted for another recipe.

How can i disable votes for users who have already voted for a node ?

Comments

Tourista created an issue. See original summary.

Tourista’s picture

I've found a piece of code to help me (https://www.drupal.org/node/2036071#comment-10833672) :

<?php
/**
 * Alter the vote before it is saved.
 *
 * @param array $vote
 *   array(
 *     'entity_type' => $content_type,
 *     'entity_id' => $content_id,
 *     'value_type' => $widget->value_type,
 *     'value' => $value,
 *     'tag' => $widget->tag,
 *   );
 * @param array $context
 *   array(
 *     'redirect' => &$redirect, // Path. Alter to redirect the user.
 *     'save' => &$save, // Boolean indicating whether the vote must be saved.
 *     'widget' => $widget, // Widget object, unalterable.
 *   );
 */
function hook_rate_vote_alter($vote, $context) {
  $content_type = 'recette';
  $rate_widget_tag = 'concours'; //Name of the rate widget

  global $user;

  //Find votes based on criteria (Here: from the same logged-in user or IP address)
  $criteria['tag'] = $rate_widget_tag;
  $criteria['uid'] = $user->uid;
  if ($user->uid == 0 ) {
    $criteria['vote_source'] = ip_address(); //"89.134.196.161";
  }
    $votes = votingapi_select_votes($criteria);

  //Disable voting if user has already voted on another node in the same content type ($context['save'] = 0;)
  if (!empty($votes)) {
    foreach ($votes as $vote) {
      $nid = $vote['entity_id'];

      $node = node_load($nid);
      if ($node->type == $content_type) {
        $context['save'] = 0;
        print "You already voted for another node in the content type.";

        return;
      }
    }
  }
}

But it doesn't work. Can someone check why it doesn't work ?

ivnish’s picture

Status: Active » Closed (outdated)

Drupal 7 is EOL. Issue will be closed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.