You are here

answers.module in Answers 6.2

File

answers.module
View source
<?php

include_once 'answers.features.inc';
module_load_include('inc', 'answers', 'includes/answers.display');
module_load_include('inc', 'answers', 'includes/answers.lock');
module_load_include('inc', 'answers', 'includes/answers.notify');
module_load_include('inc', 'answers', 'includes/answers.search');

/**
 * Implements hook_help().
 */
function answers_help($path, $arg) {
  switch ($path) {
    case "admin/help#modulename":
      return '<p>' . t('Enables users to ask questions and for other users to answer them.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function answers_menu() {
  $items['admin/settings/answers'] = array(
    'title' => 'Answers',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'answers_settings',
    ),
    'access arguments' => array(
      'administer content types',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['questions/start_ask'] = array(
    'title' => 'Add a Question',
    'description' => 'Enter a question to ask ... and start the process of asking it',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'answers_start_ask_form',
    ),
    'access arguments' => array(
      'create question content',
    ),
    'file' => 'includes/answers.search.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function answers_menu_alter(&$items) {
  if (!empty($items['node/add/answer'])) {
    $items['node/add/answer']['type'] = MENU_CALLBACK;
  }
}

/**
 * Answer administer settings form definition.
 */
function answers_settings() {
  $form['display'] = _answers_display_settings();
  $form['notify'] = _answers_notify_settings();
  return system_settings_form($form);
}

/**
 * Implements hook_nodeapi().
 *
 */
function answers_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    case 'view':
      if ($node->type == 'question') {

        // Ensure that the 'Post an Answer' link only shows if the question is not locked
        // The logic is a little complicated below to avoid updating the field when not necessary
        // The field should have the *opposite* value of the node->locked field
        $field = content_fields('field_answer_question', 'answer');
        $locked_p = $node->field_question_locked_p[0]['value'];
        if ($locked_p == $field['widget']['node_link']['full']) {
          module_load_include('inc', 'content', 'includes/content.crud');
          $field['widget']['node_link']['full'] = $locked_p ? +0 : +1;
          content_field_instance_update($field);
        }
      }
      break;
    case 'delete':
      if ($node->type == 'question') {
        $answer_nids = _answers_question_answers($node);
        foreach ($answer_nids as $answer_nid) {
          node_delete($answer_nid);
        }
      }
      break;
  }
  _answers_display_nodeapi($node, $op, $teaser, $page);
  _answers_notify_nodeapi($node, $op, $teaser);
}

/**
 * Gathers a list of answer NIDS for a given question.
 *
 * @param $question
 *   Either a numeric node NID or a node object.
 * @return
 *  Returns an array of answer node NIDs.
 */
function _answers_question_answers($question) {
  $results = array();
  $qid = is_object($question) ? $question->nid : $question;

  // Use views as a query engine
  $view = views_get_view('question_answers');
  $view
    ->set_arguments(array(
    $qid,
  ));
  $view
    ->execute();
  foreach ($view->result as $result) {
    $nid = $result->nid;
    $results[$nid] = $nid;
  }
  return $results;
}

/**
 * Implements hook_form_FORM_ID_alter() for question_node_form().
 */
function answers_form_question_node_form_alter(&$form, &$form_state) {

  // Change the name of the submit button.
  $prompt = arg(1) == 'add' ? t('Ask Your Question') : t('Update Your Question');
  $form['buttons']['submit']['#value'] = $prompt;

  // Populate title field if passed via URL if access to edit title.
  if (isset($_GET['title']) && (!isset($form['#access']) || !empty($form['#access']))) {
    drupal_set_title(t('Add some details to your question'));
    $form['title']['#default_value'] = $_GET['title'];
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for answer_node_form().
 */
function answers_form_answer_node_form_alter(&$form, &$form_state) {

  // Change the name of the submit button
  $prompt = arg(1) == 'add' ? t('Post Your Answer') : t('Update Your Answer');
  $form['buttons']['submit']['#value'] = $prompt;
  $form['#after_build'][] = 'answers_form_answer_node_form_after_build';
}
function answers_form_answer_node_form_after_build($form, &$form_state) {
  $form['field_answer_question']['#type'] = 'hidden';
  return $form;
}

/**
 * Implements hook_form_FORM_ID_alter() for views_exposed_form().
 */
function answers_form_views_exposed_form_alter(&$form, &$form_state) {
  _answers_search_form_views_exposed_form_alter($form, $form_state);
}

/**
 * Implements hook_field_access().
 */
function answers_field_access($op, $field, $account, $node = NULL) {
  if ($field['field_name'] == 'field_notify_p' && $op == 'edit') {
    return !empty($account->uid) && variable_get('answers_new_answer_notice_allow_p', TRUE);
  }

  // Lock field used programatically.
  if ($field['field_name'] == 'field_question_locked_p') {
    return FALSE;
  }

  // Hide links to the answer node in places such as the question_answers view,
  // which uses the full build mode to display answers.
  if ($field['field_name'] == 'field_answer_question' && $op == 'view') {
    $node = menu_get_object('node');
    if ($node->type != 'answer') {
      return FALSE;
    }
  }
}

/**
 * Implements hook_theme_registry_alter
 * 
 * Have drupal look in the module for content-type override templates
 */
function answers_theme_registry_alter(&$theme_registry) {
  $template = 'node';
  $originalpath = array_shift($theme_registry[$template]['theme paths']);
  $modulepath = drupal_get_path('module', 'answers');

  // Stick the original path with the module path back on top
  array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}

Functions

Namesort descending Description
answers_field_access Implements hook_field_access().
answers_form_answer_node_form_after_build
answers_form_answer_node_form_alter Implements hook_form_FORM_ID_alter() for answer_node_form().
answers_form_question_node_form_alter Implements hook_form_FORM_ID_alter() for question_node_form().
answers_form_views_exposed_form_alter Implements hook_form_FORM_ID_alter() for views_exposed_form().
answers_help Implements hook_help().
answers_menu Implements hook_menu().
answers_menu_alter Implements hook_menu_alter().
answers_nodeapi Implements hook_nodeapi().
answers_settings Answer administer settings form definition.
answers_theme_registry_alter Implements hook_theme_registry_alter
_answers_question_answers Gathers a list of answer NIDS for a given question.