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.
Options

Counting non-hidden comments

Hello,

I am trying to write a plugin to hide/unhide comments and discussions. I created a custom flag in comments and discussions tables that can take three values (0 - hidden, 1 - visible and 2 - visible to author only).

Taking this into account, what would be the right way to get comment totals? I see in the templates it is taken directly from the database (echo $Discussion->CountComments;). Should I create a function and use it in every place $Discussion->CountComments is mentioned?

Thank you.

Comments

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    Normally you do not have to replace something at every place, but you can hook into a function. The comment count is calculated by a function in the comment model. If you look at that file, you'll find the function updateCommentCount and in this function there are some events fired. You can use them like that:

    public function commentModel_beforeUpdateCommentCount_handler($sender, $args) {
        $args['Counts'] = Gdn::sql()
            ->select('c.CommentID', 'min', 'FirstCommentID')
            ->select('c.CommentID', 'max', 'LastCommentID')
            ->select('c.DateInserted', 'max', 'DateLastComment')
            ->select('c.CommentID', 'count', 'CountComments')
            ->from('Comment c')
            ->where('c.DiscussionID', $DiscussionID)
            ->where('c.YourCustomFlag', 1)
            ->get()
            ->firstRow(DATASET_TYPE_ARRAY);
    }
    

    But as you can see this doesn't take into account who is viewing that discussion. An admin should see all comments and the author might see some of those comments so for both of them this count would be wrong and you would need to have additional columns in the discussion table for them, too.
    You could add this information and display it in the discussion meta to those with appropriate rights: "x comments for everyone but you"/"x comments hidden for eveyone, y comments hidden for everyone but the discussion author."
    I personally cannot think of an elegant solution for that problem...

    And I wouldn't use flags in the database but complete words - that is more like Vanilla handles it.

  • Options
    1. If I write a function that calculates number of comments based on user type, where should it go? Into helper_functions.php in the theme?

    2. How to check that plugin is enabled from within the theme? To say something like: if plugin is enabled show new comment count, otherwise show regular count.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    You have multiple options. If you want to make that a feature of your theme, you can put all the code you need a the themehooks file of your theme.

    You can also create a stand alone plugin, but you know that this would only makes sense if the plugin itself displays the information.

    But just to answer your question: you can use if c('EnabledPlugins'.'YourPluginName', false) {... to check if a plugin is enabled

  • Options

    I think I figured it out. Right before Discussion meta is shown, replace CountComments from Discussion table with a calculated one.

    /** @var DiscussionsController $Sender */
    public function DiscussionsController_BeforeDiscussionContent_Handler($Sender) {
    
        foreach ($Sender->AnnounceData->Result() as $Discussion) {
            $Discussion->CountComments = $this->_ExposedCommentsCount($Discussion);
        }
    
        foreach ($Sender->DiscussionData->Result() as $Discussion) {
            $Discussion->CountComments = $this->_ExposedCommentsCount($Discussion);
        }
    }
    
    
    // calculate count of exposed comments for different user groups
    private function _ExposedCommentsCount($Discussion){
    ...
    }        
    
Sign In or Register to comment.