You are here

vud_node.module in Vote Up/Down 6.2

Same filename and directory in other branches
  1. 6.3 vud_node/vud_node.module
  2. 7 vud_node/vud_node.module

Adds a voting widget to nodes.

File

vud_node/vud_node.module
View source
<?php

/**
 * @file
 * Adds a voting widget to nodes.
 */
define('VUD_NODE_DISPLAY_NO', 0);

// used only by vote count
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);

/**
 * Implementation of hook_perm().
 */
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',
  );
}

/**
 * Implementation of hook_menu().
 */
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;
}

/**
 * Implementation of hook_views_api().
 */
function vud_node_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'vud_node') . '/views',
  );
}

/**
 * Access Callback from hook_menu (vud_node_menu).
 */
function vud_node_tab_view_stats($node) {

  // Immediately check if the user has any view rights
  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;
  }
}

/**
 * Advanced menu settings callback.
 */
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);
}

/**
 * Implementation of vud hook_vud_widget_message_codes_alter().
 */
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.');
}

/**
 * Implementation of hook_nodeapi().
 */
function vud_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':

      // avoid showing the widget in some node builds
      $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;
  }
}

/**
 * Implementation of vud hook_template_suggestions().
 */
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,
  );
}

/**
 * Menu callback; display all votes for a node.
 */
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();
  }
}

/**
 * Implementation of hook_link().
 */
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;
}

/**
 * Implementation of hook_content_extra_fields().
 */
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;
  }
}

Functions

Namesort descending Description
vud_node_admin_settings Advanced menu settings callback.
vud_node_content_extra_fields Implementation of hook_content_extra_fields().
vud_node_link Implementation of hook_link().
vud_node_menu Implementation of hook_menu().
vud_node_nodeapi Implementation of hook_nodeapi().
vud_node_perm Implementation of hook_perm().
vud_node_tab_view_stats Access Callback from hook_menu (vud_node_menu).
vud_node_template_suggestions Implementation of vud hook_template_suggestions().
vud_node_tracker Menu callback; display all votes for a node.
vud_node_views_api Implementation of hook_views_api().
vud_node_vud_widget_message_codes_alter Implementation of vud hook_vud_widget_message_codes_alter().

Constants