Yii 1.1: badger

Module. With small configuration adds badges (gamification logic) to your app. Custom badge giving logic and custom images.
11 followers

Features

  • Different database platforms: MySQL, PostgreSQL, SQLite..
  • Assign badges to custom user table (default is "user" but it can be changed)
  • Using custom layout (_default: layouts/main_)
  • Add your own logic how specific badge is given to user. Your conditions.
  • Given default style for badges, but it's 100% customizable
  • Create your own view files (it will not be overwritten on next version)
  • Cache badges (listed from db)

Requirements

  • PHP 5
  • tested with Yii 1.1.10
  • MySQL, PostgreSQL, SQLite.. (used Yii functions to create tables and insert data)

Installation

Copy contents into protected/modules/badger/ (so it comes as protected/modules/badger/).

OR clone it with mercurial

$ cd protected/modules
$ hg clone https://briiC@bitbucket.org/briiC/badger.yii badger

Permissions

Module creates new files so you need to give permissions (only for installing part. After that you can set to default permissions)

$ cd protected/modules/
$ sudo chmod -R 0775 badger/
$ sudo chown -R myuser:www-data badger/

Configuration

protected/config/main.php

//..
      'import'=>array(
              //..
              // Badger
              'application.modules.badger.models.*',
              //..
      ),
 
      //..
      'modules'=> array(
 
            'badger' => array(
                  //'layout' => '//layouts/mainx', //default: "//layouts/main"
                  //'userTable' => 'userx', // default: "user"
                  'cacheSec' => 3600 * 24, // cache duration. default: 3600
 
                  // Creates tables and copy necessary files
                  'install' => true, // remove/comment after succesful install
                   // drop all badger tables before installing (fresh install)
                  'dropBeforeInstall' => false, 
            ),
            //..
      ),
//..

There is two params that better must be removed or commented after you successfully run badge module.

  • install - Created tables (two: badge and _user_badge)
  • dropBeforeInstall - Tries to drop badger tables before installing

Give badge to user

$Badge = new Badge;
//$Badge->onSuccess = array( $object, 'notifyUser'); // add custom event after giving badge to user
$Badge->checkAndGiveGroup( 'Login' );

More about events

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

$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:

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.

//..
 
    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.

You can find more information on Badger page

Resources

Feedback

Please give feedback about installation/configuration process. Which part is not so easy to understand I will try get it more clear for everybody.

Also send bugs, ideas here or at https://bitbucket.org/briiC/badger.yii/issues/new

Total 5 comments

#16040 report it
briiC.lv at 2014/01/14 04:07am
Release v2.0
  • Fixed bug where you couldn't give badge to another user.
  • Added user_id param in $event->params after successful giving badge to user (it useful in onSuccess event)
#8157 report it
briiC.lv at 2012/05/13 03:36pm
Release v1.1
  • bugfix: Error while using badger module in other module.

If you are going to upgrade from previous version please read "Upgrade (bader BitBucket wiki)" to know what expect.

#8129 report it
briiC.lv at 2012/05/12 05:05am
Release v1.0

No beta anymore:

  • bugfix: Function user() doesn't exists. Use Yii::app()->user instead (thanks to Drini)

If you are going to upgrade from beta version, please read "Upgrade (bader BitBucket wiki)" to know what expect.

#8114 report it
briiC.lv at 2012/05/11 07:22am
@Drini, thanks for your feedback

oh forgot to change user() function. I got some shortcut functions and most of them i changed. Forgot user() :) I will fix it in holidays and upload upgrade.

thanks again!

#8113 report it
Sampa at 2012/05/11 06:59am
Doh

I tested this a couple days ago, and wrote a long comment which is must have forgot to submit,lol.

But I had no major problems during installation but there was a bunch of small errors, like user() instead of Yii::app()->user->id. the other errors I cant remember now.

But it seems to work for me:)

site.domain/badger is not suppossed to work,right?:p

Leave a comment

Please to leave your comment.

Create extension