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_permission() {
return array(
'view vote up/down count on nodes' => array(
'title' => t('View Vote Count on Nodes'),
),
'use vote up/down on nodes' => array(
'title' => t('Vote on Nodes'),
'description' => t('Allow users to vote nodes up/down'),
),
'administer vote up/down on nodes' => array(
'title' => t('Administer Voting on Nodes'),
),
'see vote up/down node stats' => array(
'title' => t('View Voting Statistics for Nodes'),
),
);
}
function vud_node_menu() {
$items = array();
$items['admin/config/search/voteupdown/node'] = array(
'title' => 'Nodes',
'description' => 'Vote Up/Down Node settings',
'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_type_get_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_node_view($node, $view_mode, $langcode) {
if (isset($node->in_preview) && $node->in_preview || $view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'rss') {
return;
}
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);
$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;
}
$teaser = 0;
if ($view_mode == 'teaser') {
$teaser = 1;
}
if ($node_type) {
switch ($widget_showmode) {
case VUD_NODE_DISPLAY_TEASER_ONLY:
if ($teaser == 1) {
$node->content['vud_node_widget_display'] = array(
'#markup' => theme('vud_widget', array(
'entity_id' => $node->nid,
'type' => 'node',
'tag' => $tag,
'widget_theme' => $widget,
'readonly' => !$vote_on_teaser || !$can_edit,
'widget_message_code' => $widget_message_code,
)),
'#weight' => -10,
);
}
break;
case VUD_NODE_DISPLAY_FULL_ONLY:
if ($teaser == 0) {
$node->content['vud_node_widget_display'] = array(
'#markup' => theme('vud_widget', array(
'entity_id' => $node->nid,
'type' => 'node',
'tag' => $tag,
'widget_theme' => $widget,
'readonly' => !$can_edit,
'widget_message_code' => $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(
'#markup' => theme('vud_widget', array(
'entity_id' => $node->nid,
'type' => 'node',
'tag' => $tag,
'widget_theme' => $widget,
'readonly' => $readonly,
'widget_message_code' => $widget_message_code,
)),
'#weight' => -10,
);
break;
}
}
}
}
function vud_node_template_suggestions($template_type, $plugin, $entity_id) {
$node = node_load($entity_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(
'entity_type' => 'node',
'entity_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', array(
'account' => $account,
)),
$vote['value'],
array(
'data' => format_date($vote['timestamp'], 'small'),
'class' => 'nowrap',
),
);
}
drupal_set_title(check_plain($node->title));
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
));
$output .= theme('pager', array());
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', array(
'entity_id' => $node->nid,
'type' => $type,
'tag' => $tag,
'widget_theme' => $widget,
)),
'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', array(
'entity_id' => $node->nid,
'type' => $type,
'tag' => $tag,
'widget_theme' => $widget,
)),
'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', array(
'entity_id' => $node->nid,
'type' => $type,
'tag' => $tag,
'widget_theme' => $widget,
)),
'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(
'entity_type' => $type,
'entity_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}/{$widget_theme}");
$links['vud_node_votes_reset_link'] = array(
'title' => t('Reset your vote'),
'href' => "votereset/node/{$node->nid}/{$tag}/{$widget_theme}/{$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;
}
}