diff options
Diffstat (limited to 'includes/model')
-rw-r--r-- | includes/model/AngelType_model.php | 27 | ||||
-rw-r--r-- | includes/model/EventConfig_model.php | 1 | ||||
-rw-r--r-- | includes/model/NeededAngelTypes_model.php | 68 | ||||
-rw-r--r-- | includes/model/Room_model.php | 11 | ||||
-rw-r--r-- | includes/model/ShiftEntry_model.php | 6 | ||||
-rw-r--r-- | includes/model/ShiftSignupState.php | 98 | ||||
-rw-r--r-- | includes/model/ShiftTypes_model.php | 2 | ||||
-rw-r--r-- | includes/model/ShiftsFilter.php | 112 | ||||
-rw-r--r-- | includes/model/Shifts_model.php | 280 | ||||
-rw-r--r-- | includes/model/UserAngelTypes_model.php | 63 | ||||
-rw-r--r-- | includes/model/UserDriverLicenses_model.php | 19 | ||||
-rw-r--r-- | includes/model/User_model.php | 104 |
12 files changed, 630 insertions, 161 deletions
diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index 73b746f0..fa81349d 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -91,39 +91,44 @@ function AngelType_validate_name($name, $angeltype) { * @param User $user */ function AngelTypes_with_user($user) { - return sql_select(" + $result = sql_select(" SELECT `AngelTypes`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, - `UserAngelTypes`.`coordinator` + `UserAngelTypes`.`supporter` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` AND `UserAngelTypes`.`user_id`=" . $user['UID'] . " ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** * Returns all angeltypes. */ function AngelTypes() { - return sql_select(" + $result = sql_select(" SELECT * FROM `AngelTypes` ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** * Returns AngelType id array */ function AngelType_ids() { - $angelType_source = sql_select("SELECT `id` FROM `AngelTypes`"); - if ($angelType_source === false) { - return false; - } - if (count($angelType_source) > 0) { - return $angelType_source; + $result = sql_select("SELECT `id` FROM `AngelTypes`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); } - return null; + return select_array($result, 'id', 'id'); } /** @@ -135,7 +140,7 @@ function AngelType_ids() { function AngelType($angeltype_id) { $angelType_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($angeltype_id) . "' LIMIT 1"); if ($angelType_source === false) { - return false; + engelsystem_error("Unable to load angeltype."); } if (count($angelType_source) > 0) { return $angelType_source[0]; diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index ac8d5e72..de5073d0 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -6,6 +6,7 @@ function EventConfig() { $event_config = sql_select("SELECT * FROM `EventConfig` LIMIT 1"); if ($event_config === false) { + engelsystem_error("Unable to load event config."); return false; } if (count($event_config) > 0) { diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 96ceca83..ba24c6bd 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -1,13 +1,63 @@ <?php /** + * Entity needed angeltypes describes how many angels of given type are needed for a shift or in a room. + */ + +/** + * Insert a new needed angel type. + * + * @param int $shift_id + * The shift. Can be null, but then a room_id must be given. + * @param int $angeltype_id + * The angeltype + * @param int $room_id + * The room. Can be null, but then a shift_id must be given. + * @param int $count + * How many angels are needed? + */ +function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) { + $result = sql_query(" + INSERT INTO `NeededAngelTypes` SET + `shift_id`=" . sql_null($shift_id) . ", + `angel_type_id`='" . sql_escape($angeltype_id) . "', + `room_id`=" . sql_null($room_id) . ", + `count`='" . sql_escape($count) . "'"); + if ($result === false) { + return false; + } + return sql_id(); +} + +/** + * Deletes all needed angel types from given shift. + * + * @param int $shift_id + * id of the shift + */ +function NeededAngelTypes_delete_by_shift($shift_id) { + return sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($shift_id) . "'"); +} + +/** + * Deletes all needed angel types from given room. + * + * @param int $room_id + * id of the room + */ +function NeededAngelTypes_delete_by_room($room_id) { + return sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'"); +} + +/** * Returns all needed angeltypes and already taken needs. * - * @param shiftID id of shift + * @param int $shiftID + * id of shift */ function NeededAngelTypes_by_shift($shiftId) { $needed_angeltypes_source = sql_select(" - SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted` + SELECT `NeededAngelTypes`.*, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted` FROM `NeededAngelTypes` JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` WHERE `shift_id`='" . sql_escape($shiftId) . "' @@ -15,7 +65,7 @@ function NeededAngelTypes_by_shift($shiftId) { ORDER BY `room_id` DESC "); if ($needed_angeltypes_source === false) { - return false; + engelsystem_error("Unable to load needed angeltypes."); } // Use settings from room @@ -30,18 +80,22 @@ function NeededAngelTypes_by_shift($shiftId) { ORDER BY `room_id` DESC "); if ($needed_angeltypes_source === false) { - return false; + engelsystem_error("Unable to load needed angeltypes."); } } $needed_angeltypes = []; foreach ($needed_angeltypes_source as $angeltype) { $shift_entries = ShiftEntries_by_shift_and_angeltype($shiftId, $angeltype['angel_type_id']); - if ($shift_entries === false) { - return false; + + $angeltype['taken'] = 0; + foreach($shift_entries as $shift_entry) { + if($shift_entry['freeloaded'] == 0) { + $angeltype['taken']++; + } } - $angeltype['taken'] = count($shift_entries); + $angeltype['shift_entries'] = $shift_entries; $needed_angeltypes[] = $angeltype; } diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 4d03260a..6b6e269e 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -1,8 +1,17 @@ <?php /** + * returns a list of rooms. + * @param boolean $show_all returns also hidden rooms when true + */ +function Rooms($show_all = false) { + return sql_select("SELECT * FROM `Room`" . ($show_all ? "" : " WHERE `show`='Y'") . " ORDER BY `Name`"); +} + +/** * Delete a room - * @param int $room_id + * + * @param int $room_id */ function Room_delete($room_id) { return sql_query("DELETE FROM `Room` WHERE `RID`=" . sql_escape($room_id)); diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 425b92e8..63127bc7 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -110,12 +110,16 @@ function ShiftEntries_finished_by_user($user) { * @param int $angeltype_id */ function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) { - return sql_select(" + $result = sql_select(" SELECT * FROM `ShiftEntry` WHERE `SID`=" . sql_escape($shift_id) . " AND `TID`=" . sql_escape($angeltype_id) . " "); + if ($result === false) { + engelsystem_error("Unable to load shift entries."); + } + return $result; } /** 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/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index a9cf77bd..89704a65 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -51,7 +51,7 @@ function ShiftType_create($name, $angeltype_id, $description) { function ShiftType($shifttype_id) { $shifttype = sql_select("SELECT * FROM `ShiftTypes` WHERE `id`='" . sql_escape($shifttype_id) . "'"); if ($shifttype === false) { - return false; + engelsystem_error('Unable to load shift type.'); } if ($shifttype == null) { return null; diff --git a/includes/model/ShiftsFilter.php b/includes/model/ShiftsFilter.php new file mode 100644 index 00000000..044b32dd --- /dev/null +++ b/includes/model/ShiftsFilter.php @@ -0,0 +1,112 @@ +<?php + +namespace Engelsystem; + +/** + * BO Class that stores all parameters used to filter shifts for users. + * + * @author msquare + */ +class ShiftsFilter { + + /** + * Shift is completely full. + */ + const FILLED_FILLED = 1; + + /** + * Shift has some free slots. + */ + const FILLED_FREE = 0; + + /** + * Has the user "user shifts admin" privilege? + * + * @var boolean + */ + private $userShiftsAdmin; + + private $filled = []; + + private $rooms = []; + + private $types = []; + + private $startTime = null; + + private $endTime = null; + + public function __construct($user_shifts_admin, $rooms, $types) { + $this->user_shifts_admin = $user_shifts_admin; + $this->rooms = $rooms; + $this->types = $types; + + $this->filled = [ + ShiftsFilter::FILLED_FREE + ]; + + if ($user_shifts_admin) { + $this->filled[] = ShiftsFilter::FILLED_FILLED; + } + } + + public function getStartTime() { + return $this->startTime; + } + + public function setStartTime($startTime) { + $this->startTime = $startTime; + } + + public function getEndTime() { + return $this->endTime; + } + + public function setEndTime($endTime) { + $this->endTime = $endTime; + } + + public function getTypes() { + if (count($this->types) == 0) { + return [ + 0 + ]; + } + return $this->types; + } + + public function setTypes($types) { + $this->types = $types; + } + + public function getRooms() { + if (count($this->rooms) == 0) { + return [ + 0 + ]; + } + return $this->rooms; + } + + public function setRooms($rooms) { + $this->rooms = $rooms; + } + + public function isUserShiftsAdmin() { + return $this->userShiftsAdmin; + } + + public function setUserShiftsAdmin($userShiftsAdmin) { + $this->userShiftsAdmin = $userShiftsAdmin; + } + + public function getFilled() { + return $this->filled; + } + + public function setFilled($filled) { + $this->filled = $filled; + } +} + +?>
\ No newline at end of file diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index a827c6b5..2db0a3d3 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,9 +1,83 @@ <?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`"); + if ($result === false) { + engelsystem_error("Unable to load shifts."); + } + return $result; +} + +function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) { + $SQL = "SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`, nat2.`special_needs` > 0 AS 'has_special_needs' + FROM `Shifts` + INNER JOIN `Room` USING (`RID`) + INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) + LEFT JOIN ( + SELECT COUNT(*) AS special_needs , nat3.`shift_id` + FROM `NeededAngelTypes` AS nat3 + WHERE `shift_id` IS NOT NULL + GROUP BY nat3.`shift_id` + ) AS nat2 ON nat2.`shift_id` = `Shifts`.`SID` + INNER JOIN `NeededAngelTypes` AS nat + ON nat.`count` != 0 + AND nat.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") + AND ( + (nat2.`special_needs` > 0 AND nat.`shift_id` = `Shifts`.`SID`) + OR + ( + (nat2.`special_needs` = 0 OR nat2.`special_needs` IS NULL) + AND nat.`room_id` = `RID`) + ) + LEFT JOIN ( + SELECT se.`SID`, se.`TID`, COUNT(*) as count + FROM `ShiftEntry` AS se GROUP BY se.`SID`, se.`TID` + ) AS entries ON entries.`SID` = `Shifts`.`SID` AND entries.`TID` = nat.`angel_type_id` + WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") + AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime(); + + if (count($shiftsFilter->getFilled()) == 1) { + if ($shiftsFilter->getFilled()[0] == ShiftsFilter::FILLED_FREE) { + $SQL .= " + AND ( + nat.`count` > entries.`count` OR entries.`count` IS NULL + OR EXISTS ( + SELECT `SID` + FROM `ShiftEntry` + WHERE `UID` = '" . sql_escape($user['UID']) . "' + AND `ShiftEntry`.`SID` = `Shifts`.`SID` + ) + )"; + } elseif ($_SESSION['user_shifts']['filled'][0] == ShiftsFilter::FILLED_FILLED) { + $SQL .= " + AND ( + nat.`count` <= entries.`count` + OR EXISTS ( + SELECT `SID` + FROM `ShiftEntry` + WHERE `UID` = '" . sql_escape($user['UID']) . "' + AND `ShiftEntry`.`SID` = `Shifts`.`SID` + ) + )"; + } + } + $SQL .= " + ORDER BY `start`"; + + $result = sql_select($SQL); + if ($result === false) { + engelsystem_error("Unable to load shifts by filter."); + } + return $result; +} /** * Check if a shift collides with other shifts (in time). - * @param Shift $shift - * @param array<Shift> $shifts + * + * @param Shift $shift + * @param array<Shift> $shifts */ function Shift_collides($shift, $shifts) { foreach ($shifts as $other_shift) { @@ -17,29 +91,51 @@ function Shift_collides($shift, $shifts) { } /** - * Check if an angel can sign up for given shift. + * Returns the number of needed angels/free shift entries for an angeltype. * - * @param Shift $shift - * @param AngelType $angeltype - * @param array<Shift> $user_shifts + * @param int $shift_id + * ID of the shift to check + * @param int $angeltype_id + * ID of the angeltype that should be checked */ -function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_shifts = null) { - global $user, $privileges; +function Shift_free_entries($shift_id, $angeltype_id) { + $needed_angeltypes = NeededAngelTypes_by_shift($shift_id); - if ($user_shifts == null) { - $user_shifts = Shifts_by_user($user); - if ($user_shifts === false) { - engelsystem_error('Unable to load users shifts.'); + foreach ($needed_angeltypes as $needed_angeltype) { + if ($needed_angeltype['angel_type_id'] == $angeltype_id) { + return max(0, $needed_angeltype['count'] - $needed_angeltype['taken']); } } - $collides = Shift_collides($shift, $user_shifts); + return 0; +} + +/** + * Check if an angel can sign up for given shift. + * + * @param Shift $shift + * The shift + * @param AngelType $angeltype + * The angeltype to which the user wants to sign up + * @param array<Shift> $user_shifts + * List of the users shifts + */ +function Shift_signup_allowed($user, $shift, $angeltype, $user_angeltype = null, $user_shifts = null) { + global $privileges; - if ($user_angeltype == null) { - $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error('Unable to load user angeltype.'); + $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 ($user_shifts == null) { + $user_shifts = Shifts_by_user($user); } $signed_up = false; @@ -50,45 +146,37 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_ } } - $needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']); - if ($needed_angeltypes === false) { - engelsystem_error('Unable to load needed angel types.'); + if ($signed_up) { + // you cannot join if you already singed up for this shift + return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries); } - // is the shift still running or alternatively is the user shift admin? - $user_may_join_shift = true; - - // you canot join if shift is full - foreach ($needed_angeltypes as $needed_angeltype) { - if ($needed_angeltype['angel_type_id'] == $angeltype['id']) { - if ($needed_angeltype['taken'] >= $needed_angeltype['count']) { - $user_may_join_shift = false; - } - break; - } + 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); } - // you cannot join if user alread joined a parallel or this shift - $user_may_join_shift &= ! $collides; - - // 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 ($user_angeltype == null) { + $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); } - // you can only join if the shift is in future - $user_may_join_shift &= time() < $shift['start']; + 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); + } - // User shift admins may join anybody in every shift - $user_may_join_shift |= in_array('user_shifts_admin', $privileges); + 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); + } - return $user_may_join_shift; + // Hooray, shift is free for you! + return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); } /** @@ -104,7 +192,11 @@ function Shift_delete_by_psid($shift_psid) { function Shift_delete($shift_id) { mail_shift_delete(Shift($shift_id)); - return sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + $result = sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + if ($result === false) { + engelsystem_error('Unable to delete shift.'); + } + return $result; } /** @@ -170,7 +262,7 @@ function Shift_create($shift) { * Return users shifts. */ function Shifts_by_user($user) { - return sql_select(" + $result = sql_select(" SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.* FROM `ShiftEntry` JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) @@ -179,52 +271,10 @@ function Shifts_by_user($user) { WHERE `UID`='" . sql_escape($user['UID']) . "' ORDER BY `start` "); -} - -/** - * TODO: $_REQUEST is not allowed in model! - * Returns Shift id array - */ -function Shifts_filtered() { - global $_REQUEST; - $filter = ""; - - // filterRoom (Array of integer) - Array of Room IDs (optional, for list request) - if (isset($_REQUEST['filterRoom']) && is_array($_REQUEST['filterRoom'])) { - foreach ($_REQUEST['filterRoom'] as $key => $value) { - $filter .= ", `RID`='" . sql_escape($value) . "' "; - } - } - - // filterTask (Array of integer) - Array if Task (optional, for list request) - if (isset($_REQUEST['filterTask']) && is_array($_REQUEST['filterTask'])) { - foreach ($_REQUEST['filterTask'] as $key => $value) { - // TODO $filter .= ", `RID`=" . sql_escape($value) . " "; - } - } - - // filterOccupancy (integer) - Occupancy state: (optional, for list request) - // 1 occupied, 2 free, 3 occupied and free - if (isset($_REQUEST['filterOccupancy']) && is_array($_REQUEST['filterOccupancy'])) { - foreach ($_REQUEST['filterOccupancy'] as $key => $value) { - // TODO $filter .= ", `RID`=" . sql_escape($value) . " "; - } - } - - // format filter - if ($filter != "") { - $filter = ' WHERE ' . substr($filter, 1); - } - - // real request - $shifts_source = sql_select("SELECT `SID` FROM `Shifts`" . $filter); - if ($shifts_source === false) { - return false; - } - if (count($shifts_source) > 0) { - return $shifts_source; + if ($result === false) { + engelsystem_error('Unable to load users shifts.'); } - return null; + return $result; } /** @@ -242,27 +292,29 @@ function Shift($shift_id) { $shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift_id) . "'"); if ($shifts_source === false) { - return false; + engelsystem_error('Unable to load shift.'); } - if (count($shifts_source) > 0) { - $result = $shifts_source[0]; - - $result['ShiftEntry'] = $shiftsEntry_source; - $result['NeedAngels'] = []; - - $temp = NeededAngelTypes_by_shift($shift_id); - foreach ($temp as $e) { - $result['NeedAngels'][] = [ - 'TID' => $e['angel_type_id'], - 'count' => $e['count'], - 'restricted' => $e['restricted'], - 'taken' => $e['taken'] - ]; - } - - return $result; + + if (empty($shifts_source)) { + return null; } - return null; + + $result = $shifts_source[0]; + + $result['ShiftEntry'] = $shiftsEntry_source; + $result['NeedAngels'] = []; + + $temp = NeededAngelTypes_by_shift($shift_id); + foreach ($temp as $e) { + $result['NeedAngels'][] = [ + 'TID' => $e['angel_type_id'], + 'count' => $e['count'], + 'restricted' => $e['restricted'], + 'taken' => $e['taken'] + ]; + } + + return $result; } /** diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index fe5293f8..92b35321 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -28,21 +28,26 @@ function UserAngelType_exists($user, $angeltype) { * @param User $user */ function User_angeltypes($user) { - return sql_select(" - SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`coordinator` + $result = sql_select(" + SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` FROM `UserAngelTypes` JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' "); + if ($result === false) { + engelsystem_error("Unable to load user angeltypes."); + return false; + } + return $result; } /** - * Gets unconfirmed user angeltypes for angeltypes of which the given user is a coordinator. + * Gets unconfirmed user angeltypes for angeltypes of which the given user is a supporter. * * @param User $user */ function User_unconfirmed_AngelTypes($user) { - return sql_select(" + $result = sql_select(" SELECT `UserAngelTypes`.*, `AngelTypes`.`name`, @@ -51,41 +56,49 @@ function User_unconfirmed_AngelTypes($user) { JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` JOIN `UserAngelTypes` as `UnconfirmedMembers` ON `UserAngelTypes`.`angeltype_id`=`UnconfirmedMembers`.`angeltype_id` WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' - AND `UserAngelTypes`.`coordinator`=TRUE + AND `UserAngelTypes`.`supporter`=TRUE AND `AngelTypes`.`restricted`=TRUE AND `UnconfirmedMembers`.`confirm_user_id` IS NULL GROUP BY `UserAngelTypes`.`angeltype_id` ORDER BY `AngelTypes`.`name`"); + if ($result === false) { + engelsystem_error("Unable to load user angeltypes."); + } + return $result; } /** - * Returns true if user is angeltype coordinator or has privilege admin_user_angeltypes. + * Returns true if user is angeltype supporter or has privilege admin_user_angeltypes. * * @param User $user * @param AngelType $angeltype */ -function User_is_AngelType_coordinator($user, $angeltype) { +function User_is_AngelType_supporter($user, $angeltype) { return (sql_num_query(" SELECT `id` FROM `UserAngelTypes` WHERE `user_id`='" . sql_escape($user['UID']) . "' AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' - AND `coordinator`=TRUE + AND `supporter`=TRUE LIMIT 1") > 0) || in_array('admin_user_angeltypes', privileges_for_user($user['UID'])); } /** - * Add or remove coordinator rights. + * Add or remove supporter rights. * * @param int $user_angeltype_id - * @param bool $coordinator + * @param bool $supporter */ -function UserAngelType_update($user_angeltype_id, $coordinator) { - return sql_query(" +function UserAngelType_update($user_angeltype_id, $supporter) { + $result = sql_query(" UPDATE `UserAngelTypes` - SET `coordinator`=" . sql_bool($coordinator) . " + SET `supporter`=" . sql_bool($supporter) . " WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); + if ($result === false) { + engelsystem_error("Unable to update supporter rights."); + } + return $result; } /** @@ -94,10 +107,14 @@ function UserAngelType_update($user_angeltype_id, $coordinator) { * @param int $angeltype_id */ function UserAngelTypes_delete_all($angeltype_id) { - return sql_query(" + $result = sql_query(" DELETE FROM `UserAngelTypes` WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' AND `confirm_user_id` IS NULL"); + if ($result === false) { + engelsystem_error("Unable to delete all unconfirmed users."); + } + return $result; } /** @@ -107,11 +124,15 @@ function UserAngelTypes_delete_all($angeltype_id) { * @param User $confirm_user */ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { - return sql_query(" + $result = sql_query(" UPDATE `UserAngelTypes` SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' AND `confirm_user_id` IS NULL"); + if ($result === false) { + engelsystem_error("Unable to confirm all users."); + } + return $result; } /** @@ -121,11 +142,15 @@ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { * @param User $confirm_user */ function UserAngelType_confirm($user_angeltype_id, $confirm_user) { - return sql_query(" + $result = sql_query(" UPDATE `UserAngelTypes` SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); + if ($result === false) { + engelsystem_error("Unable to confirm user angeltype."); + } + return $result; } /** @@ -152,7 +177,7 @@ function UserAngelType_create($user, $angeltype) { `user_id`='" . sql_escape($user['UID']) . "', `angeltype_id`='" . sql_escape($angeltype['id']) . "'"); if ($result === false) { - return false; + engelsystem_error("Unable to create user angeltype."); } return sql_id(); } @@ -169,7 +194,7 @@ function UserAngelType($user_angeltype_id) { WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); if ($angeltype === false) { - return false; + engelsystem_error("Unable to load user angeltype."); } if (count($angeltype) == 0) { return null; @@ -191,7 +216,7 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype) { AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' LIMIT 1"); if ($angeltype === false) { - return false; + engelsystem_error("Unable to load user angeltype."); } if (count($angeltype) == 0) { return null; diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index 19f87e63..32785c92 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -7,6 +7,7 @@ function UserDriverLicense($user_id) { $user_driver_license = sql_select("SELECT * FROM `UserDriverLicenses` WHERE `user_id`='" . sql_escape($user_id) . "'"); if ($user_driver_license === false) { + engelsystem_error('Unable to load user driver license.'); return false; } if (count($user_driver_license) > 0) { @@ -27,7 +28,7 @@ function UserDriverLicense($user_id) { * @param bool $has_license_forklift */ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) { - return sql_query(" + $result = sql_query(" INSERT INTO `UserDriverLicenses` SET `user_id`=" . sql_escape($user_id) . ", `has_car`=" . sql_bool($has_car) . ", @@ -36,6 +37,10 @@ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_li `has_license_7_5t_truck`=" . sql_bool($has_license_7_5t_truck) . ", `has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_forklift`=" . sql_bool($has_license_forklift)); + if ($result === false) { + engelsystem_error('Unable to create user driver license'); + } + return $result; } /** @@ -50,7 +55,7 @@ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_li * @param bool $has_license_forklift */ function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) { - return sql_query("UPDATE `UserDriverLicenses` SET + $result = sql_query("UPDATE `UserDriverLicenses` SET `has_car`=" . sql_bool($has_car) . ", `has_license_car`=" . sql_bool($has_license_car) . ", `has_license_3_5t_transporter`=" . sql_bool($has_license_3_5t_transporter) . ", @@ -58,6 +63,10 @@ function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_li `has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_forklift`=" . sql_bool($has_license_forklift) . " WHERE `user_id`='" . sql_escape($user_id) . "'"); + if ($result === false) { + engelsystem_error("Unable to update user driver license information"); + } + return $result; } /** @@ -66,6 +75,10 @@ function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_li * @param int $user_id */ function UserDriverLicenses_delete($user_id) { - return sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id)); + $result = sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id)); + if ($result === false) { + engelsystem_error("Unable to remove user driver license information"); + } + return $result; } ?>
\ No newline at end of file diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 576bb3f5..6d38a224 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -29,6 +29,7 @@ function User_update($user) { `Handy`='" . sql_escape($user['Handy']) . "', `email`='" . sql_escape($user['email']) . "', `email_shiftinfo`=" . sql_bool($user['email_shiftinfo']) . ", + `email_by_human_allowed`=" . sql_bool($user['email_by_human_allowed']) . ", `jabber`='" . sql_escape($user['jabber']) . "', `Size`='" . sql_escape($user['Size']) . "', `Gekommen`='" . sql_escape($user['Gekommen']) . "', @@ -40,7 +41,8 @@ function User_update($user) { `Hometown`='" . sql_escape($user['Hometown']) . "', `got_voucher`='" . sql_escape($user['got_voucher']) . "', `arrival_date`='" . sql_escape($user['arrival_date']) . "', - `planned_arrival_date`='" . sql_escape($user['planned_arrival_date']) . "' + `planned_arrival_date`='" . sql_escape($user['planned_arrival_date']) . "', + `planned_departure_date`='" . sql_escape($user['planned_departure_date']) . "' WHERE `UID`='" . sql_escape($user['UID']) . "'"); } @@ -113,12 +115,16 @@ function User_is_freeloader($user) { * @param Angeltype $angeltype */ function Users_by_angeltype_inverted($angeltype) { - return sql_select(" + $result = sql_select(" SELECT `User`.* FROM `User` LEFT JOIN `UserAngelTypes` ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`='" . sql_escape($angeltype['id']) . "') WHERE `UserAngelTypes`.`id` IS NULL ORDER BY `Nick`"); + if ($result === false) { + engelsystem_error("Unable to load users."); + } + return $result; } /** @@ -132,7 +138,7 @@ function Users_by_angeltype($angeltype) { `User`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, - `UserAngelTypes`.`coordinator`, + `UserAngelTypes`.`supporter`, `UserDriverLicenses`.* FROM `User` JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id` @@ -158,6 +164,96 @@ function User_validate_Nick($nick) { } /** + * Validate user email address. + * + * @param string $mail + * The email address to validate + * @return ValidationResult + */ +function User_validate_mail($mail) { + $mail = strip_item($mail); + return new ValidationResult(check_email($mail), $mail); +} + +/** + * Validate user jabber address + * + * @param string $jabber + * Jabber-ID to validate + * @return ValidationResult + */ +function User_validate_jabber($jabber) { + $jabber = strip_item($jabber); + if ($jabber == '') { + // Empty is ok + return new ValidationResult(true, ''); + } + return new ValidationResult(check_email($jabber), $jabber); +} + +/** + * Validate the planned arrival date + * + * @param int $planned_arrival_date + * Unix timestamp + * @return ValidationResult + */ +function User_validate_planned_arrival_date($planned_arrival_date) { + if ($planned_arrival_date == null) { + // null is not okay + return new ValidationResult(false, time()); + } + $event_config = EventConfig(); + if ($event_config == null) { + // Nothing to validate against + return new ValidationResult(true, $planned_arrival_date); + } + if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) { + // Planned arrival can not be before buildup start date + return new ValidationResult(false, $event_config['buildup_start_date']); + } + if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) { + // Planned arrival can not be after teardown end date + return new ValidationResult(false, $event_config['teardown_end_date']); + } + return new ValidationResult(true, $planned_arrival_date); +} + +/** + * Validate the planned departure date + * + * @param int $planned_arrival_date + * Unix timestamp + * @param int $planned_departure_date + * Unix timestamp + * @return ValidationResult + */ +function User_validate_planned_departure_date($planned_arrival_date, $planned_departure_date) { + if ($planned_departure_date == null) { + // null is okay + return new ValidationResult(true, null); + } + if ($planned_arrival_date > $planned_departure_date) { + // departure cannot be before arrival + return new ValidationResult(false, $planned_arrival_date); + } + $event_config = EventConfig(); + if ($event_config == null) { + // Nothing to validate against + return new ValidationResult(true, $planned_departure_date); + } + if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) { + // Planned arrival can not be before buildup start date + return new ValidationResult(false, $event_config['buildup_start_date']); + } + if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) { + // Planned arrival can not be after teardown end date + return new ValidationResult(false, $event_config['teardown_end_date']); + } + return new ValidationResult(true, $planned_departure_date); +} + +/** * Returns user by id. * * @param $user_id UID @@ -165,7 +261,7 @@ function User_validate_Nick($nick) { function User($user_id) { $user_source = sql_select("SELECT * FROM `User` WHERE `UID`='" . sql_escape($user_id) . "' LIMIT 1"); if ($user_source === false) { - return false; + engelsystem_error("Unable to load user."); } if (count($user_source) > 0) { return $user_source[0]; |