Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

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?



Best Answer


  • R_JR_J Ex-Fanboy Munich Admin

    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!

  • allanjallanj
    edited April 2016

    Thanks! The put me onto the right track. I found that BeforeSave won't always work for me in this case since ActivityModel has 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 AfterAccepted event immediately after the code you noted. Then listening for QnAPlugin_AfterAccepted_Handler does the job nicely.

    I've created a pull request with the change sound it be appropriate to include.

    Thanks again,

Sign In or Register to comment.