View source
<?php
define('VUD_NODE_DISPLAY_NO', 0);
define('VUD_NODE_DISPLAY_TEASER_ONLY', 1);
define('VUD_NODE_DISPLAY_FULL_ONLY', 2);
define('VUD_NODE_DISPLAY_BOTH', 3);
define('VUD_NODE_WIDGET_MESSAGE_TEASER_DENIED', 2);
function vud_node_perm() {
return array(
'view vote up/down count on nodes',
'use vote up/down on nodes',
'administer vote up/down on nodes',
'see vote up/down node stats',
);
}
function vud_node_menu() {
$items = array();
$items['admin/settings/voteupdown/node'] = array(
'title' => 'Nodes',
'description' => 'Vote Up/Down Node settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vud_node_admin_settings',
),
'access arguments' => array(
'administer vote up/down on nodes',
),
'weight' => -10,
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/vud-votes'] = array(
'title' => 'Voting details',
'page callback' => 'vud_node_tracker',
'access callback' => 'vud_node_tab_view_stats',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function vud_node_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'vud_node') . '/views',
);
}
function vud_node_tab_view_stats($node) {
if (!user_access('see vote up/down node stats')) {
return FALSE;
}
if (in_array($node->type, variable_get('vud_node_types', array()), TRUE)) {
return TRUE;
}
}
function vud_node_admin_settings() {
$form['vud_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node types'),
'#description' => t('Select the node types for which you want to activate voting.'),
'#default_value' => variable_get('vud_node_types', array()),
'#options' => node_get_types('names'),
);
$form['vud_node_widget'] = array(
'#title' => t('Widget selection'),
'#description' => t('Select the voting widget theme that will be displayed.'),
'#type' => 'radios',
'#default_value' => variable_get('vud_node_widget', 'updown'),
'#options' => vud_widget_get_names(),
);
$form['vud_node_widget_show'] = array(
'#type' => 'select',
'#title' => t('Widget and votes display'),
'#description' => t('When will the vote widget be displayed?'),
'#default_value' => variable_get('vud_node_widget_show', VUD_NODE_DISPLAY_BOTH),
'#options' => array(
VUD_NODE_DISPLAY_TEASER_ONLY => 'Teaser only',
VUD_NODE_DISPLAY_FULL_ONLY => 'Full display only',
VUD_NODE_DISPLAY_BOTH => 'Both teaser and full',
),
);
$form['vud_node_widget_vote_on_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Voting on teasers'),
'#description' => t('Do you want to allow voting on teasers?'),
'#default_value' => variable_get('vud_node_widget_vote_on_teaser', TRUE),
);
$form['vud_node_votes'] = array(
'#type' => 'select',
'#description' => t('When will the total vote count be displayed?'),
'#default_value' => variable_get('vud_node_votes', VUD_NODE_DISPLAY_BOTH),
'#options' => array(
VUD_NODE_DISPLAY_NO => 'Don\'t display',
VUD_NODE_DISPLAY_TEASER_ONLY => 'Teaser only',
VUD_NODE_DISPLAY_FULL_ONLY => 'Full display only',
VUD_NODE_DISPLAY_BOTH => 'Both teaser and full',
),
);
$form['vud_node_reset'] = array(
'#type' => 'radios',
'#title' => t('Votes reset'),
'#description' => t('Choose if users are allowed to reset their votes on a node.'),
'#default_value' => variable_get('vud_node_reset', 0),
'#options' => array(
0 => 'No',
1 => 'Yes',
),
);
return system_settings_form($form);
}
function vud_node_vud_widget_message_codes_alter(&$widget_message_codes) {
$widget_message_codes[VUD_NODE_WIDGET_MESSAGE_TEASER_DENIED] = t('Please go to full version of this content to vote.');
}
function vud_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
$exclude_modes = array(
NODE_BUILD_PREVIEW,
NODE_BUILD_SEARCH_INDEX,
NODE_BUILD_SEARCH_RESULT,
NODE_BUILD_RSS,
);
if (in_array($node->build_mode, $exclude_modes)) {
break;
}
if (($can_edit = user_access('use vote up/down on nodes')) || user_access('view vote up/down count on nodes')) {
$node_type = in_array($node->type, variable_get('vud_node_types', array()), TRUE);
$widget_showmode = variable_get('vud_node_widget_show', VUD_NODE_DISPLAY_BOTH);
$tag = variable_get('vud_tag', 'vote');
$widget = variable_get('vud_node_widget', 'plain');
$vote_on_teaser = (bool) variable_get('vud_node_widget_vote_on_teaser', TRUE);
$teaser = $a3;
$widget_message_code = VUD_WIDGET_MESSAGE_ERROR;
if (!$can_edit) {
$widget_message_code = VUD_WIDGET_MESSAGE_DENIED;
}
elseif (!$vote_on_teaser) {
$widget_message_code = VUD_NODE_WIDGET_MESSAGE_TEASER_DENIED;
}
if ($node_type) {
switch ($widget_showmode) {
case VUD_NODE_DISPLAY_TEASER_ONLY:
if ($teaser == 1) {
$node->content['vud_node_widget_display'] = array(
'#value' => theme('vud_widget', $node->nid, 'node', $tag, $widget, !$vote_on_teaser || !$can_edit, $widget_message_code),
'#weight' => -10,
);
}
break;
case VUD_NODE_DISPLAY_FULL_ONLY:
if ($teaser == 0) {
$node->content['vud_node_widget_display'] = array(
'#value' => theme('vud_widget', $node->nid, 'node', $tag, $widget, !$can_edit, $widget_message_code),
'#weight' => -10,
);
}
break;
case VUD_NODE_DISPLAY_BOTH:
if ($teaser == 1) {
$readonly = !$vote_on_teaser || !$can_edit;
}
else {
$readonly = !$can_edit;
}
$node->content['vud_node_widget_display'] = array(
'#value' => theme('vud_widget', $node->nid, 'node', $tag, $widget, $readonly, $widget_message_code),
'#weight' => -10,
);
break;
}
}
}
break;
}
}
function vud_node_template_suggestions($template_type, $plugin, $content_id) {
$node = node_load($content_id);
return array(
$template_type,
$template_type . '_node',
$template_type . '_node_' . $plugin['name'],
$template_type . '_node_' . $plugin['name'] . '__' . $node->type,
);
}
function vud_node_tracker() {
if ($node = menu_get_object()) {
$header = array(
array(
'data' => t('User'),
),
array(
'data' => t('Vote'),
),
array(
'data' => t('Date'),
),
);
$tag = variable_get('vud_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($vote['uid']);
$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();
}
}
function vud_node_link($type, $object, $teaser = FALSE) {
$links = array();
switch ($type) {
case 'node':
$node =& $object;
$votes_display_mode = variable_get('vud_node_votes', VUD_NODE_DISPLAY_BOTH);
$node_type = in_array($node->type, variable_get('vud_node_types', array()), TRUE);
$widget_theme = variable_get('vud_node_widget', 'plain');
$tag = variable_get('vud_tag', 'vote');
$view_vud_node_votes_count = user_access('view vote up/down count on nodes') || user_access('use vote up/down on nodes');
switch ($votes_display_mode) {
case VUD_NODE_DISPLAY_NO:
break;
case VUD_NODE_DISPLAY_TEASER_ONLY:
if ($teaser == 1 && $node_type && $view_vud_node_votes_count) {
$links['vud_node_votes_count'] = array(
'title' => theme('vud_votes', $node->nid, $type, $tag, $widget_theme),
'html' => TRUE,
);
}
break;
case VUD_NODE_DISPLAY_FULL_ONLY:
if ($teaser == 0 && $node_type && $view_vud_node_votes_count) {
$links['vud_node_votes_count'] = array(
'title' => theme('vud_votes', $node->nid, $type, $tag, $widget_theme),
'html' => TRUE,
);
}
break;
case VUD_NODE_DISPLAY_BOTH:
if ($node_type && $view_vud_node_votes_count) {
$links['vud_node_votes_count'] = array(
'title' => theme('vud_votes', $node->nid, $type, $tag, $widget_theme),
'html' => TRUE,
);
}
break;
}
if ($node_type && variable_get('vud_node_reset', 0) && user_access('reset vote up/down votes')) {
$tag = variable_get('vud_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)) {
$reset_token = drupal_get_token("votereset/node/{$node->nid}/{$tag}");
$links['vud_node_votes_reset_link'] = array(
'title' => t('Reset your vote'),
'href' => "votereset/node/{$node->nid}/{$tag}/{$reset_token}",
'attributes' => array(
'rel' => 'nofollow',
),
'html' => TRUE,
);
}
}
break;
}
return $links;
}
function vud_node_content_extra_fields($type_name) {
if (in_array($type_name, variable_get('vud_node_types', array()), TRUE)) {
$extra = array();
$extra['vud_node_widget_display'] = array(
'label' => t('Vote Up/Down on nodes'),
'description' => t('Vote Up/Down module widget.'),
'weight' => -10,
);
return $extra;
}
}