2018-03-17 12:59:53 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Interfaces;
|
|
|
|
|
2018-03-18 00:35:34 +08:00
|
|
|
use App\Facades\Utils;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Models\Award as AwardModel;
|
2018-03-17 12:59:53 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\UserAward;
|
2018-03-20 09:50:40 +08:00
|
|
|
use Log;
|
2018-03-17 12:59:53 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* Base class for the Awards, you need to extend this, and implement:
|
|
|
|
* $name
|
|
|
|
* $param_description (optional)
|
|
|
|
* public function check($parameter=null)
|
|
|
|
*
|
|
|
|
* See: http://docs.phpvms.net/customizing/awards
|
2018-03-17 12:59:53 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
abstract class Award
|
2018-03-17 12:59:53 +08:00
|
|
|
{
|
|
|
|
public $name = '';
|
2018-03-18 00:35:34 +08:00
|
|
|
public $param_description = '';
|
2018-03-17 12:59:53 +08:00
|
|
|
|
2018-03-17 13:18:03 +08:00
|
|
|
/**
|
2018-03-18 00:39:51 +08:00
|
|
|
* Each award class just needs to return true or false if it should actually
|
|
|
|
* be awarded to a user. This is the only method that needs to be implemented
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-18 07:19:05 +08:00
|
|
|
* @param null $parameter Optional parameters that are passed in from the UI
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-17 13:55:39 +08:00
|
|
|
* @return bool
|
2018-03-17 13:18:03 +08:00
|
|
|
*/
|
2018-03-18 07:19:05 +08:00
|
|
|
abstract public function check($parameter = null): bool;
|
2018-03-17 13:18:03 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/*
|
|
|
|
* You don't really need to mess with anything below here
|
|
|
|
*/
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $award;
|
|
|
|
protected $user;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-03-17 12:59:53 +08:00
|
|
|
/**
|
|
|
|
* AwardInterface constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param AwardModel $award
|
|
|
|
* @param User $user
|
2018-03-17 12:59:53 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function __construct(AwardModel $award = null, User $user = null)
|
2018-03-17 12:59:53 +08:00
|
|
|
{
|
|
|
|
$this->award = $award;
|
2018-03-17 13:18:03 +08:00
|
|
|
$this->user = $user;
|
2018-03-17 12:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:55:39 +08:00
|
|
|
/**
|
|
|
|
* Run the main handler for this award class to determine if
|
2018-03-20 09:50:40 +08:00
|
|
|
* it should be awarded or not. Declared as final to prevent a child
|
|
|
|
* from accidentally overriding and breaking something
|
2018-03-17 13:55:39 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
final public function handle(): void
|
2018-03-17 13:55:39 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the params are a JSON object or array
|
2018-04-02 03:32:01 +08:00
|
|
|
$param = $this->award->ref_model_params;
|
|
|
|
if (Utils::isObject($this->award->ref_model_params)) {
|
|
|
|
$param = json_decode($this->award->ref_model_params);
|
2018-03-18 00:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->check($param)) {
|
2018-03-17 13:55:39 +08:00
|
|
|
$this->addAward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 12:59:53 +08:00
|
|
|
/**
|
|
|
|
* Add the award to this user, if they don't already have it
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-17 13:18:03 +08:00
|
|
|
* @return bool|UserAward
|
2018-03-17 12:59:53 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
final protected function addAward()
|
2018-03-17 12:59:53 +08:00
|
|
|
{
|
|
|
|
$w = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'award_id' => $this->award->id,
|
2018-03-17 12:59:53 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$found = UserAward::where($w)->count('id');
|
2018-03-17 13:18:03 +08:00
|
|
|
if ($found > 0) {
|
2018-03-17 12:59:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Associate this award to the user now
|
|
|
|
$award = new UserAward($w);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$award->save();
|
2018-08-27 00:40:04 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::error(
|
|
|
|
'Error saving award: '.$e->getMessage(),
|
|
|
|
$e->getTrace()
|
|
|
|
);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
return;
|
2018-03-20 09:50:40 +08:00
|
|
|
}
|
2018-03-17 13:18:03 +08:00
|
|
|
|
|
|
|
return $award;
|
2018-03-17 12:59:53 +08:00
|
|
|
}
|
|
|
|
}
|