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

  1. Features
  2. Requirements
  3. Installation
  4. Resources
  5. Feedback

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

[sh]
$ 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) ~~~ [sh] $ 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
[sql]

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_LOGINFIRST 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

4 0
11 followers
1 156 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: briiC.lv
Created on: May 7, 2012
Last updated: 5 years ago

Downloads

show all

Related Extensions