redone shift coloring and shift signup state

main
msquare 8 years ago
parent 106a678808
commit 1a3b4e2a33

@ -42,8 +42,9 @@ function shift_entry_add_controller() {
} }
$type = $type[0]; $type = $type[0];
if (! Shift_signup_allowed($shift, $type)) { $shift_signup_allowed = Shift_signup_allowed(User($user_id), $shift, $type);
error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.')); if (! $shift_signup_allowed->isSignupAllowed()) {
error(_("You are not allowed to sign up for this shift. Maybe shift is full or already running."));
redirect(shift_link($shift)); redirect(shift_link($shift));
} }

@ -19,6 +19,7 @@ require_once realpath(__DIR__ . '/../includes/model/Room_model.php');
require_once realpath(__DIR__ . '/../includes/model/ShiftEntry_model.php'); require_once realpath(__DIR__ . '/../includes/model/ShiftEntry_model.php');
require_once realpath(__DIR__ . '/../includes/model/Shifts_model.php'); require_once realpath(__DIR__ . '/../includes/model/Shifts_model.php');
require_once realpath(__DIR__ . '/../includes/model/ShiftsFilter.php'); require_once realpath(__DIR__ . '/../includes/model/ShiftsFilter.php');
require_once realpath(__DIR__ . '/../includes/model/ShiftSignupState.php');
require_once realpath(__DIR__ . '/../includes/model/ShiftTypes_model.php'); require_once realpath(__DIR__ . '/../includes/model/ShiftTypes_model.php');
require_once realpath(__DIR__ . '/../includes/model/UserAngelTypes_model.php'); require_once realpath(__DIR__ . '/../includes/model/UserAngelTypes_model.php');
require_once realpath(__DIR__ . '/../includes/model/UserDriverLicenses_model.php'); require_once realpath(__DIR__ . '/../includes/model/UserDriverLicenses_model.php');

@ -88,8 +88,13 @@ function NeededAngelTypes_by_shift($shiftId) {
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']);
// TODO: Substract shift entries which are freeloader $angeltype['taken'] = 0;
$angeltype['taken'] = count($shift_entries); foreach($shift_entries as $shift_entry) {
if($shift_entry['freeloaded'] == 0) {
$angeltype['taken']++;
}
}
$angeltype['shift_entries'] = $shift_entries; $angeltype['shift_entries'] = $shift_entries;
$needed_angeltypes[] = $angeltype; $needed_angeltypes[] = $angeltype;
} }

@ -0,0 +1,98 @@
<?php
namespace Engelsystem;
/**
* BO to represent if there are free slots on a shift for a given angeltype
* and if signup for a given user is possible (or not, because of collisions, etc.)
*/
class ShiftSignupState {
/**
* Shift has free places
*/
const FREE = 'FREE';
/**
* Shift collides with users shifts
*/
const COLLIDES = 'COLLIDES';
/**
* User cannot join because of a restricted angeltype or user is not in the angeltype
*/
const ANGELTYPE = 'ANGELTYPE';
/**
* Shift is full
*/
const OCCUPIED = 'OCCUPIED';
/**
* User is admin and can do what he wants.
*/
const ADMIN = 'ADMIN';
/**
* Shift has already ended, no signup
*/
const SHIFT_ENDED = 'SHIFT_ENDED';
/**
* User is already signed up
*/
const SIGNED_UP = 'SIGNED_UP';
private $state;
private $freeEntries;
public function __construct($state, $free_entries) {
$this->state = $state;
$this->freeEntries = $free_entries;
}
/**
* Combine this state with another state from the same shift.
*
* @param ShiftSignupState $shiftSignupState
* The other state to combine
*/
public function combineWith(ShiftSignupState $shiftSignupState) {
$this->freeEntries += $shiftSignupState->getFreeEntries();
switch ($this->state) {
case ShiftSignupState::ANGELTYPE:
case ShiftSignupState::OCCUPIED:
$this->state = $shiftSignupState->getState();
}
}
/**
* Returns true, if signup is allowed
*/
public function isSignupAllowed() {
switch ($this->state) {
case ShiftSignupState::FREE:
case ShiftSignupState::ADMIN:
return true;
}
return false;
}
/**
* Return the shift signup state
*/
public function getState() {
return $this->state;
}
/**
* How many places are free in this shift for the angeltype?
*/
public function getFreeEntries() {
return $this->freeEntries;
}
}
?>

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftSignupState;
function Shifts_by_room($room) { function Shifts_by_room($room) {
$result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID']) . " ORDER BY `start`"); $result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID']) . " ORDER BY `start`");
@ -90,23 +91,23 @@ function Shift_collides($shift, $shifts) {
} }
/** /**
* Returns true if a shift has no more free slots for angels. * Returns the number of needed angels/free shift entries for an angeltype.
* *
* @param int $shift_id * @param int $shift_id
* ID of the shift to check * ID of the shift to check
* @param int $angeltype_id * @param int $angeltype_id
* ID of the angeltype that should be checked * ID of the angeltype that should be checked
*/ */
function Shift_occupied($shift_id, $angeltype_id) { function Shift_free_entries($shift_id, $angeltype_id) {
$needed_angeltypes = NeededAngelTypes_by_shift($shift_id); $needed_angeltypes = NeededAngelTypes_by_shift($shift_id);
foreach ($needed_angeltypes as $needed_angeltype) { foreach ($needed_angeltypes as $needed_angeltype) {
if ($needed_angeltype['angel_type_id'] == $angeltype_id) { if ($needed_angeltype['angel_type_id'] == $angeltype_id) {
return $needed_angeltype['taken'] < $needed_angeltype['count']; return max(0, $needed_angeltype['count'] - $needed_angeltype['taken']);
} }
} }
return true; return 0;
} }
/** /**
@ -119,17 +120,47 @@ function Shift_occupied($shift_id, $angeltype_id) {
* @param array<Shift> $user_shifts * @param array<Shift> $user_shifts
* List of the users shifts * List of the users shifts
*/ */
function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_shifts = null) { function Shift_signup_allowed($user, $shift, $angeltype, $user_angeltype = null, $user_shifts = null) {
global $user, $privileges; global $privileges;
if ($user_shifts == null) { $free_entries = Shift_free_entries($shift['SID'], $angeltype['id']);
$user_shifts = Shifts_by_user($user);
if (in_array('user_shifts_admin', $privileges)) {
if ($free_entries == 0) {
// User shift admins may join anybody in every shift
return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries);
}
return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
}
if (time() < $shift['start']) {
// you can only join if the shift is in future
return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries);
}
if ($free_entries == 0) {
// you cannot join if shift is full
return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries);
} }
if ($user_angeltype == null) { if ($user_angeltype == null) {
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
} }
if ($user_angeltype == null || ($angeltype['restricted'] == 1 && $user_angeltype != null && ! isset($user_angeltype['confirm_user_id']))) {
// you cannot join if user is not of this angel type
// you cannot join if you are not confirmed
return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries);
}
if ($user_shifts == null) {
$user_shifts = Shifts_by_user($user);
}
if (Shift_collides($shift, $user_shifts)) {
// you cannot join if user alread joined a parallel or this shift
return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries);
}
$signed_up = false; $signed_up = false;
foreach ($user_shifts as $user_shift) { foreach ($user_shifts as $user_shift) {
if ($user_shift['SID'] == $shift['SID']) { if ($user_shift['SID'] == $shift['SID']) {
@ -138,30 +169,13 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_
} }
} }
// you canot join if shift is full if ($signed_up) {
$user_may_join_shift = ! Shift_occupied($shift['SID'], $angeltype['id']); // you cannot join if you already singed up for this shift
return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries);
// you cannot join if user alread joined a parallel or this shift
$user_may_join_shift &= ! Shift_collides($shift, $user_shifts);
// you cannot join if you already singed up for this shift
$user_may_join_shift &= ! $signed_up;
// you cannot join if user is not of this angel type
$user_may_join_shift &= $user_angeltype != null;
// you cannot join if you are not confirmed
if ($angeltype['restricted'] == 1 && $user_angeltype != null) {
$user_may_join_shift &= isset($user_angeltype['confirm_user_id']);
} }
// you can only join if the shift is in future // Hooray, shift is free for you!
$user_may_join_shift &= time() < $shift['start']; return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
// User shift admins may join anybody in every shift
$user_may_join_shift |= in_array('user_shifts_admin', $privileges);
return $user_may_join_shift;
} }
/** /**

@ -122,6 +122,8 @@ class ShiftCalendarRenderer {
* The lane to render * The lane to render
*/ */
private function renderLane(ShiftCalendarLane $lane) { private function renderLane(ShiftCalendarLane $lane) {
global $user;
$shift_renderer = new ShiftCalendarShiftRenderer(); $shift_renderer = new ShiftCalendarShiftRenderer();
$html = ""; $html = "";
$rendered_until = $this->getFirstBlockStartTime(); $rendered_until = $this->getFirstBlockStartTime();
@ -131,7 +133,7 @@ class ShiftCalendarRenderer {
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
} }
list($shift_height, $shift_html) = $shift_renderer->render($shift); list($shift_height, $shift_html) = $shift_renderer->render($shift, $user);
$html .= $shift_html; $html .= $shift_html;
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW; $rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
} }

@ -12,24 +12,16 @@ class ShiftCalendarShiftRenderer {
* *
* @param Shift $shift * @param Shift $shift
* The shift to render * The shift to render
* @param User $user
* The user who is viewing the shift calendar
*/ */
public function render($shift) { public function render($shift, $user) {
$collides = $this->collides();
$info_text = ""; $info_text = "";
if ($shift['title'] != '') { if ($shift['title'] != '') {
$info_text = glyph('info-sign') . $shift['title'] . '<br>'; $info_text = glyph('info-sign') . $shift['title'] . '<br>';
} }
list($is_free, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $collides); list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $user);
$class = $this->classForSignupState($shift_signup_state);
if (isset($shift['own']) && $shift['own']) {
$class = 'primary';
} elseif ($collides) {
$class = 'default';
} elseif ($is_free) {
$class = 'danger';
} else {
$class = 'success';
}
$blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW);
$blocks = max(1, $blocks); $blocks = max(1, $blocks);
@ -50,15 +42,40 @@ class ShiftCalendarShiftRenderer {
]; ];
} }
private function renderShiftNeededAngeltypes($shift, $collides) { private function classForSignupState(ShiftSignupState $shiftSignupState) {
switch ($shiftSignupState->getState()) {
case ShiftSignupState::OCCUPIED:
return 'success';
case ShiftSignupState::SIGNED_UP:
return 'primary';
case ShiftSignupState::SHIFT_ENDED:
return 'default';
case ShiftSignupState::ANGELTYPE:
case ShiftSignupState::COLLIDES:
return 'warning';
case ShiftSignupState::ADMIN:
case ShiftSignupState::FREE:
return 'danger';
}
}
private function renderShiftNeededAngeltypes($shift, $user) {
global $privileges; global $privileges;
$html = ""; $html = "";
$is_free = false; $shift_signup_state = null;
$angeltypes = NeededAngelTypes_by_shift($shift['SID']); $angeltypes = NeededAngelTypes_by_shift($shift['SID']);
foreach ($angeltypes as $angeltype) { foreach ($angeltypes as $angeltype) {
list($angeltype_free, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $collides); list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $user);
$is_free |= $angeltype_free; 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 (in_array('user_shifts_admin', $privileges)) { if (in_array('user_shifts_admin', $privileges)) {
@ -66,12 +83,12 @@ class ShiftCalendarShiftRenderer {
} }
if ($html != '') { if ($html != '') {
return [ return [
$is_free, $shift_signup_state,
'<ul class="list-group">' . $html . '</ul>' '<ul class="list-group">' . $html . '</ul>'
]; ];
} }
return [ return [
$is_free, $shift_signup_state,
"" ""
]; ];
} }
@ -83,62 +100,48 @@ class ShiftCalendarShiftRenderer {
* The shift which is rendered * The shift which is rendered
* @param Angeltype $angeltype * @param Angeltype $angeltype
* The angeltype, containing informations about needed angeltypes and already signed up angels * The angeltype, containing informations about needed angeltypes and already signed up angels
* @param boolean $collides * @param User $user
* true if the shift collides with the users shifts * The user who is viewing the shift calendar
*/ */
private function renderShiftNeededAngeltype($shift, $angeltype, $collides) { private function renderShiftNeededAngeltype($shift, $angeltype, $user) {
global $privileges;
$is_free = false;
$entry_list = []; $entry_list = [];
$freeloader = 0;
foreach ($angeltype['shift_entries'] as $entry) { foreach ($angeltype['shift_entries'] as $entry) {
$style = ''; $style = $entry['freeloaded'] ? " text-decoration: line-through;" : '';
if ($entry['freeloaded']) {
$freeloader ++;
$style = " text-decoration: line-through;";
}
$entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>"; $entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>";
} }
if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) { $shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype);
$inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries'])); $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $shift_signup_state->getFreeEntries()), $shift_signup_state->getFreeEntries());
// is the shift still running or alternatively is the user shift admin? switch ($shift_signup_state->getState()) {
$user_may_join_shift = true; case ShiftSignupState::ADMIN:
case ShiftSignupState::FREE:
// you cannot join if user alread joined a parallel or this shift // When admin or free display a link + button for sign up
$user_may_join_shift &= ! $collides; $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 btn-primary');
break;
// 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 case ShiftSignupState::SHIFT_ENDED:
$user_may_join_shift &= time() < $shift['start']; // No link and add a text hint, when the shift ended
$entry_list[] = $inner_text . ' (' . _('ended') . ')';
break;
// User shift admins may join anybody in every shift case ShiftSignupState::ANGELTYPE:
$user_may_join_shift |= in_array('user_shifts_admin', $privileges); if ($angeltype['restricted'] == 1) {
if ($user_may_join_shift) { // User has to be confirmed on the angeltype first
$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 btn-primary');
} 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'); $entry_list[] = $inner_text . glyph('lock');
} elseif ($angeltype['restricted'] == 1) {
$entry_list[] = $inner_text;
} elseif ($collides) {
$entry_list[] = $inner_text;
} else { } else {
// Add link to join the angeltype first
$entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); $entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
} }
} break;
case ShiftSignupState::COLLIDES:
case ShiftSignupState::SIGNED_UP:
// Shift collides or user is already signed up: No signup allowed
$entry_list[] = $inner_text;
break;
unset($inner_text); case ShiftSignupState::OCCUPIED:
$is_free = true; // Shift is full
break;
} }
$shifts_row = '<li class="list-group-item">'; $shifts_row = '<li class="list-group-item">';
@ -146,7 +149,7 @@ class ShiftCalendarShiftRenderer {
$shifts_row .= join(", ", $entry_list); $shifts_row .= join(", ", $entry_list);
$shifts_row .= '</li>'; $shifts_row .= '</li>';
return [ return [
$is_free, $shift_signup_state,
$shifts_row $shifts_row
]; ];
} }

@ -18,7 +18,8 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null,
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
} }
if (Shift_signup_allowed($shift, $angeltype, $user_angeltype, $user_shifts)) { $shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype, $user_angeltype, $user_shifts);
if ($shift_signup_state->isSignupAllowed()) {
return button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up')); return button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'));
} elseif ($user_angeltype == null) { } elseif ($user_angeltype == null) {
return button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name'])); return button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']));

Loading…
Cancel
Save