You are here

vud_term.module in Vote Up/Down 6.2

Same filename and directory in other branches
  1. 6.3 vud_term/vud_term.module
  2. 7 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_perm().
 */
function vud_term_perm() {
  return array(
    'use vote up/down on terms',
    'administer vote up/down on terms',
  );
}

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

/**
 * 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_get_types('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_nodeapi().
 */
function vud_term_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      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 = $a3;
        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) {
    $content_id = $tdetails->tid;
    $tag = "n{$node->nid}";
    $reset_token = drupal_get_token("votereset/term/{$content_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', $content_id, "term", $tag, $widget),
        "<span id=\"total-votes-term-{$content_id}\">" . _vud_term_get_row_votes($content_id, $tag) . '</span>',
        l('Reset your vote', "votereset/term/{$content_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', $content_id, "term", $tag, $widget),
        "<span id=\"total-votes-term-{$content_id}\">" . _vud_term_get_row_votes($content_id, $tag) . '</span>',
      );
    }
  }
  $node->content['vud_term_show_table'] = array(
    '#value' => theme('table', $header, $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($content_id, $tag) {
  $criteria = array(
    'content_type' => 'term',
    'content_id' => $content_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, $content_id) {
  $term = taxonomy_get_term($content_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_nodeapi Implementation of hook_nodeapi().
vud_term_perm Implementation of hook_perm().
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.