You are here

function _fivestar_cast_vote in Fivestar 7.2

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

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

3 calls to _fivestar_cast_vote()
fivestar_ajax_submit in ./fivestar.module
AJAX submit handler for fivestar_custom_widget.
fivestar_form_submit in ./fivestar.module
Submit handler for the above form (non-javascript version).
_fivestar_field_helper in includes/fivestar.field.inc

File

./fivestar.module, line 182

Code

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

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

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

    // Get the user's current vote.
    $criteria = array(
      'entity_type' => $entity_type,
      'entity_id' => $id,
      'tag' => $tag,
      'uid' => $uid,
    );

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

    // Moving the calculation after saving/deleting the vote but before
    // getting the votes.
    votingapi_recalculate_results($entity_type, $id);
    return fivestar_get_votes($entity_type, $id, $tag, $uid);
  }
}