qcod / laravel-gamify

Gamify your Laravel app with Reputation Points & Achievements Badges support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Award a badge to a user on demand

hosseincode opened this issue · comments

I would like to award a specific badge to a user inside my listener that listens to an event. Is there a method similar to givePoint() that i can use for this purpose?

For example assume i have a badge called AvatarUploadedBadge and i want to award it to the authenticated user first time they upload an avatar. Is there a method i can use inside my listener that listens to the AvatarWasUploadedEvent?

Try this in your qualifier method

/**
     * Check is user qualifies for badge
     *
     * @param $user
     * @return bool
     */
    public function qualifier($user)
    {
        return !is_null($user->avatar);
    }

And you can call the syncBadges() method to assign the badge when you upload the avatar.

// upload avatar
$user->avatar = $uploadedAvatarPath;
$user->save();

// in Listener AvatarWasUploadedEvent
$user->syncBadges();