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.

Your Custom Badges?

I'm interested to see what others have done with this plugin for their forum's custom badges and also so I can see some examples of how people have done it to learn to code some myself. The default badges are an incredible start, but I can see wanting to give badges for all kinds of other stuff.

Is anyone interested in sharing their custom badge code and explain what it does / how you use it on your forum?

Comments

  • A very simple one:

    <?php
    
    class Charter implements YagaRule {
    
        public function award($sender, $user, $criteria) {
            return $user->UserID <= $criteria->Target;
        }
    
        public function form($form) {
            return $form->label('User ID smaller or equal', 'Charter')
                .$form->textbox('Target', ['class' => 'SmallInput']);
        }
    
        public function validate($criteria, $form) {
            $validation = new Gdn_Validation();
            $validation->applyRule('Target', ['Required', 'Integer']);
            $validation->validate($criteria);
            $form->setValidationResults($validation->results());
        }
    
        public function hooks() { return ['Gdn_Dispatcher_AppStartup']; }
    
        public function description() {
            $description = t('This badge will be given to all users with a UserID below the defined threshold.');
            return wrap($description, 'div', ['class' => 'InfoMessage']);
        }
    
        public function name() { return t('Charter Member'); }
    
        public function interacts() { return false; }
    
    }
    

    Since I migrated from @peregrine 's badges plugin which had a charter member badge (given to the first 100 users), I had to recreate it.

  • Awesome! Thanks for sharing that one!

  • R_JR_J Ex-Fanboy Munich Admin

    A little bit offtopc, but could you explain return $user->UserID <= $criteria->Target;, @Bleistivt?

  • @R_J said:
    A little bit offtopc, but could you explain return $user->UserID <= $criteria->Target;, Bleistivt?

    Yes, the settings (criteria) for the badge are defined by the form in the form() function of the YagaRule object. The inputs are validated in validate() once you submit the form and then serialized in an object.

    That object is always passed to the award function where you can check if the criteria defined in the form are met by the user.

    In the example above the UserID where a user stops being a "charter" member is configurable (it was 100 in the original badge) in the "Target" field.

  • R_JR_J Ex-Fanboy Munich Admin

    Shit, you already answered... It's late at night and it took me way too much time to see that the <= is a simple logical operator. I was thinking of some complicated language construct like an inverted associative array... :blush:

  • peregrineperegrine MVP
    edited August 2015

    On a related plugin. I had custom badges for red herrings and vampire bats that were given to certain individuals. Rules were subjective.

    badge design by vrijvlinder

    given to individuals that provided these

    https://en.wikipedia.org/wiki/Red_herring
    

    also had a vampire bat badge (you can guess the reason or choose your own).

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

  • Yeah, I've been considering a "You Know Nothing" badge with Jon Snow on it as a subjective badge like that. =)

  • Has anyone implemented a badge that grants automatically when a post reaches a certain number of reactions? Like, "Get 5 Awesomes on a comment".

  • hgtonighthgtonight ∞ · New Moderator

    @PFAFF said:
    Has anyone implemented a badge that grants automatically when a post reaches a certain number of reactions? Like, "Get 5 Awesomes on a comment".

    Check out the included 'post reactions' 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.

  • Oh, perfect!

Sign In or Register to comment.