You are here

discussthis.module in Discuss This! 7

Same filename and directory in other branches
  1. 5 discussthis.module
  2. 6 discussthis.module
  3. 7.2 discussthis.module

Associations discussions in forums with specific nodes

File

discussthis.module
View source
<?php

// $Id: discussthis.module,v 1.7.2.25 2010/09/07 08:04:20 alexiswilke Exp $

/**
 * @file
 *
 * Associations discussions in forums with specific nodes
 */
define('DISCUSSTHIS_DEFAULT_NODE_MESSAGE', '<div>Following is a discussion on the [node:content-type] item titled: <a href="[node:url]">[node:title]</a>.</div>' . '<div>Below is the discussion so far. Feel free to add your own comments!</div>');
include_once 'discussthis.node.inc';
include_once 'discussthis.admin.inc';

// ---------------------------------------------
// HOOK IMPLEMENTATIONS

/**
 * Implements hook_help().
 */
function discussthis_help($path) {
  $output = '';
  switch ($path) {
    case "admin/help#module_name":
      $output = '<p>' . t('Displays links to discussion forums for a given node. Administrators can select the types of nodes for which this is enabled, and for each of these, which forum new topics should be created in.') . '</p>';
      $output .= '<ul><li><a href="admin/config/discussthis">Discuss This configuration settings</a></li>';
      $output .= '<li><a href="admin/user/access#module-discussthis">Discuss This permissions configuration</a></li></ul>';
      break;
  }
  return $output;
}

/**
 * Implements hook_permission().
 */
function discussthis_permission() {
  return array(
    'administer discuss this' => array(
      'title' => t('Administer discuss this'),
      'description' => t('Access Discuss This! setting page and configure it.'),
    ),
    'override discuss this forums' => array(
      'title' => t('Override discuss this forums'),
      'description' => t('Users with this settings can override default settings for discussion name for a node.') . ' <i>' . t('NOTE: this dependents on node edit permission.') . '</i>',
    ),
    'access discuss this links' => array(
      'title' => t('Access discuss this links'),
      'description' => t('Acess the discussion on a node and discuss it.'),
    ),
    'initiate discuss this topics' => array(
      'title' => t('Initiate discuss this topics'),
      'description' => t("Users with this permission can create a discussion for nodes that haven't."),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function discussthis_menu() {
  $items = array();
  $items['admin/structure/discussthis'] = array(
    'title' => 'Discuss This',
    'description' => 'Configure discuss this module defaults, including what node types and what forums to link to.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'discussthis_admin_settings',
    ),
    'access arguments' => array(
      'administer discuss this',
    ),
    'file' => 'discussthis.admin.inc',
  );
  $items['discussthis/new/%'] = array(
    'page callback' => 'discussthis_new',
    'page arguments' => array(
      2,
    ),
    // access not properly checked on callbacks (see function)
    'access arguments' => array(
      'initiate discuss this topics',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['discussthis/create/%'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'discussthis_create_discussion_form',
      2,
    ),
    // access not properly checked on callbacks (see function)
    'access arguments' => array(
      'initiate discuss this topics',
    ),
    'file' => 'discussthis.discussion.inc',
  );
  $items['discussthis/autocomplete'] = array(
    'title' => 'Autocomplete forum topics',
    'page callback' => 'discussthis_autocomplete',
    // access not properly checked on callbacks (see function)
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'discussthis.autocomplete.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function discussthis_theme($existing, $type, $theme, $path) {
  return array(
    'discussthis_admin_settings_theming' => array(
      'render element' => 'element',
      'file' => 'discussthis.admin.inc',
    ),
    'discussion_render_wrapper' => array(
      'render element' => 'content',
      'template' => 'templates/discussion-wrapper',
      'file' => 'discussthis.discussion.inc',
    ),
    'discussion_render_item' => array(
      'render element' => 'elements',
      'template' => 'templates/discussion-item',
      'file' => 'discussthis.discussion.inc',
    ),
  );
}

/**
 * Implements hook_form_alter().
 *
 * Adds per-node override forum dropdown and topic autocomplete
 * form for node edit forms.
 */
function discussthis_form_alter(&$form, &$form_state, $form_id) {

  //
  if ($form_id == 'forum_node_form' && !empty($form_state['discussthis']['post'])) {

    // we need to tweak forum forms to make sure that captcha do not
    // both us when we're auto-posting a new entry in the forum
    // if the user has a captcha on the forum, we need to add a dummy entry
    if (module_exists('captcha') && (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE)) && !user_access('skip CAPTCHA')) {
      module_load_include('inc', 'captcha');
      $captcha_point = captcha_get_form_id_setting('forum_node_form');
      if ($captcha_point && $captcha_point->type) {

        // the captcha type is set to none so it will be ignored
        $form['buttons']['captcha']['#captcha_type'] = 'none';
      }
    }
  }

  // If the user has no rights to override the Discuss This! settings: return.
  if (!user_access('override discuss this forums')) {
    return;
  }

  // Retrieve the content type of the current modified form.
  if (isset($form['type']['#value'])) {
    $type = $form['type']['#value'];
  }
  elseif (isset($form['orig_type']['#value'])) {
    $type = $form['orig_type']['#value'];
  }
  else {
    return;
  }

  // We only support node forms => return if it's not a node.
  if ($form_id != $type . '_node_form') {
    return;
  }

  // We know it is a node, attempt loading info about it
  // WARNING: $nid can be NULL or 0 when creating a new node.
  $nid = $form['nid']['#value'];

  // Only modify the form if this node type is discussthis-enabled.
  // Note that a type may be turned off, but if the node already
  // had a discussthis post attached to it, then it is still editable
  $forum_tid = _discussthis_get_forum($nid, $type);
  if (!$forum_tid) {
    return;
  }
  $form['discussthis'] = array(
    '#type' => 'fieldset',
    '#title' => t('Discuss This'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#group' => 'additional_settings',
  );
  $name = '';

  // Retrieve the topic nid attached with this node.
  // NOTE: the topic is 0 if no comments as yet been done.
  $topic_nid = _discussthis_get_topic($nid);
  if ($topic_nid) {

    // there is a topic linked, is the node still available?
    $topic = node_load($topic_nid);
    if ($topic) {

      // TODO: also get the name of the forum to display to the user
      $name = '<strong>' . t('Current topic #@nid: @title', array(
        '@title' => $topic->title,
        '@nid' => $topic_nid,
      )) . '</strong><br /><br />';
    }
    else {
      $sql = "DELETE FROM {discussthis} WHERE nid = %d";

      // TODO Please review the conversion of this statement to the D7 database API syntax.

      /* db_query($sql, $nid) */
      db_delete('discussthis')
        ->condition('nid', $nid)
        ->execute();
      unset($topic_nid);
    }
  }

  // This case is when no topic exists for the current node: ie no yet comment.
  if (!$topic_nid) {

    // Let users override the default module of new discussion
    // (this is not shown unless there is a discussion).
    // Retrieve the , or "No Discuss This!"
    $discussthis_forums = _discussthis_build_forums_selection_list();
    $discussthis_forums[0] = 'No Discuss This!';
    $form['discussthis']['discussthis_forum'] = array(
      '#type' => 'select',
      '#title' => t('Forum for new discussion'),
      '#required' => TRUE,
      '#description' => t('Select the forum where the first discussion about this node will be created. Or select "No Discuss This!" to prevent discussions on this node. Note: if a topic is attached to this node, then this parameter will have no effect.'),
      '#options' => $discussthis_forums,
      '#default_value' => $forum_tid,
    );
  }

  // let the user assign a forum topic to this node
  // this can be done at the time a node is being created
  // any forum is acceptable (although we could have flags to know whether that's the case...)
  $form['discussthis']['discussthis_topic'] = array(
    '#type' => 'textfield',
    '#title' => t('Identifier of the forum topic to attach to this node'),
    '#description' => $name . t('Override default topic associated with this node. Leave empty for default topic. Enter a topic name and use autocompletion to associate this node with an existing topic.'),
    '#default_value' => $topic_nid ? $topic_nid : '',
    '#autocomplete_path' => 'discussthis/autocomplete',
    '#maxlength' => 255,
  );
}

/**
 * \brief Validate the comment the user just posted.
 *
 * This function validates the entered data. The comment module
 * expects a valid name, email address and URL.
 *
 * \param[in] $form The form
 * \param[in,out] $form_state The current state of the form
 */
function discussthis_create_form_validate($form, &$form_state) {
  comment_form_validate($form, $form_state);
}

Functions

Namesort descending Description
discussthis_create_form_validate \brief Validate the comment the user just posted.
discussthis_form_alter Implements hook_form_alter().
discussthis_help Implements hook_help().
discussthis_menu Implements hook_menu().
discussthis_permission Implements hook_permission().
discussthis_theme Implements hook_theme().

Constants