You are here

function fivestar_get_votes in Fivestar 6.2

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

Utility function to retreive VotingAPI votes.

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

Parameters

$type: The content type for which to retreive votes.

$cid: The content ID for which to retreive votes.

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

$uid: Optional. A user ID for which to retreive 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'.
5 calls to fivestar_get_votes()
fivestar_comment_form_alter in ./fivestar_comment.module
Form alter specification for comments.
fivestar_form in ./fivestar.module
Create the fivestar form for the current item. Note that this is not an implementation of hook_form(). We should probably change the function to reflect that.
fivestar_static in ./fivestar.module
Retreive and print out a static display of stars for a piece of content.
fivestar_views_widget_handler in ./fivestar.module
Generic VotingAPI Views formatter for displaying rating widget.
_fivestar_cast_vote in ./fivestar.module
Internal function to handle vote casting, flood control, XSS, IP based voting, etc...

File

./fivestar.module, line 350
A simple n-star voting widget, usable in other forms.

Code

function fivestar_get_votes($type, $cid, $tag = 'vote', $uid = NULL) {
  global $user;
  if (!isset($uid)) {
    $uid = $user->uid;
  }
  $criteria = array(
    'content_type' => $type,
    'content_id' => $cid,
    'value_type' => 'percent',
    'tag' => $tag,
  );
  $votes = array(
    'average' => array(),
    'count' => array(),
    'user' => array(),
  );
  $results = votingapi_select_results($criteria);
  foreach ($results as $result) {
    if ($result['function'] == 'average') {
      $votes['average'] = $result;
    }
    if ($result['function'] == 'count') {
      $votes['count'] = $result;
    }
  }
  if ($uid) {
    $user_vote = votingapi_select_votes($criteria += array(
      'uid' => $uid,
    ));
    if ($user_vote) {
      $votes['user'] = $user_vote[0];
      $votes['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['user'] = array(
      'value' => 0,
    );
  }
  return $votes;
}