You are here

function votingapi_add_votes in Voting API 7.2

Same name and namespace in other branches
  1. 6.2 votingapi.module \votingapi_add_votes()
  2. 6 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

array $votes: A vote or array of votes, each with the following structure: $vote['entity_type'] (Optional, defaults to 'node') $vote['entity_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 REQUEST_TIME)

Return value

array The same votes, with vote_id keys populated.

See also

votingapi_set_votes()

votingapi_recalculate_results()

5 calls to votingapi_add_votes()
MigrateDestinationVotingApiVote::import in ./votingapi.migrate.inc
Derived classes must implement import(), to construct one new object (pre-pppulated using field mappings in the Migration). It is expected to call prepare and complete handlers, passing them $row (the raw data from the source).
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 313
A generalized voting API for Drupal.

Code

function votingapi_add_votes(&$votes) {
  if (!empty($votes['entity_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;
}