You are here

commons_answers.module in Drupal Commons 6.2

File

modules/features/commons_answers/commons_answers.module
View source
<?php

include_once 'commons_answers.features.inc';

/**
 * Implementation of hook_nodeapi().
 */
function commons_answers_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'question' && ($op == 'update ' || $op == 'presave')) {
    commons_answers_update_question_answers_groups($node);
  }
}
function commons_answers_ctools_plugin_directory($module, $plugin) {
  if ($module == 'vud') {
    return $plugin;
  }
}

/**
** Implementation of hook_strongarm_alter().
**/
function commons_answers_strongarm_alter(&$items) {

  // Enable comments on Answer nodes.
  if (isset($items['comment_answer'])) {
    $items['comment_answer']->value = '2';
  }
}

/**
 * Implementation of hook_vud_node_widget_alter().
 */
function commons_answers_vud_node_widget_alter(&$widget, $node) {

  // Use the thumbs widget for the Anwser content type
  // and alter the widget to use Commons' green/red up/down arrows.
  if ($node->type == 'answer') {
    $widget = 'thumbs';
    drupal_add_css(drupal_get_path('module', 'commons_answers') . '/css/commons_answers.css', 'theme');
  }
}

/**
* Implementation of hook_form_alter().
**/
function commons_answers_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'question_node_form' && isset($form['#node'])) {
    $form['body_field']['body']['#title'] = t('Additional information');
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function commons_answers_form_commons_core_group_create_content_block_form_alter(&$form, &$form_state) {

  // We expose this through the contextual "Add an answer" forms.
  unset($form['node_type']['#options']['answer']);
}

/**
 * Implementation of hook_views_default_views_alter().
 */
function commons_answers_views_default_views_alter(&$views) {

  // Alter the default questions_answers view to always include the footer
  // that contains our "Add an answer" form.
  if (!empty($views['question_answers'])) {
    $views['question_answers']->display['default']->display_options['footer_format'] = '1';
    $views['question_answers']->display['default']->display_options['footer_empty'] = 1;
    $views['question_answers']->display['default']->display_options['empty'] = ' ';
    $views['question_answers']->display['default']->display_options['empty_format'] = '1';
    $views['question_answers']->display['node_content_1']->display_options['show_empty'] = 1;
  }

  // Display a '0' when no questions have been answered and use the
  // 'small' date format.
  foreach (array(
    'all_questions',
    'user_questions',
  ) as $view_id) {
    if (!empty($views[$view_id])) {
      $views[$view_id]->display['default']->display_options['fields']['created']['date_format'] = 'small';
      $views[$view_id]->display['default']->display_options['fields']['field_answer_count_value']['empty'] = '0';
      $views[$view_id]->display['default']->display_options['fields']['field_answer_count_value']['empty_zero'] = 0;
      $views[$view_id]->display['default']->display_options['fields']['field_answer_count_value']['hide_alter_empty'] = 0;
    }
  }
}

/**
 * Implementation of hook_views_pre_render().
 */
function commons_answers_views_pre_render(&$view) {
  if (user_access('create answer content') && $view->name == 'question_answers') {
    $question_nid = $view->args[0];

    // Generate a simplified Answer submission form and prepopulate it
    // with the question NID.
    $view->attachment_after = drupal_get_form('commons_answers_add_answer_form', $question_nid);
  }
  if (user_access('create question content') && in_array($view->name, array(
    'all_questions',
    'user_questions',
    'group_tab_questions',
  ))) {
    if ($view->name == 'group_tab_questions') {

      // @TODO: This should support questions that are members of multiple groups.
      $group_nid = $view->args[0];
    }
    $view->attachment_after = drupal_get_form('commons_answers_add_question_form', $group_nid);
  }
}

/**
 * Implementation of hook_og_features_registry()
 */
function commons_answers_og_features_registry() {
  $registry = array();

  // Feature: commons_answers
  $feature = new stdClass();
  $feature->id = 'commons_answers';
  $feature->name = t('Commons Answers');
  $feature->description = t('Enable your group members to create Questions and answers');
  $feature->components = array(
    'node' => array(
      'question',
    ),
  );
  $registry[$feature->id] = $feature;
  return $registry;
}

/**
 * Implementation of hook_og_create_links().
 */
function commons_answers_og_create_links($group) {
  $links = array();
  $links['create_question'] = l(t('Question'), 'node/add/question');
  return $links;
}

/**
 * Update group membership for answers so that they match that of the question.
 */
function commons_answers_update_question_answers_groups($question_node) {

  // Use the question_answers view to find answers to this question.
  $view = views_get_view('question_answers');
  $view
    ->set_display('node_content_1');
  $view
    ->set_arguments(array(
    $question_node->nid,
  ));
  $result = $view
    ->execute();
  if (!empty($view->result)) {
    foreach ($view->result as $result) {
      $answer_node = node_load($result->nid);
      $answer_node->og_groups = $question_node->og_groups;
      $answer_node->og_public = $question_node->og_public;
      node_save($answer_node);
    }
  }
}
function commons_answers_add_question_form(&$form_state, $group_nid) {
  $form['question'] = array(
    '#type' => 'fieldset',
    '#title' => t('Ask a question'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['question']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Question'),
    '#required' => TRUE,
  );
  $form['question']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Additional information'),
  );
  $form['question']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add question'),
  );
  $form['group_nid'] = array(
    '#type' => 'value',
    '#value' => $group_nid,
  );
  return $form;
}
function commons_answers_add_question_form_submit($form, &$form_state) {
  global $user;
  $account = $user;
  $node = new stdClass();
  if (!empty($form_state['values']['group_nid'])) {
    $group_nid = $form_state['values']['group_nid'];
    $group = node_load($group_nid);

    // Set the question to be private if the group is private.
    // This will automatically propogate to answers submitted
    // to this question.
    $node->og_public = !$group->og_private;
  }
  $title = $form_state['values']['title'];
  $question = $form_state['values']['body'];
  $node->title = $title;
  $node->body = $question;
  $node->type = 'question';
  $node->created = $SERVER['REQUEST_TIME'];
  $node->promote = 0;
  $node->sticky = 0;

  // Filtered HTML should be format 1 in Commons.
  $formats = filter_formats();
  if (!empty($formats)) {
    $format = array_shift($formats);
    $node->format = $format->format;
  }
  $node->status = 1;
  $node->uid = $account->uid;

  // By default, the questin
  $node->og_groups = array(
    $group_nid,
  );
  node_save($node);
  drupal_set_message(t('The question has been created.'));
}
function commons_answers_add_answer_form(&$form_state, $question_nid) {
  $form['answer'] = array(
    '#type' => 'textarea',
    '#title' => t('Add answer'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add answer'),
  );
  $form['question_nid'] = array(
    '#type' => 'value',
    '#value' => $question_nid,
  );
  return $form;
}
function commons_answers_add_answer_form_submit($form, &$form_state) {
  global $user;
  $account = $user;
  $question_nid = $form_state['values']['question_nid'];
  $question_node = node_load($question_nid);
  $answer = $form_state['values']['answer'];
  $node = new stdClass();

  // Provide a human-readable title for listings where the title
  // may be displayed, such as admin/content/node or a listing of answers.
  $node->title = substr($answer, 0, 30);
  if (strlen($answer) > 30) {
    $node->title .= '...';
  }
  $node->body = $answer;
  $node->type = 'answer';
  $node->created = $_SERVER['REQUEST_TIME'];
  $node->promote = 0;
  $node->sticky = 0;

  // Filtered HTML should be format 1 in Commons.
  $formats = filter_formats();
  if (!empty($formats)) {
    $format = array_shift($formats);
    $node->format = $format->format;
  }
  $node->status = 1;
  $node->uid = $account->uid;
  $node->field_answer_question[0]['nid'] = $question_nid;
  if (!empty($question_node->og_groups)) {
    $node->og_groups = $question_node->og_groups;
    $node->og_public = $question_node->og_public;
  }
  node_save($node);
  if (!empty($node->nid)) {
    $form_state['redirect'] = array(
      'node/' . $question_nid,
      '',
      'node-' . $node->nid,
    );
  }
}

/**
* Implementation of hook_tidy_node_links_toolkit_alter().
*/
function commons_answers_tidy_node_links_toolkit_alter(&$toolkit, $node) {
  if ($node->type == 'question') {
    drupal_add_css($module_path . '/css/tidy_node_links_answers.css');
    $toolkit
      ->set_mapping_class('CommonsAnswersTidyNodeLinksImpl');
  }
}

/**
 * Custom tidy node links mapping for questions
 */
class CommonsAnswersTidyNodeLinksImpl extends TidyNodeLinksMappingWithThemeFunctions {
  public function get_mapping() {
    $map = parent::get_mapping();
    $map['statistics_counter'] = array(
      'css_class' => 'statistics_counter',
      'display_class' => 'TidyNodeLinksElementWithThemeFunctions',
    );

    //Remove the "Post an answer" link in favor of the simplified answer submission form.
    $map['answer_field_answer_question'] = array(
      'visible' => TRUE,
      'display_class' => 'TidyNodeLinksElementWithThemeFunctions',
    );
    return $map;
  }

}