View source
<?php
include_once 'commons_answers.features.inc';
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;
}
}
function commons_answers_strongarm_alter(&$items) {
if (isset($items['comment_answer'])) {
$items['comment_answer']->value = '2';
}
}
function commons_answers_vud_node_widget_alter(&$widget, $node) {
if ($node->type == 'answer') {
$widget = 'thumbs';
drupal_add_css(drupal_get_path('module', 'commons_answers') . '/css/commons_answers.css', 'theme');
}
}
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');
}
}
function commons_answers_form_commons_core_group_create_content_block_form_alter(&$form, &$form_state) {
unset($form['node_type']['#options']['answer']);
}
function commons_answers_views_default_views_alter(&$views) {
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;
}
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;
}
}
}
function commons_answers_views_pre_render(&$view) {
if (user_access('create answer content') && $view->name == 'question_answers') {
$question_nid = $view->args[0];
$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') {
$group_nid = $view->args[0];
}
$view->attachment_after = drupal_get_form('commons_answers_add_question_form', $group_nid);
}
}
function commons_answers_og_features_registry() {
$registry = array();
$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;
}
function commons_answers_og_create_links($group) {
$links = array();
$links['create_question'] = l(t('Question'), 'node/add/question');
return $links;
}
function commons_answers_update_question_answers_groups($question_node) {
$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);
$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;
$formats = filter_formats();
if (!empty($formats)) {
$format = array_shift($formats);
$node->format = $format->format;
}
$node->status = 1;
$node->uid = $account->uid;
$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();
$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;
$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,
);
}
}
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');
}
}
class CommonsAnswersTidyNodeLinksImpl extends TidyNodeLinksMappingWithThemeFunctions {
public function get_mapping() {
$map = parent::get_mapping();
$map['statistics_counter'] = array(
'css_class' => 'statistics_counter',
'display_class' => 'TidyNodeLinksElementWithThemeFunctions',
);
$map['answer_field_answer_question'] = array(
'visible' => TRUE,
'display_class' => 'TidyNodeLinksElementWithThemeFunctions',
);
return $map;
}
}