You are here

simpleanswer.module in Answers 5.2

Adds a simple form to post a response/answer to questions using the answers module. This module does not display its content directly and requires the answers module to show its content.

@author Amanuel Tewolde http://ticklespace.com @email myfirstname (at) companydomain above

File

simpleanswer/simpleanswer.module
View source
<?php

/**
 * @file
 * Adds a simple form to post a response/answer to questions using the answers module.
 * This module does not display its content directly and requires the answers module to show its content.
 * 
 * @author Amanuel Tewolde http://ticklespace.com
 * @email myfirstname (at) companydomain above
 *
 */

/**
 * Implementation of hook_help().
 */
function simpleanswer_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables the creation of responses.');
    case 'node/add#simpleanswer':
      return t('If you want to add a simple response to questions posted using answers module, use this module.');
  }
}

/**
 * Implementation of hook_node_info().
 */
function simpleanswer_node_info() {
  return array(
    'simpleanswer' => array(
      'name' => t('Simple Answer'),
      'module' => 'simpleanswer',
      'description' => t("Enables the creation of responses to questions."),
      'has_title' => FALSE,
      'has_body' => TRUE,
      'body_label' => t('Answer'),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function simpleanswer_perm() {
  return array(
    'create responses',
    'edit own responses',
  );
}

/**
 * Implementation of hook_access().
 * TODO: find a way to keep answer nodes from showing up in Create Content list
 * 
 */
function simpleanswer_access($op, $node) {
  global $user;
  if ($op == 'create') {
    return user_access('create responses');
  }
  if ($op == 'update' || $op == 'delete') {
    if (user_access('edit own responses') && $user->uid == $node->uid) {
      return TRUE;
    }
  }
}

/**
 * Implementation of hook_form().
 */
function simpleanswer_form(&$node) {
  if (!arg(3) && arg(2) != 'edit') {
    drupal_goto();
  }
  $type = node_get_types('type', $node);
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->body_label),
    '#default_value' => $node->body,
    '#rows' => 3,
    '#cols' => 60,
    '#attributes' => array(
      'class' => 'mceNoEditor',
    ),
    '#required' => TRUE,
  );
  $form['#attributes'] = array(
    'class' => 'subscription_box',
  );
  $form['body_filter']['filter'] = filter_form($node->format);
  return $form;
}

/*
 * Implementation of hook_view()
 * since answers are only meant to be seen on question pages on request to do a $page view it is redirected to the question.
 */
function simpleanswer_view(&$node, $teaser = 0, $page = 0) {
  $node = node_prepare($node, $teaser);
  if ($page) {
    $qid = answers_answersapi('question', $node->nid);
    drupal_goto('node/' . $qid);
  }
  $node->content['body']['#value'] = theme('simpleanswer_content', $node, $teaser, $page);
  return $node;
}

/*
 * Implementation of theme to allow custome look for answers
 */
function theme_simpleanswer_content($node, $teaser = FALSE, $page = FALSE) {
  $username = theme('username', user_load(array(
    'uid' => $node->uid,
  )));
  return "<span style='float:left;padding-right: 7px;font-size: 2em;'>A:</span>" . check_markup($node->body, $node->format) . '<br />Answer by ' . $username;
}

/*
 * Implementation of hook_link() 
 * Adds an edit link to original powers and uid 1
 */
function simpleanswer_link($type, $node = 0, $main = 0) {
  global $user;
  $links = array();
  if ($node && variable_get(ANSWERS_TYPE . 'simpleanswer_editable', '0')) {
    if ($node->type == 'simpleanswer') {
      if ($node->uid == $user->uid || $user->uid == 1) {
        $links['editlink'] = array(
          'title' => t('EDIT'),
          'href' => 'node/' . $node->nid . '/edit',
        );
      }
    }
  }
  return $links;
}

/*
 * Implementation of hook_load() 
 * this loads our stuff and force disables commenting on quest nodes
 * TODO: make the disable comments optional
 */
function simpleanswer_load($node) {
  $node->comment = COMMENT_NODE_DISABLED;
  return $node;
}

/**
 * Implementation of AnswersAPI.
 * @param $op
 *   operations are:
 *     answersinfo - returns name of answer module 
 *     answerbuttons - returns a Add answer link
 *     settings - returns from elements that are added to the Answers Settings page
 *     add - returns an add from
 *     embedded - returns an answer as it will appear in a list of questions
 * @param $nid
 *   node id of the quest
 * 
 * @param $answercount
 *   current number of answers in the quest
 *   
 * */
function simpleanswer_answers($op, $nid = NULL, $answercount = 0) {
  switch ($op) {
    case 'answersinfo':
      $info['simpleanswer'] = 'Simple Answer Response';
      return $info;
      break;
    case 'answerbuttons':
      if ($answercount == 0) {
        return user_access('create responses') ? l(t('Be the first to add an Answer'), 'node/add/simpleanswer/' . $nid, array(
          'class' => 'postanswer',
        )) : '';
      }
      else {
        return user_access('create responses') ? l(t('Add an Answer'), 'node/add/simpleanswer/' . $nid, array(
          'class' => 'postanswer',
        )) : '';
      }
      break;
    case 'settings':
      $form['simpleanswer'] = array(
        '#type' => 'fieldset',
        '#description' => t('Simple Answer lets users post answers using a single textarea.'),
        '#title' => t('Simple Answer Module'),
      );
      $form['simpleanswer'][ANSWERS_TYPE . 'simpleanswer'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use Simple Answer'),
        '#return_value' => 1,
        '#default_value' => variable_get(ANSWERS_TYPE . 'simpleanswer', '0'),
      );
      $form['simpleanswer'][ANSWERS_TYPE . 'simpleanswer_editable'] = array(
        '#type' => 'checkbox',
        '#title' => t('Make Simple Answers editable'),
        '#description' => t('Allow users to edit their Simple Answers.'),
        '#return_value' => 1,
        '#default_value' => variable_get(ANSWERS_TYPE . 'simpleanswer_editable', '0'),
      );
      return $form;
      break;
    case 'add':
      return simpleanswer_form();
      break;
    case 'embedded':
      $shownode = node_load($nid);
      $shownode = node_prepare($shownode);
      $rendered = node_teaser($shownode->body);
      return $rendered;
      break;
  }
}

Functions

Namesort descending Description
simpleanswer_access Implementation of hook_access(). TODO: find a way to keep answer nodes from showing up in Create Content list
simpleanswer_answers Implementation of AnswersAPI.
simpleanswer_form Implementation of hook_form().
simpleanswer_help Implementation of hook_help().
simpleanswer_link
simpleanswer_load
simpleanswer_node_info Implementation of hook_node_info().
simpleanswer_perm Implementation of hook_perm().
simpleanswer_view
theme_simpleanswer_content