improve shift rendering

main
msquare 8 years ago
parent 6d97aa2d27
commit c48335e702

@ -65,7 +65,7 @@ function NeededAngelTypes_by_shift($shiftId) {
ORDER BY `room_id` DESC ORDER BY `room_id` DESC
"); ");
if ($needed_angeltypes_source === false) { if ($needed_angeltypes_source === false) {
return false; engelsystem_error("Unable to load needed angeltypes.");
} }
// Use settings from room // Use settings from room
@ -80,18 +80,16 @@ function NeededAngelTypes_by_shift($shiftId) {
ORDER BY `room_id` DESC ORDER BY `room_id` DESC
"); ");
if ($needed_angeltypes_source === false) { if ($needed_angeltypes_source === false) {
return false; engelsystem_error("Unable to load needed angeltypes.");
} }
} }
$needed_angeltypes = []; $needed_angeltypes = [];
foreach ($needed_angeltypes_source as $angeltype) { foreach ($needed_angeltypes_source as $angeltype) {
$shift_entries = ShiftEntries_by_shift_and_angeltype($shiftId, $angeltype['angel_type_id']); $shift_entries = ShiftEntries_by_shift_and_angeltype($shiftId, $angeltype['angel_type_id']);
if ($shift_entries === false) {
return false;
}
$angeltype['taken'] = count($shift_entries); $angeltype['taken'] = count($shift_entries);
$angeltype['shift_entries'] = $shift_entries;
$needed_angeltypes[] = $angeltype; $needed_angeltypes[] = $angeltype;
} }

@ -110,12 +110,16 @@ function ShiftEntries_finished_by_user($user) {
* @param int $angeltype_id * @param int $angeltype_id
*/ */
function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) { function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) {
return sql_select(" $result = sql_select("
SELECT * SELECT *
FROM `ShiftEntry` FROM `ShiftEntry`
WHERE `SID`=" . sql_escape($shift_id) . " WHERE `SID`=" . sql_escape($shift_id) . "
AND `TID`=" . sql_escape($angeltype_id) . " AND `TID`=" . sql_escape($angeltype_id) . "
"); ");
if ($result === false) {
engelsystem_error("Unable to load shift entries.");
}
return $result;
} }
/** /**

@ -2,7 +2,7 @@
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
function Shifts_by_room($room) { function Shifts_by_room($room) {
$result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID'])); $result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID']) . " ORDER BY `start`");
if ($result === false) { if ($result === false) {
engelsystem_error("Unable to load shifts."); engelsystem_error("Unable to load shifts.");
} }

@ -169,6 +169,10 @@ function make_navigation() {
function make_room_navigation($menu) { function make_room_navigation($menu) {
global $privileges; global $privileges;
if (! in_array('view_rooms', $privileges)) {
return $menu;
}
$rooms = Rooms(); $rooms = Rooms();
$room_menu = []; $room_menu = [];
if (in_array('admin_rooms', $privileges)) { if (in_array('admin_rooms', $privileges)) {

@ -11,7 +11,7 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen
function Room_name_render($room) { function Room_name_render($room) {
global $privileges; global $privileges;
if (in_array('admin_rooms', $privileges)) { if (in_array('view_rooms', $privileges)) {
return '<a href="' . room_link($room) . '">' . glyph('map-marker') . $room['Name'] . '</a>'; return '<a href="' . room_link($room) . '">' . glyph('map-marker') . $room['Name'] . '</a>';
} }
return glyph('map-marker') . $room['Name']; return glyph('map-marker') . $room['Name'];

@ -9,6 +9,8 @@ class ShiftCalendarRenderer {
*/ */
const MINUTES_PER_ROW = 900; const MINUTES_PER_ROW = 900;
const EMPTY_CELL = '<td class="empty"></td>';
private $shifts; private $shifts;
private $shiftsFilter; private $shiftsFilter;
@ -20,9 +22,222 @@ class ShiftCalendarRenderer {
public function render() { public function render() {
$rooms = $this->rooms(); $rooms = $this->rooms();
$slotSizes = $this->calcSlotSizes($rooms);
return ''; $first_block_start_time = $this->calcFirstBlockStartTime();
$blocks_per_slot = $this->calcBlocksPerSlot($first_block_start_time);
$slotSizes = $this->calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot);
return $this->renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot);
}
private function renderTableHead($rooms, $slotSizes) {
$shifts_table = '<thead><tr><th>' . _("Time") . '</th>';
foreach ($rooms as $room_id => $room_name) {
$colspan = $slotSizes[$room_id];
$shifts_table .= "<th" . (($colspan > 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([
'RID' => $room_id,
'Name' => $room_name
]) . "</th>\n";
}
$shifts_table .= "</tr></thead>";
return $shifts_table;
}
private function initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) {
// Slot sizes plus 1 for the time
$columns_needed = array_sum($slotSizes) + 1;
$table_line = array_fill(0, $columns_needed, ShiftCalendarRenderer::EMPTY_CELL);
$table = array_fill(0, $blocks_per_slot, $table_line);
for ($block = 0; $block < $blocks_per_slot; $block ++) {
$thistime = $first_block_start_time + ($block * ShiftCalendarRenderer::MINUTES_PER_ROW);
if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $this->shiftsFilter->getEndTime() - $this->shiftsFilter->getStartTime() > 24 * 60 * 60) {
$table[$block][0] = '<th class="row-day">' . date('Y-m-d<b\r />H:i', $thistime) . '</th>';
} elseif ($thistime % (60 * 60) == 0) {
$table[$block][0] = '<th class="row-hour">' . date('H:i', $thistime) . '</th>';
} else {
$table[$block][0] = '<th class="empty"></th>';
}
}
return $table;
}
private function calcRoomSlots($rooms, $slotSizes) {
$result = [];
$slot = 1; // 1 for the time
foreach ($rooms as $room_id => $room_name) {
$result[$room_id] = $slot;
$slot += $slotSizes[$room_id];
}
return $result;
}
private function collides() {
// TODO
return false;
}
private function renderShift($shift) {
global $privileges, $user;
$collides = $this->collides();
$is_free = false;
$shifts_row = '';
$header_buttons = "";
if (in_array('admin_shifts', $privileges)) {
$header_buttons = '<div class="pull-right">' . table_buttons([
button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'),
button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs')
]) . '</div>';
}
$info_text = "";
if ($shift['title'] != '') {
$info_text = glyph('info-sign') . $shift['title'] . '<br>';
}
$angeltypes = NeededAngelTypes_by_shift($shift['SID']);
foreach ($angeltypes as $angeltype) {
$entry_list = [];
$freeloader = 0;
foreach ($angeltype['shift_entries'] as $entry) {
$style = '';
if ($entry['freeloaded']) {
$freeloader ++;
$style = " text-decoration: line-through;";
}
if (in_array('user_shifts_admin', $privileges)) {
$entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . ' ' . table_buttons([
button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs')
]) . '</span>';
} else {
$entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>";
}
}
if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) {
$inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries']));
// is the shift still running or alternatively is the user shift admin?
$user_may_join_shift = true;
// you cannot join if user alread joined a parallel or this shift
$user_may_join_shift &= ! $collides;
// you cannot join if user is not of this angel type
$user_may_join_shift &= isset($angeltype['user_id']);
// you cannot join if you are not confirmed
if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) {
$user_may_join_shift &= isset($angeltype['confirm_user_id']);
}
// you can only join if the shift is in future or running
$user_may_join_shift &= time() < $shift['start'];
// User shift admins may join anybody in every shift
$user_may_join_shift |= in_array('user_shifts_admin', $privileges);
if ($user_may_join_shift) {
$entry_list[] = '<a href="' . page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'] . '">' . $inner_text . '</a> ' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs');
} else {
if (time() > $shift['start']) {
$entry_list[] = $inner_text . ' (' . _('ended') . ')';
} elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) {
$entry_list[] = $inner_text . glyph('lock');
} elseif ($angeltype['restricted'] == 1) {
$entry_list[] = $inner_text;
} elseif ($collides) {
$entry_list[] = $inner_text;
} else {
$entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
}
}
unset($inner_text);
$is_free = true;
}
$shifts_row .= '<li class="list-group-item">';
$shifts_row .= '<strong>' . AngelType_name_render($angeltype) . ':</strong> ';
$shifts_row .= join(", ", $entry_list);
$shifts_row .= '</li>';
}
if (in_array('user_shifts_admin', $privileges)) {
$shifts_row .= '<li class="list-group-item">' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '</li>';
}
if ($shifts_row != '') {
$shifts_row = '<ul class="list-group">' . $shifts_row . '</ul>';
}
if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) {
$class = 'primary';
} elseif ($collides && ! in_array('user_shifts_admin', $privileges)) {
$class = 'default';
} elseif ($is_free) {
$class = 'danger';
} else {
$class = 'success';
}
$blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW);
if ($blocks < 1) {
$blocks = 1;
}
return [
$blocks,
'<td class="shift" rowspan="' . $blocks . '">' . div('panel panel-' . $class, [
div('panel-heading', [
date('H:i', $shift['start']),
'&dash;',
date('H:i', $shift['end']),
'&mdash;',
ShiftType($shift['shifttype_id'])['name'],
$header_buttons
]),
div('panel-body', [
$info_text,
Room_name_render([
'RID' => $shift['RID'],
'Name' => $shift['room_name']
])
]),
$shifts_row
]) . '</td>'
];
}
private function renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) {
$table = $this->initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot);
$room_slots = $this->calcRoomSlots($rooms, $slotSizes);
foreach ($this->shifts as $shift) {
list($blocks, $shift_content) = $this->renderShift($shift);
$start_block = floor(($shift['start'] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW);
$slot = $room_slots[$shift['RID']];
while ($table[$start_block][$slot] != ShiftCalendarRenderer::EMPTY_CELL) {
$slot ++;
}
$table[$start_block][$slot] = $shift_content;
for ($block = 1; $block < $blocks; $block ++) {
$table[$start_block + $block][$slot] = '';
}
}
$result = '<tbody>';
foreach ($table as $table_line) {
$result .= '<tr>' . join('', $table_line) . '</tr>';
}
$result .= '</tbody>';
return $result;
}
private function renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) {
return div('shifts-table', [
'<table id="shifts" class="table scrollable">',
$this->renderTableHead($rooms, $slotSizes),
$this->renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot),
'</table>'
]);
} }
/** /**
@ -38,9 +253,21 @@ class ShiftCalendarRenderer {
return $rooms; return $rooms;
} }
private function calcSlotSizes($rooms) { private function calcFirstBlockStartTime() {
$first_block_start_time = ShiftCalendarRenderer::MINUTES_PER_ROW * floor($this->shiftsFilter->getStartTime() / ShiftCalendarRenderer::MINUTES_PER_ROW); $start_time = $this->shiftsFilter->getEndTime();
$blocks_per_slot = ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); foreach ($this->shifts as $shift) {
if ($shift['start'] < $start_time) {
$start_time = $shift['start'];
}
}
return ShiftCalendarRenderer::MINUTES_PER_ROW * floor(($start_time - 60 * 60) / ShiftCalendarRenderer::MINUTES_PER_ROW);
}
private function calcBlocksPerSlot($first_block_start_time) {
return ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW);
}
private function calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot) {
$parallel_blocks = []; $parallel_blocks = [];
// initialize $block array // initialize $block array

@ -46,7 +46,9 @@ class ShiftsFilterRenderer {
} }
$toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active'); $toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active');
} }
return toolbar_pills($toolbar); return div('form-group', [
toolbar_pills($toolbar)
]);
} }
/** /**

@ -6730,32 +6730,19 @@ body {
.footer a { .footer a {
color: #777777; color: #777777;
} }
#shifts td.free { #shifts.table td,
border: 1px solid #d9534f; #shifts.table th {
background-color: #f2dede; background-color: #f0f0f0;
}
a#shifts td.free:hover,
a#shifts td.free:focus {
background-color: #e4b9b9;
}
#shifts td.occupied {
border: 1px solid #5cb85c;
background-color: #dff0d8;
} }
a#shifts td.occupied:hover, #shifts.table .row-hour {
a#shifts td.occupied:focus { border-top-color: #777777;
background-color: #c1e2b3;
}
#shifts td.collides {
border: 1px solid #f0ad4e;
background-color: #fcf8e3;
} }
a#shifts td.collides:hover, #shifts.table td.shift {
a#shifts td.collides:focus { height: 1px;
background-color: #f7ecb5; padding: 0px 5px 5px 0px;
} }
#shifts td.own { #shifts.table td.shift .panel {
border: 1px solid #777777; margin-bottom: 0px;
} }
.row-day { .row-day {
border-top: 2px solid #777777; border-top: 2px solid #777777;

@ -6753,32 +6753,19 @@ body {
.footer a { .footer a {
color: #888888; color: #888888;
} }
#shifts td.free { #shifts.table td,
border: 1px solid #d9534f; #shifts.table th {
background-color: #d9534f; background-color: #f0f0f0;
}
a#shifts td.free:hover,
a#shifts td.free:focus {
background-color: #c9302c;
}
#shifts td.occupied {
border: 1px solid #5cb85c;
background-color: #5cb85c;
} }
a#shifts td.occupied:hover, #shifts.table .row-hour {
a#shifts td.occupied:focus { border-top-color: #888888;
background-color: #449d44;
}
#shifts td.collides {
border: 1px solid #f0ad4e;
background-color: #f0ad4e;
} }
a#shifts td.collides:hover, #shifts.table td.shift {
a#shifts td.collides:focus { height: 1px;
background-color: #ec971f; padding: 0px 5px 5px 0px;
} }
#shifts td.own { #shifts.table td.shift .panel {
border: 1px solid #888888; margin-bottom: 0px;
} }
.row-day { .row-day {
border-top: 2px solid #888888; border-top: 2px solid #888888;

@ -6730,32 +6730,19 @@ body {
.footer a { .footer a {
color: #777777; color: #777777;
} }
#shifts td.free { #shifts.table td,
border: 1px solid #7f528b; #shifts.table th {
background-color: #f1eaf2; background-color: #f0f0f0;
}
a#shifts td.free:hover,
a#shifts td.free:focus {
background-color: #dbcadf;
}
#shifts td.occupied {
border: 1px solid #7b9c41;
background-color: #f0f5e7;
} }
a#shifts td.occupied:hover, #shifts.table .row-hour {
a#shifts td.occupied:focus { border-top-color: #777777;
background-color: #d9e6c3;
}
#shifts td.collides {
border: 1px solid #e3a14d;
background-color: #ffffff;
} }
a#shifts td.collides:hover, #shifts.table td.shift {
a#shifts td.collides:focus { height: 1px;
background-color: #e6e6e6; padding: 0px 5px 5px 0px;
} }
#shifts td.own { #shifts.table td.shift .panel {
border: 1px solid #777777; margin-bottom: 0px;
} }
.row-day { .row-day {
border-top: 2px solid #777777; border-top: 2px solid #777777;

@ -6739,32 +6739,19 @@ body {
.footer a { .footer a {
color: #777777; color: #777777;
} }
#shifts td.free { #shifts.table td,
border: 1px solid #da1639; #shifts.table th {
background-color: #f9c3cd; background-color: #f0f0f0;
}
a#shifts td.free:hover,
a#shifts td.free:focus {
background-color: #f495a6;
}
#shifts td.occupied {
border: 1px solid #39ab50;
background-color: #c4eccc;
} }
a#shifts td.occupied:hover, #shifts.table .row-hour {
a#shifts td.occupied:focus { border-top-color: #777777;
background-color: #9edfab;
}
#shifts td.collides {
border: 1px solid #dad216;
background-color: #f9f7c3;
} }
a#shifts td.collides:hover, #shifts.table td.shift {
a#shifts td.collides:focus { height: 1px;
background-color: #f4f095; padding: 0px 5px 5px 0px;
} }
#shifts td.own { #shifts.table td.shift .panel {
border: 1px solid #777777; margin-bottom: 0px;
} }
.row-day { .row-day {
border-top: 2px solid #777777; border-top: 2px solid #777777;

@ -10,23 +10,22 @@ body {
color: @text-muted; color: @text-muted;
} }
#shifts { #shifts.table {
td { td, th {
&.free { background-color: #f0f0f0;
border: 1px solid @brand-danger; }
.bg-danger();
} .row-hour {
&.occupied { border-top-color: @gray-light;
border: 1px solid @brand-success; }
.bg-success();
} td.shift {
&.collides { height: 1px;
border: 1px solid @brand-warning; padding: 0px 5px 5px 0px;
.bg-warning();
} .panel {
&.own { margin-bottom: 0px;
border: 1px solid @gray-light; }
}
} }
} }

Loading…
Cancel
Save