View source
<?php
module_load_include('inc', 'vud', 'vud.theme');
function vud_init() {
$now = time();
if (!empty($_SESSION['vud_timestamp'])) {
unset($_SESSION['vud_timestamp']);
}
}
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;
}
}
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);
}
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;
}
function vud_perm() {
return array(
'use vote up/down',
'administer vote up/down',
'access vote up/down statistics',
'reset vote up/down votes',
);
}
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'));
}
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;
}
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'));
}
}
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];
}
}
function vud_ctools_plugin_directory($module, $plugin) {
if ($module == 'vud') {
return $plugin;
}
}
function vud_votingapi_results_alter(&$cache, $content_type, $content_id) {
$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;
}
$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;
}
}
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',
);
}