You are here

function fivestar_get_votes_multiple in Fivestar 7.2

Utility function to retrieve VotingAPI votes.

Note that this should not be used for general vote retrieval, instead the VotingAPI function votingapi_select_results() should be used, which is more efficient when retrieving multiple votes.

Parameters

$entity_type: The Entity type for which to retrieve votes.

$id: The ID for which to retrieve votes.

$tag: The VotingAPI tag for which to retrieve votes.

$uid: Optional. A user ID for which to retrieve votes.

Return value

An array of the following keys:

  • average: An array of VotingAPI results, including the average 'value'.
  • count: An array of VotingAPI results, including the count 'value'.
  • user: An array of VotingAPI results, including the user's vote 'value'.
2 calls to fivestar_get_votes_multiple()
fivestar_field_prepare_view in includes/fivestar.field.inc
Implements hook_field_prepare_view().
fivestar_get_votes in ./fivestar.module

File

./fivestar.module, line 246

Code

function fivestar_get_votes_multiple($entity_type, $ids, $tag = 'vote', $uid = NULL) {
  global $user;
  if (!isset($uid)) {
    $uid = $user->uid;
  }
  $criteria = array(
    'entity_type' => $entity_type,
    'entity_id' => $ids,
    'value_type' => 'percent',
    'tag' => $tag,
  );
  $votes = array();

  // Initialize defaults.
  foreach ($ids as $id) {
    $votes[$entity_type][$id] = array(
      'average' => array(),
      'count' => array(),
      'user' => array(),
    );
  }
  $results = votingapi_select_results($criteria);
  $user_votes = array();
  if (!empty($results)) {
    foreach (votingapi_select_votes($criteria += array(
      'uid' => $uid,
    )) as $uv) {
      $user_votes[$uv['entity_type']][$uv['entity_id']] = $uv;
    }
  }
  foreach ($results as $result) {
    if ($result['function'] == 'average') {
      $votes[$result['entity_type']][$result['entity_id']]['average'] = $result;
    }
    if ($result['function'] == 'count') {
      $votes[$result['entity_type']][$result['entity_id']]['count'] = $result;
    }
    if ($uid) {
      if (!empty($user_votes[$result['entity_type']][$result['entity_id']])) {
        $votes[$result['entity_type']][$result['entity_id']]['user'] = $user_votes[$result['entity_type']][$result['entity_id']];
        $votes[$result['entity_type']][$result['entity_id']]['user']['function'] = 'user';
      }
    }
    else {

      // If the user is anonymous, we never bother loading their existing votes.
      // Not only would it be hit-or-miss, it would break page caching. Safer to
      // always show the 'fresh' version to anon users.
      $votes[$result['entity_type']][$result['entity_id']]['user'] = array(
        'value' => 0,
      );
    }
  }
  return $votes;
}