You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+54
@@ -0,0 +1,54 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user