I need delete a vote after user votes on a node.

My rule is:

Event: User votes on a Node
Condition: check some parameters (php evaluation).
Action:

db_query ('DELETE FROM {votingapi_vote} WHERE vote_id = :vote_id', array( ':vote_id' => $vote['vote_id'] ));
votingapi_recalculate_results($vote['entity_type'], $vote['entity_id']);

And it works fine, except this Notice:

Undefined variable: vote_results in function voting_rules_votingapi_results() (row 48 in file \sites\all\modules\voting_rules\voting_rules.module).

Please, help me, if have a time

Comments

hlopes’s picture

The same error appeared in my site, when deleting a vote ( i'm assuming it only happens if it is the only vote the node has )

That means that for some reason, the $vote_results variable is not set ( hence my guess )

Adding

	if(!isset($vote_results)){
		$vote_results = array();
	}

Before the troublesome line solved my issue.

/**
 * Implements hook_votingapi_results().
 */
function voting_rules_votingapi_results($cached, $entity_type, $entity_id) {
  $entity = voting_rules_load_entity($entity_type, $entity_id);
  foreach ($cached as $result) {
    $vote_results[$result['function']] = $result;
  }

  if(!isset($vote_results)){
    $vote_results = array();
  }

  if ($entity) {
    rules_invoke_event('voting_rules_results_' . $entity_type, $vote_results, $entity);
  }
}
istryker’s picture

Status: Active » Closed (won't fix)

Why are you doing a direct db query. Just call votingapi_delete_votes(). You will still need to call recalculate results.

This is not the way to do things. It may be the only way of doing things right now though.

Closing issue. See #1882116: Need Action to delete or reset the vote

isimgt’s picture

Issue summary: View changes

Thanks HLopes, #1 working for my..

Anonymous’s picture

I was having this same problem too, when a user reverses a vote so that the content has no votes on it anymore. I added the fix from #1, and I don't get this error anymore. I'm surprised this hasn't been added to the code, that solution was provided 3 years ago?