You are here

vud.module in Vote Up/Down 6.2

Same filename and directory in other branches
  1. 8 vud.module
  2. 6.3 vud.module
  3. 7.2 vud.module
  4. 7 vud.module

File

vud.module
View source
<?php

/**
 * @file
 * Implements the core voting module on top of Voting API.
 */
module_load_include('inc', 'vud', 'vud.theme');

// Include the theme.inc file.

/**
 * Implementation of hook_init().
 */
function vud_init() {

  // Remove anonymous sessions which were previously set to prevent pressflow
  // page caching. This will eventually clean up those sessions over time.
  // @todo: remove in a future release when it can be reasonably expected that
  // no more sessions with this variable exist in the wild.
  $now = time();
  if (!empty($_SESSION['vud_timestamp'])) {
    unset($_SESSION['vud_timestamp']);
  }
}

/**
 * Implementation of hook_help().
 */
function vud_help($path, $arg) {
  switch ($path) {
    case 'admin/help#vud':
      $output = '<p>' . t('Provides a configurable up/down voting widget for other modules to use.') . '</p>';
      return $output;
  }
}

/**
 * Advanced menu settings callback.
 */
function vud_admin_advanced_settings() {
  $form['vud_tag'] = array(
    '#type' => 'textfield',
    '#title' => t('Voting API tag'),
    '#default_value' => variable_get('vud_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.'),
  );
  $form['vud_message_on_deny'] = array(
    '#type' => 'checkbox',
    '#title' => t('Message on denied permission'),
    '#default_value' => variable_get('vud_message_on_deny', FALSE),
    '#description' => t('When this flag is active, a modal window will be shown to the end user instead of avoid showing the voting links'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_menu().
 */
function vud_menu() {
  $items = array();
  $items['admin/settings/voteupdown'] = array(
    'title' => 'Vote Up/Down',
    'description' => 'Control the functioning of Vote Up/Down.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'vud_admin_advanced_settings',
    ),
    'access arguments' => array(
      'administer vote up/down',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/voteupdown/advanced'] = array(
    'title' => 'General',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['vote/%/%/%/%/%/%'] = array(
    'title' => 'Vote',
    'page callback' => 'vud_vote',
    'page arguments' => array(
      1,
      2,
      3,
      4,
      5,
      6,
    ),
    'access arguments' => array(
      'use vote up/down',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'vud.theme.inc',
  );
  $items['votereset/%/%/%/%'] = array(
    'title' => 'Reset vote',
    'page callback' => 'vud_reset',
    'page arguments' => array(
      1,
      2,
      3,
      4,
    ),
    'access arguments' => array(
      'reset vote up/down votes',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['user/%user/vote_up_down/votes'] = array(
    'title' => 'Votes',
    'page callback' => 'vud_user_votes',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'vud_can_see_votes',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['vud/%ctools_js/denied/%'] = array(
    'title' => 'Vote denied',
    'page callback' => 'vud_denied_vote',
    'page arguments' => array(
      1,
      3,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

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

/**
 * Menu access callback for 'user/%user/vote_up_down/votes'.
 *
 * @see user_view_access()
 */
function vud_can_see_votes($account) {
  return $account && $account->uid && (user_access('administer users') || $account->access && $account->status == 1 && user_access('access vote up/down statistics'));
}

/**
 * Menu callback; display all votes for a user.
 */
function vud_user_votes($account) {
  $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\n    ON n.nid = v.content_id\n    WHERE v.uid = %d AND v.tag = '%s' AND v.content_type = 'node' AND n.status = 1\n    ORDER BY v.timestamp DESC");
  $result = pager_query($sql, 25, 0, NULL, $account->uid, variable_get('vud_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;
}

/**
 * Callback to reset votes on an object.
 */
function vud_reset($type, $content_id, $tag, $token) {
  if (drupal_valid_token($token, "votereset/{$type}/{$content_id}/{$tag}", TRUE)) {
    $uid = votingapi_current_user_identifier();
    $criteria = array(
      'content_type' => $type,
      'content_id' => $content_id,
      'tag' => $tag,
    );
    $fullc = $criteria + $uid;
    votingapi_delete_votes(votingapi_select_votes($fullc));
    votingapi_recalculate_results($type, $content_id);
    drupal_goto($_SERVER['HTTP_REFERER']);
  }
  else {
    watchdog('vud', 'Could not reset votes 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 resetting your vote!", 'warning'));
  }
}

/**
 * Menu callback; show widget message.
 */
function vud_denied_vote($js = FALSE, $code = VUD_WIDGET_MESSAGE_ERROR) {
  $widget_message_codes = array(
    VUD_WIDGET_MESSAGE_ERROR => t('Sorry, there was problem on the vote.'),
    VUD_WIDGET_MESSAGE_DENIED => t('You are not allowed to vote.'),
  );
  drupal_alter('vud_widget_message_codes', $widget_message_codes);
  if ($js) {
    ctools_include('ajax');
    ctools_include('modal');
    ctools_modal_render('', $widget_message_codes[$code]);
  }
  else {
    return $widget_message_codes[$code];
  }
}

/**
 * Implementation of hook_ctools_plugin_dierctory() to let the system know
 * we implement widget plugins.
 */
function vud_ctools_plugin_directory($module, $plugin) {
  if ($module == 'vud') {
    return $plugin;
  }
}

/**
 * Implementation of votingapi hook_votingapi_results_alter().
 *
 * Add positive/negative aggregations for VotingAPI cache points.
 */
function vud_votingapi_results_alter(&$cache, $content_type, $content_id) {

  // positive points
  $sql = "SELECT SUM(v.value) as value_positives, v.tag ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value > 0 ";
  $sql .= "GROUP BY v.value_type, v.tag";
  $result = db_query($sql, $content_type, $content_id);
  while ($record = db_fetch_object($result)) {
    $cache[$record->tag]['points']['positives'] = $record->value_positives;
  }

  // negative points
  $sql = "SELECT SUM(v.value) as value_negatives, v.tag ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.value < 0 ";
  $sql .= "GROUP BY v.value_type, v.tag";
  $result = db_query($sql, $content_type, $content_id);
  while ($record = db_fetch_object($result)) {
    $cache[$record->tag]['points']['negatives'] = $record->value_negatives;
  }
}

/**
 * Implementation of hook_votingapi_metadata_alter().
 */
function vud_votingapi_metadata_alter(&$data) {
  $data['functions']['positives'] = array(
    'name' => t('Positives'),
    'description' => t('The sum of all positive votes for a content.'),
    'module' => 'vud',
  );
  $data['functions']['negatives'] = array(
    'name' => t('Negatives'),
    'description' => t('The sum of all negative votes for a content.'),
    'module' => 'vud',
  );
}

Functions

Namesort descending Description
vud_admin_advanced_settings Advanced menu settings callback.
vud_can_see_votes Menu access callback for 'user/%user/vote_up_down/votes'.
vud_ctools_plugin_directory Implementation of hook_ctools_plugin_dierctory() to let the system know we implement widget plugins.
vud_denied_vote Menu callback; show widget message.
vud_help Implementation of hook_help().
vud_init Implementation of hook_init().
vud_menu Implementation of hook_menu().
vud_perm Implementation of hook_perm().
vud_reset Callback to reset votes on an object.
vud_user_votes Menu callback; display all votes for a user.
vud_votingapi_metadata_alter Implementation of hook_votingapi_metadata_alter().
vud_votingapi_results_alter Implementation of votingapi hook_votingapi_results_alter().