You are here

function vud_vote in Vote Up/Down 6.2

Same name and namespace in other branches
  1. 6.3 vud.theme.inc \vud_vote()
  2. 7.2 vud.theme.inc \vud_vote()
  3. 7 vud.theme.inc \vud_vote()

Function for the main voting handler with Ajax support.

1 string reference to 'vud_vote'
vud_menu in ./vud.module
Implementation of hook_menu().

File

./vud.theme.inc, line 351
Theme functions

Code

function vud_vote($type, $content_id, $value, $tag, $widget, $token) {
  if (is_numeric($value) && drupal_valid_token($token, "vote/{$type}/{$content_id}/{$value}/{$tag}/{$widget}", TRUE)) {
    $vote = array();
    $casted_vote_criteria = array(
      'content_type' => $type,
      'content_id' => $content_id,
      'tag' => $tag,
    ) + votingapi_current_user_identifier();
    $casted_vote = votingapi_select_single_vote_value($casted_vote_criteria);

    // Sanity-check the incoming values.
    if ($value > 0) {
      $value = 1;
    }
    elseif ($value < 0) {
      $value = -1;
    }
    else {

      // Invalid value.
      watchdog('vud', 'Invalid vote on @type @content_id, with value @value, tag @tag and token @token', array(
        '@type' => $type,
        '@content_id' => $content_id,
        '@value' => $value,
        '@tag' => $tag,
        '@token' => $token,
      ));
      return;
    }
    $vote['value'] = $value;
    $vote['value_type'] = 'points';
    $tag = $tag ? $tag : variable_get('vud_tag', 'vote');
    $vote['tag'] = $tag;
    $vote['content_id'] = $content_id;
    $vote['content_type'] = $type;
    $votes = array(
      0 => $vote,
    );
    drupal_alter('vud_votes', $votes);

    // Do not allow to vote with the same value.
    if ($casted_vote == $votes[0]['value']) {
      return;
    }
    votingapi_set_votes($votes);
  }
  else {
    watchdog('vud', 'Could not vote on @type @content_id, with value @value, tag @tag and token @token', array(
      '@type' => $type,
      '@content_id' => $content_id,
      '@value' => $value,
      '@tag' => $tag,
      '@token' => $token,
    ));
    drupal_set_message(t("Oops! There was an error in submitting your vote!"), 'warning');
  }
  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    ctools_include('ajax');
    $plugin = vud_widget_get($widget);
    if ($function = ctools_plugin_get_function($plugin, 'ajax render')) {
      $commands = $function($type, $content_id, $value, $tag, $token, $widget);
    }
    else {
      $commands = array();
      if (!empty($plugin['widget template'])) {
        $commands[] = ctools_ajax_command_replace("#widget-{$type}-{$content_id}", theme('vud_widget', $content_id, $type, $tag, $widget));
      }
      if (!empty($plugin['votes template'])) {
        $commands[] = ctools_ajax_command_replace("#votes-{$type}-{$content_id}", theme('vud_votes', $content_id, $type, $tag, $widget));
      }
    }

    // This is the default set of commands. It can be overridden by an individual
    // widget if it wants to.
    ctools_ajax_render($commands);
  }
  else {
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
}