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

13

Comments

  • @Skisma said:
    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?

    I assume you have a badge defined in your Yaga configuration that uses the Post Count rule. Edit that badge to use the Discount Post Count rule.

    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 I have several ranks (43 to be exact) that are based off of post count. So I need to find them somewhere within the yaga folder and update them to reflect the new rule?

  • @Skisma said:
    hgtonight I have several ranks (43 to be exact) that are based off of post count. So I need to find them somewhere within the yaga folder and update them to reflect the new rule?

    You would have to modify the rank system to look at your custom count rather than post count. I thought this was so your badges were awarded properly.

    You will have to modify the RankModel::GetHighestQualifyingRank() method to look at your new column. The code is pretty self-explanatory: https://github.com/hgtonight/Application-Yaga/blob/6f7bbb00bc6868ef03c6479372359a5d9f93525d/models/class.rankmodel.php#L84-L109

    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 Ok yea that seemed pretty simple, I just changed line 86 to $Posts = $User->CountDiscussions + $User->CustomCount;

    Looking through your highlighted code it doesn't appear anything else needs to be modified.

  • @hgtonight I've been testing this out and it seems to be almost working properly! Literally so close. As of right now, ranks are being awarded when the first post following the ranks requirement is submitted. For example, to reach Rank 8 my users must have 35 posts, however, it's not until 36 posts that they rank up to this level.

    Ideally, I could just lower the post requirement of all my ranks by 1, but this seems inefficient and could cause problems with Rank 1 (which is awarded when users submit their first post) since I'd have to set the post count to 0.

  • @Bleistivt Also, currently the CustomCount only increments. This is could potentially be a problem if a user suddenly decides to spam posts because deleting those posts wouldn't discount the CustomCount, therefore it wouldn't de-rank the user.

    Would it be difficult to implement this feature in the plugin?

  • Hook into the CommentModel_DeleteComment_Handler and decrement as necessary (if it belongs to the discounted discussion).

    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.

  • @Skisma said:

    ...therefore it wouldn't de-rank the user.

    For what it is worth, Yaga does not demote ranks automatically.

    The reason: hysteresis. Moving the user back and forth constantly would either require a lot of edge case testing or completely clog the activity feed with notifications. If it is really that big of a deal that someone's rank be demoted, there is manually intervention available (along with the freeze option which prevents them from automatically progressing until unfrozen).

    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.

  • SkismaSkisma New
    edited April 2015

    @hgtonight said:
    Hook into the CommentModel_DeleteComment_Handler and decrement as necessary (if it belongs to the discounted discussion).

    Well actually, the plugin is currently not counting posts in the discounted discussion at all, which is perfect! What I'm needing is something like this:

    public function CommentModel_DeleteComment_Handler {

    if (Comment is deleted from a DiscussionID not disabled) {

    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count - 1);

    }
    

    }

    If this would theoretically work, I just need help writing the if statement.

  • R_JR_J Admin

    @AngelaMitchel said:
    I never use to believe in spell casting ...

    Looks like vf.org is using @hgtonight's Data Generator

  • @hgtonight @Bleistivt From my above comment, would this ideally work? If so, what would I need inside my if statement?

  • If comment[discussionid] is not in discount discussion id array.

    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.

  • SkismaSkisma New
    edited April 2015

    @hgtonight Ok here's what I've got, please don't laugh! lol

    I'm trying really hard here.

    public function CommentModel_DeleteComment_Handler {
    
    if ('Comment','DiscussionID'(!in_array, $ExcludeIDs)) {
    
    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count - 1);
    
        }
    
    }
    
  • SkismaSkisma New
    edited April 2015

    @hgtonight @Bleistivt What if I used the same if statement from CommentModel_BeforeNotification_Handler instead? Like this:

    public function CommentModel_DeleteComment_Handler {
    
    if (!in_array(val('DiscussionID', $Args['Comment']), $ExcludeIDs)) {
    
    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count - 1);
    
            }
    
        }
    
  • I tried this and it broke the whole site :(

    If anyone reading this can help I'd really appreciate it! It's slow at work today so I have free time to fix this. Perhaps, @R_J if you've got a few minutes?

  • R_JR_J Admin

    Sorry, I haven't really followed this discussion. And have no idea what you are trying to do. This code you've posted can't work for several reasons:

    public function CommentModel_DeleteComment_Handler {
        if (!in_array(
            val(
                'DiscussionID',
                $Args['Comment']
            ),
            $ExcludeIDs)
        ) {
            Gdn::UserModel()->SetField(
                $UserID,
                'CustomCount',
                $Count - 1
            );
        }
    }
    

    Line 1: the syntax for a function is function () { }. You have no parameters.
    Line 5, 7, 10, 12: do you understand what a variable is? I don't think so. You will no be able to create a program without basic knowledge. What do you think are the values of the variables $Args, $ExcludeIDs, $UserID, $Count? And whatever you think - where in the program flow gets the script this "knowledge"? You cannot simply copy code without at least a little bit of understanding what is happening.

    Get the basics! If you have questions, don't hesitate to ask. But don't ask for help building a house when you have no idea what a brick is...

  • @R_J Regarding Line 1: ok I see that I'm missing the () parameters, along with any code I need inside of them.

    Regarding Lines 5,7,10 and 12: I do understand what I variable is. Without looking for an exact definition, my definition of a variable is this: something that stores/assigns a value to a word, so that this word can be referenced in the code and called upon the assigned value. I'm not sure what $Args is, but I have a fair understanding of the other 3. Since the values of these variables aren't shown in this plugin, and this plugin extends GDN_Plugin, I'm assuming this is where in the program flow gets the script this "knowledge".

    Since you haven't been following this discussion, you probably didn't see the full code on the previous page. Here is the whole script, with my embarrassing piece included at the bottom:

    <?php if (!defined('APPLICATION')) exit();

    $PluginInfo['DisablePostCount'] = array(
    'Name' => 'DisablePostCount',
    'Description' => 'Sets a new field in GDN_User called CustomCount, where DiscussionIDs can be configured so their posts won't be included',
    '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 DiscussionIDs 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(val('DiscussionID', $Args['Comment']), $ExcludeIDs)) {
    
        $UserID = val('InsertUserID', $Args['Comment']);
        $Count = Gdn::UserModel()->GetID($UserID)->CustomCount;
        Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count + 1);
    
        }
    
    }
    
    public function CommentModel_DeleteComment_Handler {
    

    if (!in_array(val('DiscussionID', $Args['Comment']), $ExcludeIDs)) {

    Gdn::UserModel()->SetField($UserID, 'CustomCount', $Count - 1);

        }
    
    }
    

    }

  • SkismaSkisma New
    edited April 2015

    @Bleistivt @hgtonight To finish this up I just need to make the plugin subtract 1 from 'CustomCount' any time a comment is deleted from any discussionID that is not "excluded". After reading RJ's comment I know that I am missing () parameters in my function. I probably need some content to put inside them as well.

  • R_JR_J Admin

    @Skisma said:
    I'm not sure what $Args is, but I have a fair understanding of the other 3. Since the values of these variables aren't shown in this plugin, and this plugin extends GDN_Plugin, I'm assuming this is where in the program flow gets the script this "knowledge".

    You do not know about the scope of a variable by now. When you do not see how a variable is defined, you can normally assume it has no value yet.

    The function isset() returns true if a variable has been declared. If you would use it in the variables in your CommentModel_DeleteComment_Handler, it would return false for every variable you are using there.

    I guess you've did that because you've misunderstood bleistivt and hgtonight. They showed you the direction, not the solution.


    But it is a good start that you have an understanding of what the variables should contain. Now you have to find out how to fill them with what you would expect them to be.

    1. $Args
      bleistivt and hgtonight write very clean code so you can expect that they stick to conventions. If they use conventions, you might be able to find $Args somewhere else. Try searching through some other plugins: https://github.com/vanilla/addons/search?utf8=✓&amp;q=$args
      Please, do not only copy what you are seeing there, but be sure you understand what you see. If not, create a new discussion for that or write me a PM or read more plugins or whatever. If you had no real idea by now, you should be sure to understand what is going on, before you go on.

    2. $ExcludeIDs
      You want to exclude some discussions from some counters, right? Have you thought about where the information which IDs should be ignored is stored? Where ever that is, the variable $ExcludeIDs has to be filled with that info before you use it.

    3. $UserID
      Don't know if you need to have the ID of the discussion author, the comment author or the current user - I would expect $UserID to be one of those. Whatever you need, look at other plugins how they filled that var.

    4. $Count
      From looking at that code I would assume you try to lower a value by 1 and $Count is the old value. So you would have to store the old value in there.

    Generally: whenever you see something is not working as you expect it, try to find examples in Vanillas code. By now I have around 100 plugins in my plugins folder and they are the most valuable reference for me. I do a fulltext search and get examples. You have the function SetField and you might need the value that is set there later on? try to find where "function setfield" is defined in the code and look around for a "function gefield". If it exists, you will see its syntax there. If it does not exist, look at the other functions around SetField. One of them will most probably fulfill the purpose that you would expect GetField should do. I honestly do not know by heart if "GeField" is the corresponding function to SetField, but I would suspect it simply from the terminology.

  • SkismaSkisma New
    edited April 2015

    Ok so I've been working with RJ on this and he's been kind enough to each me a lot about how functions work, etc. Just when I was about to be ready to call this done, I ran into a problem. Before I needed to add the CommentModel_DeleteComment_Handler this plugin was working perfectly. With the CommentModel_BeforeNotification_Handler function, comments posted not in DiscussionID 13 were increasing CustomCount by 1. The plugin was literally working exactly as it should at the time. Once I added the working function for CommentModel_DeleteComment_Handler, the CommentModel_BeforeNotification_Handler function stopped working completely. Currently, CustomCount decreases by 1 each time a comment not in DiscussionID 13 is deleted. But now CustomCount never increases, ever.

    RJ had me add the following to config.php:

    $Configuration['Garden']['Errors']['LogEnabled'] = TRUE;
    $Configuration['Garden']['Errors']['LogFile'] = 'log/DebugLog.txt';
    

    And update the now broken function to:

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

    I then retrieved the DebugLog.txt file and it has this:

    14 Apr 2015 - 20:09:22: [Garden] /home2/forgehav/public_html/forums/plugins/DisablePostCount/class.disablepostcount.plugin.php, 41, Object.Method(), $ExcludeIDs: Array
    14 Apr 2015 - 20:09:22: [Garden] /home2/forgehav/public_html/forums/plugins/DisablePostCount/class.disablepostcount.plugin.php, 44, Object.Method(), $UserID: 
    14 Apr 2015 - 20:09:22: [Garden] /home2/forgehav/public_html/forums/plugins/DisablePostCount/class.disablepostcount.plugin.php, 46, Object.Method(), $Count : 
    

    Can anyone assist me with this? The function was working before, so surely there isn't an error in the code. Maybe somehow the new function is overriding the original and it's just not being executed? Here's the whole script (before I added the LogMessage() lines):

    <?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 DiscussionIDs 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);
    
            }
        }
    
        public function CommentModel_DeleteComment_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);
    
            }
        }
    }
    
Sign In or Register to comment.