-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSPAwardsMembership.php
More file actions
58 lines (45 loc) · 1.85 KB
/
SPAwardsMembership.php
File metadata and controls
58 lines (45 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Modules\Awards\Awards;
use App\Contracts\Award;
use App\Models\UserAward;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class SPAwardsMembership extends Award
{
public $name = 'SPAwards(Membership)';
public $param_description = 'The days of membership at which to give this award';
public function check($days = null): bool
{
// Ensure that the number of days parameter is provided
if (is_null($days)) {
Log::error('SPAwards(Membership) | No parameter set.');
return false;
}
// Check if the award is already granted
$award = \App\Models\Award::where('ref_model', get_class($this))
->where('ref_model_params', (string) $days)
->first();
if (!$award) {
Log::error("SPAwards(Membership) | No matching award found.");
return false;
}
$alreadyGranted = UserAward::where('user_id', $this->user->id)
->where('award_id', $award->id)
->exists();
if ($alreadyGranted) {
Log::info("SPAwards(Membership) | Award already granted to Pilot (ID: {$this->user->id}). Skipping...");
return false;
}
// Ensure the user has a valid creation date
if (is_null($this->user->created_at)) {
Log::warning('SPAwards(Membership) | User creation date is missing.');
return false;
}
// Calculate how many days the user has been a member
$membershipDays = $this->user->created_at->diffInDays(Carbon::today());
// Log for debugging
Log::info("SPAwards(Membership) | Pilot (ID: {$this->user->id}) has {$membershipDays} days, {$days} days needed.");
// Check if the membership duration meets or exceeds the required threshold
return $membershipDays >= $days;
}
}