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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
<?php
namespace Engelsystem;
class ShiftCalendarRenderer
{
/**
* 15m * 60s/m = 900s
*/
const SECONDS_PER_ROW = 900;
/**
* Height of a block in pixel.
* Do not change - corresponds with theme/css
*/
const BLOCK_HEIGHT = 30;
/**
* Distance between two shifts in pixels
*/
const MARGIN = 5;
/**
* Seconds added to the start and end time
*/
const TIME_MARGIN = 1800;
/** @var array */
private $lanes;
/** @var ShiftsFilter */
private $shiftsFilter;
/** @var int */
private $firstBlockStartTime = 0;
/** @var int */
private $lastBlockEndTime = 0;
/** @var int */
private $blocksPerSlot = null;
/** @var array[] */
private $needed_angeltypes = [];
/** @var array[] */
private $shift_entries = [];
/**
* ShiftCalendarRenderer constructor.
*
* @param array[] $shifts
* @param array[] $needed_angeltypes
* @param array[] $shift_entries
* @param ShiftsFilter $shiftsFilter
*/
public function __construct($shifts, $needed_angeltypes, $shift_entries, ShiftsFilter $shiftsFilter)
{
$this->shiftsFilter = $shiftsFilter;
$this->firstBlockStartTime = $this->calcFirstBlockStartTime($shifts);
$this->lastBlockEndTime = $this->calcLastBlockEndTime($shifts);
$this->lanes = $this->assignShiftsToLanes($shifts);
$this->needed_angeltypes = $needed_angeltypes;
$this->shift_entries = $shift_entries;
}
/**
* Assigns the shifts to different lanes per room if they collide
*
* @param array[] $shifts
* The shifts to assign
*
* @return array Returns an array that assigns a room_id to an array of ShiftCalendarLane containing the shifts
*/
private function assignShiftsToLanes($shifts)
{
// array that assigns a room id to a list of lanes (per room)
$lanes = [];
foreach ($shifts as $shift) {
$room_id = $shift['RID'];
$header = Room_name_render([
'RID' => $room_id,
'Name' => $shift['room_name']
]);
if (! isset($lanes[$room_id])) {
// initialize room with one lane
$lanes[$room_id] = [
new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot())
];
}
// Try to add the shift to the existing lanes for this room
$shift_added = false;
foreach ($lanes[$room_id] as $lane) {
/** @var ShiftCalendarLane $lane */
try {
$lane->addShift($shift);
} catch (Exception $e) {
break;
}
}
// If all lanes for this room are busy, create a new lane and add shift to it
if ($shift_added == false) {
$newLane = new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot());
$newLane->addShift($shift);
$lanes[$room_id][] = $newLane;
}
}
return $lanes;
}
/**
*
* @return int
*/
public function getFirstBlockStartTime()
{
return $this->firstBlockStartTime;
}
/**
*
* @return int
*/
public function getLastBlockEndTime()
{
return $this->lastBlockEndTime;
}
/**
*
* @return float
*/
public function getBlocksPerSlot()
{
if ($this->blocksPerSlot == null) {
$this->blocksPerSlot = $this->calcBlocksPerSlot();
}
return $this->blocksPerSlot;
}
/**
* Renders the whole calendar
*
* @return string the generated html
*/
public function render()
{
if (count($this->lanes) == 0) {
return '';
}
return div('shift-calendar', [
$this->renderTimeLane(),
$this->renderShiftLanes()
]) . $this->renderLegend();
}
/**
* Renders the lanes containing the shifts
*
* @return string
*/
private function renderShiftLanes()
{
$html = '';
foreach ($this->lanes as $room_lanes) {
foreach ($room_lanes as $lane) {
$html .= $this->renderLane($lane);
}
}
return $html;
}
/**
* Renders a single lane
*
* @param ShiftCalendarLane $lane
* The lane to render
* @return string
*/
private function renderLane(ShiftCalendarLane $lane)
{
global $user;
$shift_renderer = new ShiftCalendarShiftRenderer();
$html = '';
$rendered_until = $this->getFirstBlockStartTime();
foreach ($lane->getShifts() as $shift) {
while ($rendered_until + ShiftCalendarRenderer::SECONDS_PER_ROW <= $shift['start']) {
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
list ($shift_height, $shift_html) = $shift_renderer->render($shift, $this->needed_angeltypes[$shift['SID']], $this->shift_entries[$shift['SID']], $user);
$html .= $shift_html;
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
}
while ($rendered_until < $this->getLastBlockEndTime()) {
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
return div('lane', [
div('header', $lane->getHeader()),
$html
]);
}
/**
* Renders a tick/block for given time
*
* @param int $time
* unix timestamp
* @param boolean $label
* Should time labels be generated?
* @return string rendered tick html
*/
private function renderTick($time, $label = false)
{
if ($time % (24 * 60 * 60) == 23 * 60 * 60) {
if (! $label) {
return div('tick day');
}
return div('tick day', [
date('m-d<b\r />H:i', $time)
]);
} elseif ($time % (60 * 60) == 0) {
if (! $label) {
return div('tick hour');
}
return div('tick hour', [
date('m-d<b\r />H:i', $time)
]);
}
return div('tick');
}
/**
* Renders the left time lane including hour/day ticks
*
* @return string
*/
private function renderTimeLane()
{
$time_slot = [
div('header', [
_('Time')
])
];
for ($block = 0; $block < $this->getBlocksPerSlot(); $block ++) {
$thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW);
$time_slot[] = $this->renderTick($thistime, true);
}
return div('lane time', $time_slot);
}
/**
*
* @param array[] $shifts
* @return int
*/
private function calcFirstBlockStartTime($shifts)
{
$start_time = $this->shiftsFilter->getEndTime();
foreach ($shifts as $shift) {
if ($shift['start'] < $start_time) {
$start_time = $shift['start'];
}
}
return ShiftCalendarRenderer::SECONDS_PER_ROW * floor(($start_time - ShiftCalendarRenderer::TIME_MARGIN) / ShiftCalendarRenderer::SECONDS_PER_ROW);
}
/**
*
* @param array[] $shifts
* @return int
*/
private function calcLastBlockEndTime($shifts)
{
$end_time = $this->shiftsFilter->getStartTime();
foreach ($shifts as $shift) {
if ($shift['end'] > $end_time) {
$end_time = $shift['end'];
}
}
return ShiftCalendarRenderer::SECONDS_PER_ROW * ceil(($end_time + ShiftCalendarRenderer::TIME_MARGIN) / ShiftCalendarRenderer::SECONDS_PER_ROW);
}
/**
*
* @return int
*/
private function calcBlocksPerSlot()
{
return ceil(($this->getLastBlockEndTime() - $this->getFirstBlockStartTime()) / ShiftCalendarRenderer::SECONDS_PER_ROW);
}
/**
* Renders a legend explaining the shift coloring
*
* @return string
*/
private function renderLegend()
{
return div('legend', [
label(_('Your shift'), 'primary'),
label(_('Help needed'), 'danger'),
label(_('Other angeltype needed / collides with my shifts'), 'warning'),
label(_('Shift is full'), 'success'),
label(_('Shift running/ended'), 'default')
]);
}
}
|