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
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Query a list of groups who we swapped around and insert them into our current duty list of groups. |
112
|
|
|
*/ |
113
|
|
|
public function insertFromDutySwap() |
114
|
|
|
{ |
115
|
|
|
$dutySwaps = DutySwap::where('duty_id', $this->duty->id) |
116
|
|
|
->where('imageable_type', 'SET\Group') |
117
|
|
|
->where('date', '>=', Carbon::now()->subDays(6)) //Omit really old records. |
118
|
|
|
->orderBy('date', 'ASC') |
119
|
|
|
->get(); |
120
|
|
|
|
121
|
|
|
$this->convertListToCollection(); |
122
|
|
|
$this->swapDates = new Collection(); |
123
|
|
|
|
124
|
|
|
foreach ($dutySwaps as $swap) { |
125
|
|
|
$key = key($this->list->where('id', $swap->imageable_id)->toArray()); |
|
|
|
|
126
|
|
|
if (!is_null($key)) { |
127
|
|
|
$this->swapDates->push($swap->date); |
128
|
|
|
$this->list[$key] = [ |
129
|
|
|
'group' => $swap->imageable()->first()->users()->active()->get(), |
130
|
|
|
'id' => $swap->imageable()->first()->id, |
131
|
|
|
'date' => $swap->date, |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Convert the list of groups into a collection of group users, group id and date. |
141
|
|
|
*/ |
142
|
|
|
private function convertListToCollection() |
143
|
|
|
{ |
144
|
|
|
$count = $this->list->count(); |
|
|
|
|
145
|
|
|
$newList = new Collection(); |
146
|
|
|
for ($i = 0; $i < $count; $i++) { |
147
|
|
|
$newList->push([ |
148
|
|
|
'group' => $this->list[$i]->users()->active()->get(), |
|
|
|
|
149
|
|
|
'id' => $this->list[$i]->id, |
150
|
|
|
'date' => '', |
151
|
|
|
]); |
152
|
|
|
} |
153
|
|
|
$this->list = $newList; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $entry |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
private function buildHTMLUserRow($entry) |
162
|
|
|
{ |
163
|
|
|
$row = ''; |
164
|
|
|
foreach ($entry['group'] as $user) { |
165
|
|
|
if (Gate::allows('view')) { |
166
|
|
|
$row .= "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a> & '; |
167
|
|
|
} else { |
168
|
|
|
$row .= $user->userFullName.' & '; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$row = rtrim($row, '& '); |
172
|
|
|
|
173
|
|
|
return $row; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|