Is there a way I can allow only one vote per user (either across the entire site or per content type) per 24 hour period? Currently, the "Registered user vote rollover" option only seems to work per node.

Comments

droddis’s picture

Hey there,

Did you ever get anywhere with this request? I have a similar situation involving multiple videos where you can only vote for 1 item per day.

takyros’s picture

some news? i'm just facing a very similar scenario..

kojow7’s picture

Issue summary: View changes

Sorry, I never did find a way to do this. :(

keve’s picture

Put this code into custom module (It uses Rate module's hook_rate_vote_alter() function.)

It allows one vote (per user per content type).

**
 * 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 = 'image';
  $rate_widget_tag = 'vote'; //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;
      }
    }
  }
}
Tourista’s picture

Hello keve,

I created an empty custom module and added your code. Website doesn't work with this module enabled.

Can you please copy paste the full code which should be on this module file ?

keve’s picture

Tourista:

I guess you missed "/" from the beginning of the php, it should be "/**"

So it should start:
<?php
/**
* Alter the vote before it is saved.
*

Tourista’s picture

Thanks for your answer !
Now the site doesn't crash anymore but my users can vote for several nodes from the same content type.

My content type is "recette", my rate widget name is "concours", can you confirm that the full code should be like that ?

onevote.module :

<?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;
      }
    }
  }
}

onevote.info :

name = One Only vote
description = Rate : user can vote only one time per content type
core = 7.x
dependencies[] = rate
package = onevote
pifagor’s picture

Status: Active » Postponed (maintainer needs more info)