View source
<?php
include_once 'answers.features.inc';
module_load_include('inc', 'answers', 'includes/answers.display');
module_load_include('inc', 'answers', 'includes/answers.lock');
module_load_include('inc', 'answers', 'includes/answers.notify');
module_load_include('inc', 'answers', 'includes/answers.search');
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>';
}
}
function answers_menu() {
$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(
'create question content',
),
'file' => 'includes/answers.search.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function answers_menu_alter(&$items) {
if (!empty($items['node/add/answer'])) {
$items['node/add/answer']['type'] = MENU_CALLBACK;
}
}
function answers_settings() {
$form['display'] = _answers_display_settings();
$form['notify'] = _answers_notify_settings();
return system_settings_form($form);
}
function answers_nodeapi(&$node, $op, $teaser, $page) {
global $user;
switch ($op) {
case 'view':
if ($node->type == 'question') {
$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_display_nodeapi($node, $op, $teaser, $page);
_answers_notify_nodeapi($node, $op, $teaser);
}
function _answers_question_answers($question) {
$results = array();
$qid = is_object($question) ? $question->nid : $question;
$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;
}
function answers_form_question_node_form_alter(&$form, &$form_state) {
$prompt = arg(1) == 'add' ? t('Ask Your Question') : t('Update Your Question');
$form['buttons']['submit']['#value'] = $prompt;
if (isset($_GET['title']) && (!isset($form['#access']) || !empty($form['#access']))) {
drupal_set_title(t('Add some details to your question'));
$form['title']['#default_value'] = $_GET['title'];
}
}
function answers_form_answer_node_form_alter(&$form, &$form_state) {
$prompt = arg(1) == 'add' ? t('Post Your Answer') : t('Update Your Answer');
$form['buttons']['submit']['#value'] = $prompt;
$form['#after_build'][] = 'answers_form_answer_node_form_after_build';
}
function answers_form_answer_node_form_after_build($form, &$form_state) {
$form['field_answer_question']['#type'] = 'hidden';
return $form;
}
function answers_form_views_exposed_form_alter(&$form, &$form_state) {
_answers_search_form_views_exposed_form_alter($form, $form_state);
}
function answers_field_access($op, $field, $account, $node = NULL) {
if ($field['field_name'] == 'field_notify_p' && $op == 'edit') {
return !empty($account->uid) && variable_get('answers_new_answer_notice_allow_p', TRUE);
}
if ($field['field_name'] == 'field_question_locked_p') {
return FALSE;
}
if ($field['field_name'] == 'field_answer_question' && $op == 'view') {
$node = menu_get_object('node');
if ($node->type != 'answer') {
return FALSE;
}
}
}
function answers_theme_registry_alter(&$theme_registry) {
$template = 'node';
$originalpath = array_shift($theme_registry[$template]['theme paths']);
$modulepath = drupal_get_path('module', 'answers');
array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}