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

Disabling Post Count in Cetrain Categories or Discussions

SkismaSkisma New
edited January 2015 in Vanilla 2.0 - 2.8

Hey everyone,

I'm here with yet another question, although this time I have searched the forums here and don't think this has been asked before.

I'm wanting to know how I can disable post count in a certain category or even a certain discussion. In my Off-Topic category/forum there is a discussion where I actually allow spam posts. When I say spam posts I'm not talking about legitimate spam, but instead short, 1 -2 word posts that don't have to be relevant to any sort of specific topic. Because this one discussion doesn't require insightful, constructive posts there will likely be users who post dozens of times a day in this thread/topic (once I gain a userbase of course) and I don't want that to show on their post count.

You're probably thinking, "then why do you have such a topic" and I don't blame you. It's hard to explain, but this is something crucial to my forum that my users are going to want.

«1

Comments

  • Options

    In simple words if you are saying how do I prevent post count from displaying in certain categories.

    the answer would be via css.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    Hmmm... This is a tricky one.

    On the one hand, it is trivial to prevent a post from updating the post count. On the other hand, running dba/counts will correct the account.

    Let me have a think on it. Some one else might figure it out first. :)

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    SkismaSkisma New
    edited January 2015

    @peregrine‌ Thank you for the reply, but no that's not exactly what I'm looking for. What I'm wanting is to disable any posts within this category/discussion so that they don't count towards the users total post count. So if they have 47 posts here and 35 posts in all other places, their total post count will be 35 instead of 82.

    Edit: @hgtonight‌ thank you for giving this a thought!

  • Options

    @hgtonight said:
    On the one hand, it is trivial to prevent a post from updating the post count. On the other hand, running dba/counts will correct the account.

    The plugin dba/count handlers are registered after the applications, so the counts could just be overwritten

  • Options
    hgtonighthgtonight ∞ · New Moderator
    edited January 2015

    @Skisma‌ Hook into the Comment Model, look for the discussion ID and decrement the user post count.

    public function CommentModel_BeforeNotification_Handler($Sender) {
      $Discussion = $Sender->EventArguments('Discussion');
      $Comment = $Sender->EventArguments('Comment');
      if($Discussion->DiscussionID == C('DiscountDiscussions.ID')) {
        $Sender->SQL
                ->Update('User')
                ->Set('CountComments', 'CountComments - 1', FALSE)
                ->Where('UserID', $Comment['InsertUserID'])
                ->Put();
      }
    }
    

    Then you need to create a simple model that will recalculate the user count.

    class DiscountDiscussionModel extends Gdn_Model {
    
      public function Counts($Column, $From, $To) {
        // TODO: Add recalculation SQL here
        return TRUE;
      }
    }
    

    Finally, register the counts handler on the DBA controller.

      public function DBAController_CountJobs_Handler($Sender) {
        $Sender->Data['Jobs']['Reduce Post Count'] = "/dba/counts.json?" . http_build_query(array('table' => 'DiscountDiscussion', 'column' => 'CountComments'));
      }
    

    Hopefully someone else can help you fill in the blanks as far as what query to execute.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    How about

    $CountQuery = DBAModel::GetCountSQL('count', 'User', 'Discussion', 'CountDiscussions', 'DiscussionID', 'UserID', 'InsertUserID');
    $AndNotIn = ' and c.CategoryID not in (3)';
    Gdn::Database()->Query(substr($CountQuery, 0, -1) . $AndNotIn . ')');
    

    to exclude CategoryID 3 for example

  • Options
    hgtonighthgtonight ∞ · New Moderator

    If you are excluding an entire category (misread initial post, sorry), you change your comment model hook to something like:

    public function CommentModel_BeforeNotification_Handler($Sender) {
      $Discussion = $Sender->EventArguments('Discussion');
      $Comment = $Sender->EventArguments('Comment');
      if($Discussion->CategoryID == C('DiscountDiscussions.CategoryID')) {
        $Sender->SQL
        ->Update('User')
        ->Set('CountComments', 'CountComments - 1', FALSE)
        ->Where('UserID', $Comment['InsertUserID'])
        ->Put();
      }
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    @hgtonight‌ @Bleistivt‌ Wow you guys are awesome! I'm trying to make sense of all of this, but being a noob it's hard. lol. So do I need to add @Bleistivt‌'s code in the section where @hgtonight‌ says "// TODO: Add recalculation SQL here" on line 4?

    I think I'm going to take the php course on codecademy.com to familiarize myself a little.

  • Options

    Oh and to be clear, ultimately what I want is to have the "Post Party Countdown" discussion to have disabled post count...so anytime someone posts in there it won't count towards their total posts. However, if it's much easier to just disable the entire "Off Topic Discussion category that would be fine. http://forgehaven.com/vforums/categories/off-topic-discussion

  • Options

    @Bleistivt said:
    How about

    $CountQuery = DBAModel::GetCountSQL('count', 'User', 'Discussion', 'CountDiscussions', 'DiscussionID', 'UserID', 'InsertUserID');
    $AndNotIn = ' and c.CategoryID not in (3)';
    Gdn::Database()->Query(substr($CountQuery, 0, -1) . $AndNotIn . ')');

    to exclude CategoryID 3 for example

    On line 2, could I replace c.CategoryID with c.DiscussionID and where (3) is listed, replace it with the corresponding discussion ID?

  • Options

    Yes. You can also put multiple discussion IDs (1, 2, 3)

  • Options

    @Bleistivt‌ awesome! Just a tiny bit more clarification and I think I'm good :)

    1. So, is there a certain file this code needs to go in, or do I create a new PHP file and add it somewhere?
    2. Do I use all 3 of @hgtonight‌'s snippets from here: http://vanillaforums.org/discussion/comment/223611/#Comment_223611and include yours in the "// TODO: Add recalculation SQL here" section?
  • Options
    BleistivtBleistivt Moderator
    edited January 2015

    1 Create a basic plugin structure in the plugins directory like so:

    /plugins/DiscountDiscussions/class.discountdiscussions.plugin.php

    <?php if (!defined('APPLICATION')) exit();
    
    $PluginInfo['DiscountDiscussions'] = array(
        'Name' => 'DiscountDiscussions',
        'Description' => 'Description',
        'Version' => '1.0',
        'Author' => "Skisma",
        'MobileFriendly' => TRUE,
    );
    
    class DiscountDiscussionsPlugin extends Gdn_Plugin {
        //Event Hooks here!
    }
    

    and a model plugins/DiscountDiscussions/models/class.discountdiscussionmodel.php

    The first and the third snippet of hgtonight are event hooks that belong in the plugin, the second is the model class.

    2 Yes, what I posted was the recalculation SQL

  • Options

    @Bleistivt‌ thanks for the clarification! You made that so easy...however, it's not working.

    Here's what I've got, and keep in mind I've made the directories just as you said to.


  • Options

    Have you enabled the plugin through the dashboard?

  • Options

    @Bleistivt‌ Yup, still no go.

  • Options
    BleistivtBleistivt Moderator
    edited January 2015

    That means you have to enable debug mode in your config to see if you get any error messages. ;)

    But if you are impatient, the error is in the spoiler:

    It should be

    !

    $Discussion = $Sender->EventArguments['Discussion'];
    $Comment = $Sender->EventArguments['Comment'];
    
  • Options

    @Bleistivt‌ thanks!

    I've updated to script and uploaded it to the directory, however, it's still not working :neutral_face:

    Here's my debug: (The first two were there previously)

  • Options
    hgtonighthgtonight ∞ · New Moderator

    @Skisma You need to add $Configuration['Debug'] = TRUE; to your /conf/config.php file to enable debug mode.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    @hgtonight‌ Thanks, but I already did that, haha. The image above are the results. The first two have to do with something else.

Sign In or Register to comment.