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);
}
}
}
}
}