You are here

answers.module in Answers 7

File

answers.module
View source
<?php

/**
 * @file
 * Code for the Answers feature.
 */
include_once 'answers.features.inc';
module_load_include('inc', 'answers', 'includes/answers.notify');
module_load_include('inc', 'answers', 'includes/answers.search');

/**
 * Implement 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>';
  }
}

/*
 * Implementation of hook_menu().
 */
function answers_menu($may_cache = NULL) {
  $nid = (int) arg(1);
  global $user;
  $items['admin/config/content/answers'] = array(
    'title' => 'Answers',
    'description' => 'Configure how the question/answer service operates',
    '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(
      'add question',
    ),
    'file' => 'includes/answers.search.inc',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  return $items;
}
function answers_settings() {
  $form = array_merge(array(), _answers_notify_settings());
  return system_settings_form($form);
}

/*
 * Implement hook_node_view
 */
function answers_node_view($node, $view_mode, $langcode) {
  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_instance = field_info_instance('node', 'field_answer_question', 'answer');
    $locked_p = $node->field_question_locked_p['und'][0]['value'];
    if ($locked_p == $field_instance['widget']['settings']['node_link']['full']) {
      $field_instance['widget']['settings']['node_link']['full'] = $locked_p ? +0 : +1;
      field_update_instance($field_instance);
    }
  }
}

/*
 * Implement hook_node_insert
 */
function answers_node_insert($node) {
  _answers_notify_node_insert($node);
}

/*
 * Implement hook_node_delete
 *
 * If a question is deleted, also delete its answers
 */
function answers_node_delete($node) {
  if ($node->type == 'question') {
    $answer_nids = _answers_question_answers($node);
    foreach ($answer_nids as $answer_nid) {
      node_delete($answer_nid);
    }
  }
}

/*
 * answers_nids 
 * 
 * Input: $question: a question node or nid
 * Returns: List of answer 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('answers_to_a_question');
  $view
    ->set_arguments(array(
    $qid,
  ));
  $view
    ->execute();
  foreach ($view->result as $result) {
    $nid = $result->nid;
    $results[$nid] = $nid;
  }
  return $results;
}

/*
 * Implement hook_form_alter
 *
 */
function answers_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  _answers_notify_form_alter($form, $form_state, $form_id);
  _answers_search_form_alter($form, $form_state, $form_id);
  switch ($form_id) {
    case 'question_node_form':

      // Change the name of the submit button
      $form['actions']['submit']['#value'] = t('Ask Your Question');

      // insert a predefined question title if one has been set in the path
      if (isset($_GET['title'])) {
        $title = check_plain($_GET['title']);
        drupal_set_title(t('Add some details to your question'));
        $form['title']['#default_value'] = $title;
      }

      // Hide 'field_question_locked_p' (this is only used behind the scenes, not directly set by users)
      $form['field_question_locked_p']['#prefix'] = '<div style="display: none;">';
      $form['field_question_locked_p']['#suffix'] = '</div>';
      break;
  }
}