You are here

answers.module in Answers 6

File

answers.module
View source
<?php

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

/**
 * 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/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(
      'add question',
    ),
    'file' => 'includes/answers.search.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function answers_profile_page($mode) {
  switch ($mode) {
    case 'questions':
      return views_embed_view('user_questions');
    case 'answers':
      return views_embed_view('user_answers');
  }
}

/*
 * Implement hook_nodeapi. 
 * Tasks:
 *   - When a question is deleted, delete the answer associated with it
 *   - When a question is viewed, only show the 'Post an Answer' link if relevant
 */
function answers_nodeapi(&$node, $op, $teaser) {
  global $user;
  _answers_notify_nodeapi($node, $op, $teaser);
  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_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('question_answers');
  $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['buttons']['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;
  }
}