Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+4 votes
3.5k views
in Q2A Core by
edited by

Problematic scenario is the following:

1. Question is posted

2. forum member A starts to write an answer

3. other forum member identifies the question as duplicate and closes the question

4. forum member A sends his answer, but gets the message "you cannot post to a closed question" --- loses his answer and is frustrated

 

What I like to do: As soon as the answer to a closed-duplicate question gets posted (event), I want to "redirect" (rewrite) the answer to the original question. Per database that only means to change the parentid.

How would you do it?

 

---

Code:

<?php

    class qa_answer_on_duplicate {
        
        // main event processing function
        function process_event($event, $userid, $handle, $cookieid, $params) {
            
            if($event == 'a_post') {
                
                // query to check if recent question is duplicate now
                // if duplicate, we get the postid of the original question

                $originalQu = qa_db_read_one_value(qa_db_query_sub('SELECT closedbyid
                                            FROM `^posts`
                                            WHERE `type` = "Q"
                                            AND `closedbyid` IS NOT NULL
                                            AND `postid` = #
                                            LIMIT 1
                                            ;', $params['parentid']), true );

                if( isset($originalQu) ) {
                    // check closed-by to see if just-closed (type=NOTE) or if it is duplicate-closed (type=Q)
                    $closedByQu = qa_db_read_one_value( qa_db_query_sub('SELECT postid FROM `^posts`
                                                                        WHERE `postid` = #
                                                                        AND `type` = "Q"
                                                                        ;', $originalQu), true );

                    if(isset($closedByQu)) {
                        // update parentid of our question posted
                        // not allowed: $params['parentid'] = $originalQu;

                        qa_post_set_hidden($params['postid'], true);
                        qa_db_post_set_parent($params['postid'], $originalQu);
                        qa_post_set_hidden($params['postid'], false);
                    }
                }
                
            }
        
        }

    }

by
edited by
I tried to solve this by an event module, code bove, but it seems that I cannot change the parentid.

2 Answers

+1 vote
by
 
Best answer

Why difficult if you can make it simple. I just allow posting to duplicate questions, I let the answer through!

qa-ajax-answer.php

change line 54 from: if ((@$question['basetype']=='Q') && !isset($question['closedbyid'])) {

to: if (@$question['basetype']=='Q') {

Now we do not lose any answer anymore!

0 votes
by

qa_post_set_hidden() to hide the post then qa_db_post_set_parent() to change the parent then qa_post_set_hidden() to show it again. That should update counts and indexes correctly.

by
edited by
thx for your answer, however, I thought I just can use the event module to read all params of the posted question and then change $params['parentid'] = $originalQuID; nope?

Or must it be hidden to change $params['parentid'] ?
by
Event modules can't change data like that.
by
edited by
I tried the functions you suggested but it is not working:

  qa_post_set_hidden($params['postid'], true);
  qa_db_post_set_parent($params['postid'], $originalQu);
  qa_post_set_hidden($params['postid'], false);

I pasted the code in the question above.
by
edited by
To understand the event module better, is the data written to the db already when we access it by the event module?

Can we update the parentid of the answer?
by
I have still not figured out how to do it. Maybe we have to do something more, not only hiding the post before changing the parentid?
by
Slowly I see some light.

When the user is posting his answer to a duplicate question, the answers gets *nowhere* as the question has status closed.

The event module is called *afterwards* obviously, so trying to change the parentid is impossible as the answer does not exist (no postid).

@gidgreen: That's right? And if yes, it seems to me that there is no other way to intercept the answer before writing to DB and changing its parentid.

The only module that is capable is the filter module: http://www.question2answer.org/modules.php?module=filter

But there I cannot access the parentid...
by
It's messy. You can use a process module - init_ajax() / init_page() - to do something early on in either the Ajax request or web submission to detect this situation and temporarily unclose the question, so that the request gets processed. Then catch the event when it's created and move the answer, then reclose the question.
by
Thanks for your answer, gidgreen... halleluja, the first time I encounter a situation that q2a cannot solve easily :)

As I once wrote to you: "Can we just add an else to the qa-ajax-answer.php, in the end after:
if ((@$question['basetype']=='Q') && !isset($question['closedbyid'])) { ... }
else {
// throw error (but what q2a function?)
// ...
}

and throw an error back to the user, so that he will be informed that posting on a closed question is not possible and still has his answer text opened?"

--

You replied: "Ajax answering as currently implemented can't show an error - if there's a problem it drops back to non-Ajax answering by submitting the form. See qa-question.js."

--

My reply: "As a workaround (idea):
- I could create a plugin that checks for duplicate question.
- If ajax answer post does not work: in question.js before the html submit I implement another ajax call that checks if qu. is duplicate now and prevents submitting.

But shouldn't there be a core solution to prevent losing content?"

--

Your reply: "In the non-Ajax code the key function is qa_page_q_add_a_submit() - look how it is called."

--

Summed up: I still feel that an addition to the core can solve it! =)
...