You are here

function rate_rate_vote_alter in Rate 7

File

./rate.module, line 706
Rate module

Code

function rate_rate_vote_alter($votes, &$context) {

  // Make sure that this feature is enabled for the widget.
  if (!$context['widget']->delete_vote_on_second_click) {
    return;
  }
  $criteria = $context['criteria'];

  // This might create problems for other modules doing this so soon.
  // Which is why I don't pass in the $votes variable by reference,
  // so that it doesn't get changed before other hooks get to run.
  if (!empty($votes['entity_id'])) {
    $votes = array(
      $votes,
    );
  }

  // Handle clearing out old votes if they exists.
  if (empty($criteria)) {

    // If the calling function didn't explicitly set criteria for vote deletion,
    // build up the delete queries here.
    foreach ($votes as $vote) {
      $tmp = votingapi_current_user_identifier();
      $tmp += $vote;
      $tmp_v = votingapi_select_votes($tmp);
      if (!empty($tmp_v)) {
        votingapi_delete_votes($tmp_v);
        $context['save'] = FALSE;

        // We have to recalculate all the results for it.
        votingapi_recalculate_results($vote['entity_type'], $vote['entity_id']);
      }
    }
  }
  elseif (is_array($criteria)) {

    // The calling function passed in an explicit set of delete filters.
    if (!empty($criteria['entity_id'])) {
      $criteria = array(
        $criteria,
      );
    }
    foreach ($criteria as $c) {
      $tmp_v = votingapi_select_votes($c);
      if (!empty($tmp_v)) {
        votingapi_delete_votes($tmp_v);
        $context['save'] = FALSE;

        // We have to recalculate all the results for it.
        votingapi_recalculate_results($c['entity_type'], $c['entity_id']);
      }
    }
  }
}