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.

I Just Noticed a Strange Bug

24

Comments

  • @Bleistivt I just noticed an area where I forgot to replace DiscountDiscussions with DisablePostCount, but I fixed it.

    To my understanding, I've gotten everything done at this point besides:

    @Bleistivt said:
    Handle a hook after comment submission (possibly the CommentModel_BeforeNotification_Handler) and increment the field for that user if the discussion is not in the list of excluded discussions.

    Correct?

  • BleistivtBleistivt Moderator

    Yes. All of this logic can basically be copy & pasted from the hints I gave you and the existing plugin.

    You can remove everything after line 41, the function override is not needed anymore.

    Once you have everything done, enable debug mode (to see syntax errors) and try to enable your plugin.

  • @Bleistivt Ok great, I think I've got it!

    Before I upload, do I need to have a models folder with a model.php? Or am I fine just uploading this class.disablepostcount.plugin.php?

  • BleistivtBleistivt Moderator

    No, just the class.disablepostcount.plugin.php in the DisablePostCount folder

  • SkismaSkisma New
    edited March 2015

    @Bleistivt Sweet!

    Just uploaded and everything seemed good until i tried to enable the plugin, I get:

    The addon could not be enabled because it generated a fatal error:

    It doesn't say what the error is. Here's is all of the code pasted (keep in mind, it may not all embed):

    <?php if (!defined('APPLICATION')) exit();
    
    $PluginInfo['DisablePostCount'] = array(
        'Name' => 'DisablePostCount',
        'Description' => 'Disables post count in specified discussions.',
        'Version' => '1.0',
        'Author' => "Skisma, Bleistivt",
        'MobileFriendly' => TRUE,
        'SettingsUrl' => '/settings/disablepostcount',
        'SettingsPermission' => 'Garden.Settings.Manage'
    );
    
    class DisablePostCountPlugin extends Gdn_Plugin {
    
        public function SettingsController_DisablePostCount_Create($Sender) {
            $Sender->Permission('Garden.Settings.Manage');
            $Sender->AddSideMenu('/settings/disablepostcount');
            $Sender->SetData('Title', 'DisablePostCount'.' '.T('Settings'));
    
            $Conf = new ConfigurationModule($Sender);
            $Conf->Initialize(array(
                'DisablePostCount.IDs' => array(
                    'Control' => 'textbox',
                    'LabelCode' => 'List the DiscussionID's you wish to disable post count in, comma separated.',
                ))
            );
            $Conf->RenderAll();
        }
    
        public function Setup() {
            $this->Structure();
        }
    
        public function Structure() {
            GDN::Structure()->Table('User')
            ->Column('CustomCount', 'int(11)', 0)
            ->Set();
        }
    
        public function CommentModel_BeforeNotification_Handler($Sender) {
            DisablePostCountModel::UserCount($Sender->EventArguments['Comment']['InsertUserID']);
            DisablePostCountModel::CountComments($Sender->EventArguments['Comment']['InsertDiscussionID']);
        }
    
    }
    
    if (!function_exists('CountString')) {
        function CountString($Number, $Url = '', $Options = array()) {
            $Url = str_replace('/profile/count/comments', '/plugin/disablepostcount/comment', $Url);
            if (is_string($Options))
                $Options = array('cssclass' => $Options);
            $Options = array_change_key_case($Options);
            $CssClass = GetValue('cssclass', $Options, '');
            if ($Number === NULL && $Url) {
                $CssClass = ConcatSep(' ', $CssClass, 'Popin TinyProgress');
                $Url = htmlspecialchars($Url);
                $Result = "<span class=\"$CssClass\" rel=\"$Url\"></span>";
            } elseif ($Number) {
                $Result = " <span class=\"Count\">$Number</span>";
            } else {
                $Result = '';
            }
            return $Result;
        }
    
    }
    
  • BleistivtBleistivt Moderator

    You can see the error here. You need to escape ' in a string or use " as a delimiter.

    Like I said, you can remove everything at the end starting with if (!function_exists('CountString')) {

    Your BeforeNotification handler won't work. Take a look look at this function.

  • SkismaSkisma New
    edited March 2015

    @Bleistivt I've compared and have found that 'InsertDiscussionID' doesn't exsist and CountComments must be 'CountComments', based on the function you have me look at. Other than that I have no clue what to change.

    This is basically like me trying to translate an entire paragraph of Spanish when I only know a few Spanish words...but I'm trying really hard!

  • @Bleistivt are there any additional hints you can give? I appreciate you challenging me here, it makes me think and helps me learn:) I feel like I'm so close!

  • BleistivtBleistivt Moderator

    Sorry, I've run out of hints.

    public function CommentModel_BeforeNotification_Handler($Sender, $Args) {
        $ExcludeIDs = array_map('trim', explode(',', C('DisablePostCount.IDs')));
    
        if (!in_array($Args['Comment']->DiscussionID, $ExcludeIDs)) {
    
            $UserID = $Args['Comment']->InsertUserID;
            $Count = Gdn::UserModel()->GetID($UserID)->CustomCount;
    
            Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count + 1);
        }
    }
    

    One of many possible solutions: increment the new field by 1 (line 9) if the comments DiscussionID isn't found in the list of discussions to exclude (line 4).

    But you could also count the posts each time, which is more robust (see what I linked to in my previous post).

  • SkismaSkisma New
    edited March 2015

    @Bleistivt Ok, I can understand that since you explained what was happening.

    I just updated the plugin with the new code and it's still giving me the fatal error. I checked to see if maybe I had an extra curly bracket, but I don't seem to. I also tried moving the CommentModel_BeforeNotification_Handler outside of the class, but that didn't work either. I'm trying everything I can before asking for help, but I can't think of anything else.

    <?php if (!defined('APPLICATION')) exit();
    
    $PluginInfo['DisablePostCount'] = array(
        'Name' => 'DisablePostCount',
        'Description' => 'Disables post count in specified discussions.',
        'Version' => '1.0',
        'Author' => "Skisma, Bleistivt",
        'MobileFriendly' => TRUE,
        'SettingsUrl' => '/settings/disablepostcount',
        'SettingsPermission' => 'Garden.Settings.Manage'
    );
    
    class DisablePostCountPlugin extends Gdn_Plugin {
    
        public function SettingsController_DisablePostCount_Create($Sender) {
            $Sender->Permission('Garden.Settings.Manage');
            $Sender->AddSideMenu('/settings/disablepostcount');
            $Sender->SetData('Title', 'DisablePostCount'.' '.T('Settings'));
    
            $Conf = new ConfigurationModule($Sender);
            $Conf->Initialize(array(
                'DisablePostCount.IDs' => array(
                    'Control' => 'textbox',
                    'LabelCode' => 'List the DiscussionID's you wish to disable post count in, comma separated.',
                ))
            );
            $Conf->RenderAll();
        }
    
        public function Setup() {
            $this->Structure();
        }
    
        public function Structure() {
            GDN::Structure()->Table('User')
            ->Column('CustomCount', 'int(11)', 0)
            ->Set();
        }   
    
        public function CommentModel_BeforeNotification_Handler($Sender, $Args) {
            $ExcludeIDs = array_map('trim', explode(',', C('DisablePostCount.IDs')));
    
        if (!in_array($Args['Comment']->DiscussionID, $ExcludeIDs)) {
    
        $UserID = $Args['Comment']->InsertUserID;
        $Count = Gdn::UserModel()->GetID($UserID)->CustomCount;
    
        Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count + 1);
    
            }
    
        }
    
    }
    
  • edited March 2015

    You need to use a text editor that recognizes codes so you can see your errors.

    You have been told to escape quotes before.... please read a tutorial.

    http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php

    'LabelCode' => 'List the DiscussionID\'s\ you wish to disable post count in, comma separated.',

    You should try looking at the cgi error logs to see the specific errors. Usually that gives you a clue instead of removing or adding lines of code you don't know what they do.

  • BleistivtBleistivt Moderator

    It looks like the comment argument is actually an array here. In case of doubt just use:

     ...
        if (!in_array(val('DiscussionID', $Args['Comment']), $ExcludeIDs)) {
    
            $UserID = val('InsertUserID', $Args['Comment']);
     ...
    
  • SkismaSkisma New
    edited March 2015

    @vrijvlinder Thanks for pointing that out! I just removed it all together and was able to enable:)

    @Bleistivt Thanks! I just tried using this piece instead to see if I'd get any better results, unfortunately I'm still getting nothing. I've tried 3 different pieces of code and none of them seem to execute the goal here. When enabling the plugin I went to the settings and put in 13, then clicked save. I then went to discussion 13 and posted a test comment. I checked my profile and my post count had incremented to my total posts, including the ones in the discussion that are supposed to be disabled. When comparing this what was happening at the time I posted this topic (http://vanillaforums.org/discussion/29547/i-just-noticed-a-strange-bug/p1), the only difference is that the post count doesn't ever get discounted when re-visiting my profile. The only way to discount the post count is if I run dba/counts. Here are the 3 different pieces of code I've tried:

     public function CommentModel_BeforeNotification_Handler($Sender, $Args) {
    
    $ExcludeIDs = array_map('trim', explode(',', C('DisablePostCount.IDs')));
    
    if (!in_array($Args['Comment']->DiscussionID, $ExcludeIDs)) {
    
    $UserID = $Args['Comment']->InsertUserID;
    
    $Count = Gdn::UserModel()->GetID($UserID)->CustomCount;
    
    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count + 1);
    
        }
    }
    

    ============================================================================

     public function CommentModel_BeforeNotification_Handler($Sender, $Args) {
    
    $ExcludeIDs = array_map('trim', explode(',', C('DisablePostCount.IDs')));
    
     if (!in_array(val('DiscussionID', $Args['Comment']), $ExcludeIDs)) {
    
    $UserID = val('InsertUserID', $Args['Comment']);
    
    $Count = Gdn::UserModel()->GetID($UserID)->CustomCount;
    
    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count + 1);
    
        }
    }
    

    ============================================================================

    public static function UserCount($UserID) {
    
            if (!C('DisablePostCount.IDs')) {
            return Gdn::UserModel()->ProfileCount($UserID, 'CountComments');
    }
    
    $ExcludeIDs = array_map('trim', explode(',', C('DisablePostCount.IDs')));
    $ExcludeIDs = array_filter($ExcludeIDs, 'ctype_digit');
    
    $Count = Gdn::SQL()
    ->From('Comment')
    ->Where('InsertUserID', $UserID)
    ->WhereNotIn('DiscussionID', $ExcludeIDs)
    ->GetCount();
    
    Gdn::UserModel()->SetField($UserID, 'CountComments', $Count);
    
    return $Count;
    
        }
    
  • BleistivtBleistivt Moderator
    edited March 2015

    Correct.

    That is not what the new plugin is doing: If you read my previous posts, it manages a separate field that is incremented when a comment is posted in a discussion that is not "blacklisted". It leaves the comment count untouched.

    To see it working, you'll need to look at the field "CustomCount" in the user table of your database.

    The next step is to copy the PostCount Yaga rule and adapt it to use the new field "CustomCount" instead.

  • @Bleistivt Oh I see, so basically I'm modifying it so that the YAGA ranks are awarded based off of CustomCount, rather than post count. That makes sense now.

    @Bleistivt said:

    To see it working, you'll need to look at the field "CustomCount" in the user table of your database.

    Are you referring to the file GDN_User.sql? Or is there a way I can check this without downloading and opening this file?

  • SkismaSkisma New
    edited March 2015

    Update: I've looked into this a little further and can see that I just needed to scroll further to the right and there is a field called "CustomCount". It was the very last thing so I had to scroll pretty far just to see it.

    I noticed that when using the 3rd piece of code in my above comment that this didn't work, but looking at the code it appears this is because it's referencing 'CountComments' instead of 'CustomCount'.

    I then put in the 2nd piece of code and tested, it works great!

    Now I just need to alter the PostCount Yaga rule.

    @hgtonight mind helping me locate which file this is?

  • Should be in the library of Yaga.

    1. Copy /applications/yaga/library/rules/class.postcount.php to /applications/yaga/library/rules/class.discountedcount.php.
    2. In your newly copied file, change the class name to DiscountedCount (on line 10)
    3. Change line 14 to $CountPosts = $User->CountDiscussions + $User->CustomCount;
    4. Change line 75 to be `return "Discounted Post Count"
    5. Update your badge to use the new class.

    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.

  • @hgtonight Sweet! Ok so here's what I've gotten so far:

    1. I've downloaded and renamed class.postcount.php to class.discountedcount.php (I'm going to upload to /applications/yaga/library/rules once I'm done modifying).
    2. I've changed the class name
    3. I've updated this line
    4. This is where I'm unsure. Are you wanting me to change return T('Yaga.Rules.PostCount'); to return ('Discounted Post Count');?

    Also, I wanted to get verification of my understanding based on line 14. Looking at this it is my understanding that once I update everything, ranks will be awarded based on the combined total of the user's CountDiscussions and CustomCount numbers. Correct?

  • SkismaSkisma New
    edited March 2015

    @hgtonight I'm assuming you're answering both questions with that? lol

    Regarding step 5, do you mind expanding on this a bit? I'm not sure what this means. What do you mean by updating my badge?

Sign In or Register to comment.