I am developing a new answers module which can let poster select the best answer based on the Answers module,now i got a problem that i don't know how to put the best answer to the top of answers once it is selected.

I set if {answers} 's status == 1 means it is the best answer.I think i should add codes at below tow functions,but i don't know how to do .Please hlep

function simpleanswer_view(&$node, $teaser = 0, $page = 0) {

if($page) {
$qid = quest_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 "A:" . $node->body . '
Answer by ' . $username;

}

Comments

CWitt-1’s picture

I would also love this to be answered.

onhover’s picture

This is just a guess, I'm not an experienced module writer, but would it work to forget about your status==1, and use the comes-with-nodes weight property and give the best answer a -10 weight?

huangweiqiu’s picture

thanks but it doesn't work.

Hardik C’s picture

diff --git a/answers.module b/answers.module
index 230c355..51c3e7e 100644
--- a/answers.module
+++ b/answers.module
@@ -5,6 +5,7 @@
*/

include_once('answers.features.inc');
+module_load_include('inc', 'answers', 'includes/answers.field_utils');
module_load_include('inc', 'answers', 'includes/answers.notify');
module_load_include('inc', 'answers', 'includes/answers.search');
module_load_include('inc', 'answers', 'includes/answers.lock');
@@ -71,7 +72,7 @@ function answers_node_view($node, $view_mode, $langcode) {
// 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_instance = field_info_instance('node', 'field_answer_question', 'answer');
- $locked_p = $node->field_question_locked_p['und'][0]['value'];
+ $locked_p = answers_field_get_value($node, 'field_question_locked_p');
if ($locked_p == $field_instance['widget']['settings']['node_link']['full']) {
$field_instance['widget']['settings']['node_link']['full'] = $locked_p ? +0 : +1;
field_update_instance($field_instance);
diff --git a/includes/answers.field_utils.inc b/includes/answers.field_utils.inc
new file mode 100644
index 0000000..801ac56
--- /dev/null
+++ b/includes/answers.field_utils.inc
@@ -0,0 +1,23 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Field utility functions for the 'Answers' module
+ *
+ *
+ */
+
+function answers_field_get_value($node, $field) {
+ $items = field_get_items('node', $node, $field);
+ return $items[0]['value'];
+}
+
+
+function answers_field_get_node_reference($source_node, $field) {
+ $items = field_get_items('node', $source_node, $field);
+ $target_nid = $items[0]['nid'];
+ return node_load($target_nid);
+}
+
diff --git a/includes/answers.lock.inc b/includes/answers.lock.inc
index 0a6101b..ba4c852 100644
--- a/includes/answers.lock.inc
+++ b/includes/answers.lock.inc
@@ -32,6 +32,8 @@
*
*/

+module_load_include('inc', 'answers', 'includes/answers.field_utils');
+
function answers_reset_question_locks () {
$modules = module_implements('answers_lock_question_p');
$result = db_query("SELECT * from {node} WHERE type = 'question';");
@@ -61,11 +63,13 @@ function answers_lock_question_p($question, $modules=NULL) {
}

function answers_lock_question($question) {
- $question->field_question_locked_p['und'][0]['value'] = 1;
+ $lang = field_language('node', $question, 'field_question_locked_p');
+ $question->field_question_locked_p[$lang][0]['value'] = 1;
node_save($question);
}

function answers_unlock_question($question) {
- $question->field_question_locked_p['und'][0]['value'] = 0;
+ $lang = field_language('node', $question, 'field_question_locked_p');
+ $question->field_question_locked_p[$lang][0]['value'] = 0;
node_save($question);
}
diff --git a/includes/answers.notify.inc b/includes/answers.notify.inc
index 7b66f3d..c54946b 100644
--- a/includes/answers.notify.inc
+++ b/includes/answers.notify.inc
@@ -15,6 +15,8 @@
*
*/

+module_load_include('inc', 'answers', 'includes/answers.field_utils');
+
/*
* Add settings to the notification form
*/
@@ -75,9 +77,9 @@ function _answers_notify_form_alter(&$form, &$form_state, $form_id) {
*/
function _answers_notify_node_insert($node) {
if ($node->type=='answer') {
- $question_id = $node->field_answer_question['und'][0]['nid']; // extract the nid of the question
- if ($question_id) {
- answers_notify_new_answer($question_id);
+ $question = answers_field_get_node_reference($node, 'field_answer_question'); // extract the nid of the question
+ if ($question->nid) {
+ answers_notify_new_answer($question->nid);
}
}
}
@@ -89,7 +91,7 @@ function answers_notify_new_answer($nid) {
global $user;

$question = node_load($nid);
- $notify_p = $question->field_notify_p['und'][0]['value']; // extract the nid of the question
+ $notify_p = answers_field_get_value($question, 'field_notify_p'); // extract the nid of the question

if ($notify_p) {