summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2016-11-12 23:00:46 +0100
committermsquare <msquare@notrademark.de>2016-11-12 23:00:46 +0100
commit1a3b4e2a334c57cc403be1d8e21fd8e162d9df5c (patch)
treebd61f7ab438d50a57ccb34866022aab13d4fdfcb /includes/model
parent106a6788086f9be3ef6d30d1844f512fcbf22e23 (diff)
redone shift coloring and shift signup state
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/NeededAngelTypes_model.php9
-rw-r--r--includes/model/ShiftSignupState.php98
-rw-r--r--includes/model/Shifts_model.php74
3 files changed, 149 insertions, 32 deletions
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 @@
+<?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;
+ }
+}
+
+?> \ 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 @@
<?php
use Engelsystem\ShiftsFilter;
+use Engelsystem\ShiftSignupState;
function Shifts_by_room($room) {
$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
* ID of the shift to check
* @param int $angeltype_id
* 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);
foreach ($needed_angeltypes as $needed_angeltype) {
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
* 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;
- if ($user_shifts == null) {
- $user_shifts = Shifts_by_user($user);
+ $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 (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;
foreach ($user_shifts as $user_shift) {
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
- $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);
}
/**