Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

[Core Hack] Prevent user from select their answer as best answer when not enough 48 hours/2days

+2 votes
75 views
asked Jul 16 in Plugins by Bùi Lê Chí Bảo

My Q2A site got a lot of people who trying to post spammy, low-quality posts,... in order to earn point because my site have reward at end of the month. One of the most big problem I have is people mark (select) their answer as the best answer. So I went to Stackoverflow to learn how their solve these problem, and I found an very interesting point:

Now, there are some special rules around owner-accepted answers, to prevent gaming:

  1. Wait 48 hours. You must wait 2 days from the time you originally asked your question before you can accept your own answer. This gives other users a chance to answer the question in good faith, and earn the accepted answer.

Ref: https://meta.stackexchange.com/questions/6044

Sounds good, how to do that?

It's require a little core hacks, because the functions in core we are going to touch in is not allowed to override, so override it via plugin is not possible.

In \qa-include\pages\question-view.php file:

1) Locate line 448 - function qa_page_q_answer_view()

2) Scroll down, locate this part:

if ($question['aselectable'] && !$answer['hidden'] && !$answer['queued']) {
    if ($isselected)
        $a_view['unselect_tags']='title="'.qa_lang_html('question/unselect_popup').'" name="'.$prefix.'dounselect"'.$clicksuffix;
    else
    {
        $a_view['select_tags']='title="'.qa_lang_html('question/select_popup').'" name="'.$prefix.'doselect"'.$clicksuffix;
    }
}

3) Replace the above part with the code below. Note that you can change the error messages by editing the $error variable. And also you can change the limt (2 days) by edit value "4147200", this value is the time count in second.

if ($question['aselectable'] && !$answer['hidden'] && !$answer['queued']) {
    if ($isselected)
        $a_view['unselect_tags']='title="'.qa_lang_html('question/unselect_popup').'" name="'.$prefix.'dounselect"'.$clicksuffix;
    else
    {
        $a_view['select_tags']='title="'.qa_lang_html('question/select_popup').'" name="'.$prefix.'doselect"'.$clicksuffix;
        if ($question['userid'] == $answer['userid']) 
            if ((strtotime($answer['created'])-strtotime($question['created'])) < 4147200)
        {
            $error = "'You need to wait for 2 days to select your answer as best answer or  waiting answers from your fellow members.'";
            $a_view['select_tags']="onclick=\"alert(".$error.");\""; 
        }
    }
}

4) That it! This feature should be working now.

BONUS: And here is what happen when you mark your answer as best answer when not enough 2 days:

Q2A version: 1.7.4
commented Jul 21 by Waterfr Villa
nice idea for a helpful plugin, did you get it as a plugin yet?

1 Answer

+3 votes
answered Jul 16 by Scott

You don't need a core hack for this, it can be done with plugins.

For example, make a layer plugin that checks the age of the question and removes the best answer tick if under your threshold. (Or, change the code for the tick to show your message using JS.)

Then, for security purposes, you'd need to make an event plugin that looks for the 'a_select' event and deselects the answer if the age is below the threshold.


Also from a quick look at your code I'm not sure it actually prevents the answer being selected - it just adds a JS notice which can be bypassed using browser dev tools. 

commented Jul 16 by Bùi Lê Chí Bảo
Thanks Scott, I got your points. I will update the instructions that use your method.
I even don't know this could be done by use a event module :)
...