You are here

vud_term.module in Vote Up/Down 7

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

Adds a voting widget to terms.

File

vud_term/vud_term.module
View source
<?php

/**
 * @file
 * Adds a voting widget to terms.
 */

/**
 * Implementation of hook_menu().
 */
function vud_term_menu() {
  $items = array();
  $items['admin/config/search/voteupdown/term'] = array(
    'title' => 'Terms',
    'description' => 'Vote Up/Down Term settings',
    'page arguments' => array(
      'vud_term_admin_settings',
    ),
    'access arguments' => array(
      'administer vote up/down on terms',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_permission().
 */
function vud_term_permission() {
  return array(
    'use vote up/down on terms' => array(
      'title' => t('Vote on Terms'),
    ),
    'administer vote up/down on terms' => array(
      'title' => t('Administer Vote Up/Down on Comments'),
    ),
  );
}

/**
 * Advanced menu settings callback.
 */
function vud_term_admin_settings() {
  $form['vud_term_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node types'),
    '#description' => t('Select the node types for which you want to activate voting on terms.'),
    '#default_value' => variable_get('vud_term_types', array()),
    '#options' => node_type_get_names(),
  );
  $form['vud_term_widget'] = array(
    '#type' => 'radios',
    '#title' => t('Widget selection'),
    '#description' => t('Select the voting widget theme that will be displayed.'),
    '#default_value' => variable_get('vud_term_widget', 'plain'),
    '#options' => vud_widget_get_names(),
  );
  $form['vud_term_votetable_show'] = array(
    '#type' => 'select',
    '#title' => t('Widget display'),
    '#description' => t('Choose when the voting table will be displayed.'),
    '#default_value' => variable_get('vud_term_votetable_show', 1),
    '#options' => array(
      0 => 'Teaser only',
      1 => 'Full display only',
      2 => 'Both teaser and full',
    ),
  );
  $form['vud_term_reset'] = array(
    '#type' => 'radios',
    '#title' => t('Votes reset'),
    '#description' => t('Choose if users are allowed to reset their vote on a term, on a node.'),
    '#default_value' => variable_get('vud_term_reset', 0),
    '#options' => array(
      0 => 'No',
      1 => 'Yes',
    ),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_node_view().
 */
function vud_term_node_view($node, $view_mode, $langcode) {
  if (user_access('use vote up/down on terms')) {
    $node_type = in_array($node->type, variable_get('vud_term_types', array()), TRUE);
    $showmode = variable_get('vud_term_votetable_show', 1);
    $teaser = 0;
    if ($view_mode == 'teaser') {
      $teaser = 1;
    }
    if ($node_type) {
      switch ($showmode) {
        case 0:
          if ($teaser == 1) {
            _vud_term_generate_table($node);
          }
          break;
        case 1:
          if ($teaser == 0) {
            _vud_term_generate_table($node);
          }
          break;
        case 2:
          _vud_term_generate_table($node);
          break;
      }
    }
  }
}

/**
 * Function to generate the voting table.
 */
function _vud_term_generate_table(&$node) {
  $widget = variable_get('vud_term_widget', 'plain');
  $header = array();
  $rows = array();
  foreach ($node->taxonomy as $term => $tdetails) {
    $entity_id = $tdetails->tid;
    $tag = "n{$node->nid}";
    $reset_token = drupal_get_token("votereset/term/{$entity_id}/{$tag}");
    if (variable_get('vud_term_reset', 0) && user_access('reset vote up/down votes')) {
      $header = array(
        array(
          'data' => t('Term'),
        ),
        array(
          'data' => t('Description'),
        ),
        array(
          'data' => t('Vote'),
        ),
        array(
          'data' => t('Score'),
        ),
        array(
          'data' => t('Reset'),
        ),
      );
      $rows[] = array(
        check_plain($tdetails->name),
        check_plain($tdetails->description),
        theme('vud_widget', array(
          'entity_id' => $entity_id,
          'type' => 'term',
          'tag' => $tag,
          'widget_theme' => $widget,
        )),
        "<span id=\"total-votes-term-{$entity_id}\">" . _vud_term_get_row_votes($entity_id, $tag) . '</span>',
        l('Reset your vote', "votereset/term/{$entity_id}/{$tag}/{$reset_token}"),
      );
    }
    else {
      $header = array(
        array(
          'data' => t('Term'),
        ),
        array(
          'data' => t('Description'),
        ),
        array(
          'data' => t('Vote'),
        ),
        array(
          'data' => t('Score'),
        ),
      );
      $rows[] = array(
        check_plain($tdetails->name),
        check_plain($tdetails->description),
        theme('vud_widget', array(
          'entity_id' => $entity_id,
          'type' => 'term',
          'tag' => $tag,
          'widget_theme' => $widget,
        )),
        "<span id=\"total-votes-term-{$entity_id}\">" . _vud_term_get_row_votes($entity_id, $tag) . '</span>',
      );
    }
  }
  $node->content['vud_term_show_table'] = array(
    '#markup' => theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'class' => 'vud-term-table',
        'id' => "term-table-{$node->nid}",
      ),
    )),
    '#weight' => 0,
  );
}

/**
 * Function to get votes for a particular term row on a node.
 */
function _vud_term_get_row_votes($entity_id, $tag) {
  $criteria = array(
    'entity_type' => 'term',
    'entity_id' => $entity_id,
    'value_type' => 'points',
    'tag' => $tag,
    'function' => 'sum',
  );
  $vote_result = (int) votingapi_select_single_result_value($criteria);
  return $vote_result;
}

/**
 * Implementation of vud hook_template_suggestions().
 */
function vud_term_template_suggestions($template_type, $plugin, $entity_id) {
  $term = taxonomy_get_term($entity_id);
  return array(
    $template_type,
    $template_type . '_term',
    $template_type . '_term_' . $plugin['name'],
    $template_type . '_term_' . $plugin['name'] . '__' . $term->vid,
  );
}

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

Functions

Namesort descending Description
vud_term_admin_settings Advanced menu settings callback.
vud_term_menu Implementation of hook_menu().
vud_term_node_view Implementation of hook_node_view().
vud_term_permission Implementation of hook_permission().
vud_term_template_suggestions Implementation of vud hook_template_suggestions().
vud_term_views_api Implementation of hook_views_api().
_vud_term_generate_table Function to generate the voting table.
_vud_term_get_row_votes Function to get votes for a particular term row on a node.