You are here

function votingapi_add_votes in Voting API 6.2

Same name and namespace in other branches
  1. 6 votingapi.module \votingapi_add_votes()
  2. 7.2 votingapi.module \votingapi_add_votes()

Save a collection of votes to the database.

This function does most of the heavy lifting for VotingAPI the main votingapi_set_votes() function, but does NOT automatically triger re-tallying of results. As such, it's useful for modules that must insert their votes in separate batches without triggering unecessary recalculation.

Remember that any module calling this function implicitly assumes responsibility for calling votingapi_recalculate_results() when all votes have been inserted.

Parameters

$votes: A vote or array of votes, each with the following structure: $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())

Return value

The same votes, with vote_id keys populated.

See also

votingapi_set_votes()

votingapi_recalculate_results()

4 calls to votingapi_add_votes()
VotingAPITestCase::testAddMultipleVotes in tests/votingapi.test
Add multiple votes and ensure that the vote calculations are working.
VotingAPITestCase::testAddVote in tests/votingapi.test
Add a vote and ensure that the vote was stored properly.
VotingAPITestCase::testMinimalAdd in tests/votingapi.test
Ensure that the optional fields are truly optional.
votingapi_set_votes in ./votingapi.module
Cast a vote on a particular piece of content.

File

./votingapi.module, line 239
A generalized voting API for Drupal.

Code

function votingapi_add_votes(&$votes) {
  if (!empty($votes['content_id'])) {
    $votes = array(
      $votes,
    );
  }
  $function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_add_vote';
  foreach ($votes as $key => $vote) {
    _votingapi_prep_vote($vote);
    $function($vote);
    $votes[$key] = $vote;
  }
  module_invoke_all('votingapi_insert', $votes);
  return $votes;
}