Wiki

Clone wiki

badger.yii / Define_badges

Create/Add/Define custom badges

To add new badge into module you have to do 4 steps

1. Insert new badge into database table

insert into badge(name,slug,"desc") VALUES('Custom label','custom-slug','custom description of this badge');

OR

<?php

$cmd = Yii::app()->db->createCommand();

$cmd->insert('badge',array(
      'name' => 'Custom label',
      'slug' => 'custom-slug',
      'desc' => 'custom description of this badge',
));

?>

2. Add constant to Badge class

Edit modules/badger/models/Badge.php:

<?php
class Badge extends BadgeParent
{
    // Sync manualy badge constants with database
    // Images must be as badge slug:
    //          modules/badge/assets/images/login-first.png

    # Login
    const BADGE_LOGIN_FIRST = 'login-first';

    # Custom
    const BADGE_CUSTOM_SLUG = 'custom-slug'; // <------------------- same as you inserted into "slug" column

//..

?>

3. Write this badge logic into Badge class

Keep editing modules/badger/models/Badge.php and find check function. Use previously defined constant BADGE_CUSTOM_SLUG.

<?php
//..

    public function check( $slug ) {

        switch( $slug ) {

                  # Login
                  # ---------------------------------------------------------------------
                  case self::BADGE_LOGIN_FIRST :
                        # do your specific check to award or not
                        // $canUserHaveBadge = (2 + 2) == 4;
                        // $this->success = $canUserhaveBadge;

                        # or just give him this badge (without checking anything)
                        $this->success = true;
                  break;

                  # Custom
                  # ---------------------------------------------------------------------
                  case self::BADGE_CUSTOM_SLUG :
                        // Check if MyModel count is reached 1000 (or more)
                        $this->success = MyModel::model()->my()->count() >= 1000;
                  break;

        }

    }

//..
?>

As you see in BADGE_LOGIN_FIRST cae, there is set directly as true. It's because you must run badge check only on successful user login and there is no more to check.

4. Draw image

Draw your own badge image. You can use template modules/badger/res/badges.svg (no copyrights). But you can draw any image (and size) as you like.

Updated