1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Meeting; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Siak\Tontine\Model\Member; |
7
|
|
|
use Siak\Tontine\Model\Round; |
8
|
|
|
use Siak\Tontine\Model\Session; |
9
|
|
|
use Siak\Tontine\Service\TenantService; |
10
|
|
|
use Siak\Tontine\Service\Tontine\MemberService; |
11
|
|
|
|
12
|
|
|
class PresenceService |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param TenantService $tenantService |
16
|
|
|
*/ |
17
|
|
|
public function __construct(private TenantService $tenantService, |
18
|
|
|
private MemberService $memberService, private SessionService $sessionService) |
19
|
|
|
{} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Round $round |
23
|
|
|
* |
24
|
|
|
* @return Collection |
25
|
|
|
*/ |
26
|
|
|
public function getRoundAbsences(Round $round): Collection |
27
|
|
|
{ |
28
|
|
|
return Member::select('members.id', 'members.name', 'absences.session_id') |
|
|
|
|
29
|
|
|
->join('absences', 'absences.member_id', '=', 'members.id') |
30
|
|
|
->join('sessions', 'absences.session_id', '=', 'sessions.id') |
31
|
|
|
->where('sessions.round_id', $round->id) |
32
|
|
|
->get() |
33
|
|
|
// Group the data (member name) by session id and member id. |
34
|
|
|
->groupBy('session_id') |
35
|
|
|
->map(fn($members) => $members->groupBy('id') |
36
|
|
|
->map(fn($_members) => $_members->first()->name)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Session $session |
41
|
|
|
* |
42
|
|
|
* @return Collection |
43
|
|
|
*/ |
44
|
|
|
public function getSessionAbsences(Session $session): Collection |
45
|
|
|
{ |
46
|
|
|
return $session->absents()->pluck('name', 'id'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Member $member |
51
|
|
|
* |
52
|
|
|
* @return Collection |
53
|
|
|
*/ |
54
|
|
|
public function getMemberAbsences(Member $member): Collection |
55
|
|
|
{ |
56
|
|
|
$round = $this->tenantService->round(); |
57
|
|
|
return $member->absences()->where('round_id', $round->id)->pluck('title', 'id'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Session $session |
62
|
|
|
* @param Member $member |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function togglePresence(Session $session, Member $member) |
67
|
|
|
{ |
68
|
|
|
!$session->absents()->find($member->id) ? |
69
|
|
|
$session->absents()->attach($member->id) : |
70
|
|
|
$session->absents()->detach($member->id); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the number of sessions in the selected round. |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function getSessionCount(): int |
79
|
|
|
{ |
80
|
|
|
return $this->sessionService->active()->getSessionCount(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get a paginated list of sessions in the selected round. |
85
|
|
|
* |
86
|
|
|
* @param int $page |
87
|
|
|
* |
88
|
|
|
* @return Collection |
89
|
|
|
*/ |
90
|
|
|
public function getSessions(int $page = 0): Collection |
91
|
|
|
{ |
92
|
|
|
return $this->sessionService->active() |
93
|
|
|
->withCount(['absents']) |
94
|
|
|
->getSessions($page, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Find a session. |
99
|
|
|
* |
100
|
|
|
* @param int $sessionId |
101
|
|
|
* |
102
|
|
|
* @return Session|null |
103
|
|
|
*/ |
104
|
|
|
public function getSession(int $sessionId): ?Session |
105
|
|
|
{ |
106
|
|
|
return $this->sessionService->active() |
107
|
|
|
->withCount(['absents']) |
108
|
|
|
->getSession($sessionId); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the number of members. |
113
|
|
|
* |
114
|
|
|
* @param string $search |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function getMemberCount(string $search = ''): int |
119
|
|
|
{ |
120
|
|
|
return $this->memberService->active()->getMemberCount($search); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get a paginated list of members. |
125
|
|
|
* |
126
|
|
|
* @param string $search |
127
|
|
|
* @param int $page |
128
|
|
|
* |
129
|
|
|
* @return Collection |
130
|
|
|
*/ |
131
|
|
|
public function getMembers(string $search, int $page = 0): Collection |
132
|
|
|
{ |
133
|
|
|
$round = $this->tenantService->round(); |
134
|
|
|
return $this->memberService->active() |
135
|
|
|
->withCount(['absences' => fn($query) => $query->where('round_id', $round->id)]) |
136
|
|
|
->getMembers($search, $page); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Find a member. |
141
|
|
|
* |
142
|
|
|
* @param int $memberId |
143
|
|
|
* |
144
|
|
|
* @return Member|null |
145
|
|
|
*/ |
146
|
|
|
public function getMember(int $memberId): ?Member |
147
|
|
|
{ |
148
|
|
|
$round = $this->tenantService->round(); |
149
|
|
|
return $this->memberService->active() |
150
|
|
|
->withCount(['absences' => fn($query) => $query->where('round_id', $round->id)]) |
151
|
|
|
->getMember($memberId); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|