Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New way of badge added #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ class FirstContribution extends BadgeType
* @param $user
* @return bool
*/
public function qualifier($user)
public function islevelArchived($user)
{
return $user->getPoints() >= 1000;
}
}
```

As you can see this badge has a `$description` field and a `qualifier($user)` method.
As you can see this badge has a `$description` field and a `islevelArchived($user)` method.
Gamify package will listen for any change in reputation point and it will run the user against all the available badges and assign all the badges user is qualified.

#### Change badge name
Expand Down
8 changes: 7 additions & 1 deletion src/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace QCod\Gamify;

use Illuminate\Database\Eloquent\Model;
use QCod\Gamify\Events\BadgeGivenEvent;
use QCod\Gamify\Events\BadgeRemovedEvent;

class Badge extends Model
{
Expand All @@ -24,7 +26,9 @@ public function users()
*/
public function awardTo($user)
{
$this->users()->attach($user);
$event = config('gamify.events.badgeGiven', 'QCod\Gamify\Events\BadgeGivenEvent');
event(new $event($this, $user));
$this->users()->syncWithoutDetaching($user);
}

/**
Expand All @@ -34,6 +38,8 @@ public function awardTo($user)
*/
public function removeFrom($user)
{
$event = config('gamify.events.badgeRemoved', 'QCod\Gamify\Events\BadgeGivenEvent');
event(new $event($this, $user));
$this->users()->detach($user);
}
}
55 changes: 44 additions & 11 deletions src/BadgeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

abstract class BadgeType
{
Expand All @@ -13,11 +14,18 @@ abstract class BadgeType
*/
protected $model;


/**
* @var Array
*/
public $_extraVariablesForQualify;

/**
* BadgeType constructor.
*/
public function __construct()
public function __construct(...$extraVariablesForQualify)
{
$this->_extraVariablesForQualify = $extraVariablesForQualify;
$this->model = $this->storeBadge();
}

Expand All @@ -27,7 +35,7 @@ public function __construct()
* @param $user
* @return bool
*/
abstract public function qualifier($user);
abstract public function islevelArchived($user);

/**
* Get name of badge
Expand Down Expand Up @@ -65,6 +73,20 @@ public function getIcon()
: $this->getDefaultIcon();
}


/**
* Get the model for badge
*
* @return Model
*/
public function getModel($level = null)
{
if ($level) {
return $this->model[$level];
}
return $this->model;
}

/**
* Get the level for badge
*
Expand Down Expand Up @@ -92,9 +114,12 @@ public function getLevel()
*
* @return mixed
*/
public function getBadgeId()
public function getBadgeId($level = false)
{
return $this->model->getKey();
if (!$level) {
$level = $this->getLevel();
}
return $this->model[$level]->getKey();
}

/**
Expand Down Expand Up @@ -129,16 +154,24 @@ protected function getDefaultIcon()
*/
protected function storeBadge()
{
$badge = app(config('gamify.badge_model'))
->firstOrNew(['name' => $this->getName()])
$badgeName = get_class($this);

return cache()->tags('laravel-gamify')->rememberForever('gamify.badges.' . $badgeName, function () {
$levels = (property_exists($this, 'levels')) ? $this->levels : [$this->getLevel() => $this->getDescription()];
$return = [];
foreach ($levels as $levelKey => $levelDescription) {
$badge = app(config('gamify.badge_model'))
->firstOrNew(['name' => $this->getName(), 'level' => $levelKey])
->forceFill([
'level' => $this->getLevel(),
'description' => $this->getDescription(),
'level' => $levelKey,
'description' => $levelDescription,
'icon' => $this->getIcon()
]);

$badge->save();

return $badge;
$badge->save();
$return[$levelKey] = $badge;
}
return $return;
});
}
}
2 changes: 1 addition & 1 deletion src/Console/stubs/badge.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DummyClass extends BadgeType
* @param $user
* @return bool
*/
public function qualifier($user)
public function islevelArchived($user)
{
return $user->getPoints() >= 1000;
}
Expand Down
55 changes: 55 additions & 0 deletions src/Events/BadgeGivenEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace QCod\Gamify\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;

class BadgeGivenEvent implements ShouldBroadcast
{
use Dispatchable, SerializesModels;

/**
* @var User
*/
public $user;

/**
* @var Model
*/
public $badge;

/**
* Create a new event instance.
*
* @param $user
* @param $point integer
* @param $increment
*/
public function __construct(Model $badge, User $user)
{
$this->user = $user;
$this->badge = $badge;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]
*/
public function broadcastOn()
{
$channelName = config('gamify.channel_name') . $this->user->getKey();

if (config('gamify.broadcast_on_private_channel')) {
return new PrivateChannel($channelName);
}

return new Channel($channelName);
}
}
55 changes: 55 additions & 0 deletions src/Events/BadgeRemovedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace QCod\Gamify\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;

class BadgeRemovedEvent implements ShouldBroadcast
{
use Dispatchable, SerializesModels;

/**
* @var User
*/
public $user;

/**
* @var Model
*/
public $badge;

/**
* Create a new event instance.
*
* @param $user
* @param $point integer
* @param $increment
*/
public function __construct(Model $badge, User $user)
{
$this->user = $user;
$this->badge = $badge;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]
*/
public function broadcastOn()
{
$channelName = config('gamify.channel_name') . $this->user->getKey();

if (config('gamify.broadcast_on_private_channel')) {
return new PrivateChannel($channelName);
}

return new Channel($channelName);
}
}
14 changes: 5 additions & 9 deletions src/GamifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use QCod\Gamify\Listeners\SyncBadges;
use Illuminate\Support\ServiceProvider;
use QCod\Gamify\Console\MakeBadgeCommand;
use QCod\Gamify\Console\MakePointCommand;
Expand Down Expand Up @@ -42,9 +41,6 @@ public function boot()
MakeBadgeCommand::class,
]);
}

// register event listener
Event::listen(ReputationChanged::class, SyncBadges::class);
}

/**
Expand All @@ -55,11 +51,11 @@ public function boot()
public function register()
{
$this->app->singleton('badges', function () {
return cache()->rememberForever('gamify.badges.all', function () {
return $this->getBadges()->map(function ($badge) {
return new $badge;
});
//return cache()->rememberForever('gamify.badges.all', function () {
return $this->getBadges()->map(function ($badge) {
return new $badge;
});
//});
});
}

Expand All @@ -79,7 +75,7 @@ protected function getBadges()

foreach (glob(app_path('/Gamify/Badges/') . '*.php') as $file) {
if (is_file($file)) {
$badges[] = app($badgeRootNamespace . '\\' . pathinfo($file, PATHINFO_FILENAME));
$badges[] = $badgeRootNamespace . '\\' . pathinfo($file, PATHINFO_FILENAME); // this was cousing double construct becouse of singleton construct again.
}
}

Expand Down
37 changes: 27 additions & 10 deletions src/HasBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,35 @@ public function badges()
}

/**
* Sync badges for qiven user
*
* @param $user
*/
public function syncBadges($user = null)
* Give badge to user
*
* @param BadgeType $badge
* @param $preDefinedBadgeLevel int
* @return bool
*/
public function giveBadge(BadgeType $badge, $forceLevel = null)
{
$user = is_null($user) ? $this : $user;
if (!is_null($forceLevel)) {
return $badge->getModel($forceLevel)->awardTo($this);
}

$badgeIds = app('badges')->filter
->qualifier($user)
->map->getBadgeId();

$user->badges()->sync($badgeIds);
if (!($level = $badge->islevelArchived($this, $badge->_extraVariablesForQualify))) {
return false;
}
if (!is_numeric($level)) {
$level = config('gamify.badge_default_level', 1);
}

return $badge->getModel($level)->awardTo($this);
}

public function removeBadge(BadgeType $badge)
{
if (!($level = $badge->islevelArchived($this, $badge->_extraVariablesForQualify))) {
return false;
}

return $this->model[$level]->removeBadge($this);
}
}
19 changes: 0 additions & 19 deletions src/Listeners/SyncBadges.php

This file was deleted.

Loading