|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SET\Handlers\Duty; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Gate; |
|
8
|
|
|
use SET\Duty; |
|
9
|
|
|
use SET\DutySwap; |
|
10
|
|
|
|
|
11
|
|
|
class DutyGroups extends DutyHelper |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct(Duty $duty) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($duty); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function htmlOutput() |
|
19
|
|
|
{ |
|
20
|
|
|
$newCollection = new Collection(); |
|
21
|
|
|
|
|
22
|
|
|
foreach ($this->list as $entry) { |
|
23
|
|
|
$row = $this->buildHTMLUserRow($entry); |
|
24
|
|
|
|
|
25
|
|
|
$newCollection->push([ |
|
26
|
|
|
'row' => $row, |
|
27
|
|
|
'id' => $entry['id'], |
|
28
|
|
|
'date' => $entry['date'], |
|
29
|
|
|
]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $newCollection; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function emailOutput() |
|
36
|
|
|
{ |
|
37
|
|
|
$collection = $this->list->map(function ($value) { |
|
38
|
|
|
return [ |
|
39
|
|
|
'users' => $value['group'], |
|
40
|
|
|
'date' => $value['date'], |
|
41
|
|
|
]; |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
return $collection; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get function for the list. List is stored on the helper class. |
|
49
|
|
|
* |
|
50
|
|
|
* @return Collection |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getList() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->list; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function recordNextEntry() |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->list->count() < 2) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$nextGroupID = $this->list->toArray()[1]['id']; |
|
64
|
|
|
$this->duty->groups()->updateExistingPivot($nextGroupID, ['last_worked' => Carbon::today()]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getLastWorked() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->lastWorkedUser = $this->duty->groups()->orderBy('duty_group.last_worked', 'DESC')->first(); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function queryList() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->list = $this->duty->groups()->orderBy('name')->get(); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Take our list of groups and merge them with dates so that each group is assigned a duty date. |
|
83
|
|
|
* |
|
84
|
|
|
* @return DutyGroups |
|
85
|
|
|
*/ |
|
86
|
|
|
public function combineListWithDates() |
|
87
|
|
|
{ |
|
88
|
|
|
$dates = (new DutyDates($this->duty))->getDates(); |
|
89
|
|
|
$newDatesList = array_values(array_diff($dates, $this->swapDates->toArray())); |
|
90
|
|
|
$count = $this->list->count(); |
|
91
|
|
|
$dateCounter = 0; |
|
92
|
|
|
|
|
93
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
94
|
|
|
// Does list[i] already have date assigned? Is yes, skip assignment |
|
95
|
|
|
if (!empty($this->list[$i]['date'])) { |
|
96
|
|
|
continue; |
|
97
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
98
|
|
|
$this->list[$i] = [ |
|
99
|
|
|
'group' => $this->list[$i]['group'], |
|
100
|
|
|
'id' => $this->list[$i]['id'], |
|
101
|
|
|
'date' => $newDatesList[$dateCounter++], |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
$this->list = $this->list->sortBy('date'); |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Query a list of groups who we swapped around and insert them into our current duty list of groups. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function insertFromDutySwap() |
|
113
|
|
|
{ |
|
114
|
|
|
$dutySwaps = DutySwap::where('duty_id', $this->duty->id) |
|
115
|
|
|
->where('imageable_type', 'SET\Group') |
|
116
|
|
|
->where('date', '>=', Carbon::now()->subMonth()) //Omit really old records. |
|
117
|
|
|
->orderBy('date', 'ASC') |
|
118
|
|
|
->get(); |
|
119
|
|
|
|
|
120
|
|
|
$this->convertListToCollection(); |
|
121
|
|
|
$this->swapDates = new Collection(); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($dutySwaps as $swap) { |
|
124
|
|
|
$key = key($this->list->where('id', $swap->imageable_id)->toArray()); |
|
|
|
|
|
|
125
|
|
|
if (! is_null($key)) { |
|
126
|
|
|
$this->swapDates->push($swap->date); |
|
127
|
|
|
$this->list[$key] = [ |
|
128
|
|
|
'group' => $swap->imageable()->first()->users()->active()->get(), |
|
129
|
|
|
'id' => $swap->imageable()->first()->id, |
|
130
|
|
|
'date' => $swap->date, |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Convert the list of groups into a collection of group users, group id and date |
|
139
|
|
|
*/ |
|
140
|
|
|
private function convertListToCollection() |
|
141
|
|
|
{ |
|
142
|
|
|
$count = $this->list->count(); |
|
|
|
|
|
|
143
|
|
|
$newList = new Collection(); |
|
144
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
145
|
|
|
$newList->push([ |
|
146
|
|
|
'group' => $this->list[$i]->users()->active()->get(), |
|
|
|
|
|
|
147
|
|
|
'id' => $this->list[$i]->id, |
|
148
|
|
|
'date' => '', |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
$this->list = $newList; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $entry |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
private function buildHTMLUserRow($entry) |
|
160
|
|
|
{ |
|
161
|
|
|
$row = ''; |
|
162
|
|
|
foreach ($entry['group'] as $user) { |
|
163
|
|
|
if (Gate::allows('view')) { |
|
164
|
|
|
$row .= "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a> & '; |
|
165
|
|
|
} else { |
|
166
|
|
|
$row .= $user->userFullName.' & '; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
$row = rtrim($row, '& '); |
|
170
|
|
|
|
|
171
|
|
|
return $row; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|