2016-10-03 17:41:14 +02:00
|
|
|
<?php
|
|
|
|
namespace Engelsystem;
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
class ShiftCalendarRenderer
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
2016-10-07 17:22:48 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var array */
|
2017-01-02 03:57:23 +01:00
|
|
|
private $lanes;
|
2016-10-05 22:28:39 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var ShiftsFilter */
|
2017-01-02 03:57:23 +01:00
|
|
|
private $shiftsFilter;
|
2016-10-05 22:28:39 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var int */
|
|
|
|
private $firstBlockStartTime = 0;
|
2016-11-14 17:58:15 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var int */
|
|
|
|
private $lastBlockEndTime = 0;
|
2016-11-05 10:06:06 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var int */
|
2017-01-02 03:57:23 +01:00
|
|
|
private $blocksPerSlot = null;
|
2016-11-05 10:06:06 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var array[] */
|
|
|
|
private $needed_angeltypes = [];
|
2016-12-27 23:02:05 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var array[] */
|
|
|
|
private $shift_entries = [];
|
2016-12-27 23:02:05 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* ShiftCalendarRenderer constructor.
|
|
|
|
*
|
2017-07-28 19:15:52 +02:00
|
|
|
* @param array[] $shifts
|
|
|
|
* @param array[] $needed_angeltypes
|
|
|
|
* @param array[] $shift_entries
|
|
|
|
* @param ShiftsFilter $shiftsFilter
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-10-05 22:28:39 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Assigns the shifts to different lanes per room if they collide
|
|
|
|
*
|
2017-07-28 19:15:52 +02:00
|
|
|
* @param array[] $shifts
|
|
|
|
* The shifts to assign
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array Returns an array that assigns a room_id to an array of ShiftCalendarLane containing the shifts
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
private function assignShiftsToLanes($shifts)
|
|
|
|
{
|
|
|
|
// array that assigns a room id to a list of lanes (per room)
|
|
|
|
$lanes = [];
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
foreach ($shifts as $shift) {
|
|
|
|
$room_id = $shift['RID'];
|
|
|
|
$header = Room_name_render([
|
2017-07-28 19:15:52 +02:00
|
|
|
'RID' => $room_id,
|
2017-01-02 15:43:36 +01:00
|
|
|
'Name' => $shift['room_name']
|
|
|
|
]);
|
2017-07-28 19:15:52 +02:00
|
|
|
if (! isset($lanes[$room_id])) {
|
2017-01-02 15:43:36 +01:00
|
|
|
// 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) {
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var ShiftCalendarLane $lane */
|
2017-07-28 19:15:52 +02:00
|
|
|
try {
|
|
|
|
$lane->addShift($shift);
|
|
|
|
} catch (Exception $e) {
|
2017-01-02 15:43:36 +01:00
|
|
|
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());
|
2017-07-28 19:15:52 +02:00
|
|
|
$newLane->addShift($shift);
|
2017-01-02 15:43:36 +01:00
|
|
|
$lanes[$room_id][] = $newLane;
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
return $lanes;
|
|
|
|
}
|
2016-11-05 10:06:06 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
public function getFirstBlockStartTime()
|
|
|
|
{
|
|
|
|
return $this->firstBlockStartTime;
|
|
|
|
}
|
2016-11-14 17:58:15 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
public function getLastBlockEndTime()
|
|
|
|
{
|
|
|
|
return $this->lastBlockEndTime;
|
|
|
|
}
|
2016-10-07 17:22:48 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return float
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
public function getBlocksPerSlot()
|
|
|
|
{
|
|
|
|
if ($this->blocksPerSlot == null) {
|
|
|
|
$this->blocksPerSlot = $this->calcBlocksPerSlot();
|
|
|
|
}
|
|
|
|
return $this->blocksPerSlot;
|
2016-10-07 17:22:48 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Renders the whole calendar
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string the generated html
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
if (count($this->lanes) == 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return div('shift-calendar', [
|
2017-07-28 19:15:52 +02:00
|
|
|
$this->renderTimeLane(),
|
|
|
|
$this->renderShiftLanes()
|
|
|
|
]) . $this->renderLegend();
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the lanes containing the shifts
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
private function renderShiftLanes()
|
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$html = '';
|
2017-01-02 15:43:36 +01:00
|
|
|
foreach ($this->lanes as $room_lanes) {
|
|
|
|
foreach ($room_lanes as $lane) {
|
|
|
|
$html .= $this->renderLane($lane);
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a single lane
|
|
|
|
*
|
2017-07-28 19:15:52 +02:00
|
|
|
* @param ShiftCalendarLane $lane
|
|
|
|
* The lane to render
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
private function renderLane(ShiftCalendarLane $lane)
|
|
|
|
{
|
|
|
|
global $user;
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
$shift_renderer = new ShiftCalendarShiftRenderer();
|
2017-01-03 14:12:17 +01:00
|
|
|
$html = '';
|
2017-01-02 15:43:36 +01:00
|
|
|
$rendered_until = $this->getFirstBlockStartTime();
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-07-28 19:15:52 +02:00
|
|
|
|
|
|
|
list ($shift_height, $shift_html) = $shift_renderer->render($shift, $this->needed_angeltypes[$shift['SID']], $this->shift_entries[$shift['SID']], $user);
|
2017-01-02 15:43:36 +01:00
|
|
|
$html .= $shift_html;
|
|
|
|
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
|
|
|
|
}
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
while ($rendered_until < $this->getLastBlockEndTime()) {
|
|
|
|
$html .= $this->renderTick($rendered_until);
|
|
|
|
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
|
|
|
|
}
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
return div('lane', [
|
|
|
|
div('header', $lane->getHeader()),
|
|
|
|
$html
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a tick/block for given time
|
|
|
|
*
|
2017-07-28 19:15:52 +02:00
|
|
|
* @param int $time
|
|
|
|
* unix timestamp
|
|
|
|
* @param boolean $label
|
|
|
|
* Should time labels be generated?
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string rendered tick html
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
private function renderTick($time, $label = false)
|
|
|
|
{
|
|
|
|
if ($time % (24 * 60 * 60) == 23 * 60 * 60) {
|
2017-07-28 19:15:52 +02:00
|
|
|
if (! $label) {
|
2017-01-02 15:43:36 +01:00
|
|
|
return div('tick day');
|
|
|
|
}
|
|
|
|
return div('tick day', [
|
|
|
|
date('m-d<b\r />H:i', $time)
|
|
|
|
]);
|
|
|
|
} elseif ($time % (60 * 60) == 0) {
|
2017-07-28 19:15:52 +02:00
|
|
|
if (! $label) {
|
2017-01-02 15:43:36 +01:00
|
|
|
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
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
private function renderTimeLane()
|
|
|
|
{
|
|
|
|
$time_slot = [
|
|
|
|
div('header', [
|
2017-01-03 14:12:17 +01:00
|
|
|
_('Time')
|
2017-01-02 15:43:36 +01:00
|
|
|
])
|
|
|
|
];
|
2017-07-28 19:15:52 +02:00
|
|
|
for ($block = 0; $block < $this->getBlocksPerSlot(); $block ++) {
|
2017-01-02 15:43:36 +01:00
|
|
|
$thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW);
|
|
|
|
$time_slot[] = $this->renderTick($thistime, true);
|
|
|
|
}
|
|
|
|
return div('lane time', $time_slot);
|
|
|
|
}
|
2016-10-07 17:22:48 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
|
|
|
* @param array[] $shifts
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
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);
|
2016-10-07 17:22:48 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
|
|
|
* @param array[] $shifts
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
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);
|
2016-11-14 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-28 19:15:52 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
private function calcBlocksPerSlot()
|
|
|
|
{
|
|
|
|
return ceil(($this->getLastBlockEndTime() - $this->getFirstBlockStartTime()) / ShiftCalendarRenderer::SECONDS_PER_ROW);
|
|
|
|
}
|
2016-11-14 17:58:15 +01:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Renders a legend explaining the shift coloring
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
|
|
|
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')
|
|
|
|
]);
|
|
|
|
}
|
2016-10-03 17:41:14 +02:00
|
|
|
}
|