Q&A events
Hi all,
I'm wondering if there is a way to have a plug-in listen for changes in the state of a Q&A thread? For example, when an answer is marked as accepted I'd like to know so I can index that in my data store (I'm trying to make an Algolia plug-in).
The Q&A plug-in doesn't use fireEvent at all. Is there some way to do this?
Thanks,
Allan
Tagged:
0
Best Answer
-
R_J
Admin
Oh yes, I was too hasty...
The function that is called is DiscussionController_QnA_Create. It calls
$ActivityModel->Save($Activity);and I guess that's what you use. The ActivityModel has a BeforeSave event that you should be able to track down6
Answers
You can't hook into the plugin, but you can use the same routine to check for an answer. Here is the method of the plugin that "marks the question as answered":
public function CommentModel_BeforeUpdateCommentCount_Handler($Sender, $Args) { $Discussion =& $Args['Discussion']; // Mark the question as answered. if (strtolower($Discussion['Type']) == 'question' && !$Discussion['Sink'] && !in_array($Discussion['QnA'], array('Answered', 'Accepted')) && $Discussion['InsertUserID'] != Gdn::Session()->UserID) { $Sender->SQL->Set('QnA', 'Answered'); } }So basically you can copy it and instead of the line
$Sender->SQL->Set('QnA', 'Answered');you can insert your code.I'm not sure I've used the most up to date code, so please use that method from your own copy of the plugin.
That sounds like it would be ideal, but that handler doesn't appear to execute when I click the "Did this answer the question" buttons. I've just added some trace code and it doesn't get hit.
I'm trying to trace the code through to see how it hooks together, but any other suggestions would be very warmly welcomed!
Oh yes, I was too hasty...
The function that is called is DiscussionController_QnA_Create. It calls
$ActivityModel->Save($Activity);and I guess that's what you use. The ActivityModel has a BeforeSave event that you should be able to track downThanks! The put me onto the right track. I found that
BeforeSavewon't always work for me in this case sinceActivityModelhas a check to see if the user should be notified of the changes. In this case I don't want that check, I always want to know when an answer has been marked as accepted, regardless of who asked it or answered it.As such i've added a
AfterAcceptedevent immediately after the code you noted. Then listening forQnAPlugin_AfterAccepted_Handlerdoes the job nicely.I've created a pull request with the change sound it be appropriate to include.
Thanks again,
Allan