You are here

function _fivestar_cast_vote in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar.module \_fivestar_cast_vote()
  2. 6.2 fivestar.module \_fivestar_cast_vote()
  3. 7.2 fivestar.module \_fivestar_cast_vote()

Internal function to handle vote casting, flood control, XSS, IP based voting, etc...

5 calls to _fivestar_cast_vote()
fivestar_comment_insert in ./fivestar_comment.module
Insert a fivestar comment value.
fivestar_comment_update in ./fivestar_comment.module
Update a fivestar comment value.
fivestar_field in ./fivestar_field.inc
Implementation of hook_field().
fivestar_form_submit in ./fivestar.module
Submit handler for the above form (non-javascript version).
fivestar_vote in ./fivestar.module
Callback function for fivestar/vote.

File

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

Code

function _fivestar_cast_vote($type, $cid, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE) {
  global $user;
  $tag = empty($tag) ? 'vote' : $tag;
  $uid = empty($uid) ? $user->uid : $uid;

  // Bail out if the user's trying to vote on an invalid object.
  if (!$skip_validation && !fivestar_validate_target($type, $cid, $uid)) {
    return array();
  }

  // Sanity-check the incoming values.
  if (is_numeric($cid) && is_numeric($value)) {
    if ($value > 100) {
      $value = 100;
    }

    // Lets not allow negative numbers.
    if ($value < 0) {
      $value = 0;
    }

    // Get the user's current vote.
    $criteria = array(
      'content_type' => $type,
      'content_id' => $cid,
      'tag' => $tag,
      'uid' => $uid,
    );

    // Get the unique identifier for the user (IP Address if anonymous).
    $user_criteria = votingapi_current_user_identifier();
    $user_votes = votingapi_select_votes($criteria + $user_criteria);
    if ($value == 0) {
      votingapi_delete_votes($user_votes);
      return $user_votes[0];
    }
    else {
      $votes = $criteria += array(
        'value' => $value,
      );
      votingapi_set_votes($votes, $user_votes);
      return $votes[0];
    }
  }
  return array();
}