You are here

function votingapi_add_vote in Voting API 5

Add a new vote for a given piece of content. If the user has already voted, this casts an additional vote. In most cases, this should not be called directly 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.

$value: An int representing the value of the vote being cast.

$value_type: An optional int representing the meaning of the value param. Three standard types are handled by votingapi: 'percent' -- 'Value' is a number from 0-100. The API will cache an average for all votes. 'points' -- 'Value' is a positive or negative int. The API will cache the sum of all votes. 'option' -- 'Value' is an int representing a specific option. The API will cache a vote-count for each option. Other value_types can be passed in, but no default actions will be taken with them by the API. If no value is passed in, 'percent' is the default.

$tag: A string to separate multiple voting criteria. For example, a voting system that rates software for 'stability' and 'features' would cast two votes, each with a different tag. If none is specified, the default 'vote' tag is used.

$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

The $vote object cast.

1 call to votingapi_add_vote()
votingapi_set_vote in ./votingapi.module
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.

File

./votingapi.module, line 173

Code

function votingapi_add_vote($content_type, $content_id, $value, $value_type = VOTINGAPI_VALUE_DEFAULT_TYPE, $tag = VOTINGAPI_VALUE_DEFAULT_TAG, $uid = NULL) {
  if ($uid == NULL) {
    global $user;
    $uid = $user->uid;
  }
  $vobj->vote_id = db_next_id('{votingapi_vote}');
  $vobj->content_type = $content_type;
  $vobj->content_id = $content_id;
  $vobj->value = $value;
  $vobj->value_type = $value_type;
  $vobj->tag = $tag;
  $vobj->uid = $uid;
  $vobj->timestamp = time();
  $vobj->hostname = $_SERVER['REMOTE_ADDR'];

  // Append internal IP if it exists.
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $vobj->hostname .= '-' . $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  db_query("INSERT INTO {votingapi_vote} (vote_id, content_type, content_id, value, value_type, tag, uid, timestamp, hostname) VALUES (%d, '%s', %d, %f, '%s', '%s', %d, %d, '%s')", $vobj->vote_id, $vobj->content_type, $vobj->content_id, $vobj->value, $vobj->value_type, $vobj->tag, $vobj->uid, $vobj->timestamp, $vobj->hostname);

  // Give other modules a chance to act on the insert operation.
  votingapi_invoke('insert', $vobj);
  return $vobj;
}