You are here

vud_comment.module in Vote Up/Down 7

Same filename and directory in other branches
  1. 6.3 vud_comment/vud_comment.module
  2. 6.2 vud_comment/vud_comment.module

Adds a voting widget to comments.

File

vud_comment/vud_comment.module
View source
<?php

/**
 * @file
 * Adds a voting widget to comments.
 */
define('VUD_COMMENT_DISPLAY_HIDDEN', 0);
define('VUD_COMMENT_DISPLAY_NORMAL', 1);

/**
 * Implementation of hook_menu().
 */
function vud_comment_menu() {
  $items = array();
  $items['admin/config/search/voteupdown/comment'] = array(
    'title' => 'Comments',
    'description' => 'Vote Up/Down Comment settings',
    'page arguments' => array(
      'vud_comment_admin_settings',
    ),
    'access arguments' => array(
      'administer vote up/down on comments',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_permission().
 */
function vud_comment_permission() {
  return array(
    'administer vote up/down on comments' => array(
      'title' => t('Administer Vote Up/Down on Comments'),
    ),
    'use vote up/down on comments' => array(
      'title' => t('Vote on Comments'),
    ),
    'view vote up/down count on comments' => array(
      'title' => t('View Vote Count on comments'),
    ),
  );
}

/**
 * Menu callback for administration settings.
 */
function vud_comment_admin_settings() {
  $form['vud_comment_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node types'),
    '#description' => t('Select the node types for which voting on comments will be activated.'),
    '#default_value' => variable_get('vud_comment_node_types', array()),
    '#options' => node_type_get_names(),
  );
  $form['vud_comment_widget'] = array(
    '#type' => 'radios',
    '#title' => t('Widget selection'),
    '#description' => t('Select the voting widget theme that will be displayed.'),
    '#default_value' => variable_get('vud_comment_widget', 'plain'),
    '#options' => vud_widget_get_names(),
  );
  $form['vud_comment_widget_display'] = array(
    '#type' => 'radios',
    '#title' => t('Widget display'),
    '#description' => t('Select how the voting widget will be displayed.'),
    '#default_value' => variable_get('vud_comment_widget_display', VUD_COMMENT_DISPLAY_NORMAL),
    '#options' => array(
      VUD_COMMENT_DISPLAY_NORMAL => t('Append to the comment content.'),
      VUD_COMMENT_DISPLAY_HIDDEN => t('Do not append. I will manually insert <code>$comment->vud_comment_widget</code> variable on my theme.'),
    ),
  );
  $form['vud_comment_votes'] = array(
    '#type' => 'radios',
    '#title' => t('Votes display'),
    '#description' => t('Choose if the total number of votes will be displayed in the links part.'),
    '#default_value' => variable_get('vud_comment_votes', 1),
    '#options' => array(
      0 => 'No',
      1 => 'Yes',
    ),
  );
  $form['vud_comment_reset'] = array(
    '#type' => 'radios',
    '#title' => t('Votes reset'),
    '#description' => t('Choose if users are allowed to reset their vote on a comment.'),
    '#default_value' => variable_get('vud_comment_reset', 0),
    '#options' => array(
      0 => 'No',
      1 => 'Yes',
    ),
  );
  ctools_include('dependent');
  $form['vud_comment_dim_active'] = array(
    '#type' => 'radios',
    '#title' => t('Dim comments'),
    '#description' => t('Choose if comments should be dimmed.'),
    '#default_value' => variable_get('vud_comment_dim_active', 0),
    '#options' => array(
      0 => 'No',
      1 => 'Yes',
    ),
  );
  $form['vud_comment_dim_threshold'] = array(
    '#type' => 'textfield',
    '#title' => t('Dim threshold'),
    '#default_value' => variable_get('vud_comment_dim_threshold', 0),
    '#description' => t('Comments with total votes less or equal to this values will be dimmed.'),
    '#process' => array(
      'ctools_dependent_process',
    ),
    '#dependency' => array(
      'radio:vud_comment_dim_active' => array(
        1,
      ),
    ),
  );
  return system_settings_form($form);
}
function vud_comment_admin_settings_validate($form, &$form_state) {
  if ($form_state['values']['vud_comment_dim_active'] == 1 && !is_numeric($form_state['values']['vud_comment_dim_threshold'])) {
    form_set_error('vud_comment_dim_threshold', t('You must use a numeric value for the dim threshold.'));
  }
}

/**
 * Implementation of hook_comment_view().
 */
function vud_comment_comment_view($comment, $view_mode, $langcode) {
  if (!isset($comment->cid)) {
    return;
  }
  $type = _vud_comment_get_node_type($comment->nid);
  $comment_allow = in_array($type, variable_get('vud_comment_node_types', array()), TRUE);
  $can_edit = user_access('use vote up/down on comments');
  if ($comment_allow && ($can_edit || user_access('view vote up/down count on comments'))) {
    $tag = variable_get('vud_tag', 'vote');
    $widget_theme = variable_get('vud_comment_widget', 'plain');
    $variables = array(
      'entity_id' => $comment->cid,
      'type' => 'comment',
      'tag' => $tag,
      'widget_theme' => $widget_theme,
      'readonly' => !$can_edit,
    );
    $comment->vud_comment_widget = theme('vud_widget', $variables);
    if (variable_get('vud_comment_widget_display', VUD_COMMENT_DISPLAY_NORMAL) == VUD_COMMENT_DISPLAY_NORMAL) {

      // TODO: Pass ta renderable array.
      $comment->content['vud_comment_widget'] = array(
        '#markup' => $comment->vud_comment_widget,
      );
    }
  }
}

/**
 * Override or insert variables into the comment templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 */
function vud_comment_preprocess_comment(&$variables) {
  if (variable_get('vud_comment_dim_active', 0) != '1') {
    return;
  }

  // Get total votes for this comment.
  $entity_id = $variables['comment']->cid;
  $tag = variable_get('vud_tag', 'vote');
  $criteria = array(
    'entity_type' => 'comment',
    'entity_id' => $entity_id,
    'value_type' => 'points',
    'tag' => $tag,
    'function' => 'sum',
  );
  $vud_sum = (int) votingapi_select_single_result_value($criteria);
  $vud_dim_threshold = (int) variable_get('vud_comment_dim_threshold', 0);
  if ($vud_sum <= $vud_dim_threshold) {
    $variables['status'] .= ' vud-comment-dimmed';
  }
}

/**
 * Function to return the node type of a particular node ID.
 */
function _vud_comment_get_node_type($nid) {
  return db_query("SELECT type from {node} where nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField();
}

/**
 * Implementation of hook_link().
 */
function vud_comment_link($type, $object, $teaser = FALSE) {
  $links = array();
  switch ($type) {
    case 'comment':
      $comment =& $object;
      $votes_display_mode = variable_get('vud_comment_votes', 1);
      $widget_theme = variable_get('vud_comment_widget', 'plain');
      $tag = variable_get('vud_tag', 'vote');
      $node = node_load($comment->nid);
      $votable_node_type_comment = in_array($node->type, variable_get('vud_comment_node_types', array()), TRUE);
      if ($votable_node_type_comment && $votes_display_mode) {
        $links['vud_comment_votes_count'] = array(
          'title' => theme('vud_votes', $comment->cid, $type, $tag, $widget_theme),
          'html' => TRUE,
        );
      }
      if ($votable_node_type_comment && variable_get('vud_comment_reset', 0) && user_access('reset vote up/down votes')) {
        $criteria = array(
          'entity_type' => $type,
          'entity_id' => $comment->cid,
          '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/comment/{$comment->cid}/{$tag}/{$widget_theme}");
          $links['vud_comment_votes_reset_link'] = array(
            'title' => t('Reset your vote'),
            'href' => "votereset/comment/{$comment->cid}/{$tag}/{$widget_theme}/{$reset_token}",
            'attributes' => array(
              'rel' => 'nofollow',
            ),
            'html' => TRUE,
          );
        }
      }
      break;
  }
  return $links;
}

/**
 * Implementation of vud hook_template_suggestions().
 */
function vud_comment_template_suggestions($template_type, $plugin, $entity_id) {
  $comment = comment_load($entity_id);
  $node = node_load($comment->nid);
  return array(
    $template_type,
    $template_type . '_comment',
    $template_type . '_comment_' . $plugin['name'],
    $template_type . '_comment_' . $plugin['name'] . '__' . $node->type,
  );
}

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

Functions

Namesort descending Description
vud_comment_admin_settings Menu callback for administration settings.
vud_comment_admin_settings_validate
vud_comment_comment_view Implementation of hook_comment_view().
vud_comment_link Implementation of hook_link().
vud_comment_menu Implementation of hook_menu().
vud_comment_permission Implementation of hook_permission().
vud_comment_preprocess_comment Override or insert variables into the comment templates.
vud_comment_template_suggestions Implementation of vud hook_template_suggestions().
vud_comment_views_api Implementation of hook_views_api().
_vud_comment_get_node_type Function to return the node type of a particular node ID.

Constants

Namesort descending Description
VUD_COMMENT_DISPLAY_HIDDEN @file Adds a voting widget to comments.
VUD_COMMENT_DISPLAY_NORMAL