You are here

function _fivestar_cast_vote in Fivestar 6.2

Same name and namespace in other branches
  1. 5 fivestar.module \_fivestar_cast_vote()
  2. 6 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 includes/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 296
A simple n-star voting widget, usable in other forms.

Code

function _fivestar_cast_vote($type, $cid, $value, $tag = NULL, $uid = NULL, $result = FALSE, $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, $tag, $uid)) {
    return array();
  }

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

    // 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);
    }
    else {
      $votes = $criteria += array(
        'value' => $value,
      );
      votingapi_set_votes($votes, $user_votes);
    }
    return fivestar_get_votes($type, $cid, $tag, $uid);
  }
}