You are here

function votingapi_set_vote in Voting API 5

Same name and namespace in other branches
  1. 6 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

$content_type: A string identifying the type of content being rated. Node, comment, aggregator item, etc.

$content_id: The key ID of the content being rated.

$vote: This is slightly ugly. $vote can be one of three possible data types: 1: $vote is a number, and is inserted as a vote with default value_type and tag. 2: $vote is an object, with $vote->value, $vote->value_type, and $vote->tag properties. 3: $vote is an array of $vote objects as used in #2, and is iterated through. See docs for votingapi_add_vote() for details on vote_types and tags.

$uid: The uid of the user casting the vote. If none is specified, the currently logged in user's uid will be inserted.

Return value

An array of the votingapi_cache records affected by the vote.

1 call to votingapi_set_vote()
votingapi_generate_create_votes in ./votingapi_generate.module

File

./votingapi.module, line 73

Code

function votingapi_set_vote($content_type, $content_id, $vote, $uid = NULL, $recursion = FALSE) {
  if ($uid == NULL) {
    global $user;
    $uid = $user->uid;
  }
  if (is_array($vote)) {
    foreach ($vote as $vobj) {
      $user_votes[] = votingapi_set_vote($content_type, $content_id, $vobj, $uid, TRUE);
    }
  }
  else {
    if (is_numeric($vote)) {
      $vobj->value = $vote;
      $vobj->value_type = VOTINGAPI_VALUE_DEFAULT_TYPE;
      $vobj->tag = VOTINGAPI_VALUE_DEFAULT_TAG;
      votingapi_set_vote($content_type, $content_id, $vobj, $uid, TRUE);
    }
    else {
      if (is_object($vote)) {
        if (!isset($vote->value_type)) {
          $vote->value_type = VOTINGAPI_VALUE_DEFAULT_TYPE;
        }
        if (!isset($vote->tag)) {
          $vote->tag = VOTINGAPI_VALUE_DEFAULT_TAG;
        }
        $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid);
        while ($vobj = db_fetch_object($result)) {
          votingapi_change_vote($vobj, $vote->value);
          $exists = TRUE;
        }
        if (!$exists) {
          votingapi_add_vote($content_type, $content_id, $vote->value, $vote->value_type, $vote->tag, $uid);
        }
      }
    }
  }
  if ($recursion) {
    return $vobj;
  }
  else {
    if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') {
      return votingapi_recalculate_results($content_type, $content_id);
    }
  }
}