1. Go to this page and download the library: Download qcod/laravel-gamify library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use QCod\Gamify\Gamify;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable, Gamify;
namespace App\Gamify\Points;
use QCod\Gamify\PointType;
class PostCreated extends PointType
{
/**
* Number of points
*
* @var int
*/
public $points = 20;
/**
* Point constructor
*
* @param $subject
*/
public function __construct($subject)
{
$this->subject = $subject;
}
/**
* User who will be receive points
*
* @return mixed
*/
public function payee()
{
return $this->getSubject()->user;
}
}
$user = $request->user();
$post = $user->posts()->create($request->only(['title', 'body']));
// you can use helper function
givePoint(new PostCreated($post));
// or via HasReputation trait method
$user->givePoint(new PostCreated($post));
// via helper function
undoPoint(new PostCreated($post));
$post->delete();
// or via HasReputation trait method
$user->undoPoint(new PostCreated($post));
$post->delete();
// get integer point
$user->getPoints(); // 20
// formatted result
$user->getPoints(true); // if point is more than 1000 1K+
foreach($user->reputations as $reputation) {
// name of the point type
$reputation->name
// payee user
$reputation->payee
// how many points
$reputation->point
// model on which point was given
$reputation->subject
}
/**
* Get all the post's reputation.
*/
public function reputations()
{
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
}
class PostCreated extends PointType
{
public $points = 20;
protected $payee = 'user';
// dont need this, payee property will return subject realtion
// public function payee()
// {
// return $this->getSubject()->user;
// }
}
class PostCreated extends PointType
{
protected $payee = 'user';
public function getPoints()
{
return $this->getSubject()->user->getPoint() * 10;
}
}
class PostCreated extends PointType
{
// prevent duplicate point
public $allowDuplicates = false;
protected $payee = 'user';
}
class ReputationChanged implements ShouldBroadcast {
...
public function __construct(Model $user, int $point, bool $increment)
{
$this->user = $user;
$this->point = $point;
$this->increment = $increment;
}
}
// All the levels for badge
'badge_levels' => [
'beginner' => 1,
'intermediate' => 2,
'advanced' => 3,
],
// Default level
'badge_default_level' => 1
namespace App\Gamify\Badges;
use QCod\Gamify\BadgeType;
class FirstContribution extends BadgeType
{
/**
* Description for badge
*
* @var string
*/
protected $description = '';
/**
* Check is user qualifies for badge
*
* @param $user
* @return bool
*/
public function qualifier($user)
{
return $user->getPoints() >= 1000;
}
}
// to add point
$user->addPoint($point = 1);
// to reduce point
$user->reducePoint($point = 1);
// to reset point back to zero
$user->resetPoint();
return [
// Model which will be having points, generally it will be User
'payee_model' => '\App\User',
// Reputation model
'reputation_model' => '\QCod\Gamify\Reputation',
// Allow duplicate reputation points
'allow_reputation_duplicate' => true,
// Broadcast on private channel
'broadcast_on_private_channel' => true,
// Channel name prefix, user id will be suffixed
'channel_name' => 'user.reputation.',
// Badge model
'badge_model' => '\QCod\Gamify\Badge',
// Where all badges icon stored
'badge_icon_folder' => 'images/badges/',
// Extention of badge icons
'badge_icon_extension' => '.svg',
// All the levels for badge
'badge_levels' => [
'beginner' => 1,
'intermediate' => 2,
'advanced' => 3,
],
// Default level
'badge_default_level' => 1
];