From 46528fe1d888ae09d125c2ae8ac1952f1d7f29f6 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 27 Dec 2016 23:02:05 +0100 Subject: shift view performance improvements --- includes/view/ShiftCalendarRenderer.php | 26 ++++++++++++++-- includes/view/ShiftCalendarShiftRenderer.php | 45 +++++++++++++++++----------- includes/view/Shifts_view.php | 5 ++-- 3 files changed, 53 insertions(+), 23 deletions(-) (limited to 'includes/view') diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 7f39d64e..d8d31bd0 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -35,11 +35,17 @@ class ShiftCalendarRenderer { private $blocksPerSlot = null; - public function __construct($shifts, ShiftsFilter $shiftsFilter) { + private $needed_angeltypes = null; + + private $shift_entries = null; + + 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; } /** @@ -127,6 +133,7 @@ class ShiftCalendarRenderer { $html .= $this->renderLane($lane); } } + return $html; } @@ -142,16 +149,31 @@ class ShiftCalendarRenderer { $shift_renderer = new ShiftCalendarShiftRenderer(); $html = ""; $rendered_until = $this->getFirstBlockStartTime(); + + $needed_angeltypes = []; + $shift_entries = []; + foreach ($lane->getShifts() as $shift) { + $needed_angeltypes[$shift['SID']] = []; + $shift_entries[$shift['SID']] = []; + } + foreach ($this->needed_angeltypes as $needed_angeltype) { + $needed_angeltypes[$needed_angeltype['shift_id']][] = $needed_angeltype; + } + foreach ($this->shift_entries as $shift_entry) { + $shift_entries[$shift_entry['SID']][] = $shift_entry; + } + 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, $user); + list($shift_height, $shift_html) = $shift_renderer->render($shift, $needed_angeltypes[$shift['SID']], $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; diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index e3ceb47f..2a345692 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -15,19 +15,19 @@ class ShiftCalendarShiftRenderer { * @param User $user * The user who is viewing the shift calendar */ - public function render($shift, $user) { + public function render($shift, $needed_angeltypes, $shift_entries, $user) { $info_text = ""; if ($shift['title'] != '') { $info_text = glyph('info-sign') . $shift['title'] . '
'; } - list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $user); + list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $needed_angeltypes, $shift_entries, $user); $class = $this->classForSignupState($shift_signup_state); $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); $blocks = max(1, $blocks); return [ $blocks, - '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ + div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ $this->renderShiftHead($shift), div('panel-body', [ $info_text, @@ -38,7 +38,7 @@ class ShiftCalendarShiftRenderer { ]), $shifts_row, div('shift-spacer') - ]) . '' + ]) ]; } @@ -63,27 +63,36 @@ class ShiftCalendarShiftRenderer { } } - private function renderShiftNeededAngeltypes($shift, $user) { + private function renderShiftNeededAngeltypes($shift, $needed_angeltypes, $shift_entries, $user) { global $privileges; + $shift_entries_filtered = []; + foreach ($needed_angeltypes as $needed_angeltype) { + $shift_entries_filtered[$needed_angeltype['id']] = []; + } + foreach ($shift_entries as $shift_entry) { + $shift_entries_filtered[$shift_entry['TID']][] = $shift_entry; + } + $html = ""; $shift_signup_state = null; - $angeltypes = NeededAngelTypes_by_shift($shift['SID']); - foreach ($angeltypes as $angeltype) { - list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $user); - if ($shift_signup_state == null) { - $shift_signup_state = $angeltype_signup_state; - } else { - $shift_signup_state->combineWith($angeltype_signup_state); + foreach ($needed_angeltypes as $angeltype) { + if ($angeltype['count'] > 0 || count($shift_entries_filtered[$angeltype['id']]) > 0) { + list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $shift_entries_filtered[$angeltype['id']], $angeltype, $user); + if ($shift_signup_state == null) { + $shift_signup_state = $angeltype_signup_state; + } else { + $shift_signup_state->combineWith($angeltype_signup_state); + } + $html .= $angeltype_html; } - $html .= $angeltype_html; } if ($shift_signup_state == null) { $shift_signup_state = new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, 0); } if (in_array('user_shifts_admin', $privileges)) { - $html .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; + $html .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'], _("Add more angels"), 'btn-xs') . '
  • '; } if ($html != '') { return [ @@ -107,13 +116,13 @@ class ShiftCalendarShiftRenderer { * @param User $user * The user who is viewing the shift calendar */ - private function renderShiftNeededAngeltype($shift, $angeltype, $user) { + private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user) { $entry_list = []; - foreach ($angeltype['shift_entries'] as $entry) { + foreach ($shift_entries as $entry) { $style = $entry['freeloaded'] ? " text-decoration: line-through;" : ''; $entry_list[] = "" . User_Nick_render($entry) . ""; } - $shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype); + $shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype, null, null, $angeltype, $shift_entries); $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $shift_signup_state->getFreeEntries()), $shift_signup_state->getFreeEntries()); switch ($shift_signup_state->getState()) { case ShiftSignupState::ADMIN: @@ -174,7 +183,7 @@ class ShiftCalendarShiftRenderer { button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') ]) . ''; } - $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; + $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . $shift['name']; return div('panel-heading', [ '' . $shift_heading . '', $header_buttons diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index bd4f19c3..c2c0912e 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -14,13 +14,12 @@ function Shift_editor_info_render($shift) { function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null, $user_shifts = null) { global $user; - + if ($user_angeltype == null) { $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); } - $shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype, $user_angeltype, $user_shifts); - if ($shift_signup_state->isSignupAllowed()) { + if ($angeltype['shift_signup_state']->isSignupAllowed()) { return button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up')); } elseif ($user_angeltype == null) { return button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name'])); -- cgit v1.2.3-70-g09d2