You've already forked Atomcms-edit
55 lines
1.2 KiB
PHP
Executable File
55 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class UserChallengeProgress extends Model
|
|
{
|
|
#[\Override]
|
|
protected $table = 'user_challenge_progress';
|
|
|
|
#[\Override]
|
|
protected $fillable = ['user_id', 'challenge_id', 'progress', 'completed', 'claimed', 'completed_at'];
|
|
|
|
#[\Override]
|
|
protected $casts = [
|
|
'progress' => 'integer',
|
|
'completed' => 'boolean',
|
|
'claimed' => 'boolean',
|
|
'completed_at' => 'datetime',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function challenge(): BelongsTo
|
|
{
|
|
return $this->belongsTo(DailyChallenge::class, 'challenge_id');
|
|
}
|
|
|
|
public function addProgress(int $amount): bool
|
|
{
|
|
if ($this->completed) {
|
|
return false;
|
|
}
|
|
|
|
$this->increment('progress', $amount);
|
|
$this->refresh();
|
|
|
|
if ($this->progress >= $this->challenge->target && ! $this->completed) {
|
|
$this->update([
|
|
'completed' => true,
|
|
'completed_at' => now(),
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|