function plus1_vote in Plus 1 7
Same name and namespace in other branches
- 6.2 plus1.module \plus1_vote()
- 6 plus1.module \plus1_vote()
Page callback.
Parameters
$entity_type: Type of the entity, node or comment
$entity_id: A node, comment or taxonomy term ID.
$tag: Tag name to vote for.
$value: Value of vote, can be only 1 or -1. if another value will be provided, it will be handled as error. A tag for different voting on same entity type Submits the vote request and refreshes the page without JavaScript. Otherwise, it submits the vote request and returns JSON to be parsed by jQuery.
1 string reference to 'plus1_vote'
- plus1_menu in ./
plus1.module - Implements hook_menu().
File
- ./
plus1.module, line 130
Code
function plus1_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
global $user;
if (!drupal_valid_token($_GET['token'], $entity_id, true)) {
watchdog('plus1', 'Voting form error: Invalid token.');
return drupal_access_denied();
}
$voted = plus1_get_votes($entity_type, $entity_id, $user->uid, $tag);
$vote_source = $user->uid ? $user->uid : ip_address();
drupal_alter('plus1_source', $vote_source);
// If the voter has not already voted.
if (!$voted) {
$votes[] = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'value_type' => 'points',
'value' => 1,
'tag' => $tag ? $tag : 'plus1_' . $entity_type . '_vote',
'vote_source' => $vote_source,
);
votingapi_set_votes($votes);
$criteria = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'function' => 'sum',
'tag' => $tag ? $tag : 'plus1_' . $entity_type . '_vote',
);
$results = votingapi_select_results($criteria);
$score = $results[0]['value'] ? $results[0]['value'] : 0;
module_invoke_all('plus1_voted', 'vote', $entity_type, $entity_id, $tag, $score, $user);
if (isset($_GET['json'])) {
// Return json to client side, taking into consideration entity type.
drupal_json_output(theme('plus1_json_response__' . $entity_type . '__' . $tag, array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'tag' => $tag,
'score' => $score,
'vote_type' => 'vote',
)));
}
else {
drupal_set_message(t('Thank you for your vote.'));
// go to the page where user pressed vote button
drupal_goto();
}
}
}