You are here

vote_up_down.module in Vote Up/Down 6

Same filename and directory in other branches
  1. 5 vote_up_down.module

File

vote_up_down.module
View source
<?php

/**
 * @file
 * Allows adding an up/down voting widget to selected content types & comments.
 */
if (module_exists('views')) {
  module_load_include('inc', 'vote_up_down', 'vote_up_down.views');
}

// if (module_exists('actions')) {
//   module_load_include('inc', 'vote_up_down', 'vote_up_down_actions');
//  }

/**
 * Implementation of hook_help().
 */
function vote_up_down_help($path, $arg) {
  switch ($path) {
    case 'admin/help#vote_up_down':
      $output .= '<p>' . t('This module can be used to add an up/down voting widget to selected node types and comments.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_perm().
 */
function vote_up_down_perm() {
  return array(
    'view up/down votes',
    'use vote up',
    'use vote down',
    'access vote up/down statistics',
    'administer vote up/down',
  );
}

/**
 * Implementation of hook_access() for the voting page.
 */
function vote_up_down_access() {
  return user_access('use vote up') || user_access('use vote down');
}

/**
 * Implementation of hook_settings().
 */
function vote_up_down_admin_settings() {
  $form['vote_node_types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node types'),
    '#description' => t('Select the node types you want to activate voting on.'),
  );
  $form['vote_node_types']['vote_up_down_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Types'),
    '#default_value' => variable_get('vote_up_down_node_types', array()),
    '#options' => node_get_types('names'),
  );
  $form['vote_widget_settings_node'] = array(
    '#type' => 'fieldset',
    '#title' => t('Vote widget settings for nodes'),
    '#description' => t('For more control over the voting widget placement, it can be inserted directly in the theme. See the included template.php and node-storylink.tpl.php for an example. When this method is used, turn off the widget display here.'),
  );
  $form['vote_widget_settings_node']['vote_up_down_widget_style_node'] = array(
    '#type' => 'radios',
    '#title' => t('Vote widget style'),
    '#default_value' => variable_get('vote_up_down_widget_style_node', 0),
    '#options' => array(
      0 => t('Default +1/-1 vote style'),
      1 => t('Alternative +1 vote style'),
    ),
    '#description' => t('If set to alternative style, it\'s recomended to turn off the link display of vote points.'),
  );
  $form['vote_widget_settings_node']['vote_up_down_widget_node'] = array(
    '#type' => 'select',
    '#title' => t('Vote widget display'),
    '#default_value' => variable_get('vote_up_down_widget_node', 3),
    '#options' => array(
      0 => t('Disabled'),
      1 => t('Teaser view'),
      2 => t('Full-page view'),
      3 => t('Teasers and full-page view'),
    ),
    '#description' => t('When to display the vote widget for nodes?'),
  );
  $form['vote_widget_settings_node']['vote_up_down_link_node'] = array(
    '#type' => 'select',
    '#title' => t('Link display of vote points'),
    '#default_value' => variable_get('vote_up_down_link_node', 3),
    '#options' => array(
      0 => t('Disabled'),
      1 => t('Teaser view'),
      2 => t('Full-page view'),
      3 => t('Teasers and full-page view'),
    ),
    '#description' => t('When to display vote points for nodes?'),
  );
  $form['vote_widget_settings_comment'] = array(
    '#type' => 'fieldset',
    '#title' => t('Vote widget settings for comments'),
    '#description' => t('For more control over the voting widget placement, it can be inserted directly in the theme. See the included node-storylink.tpl.php for an example. When this method is used, turn off the widget display here.'),
  );
  $form['vote_widget_settings_comment']['vote_up_down_widget_style_comment'] = array(
    '#type' => 'radios',
    '#title' => t('Vote widget style'),
    '#default_value' => variable_get('vote_up_down_widget_style_comment', 0),
    '#options' => array(
      0 => t('Default +1/-1 vote style'),
      1 => t('Alternative +1 vote style'),
    ),
    '#description' => t('If set to alternative style, it\'s recomended to turn off the link display of vote points.'),
  );
  $form['vote_widget_settings_comment']['vote_up_down_widget_comment'] = array(
    '#type' => 'radios',
    '#title' => t('Vote widget display'),
    '#default_value' => variable_get('vote_up_down_widget_comment', 1),
    '#options' => array(
      0 => t('Do not display widget'),
      1 => t('Display widget'),
    ),
  );
  $form['vote_widget_settings_comment']['vote_up_down_link_comment'] = array(
    '#type' => 'radios',
    '#title' => t('Link display of vote points'),
    '#default_value' => variable_get('vote_up_down_link_comment', 1),
    '#options' => array(
      0 => t('Do not display link'),
      1 => t('Display link'),
    ),
  );
  return system_settings_form($form);
}
function vote_up_down_admin_advanced_settings() {
  $form['vote_widget_settings_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
  );
  $form['vote_widget_settings_advanced']['vote_up_down_anonymous_vote'] = array(
    '#type' => 'radios',
    '#title' => t('Allow anonymous voting'),
    '#default_value' => variable_get('vote_up_down_anonymous_vote', 0),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#description' => t('If yes, anonymous users may cast votes, restricted to one vote per day per IP address.<br />This will not work if "use vote up" & "use vote down" & "view up/down votes" permissions are not given to anonymous users.'),
  );
  $form['vote_widget_settings_advanced']['vote_up_down_reset_vote'] = array(
    '#type' => 'radios',
    '#title' => t('Allow users to reset their votes'),
    '#default_value' => variable_get('vote_up_down_reset_vote', 0),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#description' => t('If yes, a link will be displayed on nodes and comments that will allow users to reset their vote.'),
  );
  $form['vote_widget_settings_advanced']['vote_up_down_tag'] = array(
    '#type' => 'textfield',
    '#title' => t('Voting API tag'),
    '#default_value' => variable_get('vote_up_down_tag', 'vote'),
    '#description' => t('Since Vote Up/Down uses Voting API, all votes will be tagged with this term. (default: vote)<br />This tag is useful is you have deployed various modules that use Voting API. It should always be a unique value. Usually, there is NO need to change this.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_menu().
 */
function vote_up_down_menu() {
  $items = array();
  $items['admin/settings/voteupdown'] = array(
    'title' => 'Vote Up/Down configuration',
    'description' => 'Control the functioning of Vote Up/Down.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'vote_up_down_admin_settings',
    ),
    'access arguments' => array(
      'administer vote up/down',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/voteupdown/general'] = array(
    'title' => 'General',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/settings/voteupdown/advanced'] = array(
    'title' => 'Advanced',
    'description' => 'Advanced configuration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'vote_up_down_admin_advanced_settings',
    ),
    'access arguments' => array(
      'administer vote up/down',
    ),
    'type' => MENU_LOCAL_TASK,
  );

  // vote_up_down/$type/$cid/$value/$tag/$ajax/$alt
  $items['vote_up_down'] = array(
    'title' => 'Vote',
    'page callback' => 'vote_up_down_vote',
    'access callback' => 'vote_up_down_access',
    'type' => MENU_CALLBACK,
  );
  $items['voteupdown'] = array(
    'title' => 'Voting statistics',
    'page callback' => 'vote_up_down_page',
    'access arguments' => array(
      'access vote up/down statistics',
    ),
  );
  $items['voteupdown/usersvotes'] = array(
    'title' => 'Users by votes',
    'page callback' => 'vote_up_down_page',
    'access arguments' => array(
      'access vote up/down statistics',
    ),
  );
  $items['node/%node/votes'] = array(
    'title' => 'Voting details',
    'page callback' => 'vote_up_down_tracker',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'vote_up_down_node_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['user/%user/votesupdown'] = array(
    'title' => 'Your votes',
    'page callback' => 'vote_up_down_user_votes',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'access vote up/down statistics',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function vote_up_down_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
      $widget = vote_up_down_widget_form($node);
      if (!empty($widget)) {
        $node->content['vote_up_down'] = array(
          '#value' => $widget,
          '#weight' => -10,
        );
      }
      break;
  }
}

/**
 * Node template preprocess. Adding vote_up_down widget to variable list.
 */
function vote_up_down_preprocess_node(&$variables) {
  if (user_access('view up/down votes')) {
    $node = $variables['node'];
    $node_type = in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
    if ($node->nid && $node_type) {
      $style = variable_get('vote_up_down_widget_style_node', 0) == 1 ? '_alt' : '';
      $variables['vote_up_down'] = theme("vote_up_down_widget{$style}", $node->nid, 'node');
    }
  }
}

/**
 * Implementation of hook_comment().
 */
function vote_up_down_comment(&$comment, $op) {
  switch ($op) {
    case 'view':
      if (variable_get('vote_up_down_widget_comment', 1)) {
        if (user_access('view up/down votes')) {
          $style = variable_get('vote_up_down_widget_style_comment', 0) == 1 ? '_alt' : '';
          $comment->comment = theme("vote_up_down_widget{$style}", $comment->cid, 'comment') . $comment->comment;
        }
      }
      break;
  }
}

/**
 * Comment template preprocess. Adding vote_up_down widget to variable list.
 */
function vote_up_down_preprocess_comment(&$variables) {
  if (user_access('view up/down votes')) {
    $comment = $variables['comment'];
    if ($comment->cid) {
      $style = variable_get('vote_up_down_widget_style_comment', 0) == 1 ? '_alt' : '';
      $variables['vote_up_down'] = theme("vote_up_down_widget{$style}", $comment->cid, 'comment');
    }
  }
}

/**
 * Implementation of hook_link().
 */
function vote_up_down_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  switch ($type) {
    case 'node':
      $node_type = in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
      $style = variable_get('vote_up_down_widget_style_node', 0) == 1 ? '_alt' : '';
      if ($node_type && user_access('view up/down votes')) {
        if (variable_get('vote_up_down_reset_vote', 0) && (user_access('use vote up') || user_access('use vote down'))) {
          $tag = variable_get('vote_up_down_tag', 'vote');
          $criteria = array(
            'content_type' => $type,
            'content_id' => $node->nid,
            'tag' => $tag,
          );
          $criteria += votingapi_current_user_identifier();
          $user_vote = votingapi_select_single_vote_value($criteria);
          if (!is_null($user_vote)) {
            $token = drupal_get_token("vote_up_down/{$type}/{$node->nid}/0");
            $links['vote_up_down_reset'] = array(
              'title' => t('Reset vote'),
              'href' => "vote_up_down/{$type}/{$node->nid}/0",
              'attributes' => array(
                'title' => t('Reset your vote.'),
              ),
              'query' => drupal_get_destination() . '&token=' . $token,
            );
          }
        }
        if ($teaser && variable_get('vote_up_down_link_node', 0) && variable_get('vote_up_down_link_node', 0) != 2 && $style != "_alt") {
          $links['vote_up_down_points'] = array(
            'title' => theme('vote_up_down_points', $node->nid, $type),
            'html' => TRUE,
          );
        }
        else {
          if (!$teaser && variable_get('vote_up_down_link_node', 0) > 1 && $style != "_alt") {
            $links['vote_up_down_points'] = array(
              'title' => theme('vote_up_down_points', $node->nid, $type),
              'html' => TRUE,
            );
          }
        }
      }
      break;
    case 'comment':
      if (variable_get('vote_up_down_reset_vote', 0) && (user_access('use vote up') || user_access('use vote down')) && (variable_get('vote_up_down_widget_comment', 0) || variable_get('vote_up_down_link_comment', 0))) {
        $tag = variable_get('vote_up_down_tag', 'vote');
        $criteria = array(
          'content_type' => $type,
          'content_id' => $node->cid,
          'tag' => $tag,
        );
        $criteria += votingapi_current_user_identifier();
        $user_vote = votingapi_select_single_vote_value($criteria);
        if (!is_null($user_vote)) {
          $token = drupal_get_token("vote_up_down/{$type}/{$node->cid}/0");
          $links['vote_up_down_reset_c'] = array(
            'title' => t('Reset vote'),
            'href' => "vote_up_down/{$type}/{$node->cid}/0",
            'attributes' => array(
              'title' => t('Reset your vote.'),
            ),
            'query' => drupal_get_destination() . '&token=' . $token,
          );
        }
      }
      if (variable_get('vote_up_down_link_comment', 0) && user_access('view up/down votes')) {
        $links['vote_up_down_points_c'] = array(
          'title' => theme('vote_up_down_points', $node->cid, $type, NULL),
          'html' => TRUE,
        );
      }
      break;
  }
  return $links;
}

/**
 * Implementation of hook_block().
 */
function vote_up_down_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Who voted on this?');
    return $blocks;
  }
  if ($op == 'view') {
    $block = array();
    switch ($delta) {
      case 0:
        if (arg(0) == 'node' & is_numeric(arg(1))) {

          // Check that this node type is enabled for vote_up_down, and if not don't display the block.
          $node = node_load(arg(1));
          if (!vote_up_down_node_access($node)) {
            return;
          }
          $block['subject'] = t('Who voted on this?');
          $nid = arg(1);
          $tag = variable_get('vote_up_down_tag', 'vote');
          $votes = array(
            'content_id' => $nid,
            'tag' => $tag,
          );

          // TODO: support multiple 'tag' in a nested table?
          $voteresults = votingapi_select_votes($votes);
          $block['content'] = theme('vote_up_down_whovoted_block', $nid, $voteresults);
        }
        break;
    }
    return $block;
  }
}

/**
 * Implementation of hook_userpoints().
 */
function vote_up_down_userpoints($op, $params = array()) {
  switch ($op) {
    case 'setting':
      $form['vote_updown_points'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('Vote up/down points'),
      );
      $form['vote_updown_points']['userpoints_vote_up_down'] = array(
        '#type' => 'textfield',
        '#title' => t('Points for voting on a node (requires the Vote Up/Down module)'),
        '#default_value' => variable_get('userpoints_vote_up_down', 0),
        '#size' => 5,
        '#maxlength' => 5,
      );
      $form['vote_updown_points']['userpoints_actions'] = array(
        '#type' => 'textfield',
        '#title' => t('Points for actions (requires the Actions module)'),
        '#default_value' => variable_get('userpoints_actions', 0),
        '#size' => 5,
        '#maxlength' => 5,
      );
      return $form;
      break;
  }
}

/**
 * Menu callback; Handles voteupdown and subpages.
 */
function vote_up_down_page() {
  $op = arg(1);
  switch ($op) {
    case 'usersvotes':
      $output = vote_up_down_users_votes();
      break;
    default:
      $items = array();
      $items[] = l(t('Users by votes'), 'voteupdown/usersvotes');
      $output = theme('item_list', $items);
  }
  return $output;
}

/**
 * Users votes page for the vote_up_down_page data.
 */
function vote_up_down_users_votes() {
  $sql = "SELECT COUNT(v.value) AS number_votes, SUM(v.value) AS total_votes, v.uid, v.tag, u.uid, u.name FROM {votingapi_vote} v INNER JOIN {users} u on v.uid = u.uid WHERE v.tag = '%s' GROUP BY u.name";
  $sql_cnt = "SELECT COUNT(DISTINCT(uid)) FROM {votingapi_vote}";
  $header = array(
    array(
      'data' => t('User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Votes'),
      'field' => 'number_votes',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Vote sum'),
      'field' => 'total_votes',
    ),
  );
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 30, 0, $sql_cnt, variable_get('vote_up_down_tag', 'vote'));
  $rows = array();
  while ($vote = db_fetch_object($result)) {
    $rows[] = array(
      theme('username', $vote),
      $vote->number_votes,
      $vote->total_votes,
    );
  }
  drupal_set_title(t('Users by votes'));
  $output = theme('table', $header, $rows);
  $output .= theme('pager', NULL, 30, 0);
  return $output;
}

/**
 * Menu callback; display all votes for a node.
 */
function vote_up_down_tracker() {
  if ($node = menu_get_object()) {
    $header = array(
      array(
        'data' => t('User'),
      ),
      array(
        'data' => t('Vote'),
      ),
      array(
        'data' => t('Date'),
      ),
    );
    $tag = variable_get('vote_up_down_tag', 'vote');
    $criteria = array(
      'content_type' => 'node',
      'content_id' => $node->nid,
      'tag' => $tag,
    );
    $votes = votingapi_select_votes($criteria);
    $rows = array();
    foreach ($votes as $vote) {
      $account = user_load(array(
        'uid' => $vote['uid'],
        'status' => 1,
      ));
      $rows[] = array(
        theme('username', $account),
        $vote['value'],
        array(
          'data' => format_date($vote['timestamp'], 'small'),
          'class' => 'nowrap',
        ),
      );
    }
    drupal_set_title(check_plain($node->title));
    $output = theme('table', $header, $rows);
    $output .= theme('pager', NULL, 30);
    return $output;
  }
  else {
    drupal_not_found();
  }
}

/**
 * Menu callback; display all votes for a user.
 */
function vote_up_down_user_votes() {
  if ($account = user_load(array(
    'uid' => arg(1),
    'status' => 1,
  ))) {
    if ($account->status || user_access('administer users')) {
      $header = array(
        array(
          'data' => t('Node'),
        ),
        array(
          'data' => t('Vote'),
        ),
        array(
          'data' => t('Date'),
        ),
      );
      $sql = db_rewrite_sql("SELECT n.nid, n.title, v.value, v.timestamp FROM {node} n LEFT JOIN {votingapi_vote} v ON n.nid = v.content_id WHERE v.uid = %d AND v.tag = '%s' AND v.content_type = 'node' AND n.status = 1 ORDER BY v.timestamp DESC");
      $result = pager_query($sql, 25, 0, NULL, $account->uid, variable_get('vote_up_down_tag', 'vote'));
      $rows = array();
      while ($node = db_fetch_object($result)) {
        $rows[] = array(
          l($node->title, 'node/' . $node->nid),
          $node->value,
          t('!time ago', array(
            '!time' => format_interval(time() - $node->timestamp),
          )),
        );
      }
      drupal_set_title(check_plain($account->name));
      $output = theme('table', $header, $rows);
      $output .= theme('pager', NULL, 25);
      return $output;
    }
    else {
      drupal_access_denied();
    }
  }
  else {
    drupal_not_found();
  }
}

/**
 * Function for the main voting handler with Ajax support.
 */
function vote_up_down_vote($type, $cid, $value, $tag = NULL, $ajax = FALSE, $alt = FALSE) {
  if (is_numeric($cid) && is_numeric($value) && isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], "vote_up_down/{$type}/{$cid}/{$value}")) {
    $vote = array();

    // Sanity-check the incoming values.
    if ($value > 0) {
      $vote['value'] = 1;
    }
    else {
      if ($value < 0) {
        $vote['value'] = -1;
      }
      else {
        $vote['value'] = 0;
      }
    }
    $vote['value_type'] = 'points';
    $tag = $tag ? $tag : variable_get('vote_up_down_tag', 'vote');
    $vote['tag'] = $tag;

    // Userpoints integration.
    if (module_exists('userpoints')) {
      global $user;
      if ($type == 'node') {
        $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.uid FROM {node} n WHERE n.nid = %d'), $cid));
      }
      else {
        $node = db_fetch_object(db_query(db_rewrite_sql('SELECT c.uid FROM {comments} c WHERE c.cid = %d', 'c', 'cid'), $cid));
      }
      $criteria = array(
        'content_type' => $type,
        'content_id' => $cid,
        'uid' => $user->uid,
        'tag' => $tag,
      );
      $user_vote = votingapi_select_single_vote_value($criteria);
      if ($user->uid && !$user_vote && $node->uid != $user->uid && $vote['value'] != 0) {
        userpoints_userpointsapi('points', variable_get('userpoints_vote_up_down', 0), $user->uid, 'Vote Up/Down points');
      }
    }

    // Do the voting via Voting API.
    if ($vote['value'] == 0) {
      $criteria = array(
        'content_type' => $type,
        'content_id' => $cid,
        'tag' => $tag,
      );
      $criteria += votingapi_current_user_identifier();
      votingapi_delete_votes(votingapi_select_votes($criteria));
      votingapi_recalculate_results($type, $cid);
    }
    else {
      $vote['content_id'] = $cid;
      $vote['content_type'] = $type;
      $votes = array(
        0 => $vote,
      );
      votingapi_set_votes($votes);
    }
    if ($ajax) {
      if ($alt == TRUE) {
        print theme('vote_up_down_points_alt', $cid, $type, NULL, 'alt');
      }
      else {
        print theme('vote_up_down_points', $cid, $type, NULL, '');
      }
      exit;
    }
    else {
      drupal_goto(drupal_get_destination());
    }
  }
}

/**
 * Themeing functions for widget and points display.
 * Each of the functions below has a separate description of its own.
 */

/**
 * Preprocessing function for +1/-1 style.
 */
function template_preprocess_vote_up_down_widget(&$variables) {
  global $user;
  $cid = $variables['cid'];
  $type = $variables['type'];
  $tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vote_up_down_tag', 'vote');
  $variables['link1'] = '';
  $variables['link2'] = '';
  drupal_add_css(drupal_get_path('module', 'vote_up_down') . '/vote_up_down.css');
  drupal_add_js(drupal_get_path('module', 'vote_up_down') . '/ajax_vote_up_down.js');
  $variables['points'] = theme('vote_up_down_points', $cid, $type, $tag);
  if ((user_access('use vote up') || user_access('use vote down')) && $user->uid || variable_get('vote_up_down_anonymous_vote', 0)) {
    $criteria = array(
      'content_type' => $type,
      'content_id' => $cid,
      'tag' => $tag,
    );
    $criteria += votingapi_current_user_identifier();
    $user_vote = votingapi_select_single_vote_value($criteria);
    if ($user_vote > 0) {
      $variables['class1'] = 'vote-up-act';
      $variables['class2'] = 'vote-down-inact';
    }
    else {
      if ($user_vote < 0) {
        $variables['class1'] = 'vote-up-inact';
        $variables['class2'] = 'vote-down-act';
      }
      else {
        $variables['class1'] = 'vote-up-inact';
        $variables['class2'] = 'vote-down-inact';
      }
    }

    // Voting up cases
    if (user_access('use vote up')) {
      $token = drupal_get_token("vote_up_down/{$type}/{$cid}/1");
      $variables['title1'] = url("vote_up_down/{$type}/{$cid}/1/{$tag}/1", array(
        'query' => 'token=' . $token,
      ));
      $variables['link1'] = l('', "vote_up_down/{$type}/{$cid}/1/{$tag}", array(
        'attributes' => array(
          'class' => $variables['class1'],
          'title' => t('Vote up'),
        ),
        'query' => drupal_get_destination() . '&token=' . $token,
      ));
    }
    else {
      $variables['class1'] = 'up-inact';
      $variables['title1'] = t('You have no privileges to vote up.');
    }

    // Voting down cases
    if (user_access('use vote down')) {
      $token = drupal_get_token("vote_up_down/{$type}/{$cid}/-1");
      $variables['title2'] = url("vote_up_down/{$type}/{$cid}/-1/{$tag}/-1", array(
        'query' => 'token=' . $token,
      ));
      $variables['link2'] = l('', "vote_up_down/{$type}/{$cid}/-1/{$tag}", array(
        'attributes' => array(
          'class' => $variables['class2'],
          'title' => t('Vote down'),
        ),
        'query' => drupal_get_destination() . '&token=' . $token,
      ));
    }
    else {
      $variables['class2'] = 'down-inact';
      $variables['title2'] = t('You have no privileges to vote down.');
    }
  }
  else {
    $variables['class1'] = 'up-inact';
    $variables['class2'] = 'down-inact';
    $variables['title1'] = t('You must login to vote!');
    $variables['title2'] = t('You must login to vote!');
  }
}

/**
 * Preprocessing function for the widget in the alternate +1 style.
 */
function template_preprocess_vote_up_down_widget_alt(&$variables) {
  global $user;
  $cid = $variables['cid'];
  $type = $variables['type'];
  $tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vote_up_down_tag', 'vote');
  drupal_add_css(drupal_get_path('module', 'vote_up_down') . '/vote_up_down.css');
  drupal_add_js(drupal_get_path('module', 'vote_up_down') . '/ajax_vote_up_down.js');
  $variables['points'] = theme('vote_up_down_points_alt', $cid, $type, $tag, 'alt');
  if ((user_access('use vote up') || user_access('use vote down')) && ($user->uid || variable_get('vote_up_down_anonymous_vote', 0))) {
    $criteria = array(
      'content_type' => $type,
      'content_id' => $cid,
      'tag' => $tag,
    );
    $criteria += votingapi_current_user_identifier();
    $user_vote = votingapi_select_single_vote_value($criteria);
    if ($user_vote > 0) {
      $variables['class'] = 'vote-up-act';
    }
    else {
      $variables['class'] = 'vote-up-inact';
    }
    $token = drupal_get_token("vote_up_down/{$type}/{$cid}/1");
    $variables['title'] = url("vote_up_down/{$type}/{$cid}/1/{$tag}/1/alt", array(
      'query' => 'token=' . $token,
    ));
    $variables['link'] = l('', "vote_up_down/{$type}/{$cid}/1/{$tag}", array(
      'attributes' => array(
        'class' => $variables['class'],
        'title' => t('Vote up'),
      ),
      'query' => drupal_get_destination() . '&token=' . $token,
    ));
  }
  else {
    $variables['class'] = 'up-inact';
    $variables['title'] = t('You must login to vote!');
  }
}

/**
 * Preprocess function for the points in the normal display style.
 */
function template_preprocess_vote_up_down_points(&$variables) {
  $tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vote_up_down_tag', 'vote');
  $criteria = array(
    'content_type' => $variables['type'],
    'content_id' => $variables['cid'],
    'value_type' => 'points',
    'tag' => $tag,
    'function' => 'sum',
  );
  $vote_result = (int) votingapi_select_single_result_value($criteria);
  if ($vote_result > 0) {
    $variables['class'] = 'positive';
  }
  else {
    if ($vote_result < 0) {
      $variables['class'] = 'negative';
    }
    else {
      $variables['class'] = 'neutral';
    }
  }
  $variables['points_labelled'] = format_plural($vote_result, '1 point', '@count points');
}

/**
 * Preprocess function for the points in the alt display style.
 */
function template_preprocess_vote_up_down_points_alt(&$variables) {
  $tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vote_up_down_tag', 'vote');
  $criteria = array(
    'content_type' => $variables['type'],
    'content_id' => $variables['cid'],
    'value_type' => 'points',
    'tag' => $tag,
    'function' => 'sum',
  );
  $vote_result = (int) votingapi_select_single_result_value($criteria);
  if ($vote_result > 0) {
    $variables['class'] = 'positive';
    $variables['points'] = '+' . $vote_result;
  }
  else {
    $variables['points'] = $vote_result;
    if ($vote_result < 0) {
      $variables['class'] = 'negative';
    }
    else {
      $variables['class'] = 'neutral';
    }
  }
  $variables['label'] = format_plural($vote_result, 'point', 'points');
}

/**
 * Theme function for the 'Who Voted' block.
 */
function theme_vote_up_down_whovoted_block($nid, $voteresults) {
  if ($voteresults) {
    $items = array();
    foreach ($voteresults as $vote) {
      $account = user_load(array(
        'uid' => $vote['uid'],
        'status' => 1,
      ));
      $items[] = theme('username', $account) . ' (' . $vote['value'] . ')';
    }
    $output = theme('item_list', $items);
  }
  else {
    $output = t('No one\'s voted yet! Why don\'t you?');
  }
  return $output;
}

/**
 * Implementation of hook_theme().
 */
function vote_up_down_theme() {
  return array(
    'vote_up_down_widget' => array(
      'file' => 'vote_up_down.module',
      'arguments' => array(
        'cid' => NULL,
        'type' => NULL,
      ),
      'template' => 'theme/vote_up_down_widget',
    ),
    'vote_up_down_widget_alt' => array(
      'file' => 'vote_up_down.module',
      'arguments' => array(
        'cid' => NULL,
        'type' => NULL,
      ),
      'template' => 'theme/vote_up_down_widget_alt',
    ),
    'vote_up_down_points' => array(
      'file' => 'vote_up_down.module',
      'arguments' => array(
        'cid' => NULL,
        'type' => NULL,
        'tag' => NULL,
      ),
      'template' => 'theme/vote_up_down_points',
    ),
    'vote_up_down_points_alt' => array(
      'file' => 'vote_up_down.module',
      'arguments' => array(
        'cid' => NULL,
        'type' => NULL,
        'tag' => NULL,
      ),
      'template' => 'theme/vote_up_down_points_alt',
    ),
    'vote_up_down_whovoted_block' => array(
      'file' => 'vote_up_down.module',
      'arguments' => array(
        'nid' => NULL,
        'voteresults' => NULL,
      ),
    ),
  );
}

/**
 * Check whether the current user has access to the given permission for the
 * given node. This requires both that they have access the required permission,
 * and that the node type is enabled for vote up/down.
 * @param $node
 *   A node object
 * @param $perm
 *   (Optional) the name of a permission to pass to user_access()
 * @return boolean TRUE if the user has access to the node vote up/down statistics.
 */
function vote_up_down_node_access($node, $perm = 'access vote up/down statistics') {
  if (user_access($perm)) {
    return in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
  }
  return FALSE;
}

/**
 * Implementation of the widget function.
 * Takes a node as input and output the widget to vote upon it.
 *
 * @param $node
 *   A node object.
 * @return
 */
function vote_up_down_widget_form($node) {
  $teaser = $node->teaser;
  $return = '';
  if (user_access('view up/down votes')) {
    $node_type = in_array($node->type, variable_get('vote_up_down_node_types', array()), TRUE);
    if ($node_type) {
      $style = variable_get('vote_up_down_widget_style_node', 0) == 1 ? '_alt' : '';
      if ($teaser && variable_get('vote_up_down_widget_node', 0) && variable_get('vote_up_down_widget_node', 0) != 2) {
        $return = theme("vote_up_down_widget{$style}", $node->nid, 'node');
      }
      else {
        if (!$teaser && variable_get('vote_up_down_widget_node', 0) > 1) {
          $return = theme("vote_up_down_widget{$style}", $node->nid, 'node');
        }
      }
    }
  }
  return $return;
}

/**
 * Function to enable the integration with views API 2
 * @return
 */
function vote_up_down_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'vote_up_down') . '/views',
  );
}

Functions

Namesort descending Description
template_preprocess_vote_up_down_points Preprocess function for the points in the normal display style.
template_preprocess_vote_up_down_points_alt Preprocess function for the points in the alt display style.
template_preprocess_vote_up_down_widget Preprocessing function for +1/-1 style.
template_preprocess_vote_up_down_widget_alt Preprocessing function for the widget in the alternate +1 style.
theme_vote_up_down_whovoted_block Theme function for the 'Who Voted' block.
vote_up_down_access Implementation of hook_access() for the voting page.
vote_up_down_admin_advanced_settings
vote_up_down_admin_settings Implementation of hook_settings().
vote_up_down_block Implementation of hook_block().
vote_up_down_comment Implementation of hook_comment().
vote_up_down_help Implementation of hook_help().
vote_up_down_link Implementation of hook_link().
vote_up_down_menu Implementation of hook_menu().
vote_up_down_nodeapi Implementation of hook_nodeapi().
vote_up_down_node_access Check whether the current user has access to the given permission for the given node. This requires both that they have access the required permission, and that the node type is enabled for vote up/down.
vote_up_down_page Menu callback; Handles voteupdown and subpages.
vote_up_down_perm Implementation of hook_perm().
vote_up_down_preprocess_comment Comment template preprocess. Adding vote_up_down widget to variable list.
vote_up_down_preprocess_node Node template preprocess. Adding vote_up_down widget to variable list.
vote_up_down_theme Implementation of hook_theme().
vote_up_down_tracker Menu callback; display all votes for a node.
vote_up_down_userpoints Implementation of hook_userpoints().
vote_up_down_users_votes Users votes page for the vote_up_down_page data.
vote_up_down_user_votes Menu callback; display all votes for a user.
vote_up_down_views_api Function to enable the integration with views API 2
vote_up_down_vote Function for the main voting handler with Ajax support.
vote_up_down_widget_form Implementation of the widget function. Takes a node as input and output the widget to vote upon it.