You are here

function votingapi_set_vote in Voting API 6

Same name and namespace in other branches
  1. 5 votingapi.module \votingapi_set_vote()

Cast a vote on a particular piece of content. If a vote already exists, its value is changed. In most cases, this is the function that should be used by external modules.

Parameters

$votes: A single vote object, or an array of vote objects, with the following properties: $vote->content_type (Optional, defaults to 'node') $vote->content_id (Required) $vote->value_type (Optional, defaults to 'percent') $vote->value (Required) $vote->tag (Optional, defaults to 'vote') $vote->uid (Optional, defaults to current user) $vote->vote_source (Optional, defaults to current IP) $vote->timestamp (Optional, defaults to time())

$criteria: A keyed array used to determine what votes will be deleted when the current vote is cast. If no value is specified, all votes for the current content by the current user will be reset. If an empty array is passed in, no votes will be reset and all incoming votes will be saved IN ADDITION to existing ones. $criteria['vote_id'] (If this is set, all other keyes are skipped) $criteria['content_type'] $criteria['content_type'] $criteria['value_type'] $criteria['tag'] $criteria['uid'] $criteria['vote_source'] $criteria['timestamp'] (If this is set, records with timestamps GREATER THAN the set value will be selected.)

Return value

An array of vote result records affected by the vote. The values are contained in a nested array keyed thusly: $value = $results[$content_type][$content_id][$tag][$value_type][$function] If $silent is TRUE, this array will be empty.

File

./votingapi.module, line 131

Code

function votingapi_set_vote($votes = array(), $criteria = NULL) {
  if (is_object($votes)) {
    $votes = array(
      $votes,
    );
  }
  $touched = array();
  foreach ($votes as $key => $vote) {

    // First nuke existing votes by this user.
    $vote = _votingapi_prep_vote($vote);
    if (!isset($criteria)) {
      $criteria = votingapi_current_user_identifier();
      $criteria += (array) $vote;
    }
    if (!empty($criteria)) {
      votingapi_delete_votes(votingapi_select_votes($criteria));
    }
    $votes[$key] = votingapi_add_votes($vote);
    $touched[$vote->content_type][$vote->content_id] = TRUE;
  }
  if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') {
    foreach ($touched as $type => $ids) {
      foreach ($ids as $id => $bool) {
        $touched[$type][$id] = votingapi_recalculate_results($type, $id);
      }
    }
  }
  return $touched;
}