diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php
index 9e252a4f..1b1a4d02 100644
--- a/includes/controller/shift_entries_controller.php
+++ b/includes/controller/shift_entries_controller.php
@@ -42,8 +42,9 @@ function shift_entry_add_controller() {
}
$type = $type[0];
- if (! Shift_signup_allowed($shift, $type)) {
- error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.'));
+ $shift_signup_allowed = Shift_signup_allowed(User($user_id), $shift, $type);
+ 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));
}
diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php
index f3d4f5b1..029c1788 100644
--- a/includes/engelsystem_provider.php
+++ b/includes/engelsystem_provider.php
@@ -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/Shifts_model.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/UserAngelTypes_model.php');
require_once realpath(__DIR__ . '/../includes/model/UserDriverLicenses_model.php');
diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php
index 7309f7cd..ba24c6bd 100644
--- a/includes/model/NeededAngelTypes_model.php
+++ b/includes/model/NeededAngelTypes_model.php
@@ -88,8 +88,13 @@ function NeededAngelTypes_by_shift($shiftId) {
foreach ($needed_angeltypes_source as $angeltype) {
$shift_entries = ShiftEntries_by_shift_and_angeltype($shiftId, $angeltype['angel_type_id']);
- // TODO: Substract shift entries which are freeloader
- $angeltype['taken'] = count($shift_entries);
+ $angeltype['taken'] = 0;
+ foreach($shift_entries as $shift_entry) {
+ if($shift_entry['freeloaded'] == 0) {
+ $angeltype['taken']++;
+ }
+ }
+
$angeltype['shift_entries'] = $shift_entries;
$needed_angeltypes[] = $angeltype;
}
diff --git a/includes/model/ShiftSignupState.php b/includes/model/ShiftSignupState.php
new file mode 100644
index 00000000..f9226375
--- /dev/null
+++ b/includes/model/ShiftSignupState.php
@@ -0,0 +1,98 @@
+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;
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 392adfaa..306d3e13 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -1,5 +1,6 @@
$user_shifts
* List of the users shifts
*/
-function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_shifts = null) {
- global $user, $privileges;
+function Shift_signup_allowed($user, $shift, $angeltype, $user_angeltype = null, $user_shifts = null) {
+ global $privileges;
+
+ $free_entries = Shift_free_entries($shift['SID'], $angeltype['id']);
+
+ 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) {
+ $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 ($user_angeltype == null) {
- $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
+ 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;
@@ -138,30 +169,13 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_
}
}
- // you canot join if shift is full
- $user_may_join_shift = ! Shift_occupied($shift['SID'], $angeltype['id']);
-
- // 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']);
+ if ($signed_up) {
+ // you cannot join if you already singed up for this shift
+ return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries);
}
- // you can only join if the shift is in future
- $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);
-
- return $user_may_join_shift;
+ // Hooray, shift is free for you!
+ return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
}
/**
diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php
index 70948ec5..3f0d81f7 100644
--- a/includes/view/ShiftCalendarRenderer.php
+++ b/includes/view/ShiftCalendarRenderer.php
@@ -122,6 +122,8 @@ class ShiftCalendarRenderer {
* The lane to render
*/
private function renderLane(ShiftCalendarLane $lane) {
+ global $user;
+
$shift_renderer = new ShiftCalendarShiftRenderer();
$html = "";
$rendered_until = $this->getFirstBlockStartTime();
@@ -131,7 +133,7 @@ class ShiftCalendarRenderer {
$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;
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
}
diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php
index ac29b40d..86a6f91d 100644
--- a/includes/view/ShiftCalendarShiftRenderer.php
+++ b/includes/view/ShiftCalendarShiftRenderer.php
@@ -12,24 +12,16 @@ class ShiftCalendarShiftRenderer {
*
* @param Shift $shift
* The shift to render
+ * @param User $user
+ * The user who is viewing the shift calendar
*/
- public function render($shift) {
- $collides = $this->collides();
+ public function render($shift, $user) {
$info_text = "";
if ($shift['title'] != '') {
$info_text = glyph('info-sign') . $shift['title'] . '
';
}
- list($is_free, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $collides);
-
- if (isset($shift['own']) && $shift['own']) {
- $class = 'primary';
- } elseif ($collides) {
- $class = 'default';
- } elseif ($is_free) {
- $class = 'danger';
- } else {
- $class = 'success';
- }
+ list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $user);
+ $class = $this->classForSignupState($shift_signup_state);
$blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW);
$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;
$html = "";
- $is_free = false;
+ $shift_signup_state = null;
$angeltypes = NeededAngelTypes_by_shift($shift['SID']);
foreach ($angeltypes as $angeltype) {
- list($angeltype_free, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $collides);
- $is_free |= $angeltype_free;
+ 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);
+ }
$html .= $angeltype_html;
}
if (in_array('user_shifts_admin', $privileges)) {
@@ -66,12 +83,12 @@ class ShiftCalendarShiftRenderer {
}
if ($html != '') {
return [
- $is_free,
+ $shift_signup_state,
'