1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Planning; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Siak\Tontine\Model\Guild; |
8
|
|
|
use Siak\Tontine\Model\Round; |
9
|
|
|
use Siak\Tontine\Model\Session; |
10
|
|
|
|
11
|
|
|
trait SessionTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private bool $filterActive = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param bool $filter |
20
|
|
|
* |
21
|
|
|
* @return self |
22
|
|
|
*/ |
23
|
|
|
public function active(bool $filter = true): self |
24
|
|
|
{ |
25
|
|
|
$this->filterActive = $filter; |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Guild $guild |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function getGuildSessionCount(Guild $guild): int |
35
|
|
|
{ |
36
|
|
|
return $guild->sessions() |
37
|
|
|
->when($this->filterActive, fn(Builder $query) => $query->active()) |
38
|
|
|
->count(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Guild $guild |
43
|
|
|
* @param int $page |
44
|
|
|
* @param bool $orderAsc |
45
|
|
|
* |
46
|
|
|
* @return Collection |
47
|
|
|
*/ |
48
|
|
|
public function getGuildSessions(Guild $guild, int $page = 0, bool $orderAsc = true): Collection |
49
|
|
|
{ |
50
|
|
|
return $guild->sessions() |
51
|
|
|
->when($this->filterActive, fn(Builder $query) => $query->active()) |
52
|
|
|
->orderBy('day_date', $orderAsc ? 'asc' : 'desc') |
53
|
|
|
->page($page, $this->tenantService->getLimit()) |
54
|
|
|
->get(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Guild $guild |
59
|
|
|
* @param int $sessionId The session id |
60
|
|
|
* |
61
|
|
|
* @return Session|null |
62
|
|
|
*/ |
63
|
|
|
public function getGuildSession(Guild $guild, int $sessionId): ?Session |
64
|
|
|
{ |
65
|
|
|
return $guild->sessions() |
66
|
|
|
->when($this->filterActive, fn(Builder $query) => $query->active()) |
67
|
|
|
->find($sessionId); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Guild $guild |
72
|
|
|
* @param Session $currSession |
73
|
|
|
* @param bool $getAfter Get the sessions after or before the provided one |
74
|
|
|
* @param bool $withCurr Keep the provided session in the list |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function getSessionCount(Guild $guild, Session $currSession, |
79
|
|
|
bool $getAfter = false, bool $withCurr = true): int |
80
|
|
|
{ |
81
|
|
|
$operator = $getAfter ? ($withCurr ? '>=' : '>') : ($withCurr ? '<=' : '<'); |
82
|
|
|
return $guild->sessions() |
83
|
|
|
->where('day_date', $operator, $currSession->day_date)->count(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find the prev session. |
88
|
|
|
* |
89
|
|
|
* @param Round $round |
90
|
|
|
* @param Session $session |
91
|
|
|
* |
92
|
|
|
* @return Session|null |
93
|
|
|
*/ |
94
|
|
|
private function getPrevSession(Round $round, Session $session): ?Session |
95
|
|
|
{ |
96
|
|
|
return $round->sessions() |
97
|
|
|
->where('day_date', '<', $session->day_date) |
98
|
|
|
->orderBy('day_date', 'desc') |
99
|
|
|
->first(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Find the next session. |
104
|
|
|
* |
105
|
|
|
* @param Round $round |
106
|
|
|
* @param Session $session |
107
|
|
|
* |
108
|
|
|
* @return Session|null |
109
|
|
|
*/ |
110
|
|
|
private function getNextSession(Round $round, Session $session): ?Session |
111
|
|
|
{ |
112
|
|
|
return $round->sessions() |
113
|
|
|
->where('day_date', '>', $session->day_date) |
114
|
|
|
->orderBy('day_date', 'asc') |
115
|
|
|
->first(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|