2013-12-09 17:10:07 +01:00
|
|
|
<?php
|
2016-10-02 23:00:01 +02:00
|
|
|
use Engelsystem\ShiftsFilter;
|
2016-11-12 23:00:46 +01:00
|
|
|
use Engelsystem\ShiftSignupState;
|
2016-10-02 23:00:01 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
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;
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
|
|
|
$SQL = "SELECT * FROM (
|
2016-12-29 14:16:00 +01:00
|
|
|
SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`
|
2016-12-27 16:02:13 +01:00
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `Room` USING (`RID`)
|
|
|
|
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
|
2016-12-29 14:16:00 +01:00
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
|
|
|
|
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
|
|
|
|
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
|
|
|
|
AND `NeededAngelTypes`.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ")
|
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
|
|
|
AND `Shifts`.`PSID` IS NULL
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `Room` USING (`RID`)
|
|
|
|
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
|
2016-12-27 16:02:13 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
|
|
|
|
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
|
2016-12-29 14:16:00 +01:00
|
|
|
AND `NeededAngelTypes`.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ")
|
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
|
|
|
AND NOT `Shifts`.`PSID` IS NULL) as tmp_shifts
|
|
|
|
|
2016-12-29 14:51:43 +01:00
|
|
|
ORDER BY `start`";
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = sql_select($SQL);
|
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error("Unable to load shifts by filter.");
|
|
|
|
}
|
|
|
|
return $result;
|
2016-10-02 23:00:01 +02:00
|
|
|
}
|
2014-12-07 17:07:19 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
|
|
|
$SQL = "SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup`
|
2016-12-27 23:02:05 +01:00
|
|
|
FROM `Shifts`
|
2016-12-28 00:16:59 +01:00
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
2016-12-27 23:02:05 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
|
|
|
|
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
|
2016-12-28 00:48:16 +01:00
|
|
|
AND `Shifts`.`PSID` IS NULL
|
2016-12-28 14:32:26 +01:00
|
|
|
|
2016-12-28 00:48:16 +01:00
|
|
|
UNION
|
2016-12-28 14:32:26 +01:00
|
|
|
|
2016-12-28 01:09:40 +01:00
|
|
|
SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup`
|
2016-12-28 00:48:16 +01:00
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
|
|
|
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
|
|
|
|
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
|
|
|
|
AND NOT `Shifts`.`PSID` IS NULL";
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = sql_select($SQL);
|
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error("Unable to load needed angeltypes by filter.");
|
|
|
|
}
|
|
|
|
return $result;
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
|
|
|
|
{
|
|
|
|
$result = sql_select("SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup`
|
2016-12-27 23:02:05 +01:00
|
|
|
FROM `Shifts`
|
2016-12-28 00:16:59 +01:00
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
2016-12-27 23:02:05 +01:00
|
|
|
WHERE `Shifts`.`SID`=" . sql_escape($shift['SID']) . "
|
|
|
|
AND `AngelTypes`.`id`=" . sql_escape($angeltype['id']) . "
|
2016-12-28 00:48:16 +01:00
|
|
|
AND `Shifts`.`PSID` IS NULL
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
2016-12-28 01:09:40 +01:00
|
|
|
SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup`
|
2016-12-28 00:16:59 +01:00
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
|
|
|
WHERE `Shifts`.`SID`=" . sql_escape($shift['SID']) . "
|
|
|
|
AND `AngelTypes`.`id`=" . sql_escape($angeltype['id']) . "
|
2016-12-28 00:48:16 +01:00
|
|
|
AND NOT `Shifts`.`PSID` IS NULL");
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error("Unable to load needed angeltypes by filter.");
|
|
|
|
}
|
|
|
|
if (count($result) == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $result[0];
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
|
|
|
$SQL = "SELECT `User`.`Nick`, `User`.`email`, `User`.`email_shiftinfo`, `User`.`Sprache`, `User`.`Gekommen`, `ShiftEntry`.`UID`, `ShiftEntry`.`TID`, `ShiftEntry`.`SID`, `ShiftEntry`.`Comment`, `ShiftEntry`.`freeloaded`
|
2016-12-27 23:02:05 +01:00
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID`
|
|
|
|
JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID`
|
|
|
|
WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ")
|
|
|
|
AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime() . "
|
|
|
|
ORDER BY `Shifts`.`start`";
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = sql_select($SQL);
|
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error("Unable to load shift entries by filter.");
|
|
|
|
}
|
|
|
|
return $result;
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 22:23:18 +01:00
|
|
|
/**
|
|
|
|
* Check if a shift collides with other shifts (in time).
|
2016-10-02 23:00:01 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param Shift $shift
|
|
|
|
* @param array<Shift> $shifts
|
2014-12-25 22:23:18 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_collides($shift, $shifts)
|
|
|
|
{
|
|
|
|
foreach ($shifts as $other_shift) {
|
|
|
|
if ($shift['SID'] != $other_shift['SID']) {
|
|
|
|
if (! ($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
return false;
|
2014-12-25 22:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 12:50:52 +01:00
|
|
|
/**
|
2016-11-12 23:00:46 +01:00
|
|
|
* Returns the number of needed angels/free shift entries for an angeltype.
|
2016-11-12 12:50:52 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_free_entries($needed_angeltype, $shift_entries)
|
|
|
|
{
|
|
|
|
$taken = 0;
|
|
|
|
foreach ($shift_entries as $shift_entry) {
|
|
|
|
if ($shift_entry['freeloaded'] == 0) {
|
|
|
|
$taken ++;
|
|
|
|
}
|
2016-11-12 12:50:52 +01:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
return max(0, $needed_angeltype['count'] - $taken);
|
2016-11-12 12:50:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 22:23:18 +01:00
|
|
|
/**
|
2016-12-24 17:00:03 +01:00
|
|
|
* Check if shift signup is allowed from the end users point of view (no admin like privileges)
|
2014-12-25 22:23:18 +01:00
|
|
|
*
|
2016-11-12 20:15:25 +01:00
|
|
|
* @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
|
2016-12-24 17:00:03 +01:00
|
|
|
* @param boolean $angeltype_supporter
|
|
|
|
* True, if the user has angeltype supporter rights for the angeltype, which enables him to sign somebody up for the shift.
|
2014-12-25 22:23:18 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_signup_allowed_angel($user, $shift, $angeltype, $user_angeltype, $user_shifts, $needed_angeltype, $shift_entries)
|
|
|
|
{
|
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
2016-11-12 23:00:46 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($user['Gekommen'] == 0) {
|
|
|
|
return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries);
|
|
|
|
}
|
2016-12-27 19:13:24 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($user_shifts == null) {
|
|
|
|
$user_shifts = Shifts_by_user($user);
|
|
|
|
}
|
2016-11-14 17:58:15 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$signed_up = false;
|
|
|
|
foreach ($user_shifts as $user_shift) {
|
|
|
|
if ($user_shift['SID'] == $shift['SID']) {
|
|
|
|
$signed_up = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-14 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($signed_up) {
|
|
|
|
// you cannot join if you already singed up for this shift
|
2016-11-14 17:58:15 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-14 17:58:15 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (time() > $shift['start']) {
|
|
|
|
// you can only join if the shift is in future
|
2016-11-12 23:00:46 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if ($free_entries == 0) {
|
|
|
|
// you cannot join if shift is full
|
2016-11-12 23:00:46 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-12-24 17:00:03 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($user_angeltype == null) {
|
|
|
|
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
|
|
|
|
}
|
2014-12-25 22:23:18 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($user_angeltype == null || ($angeltype['no_self_signup'] == 1 && $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
|
2016-11-12 23:00:46 +01:00
|
|
|
// you cannot join if you are not confirmed
|
2016-12-24 17:00:03 +01:00
|
|
|
// you cannot join if angeltype has no self signup
|
|
|
|
|
2016-11-12 23:00:46 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 23:00:46 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (Shift_collides($shift, $user_shifts)) {
|
|
|
|
// you cannot join if user alread joined a parallel or this shift
|
2016-11-12 23:00:46 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 23:00:46 +01:00
|
|
|
|
|
|
|
// Hooray, shift is free for you!
|
|
|
|
return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
|
2014-12-25 22:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:47:10 +01:00
|
|
|
/**
|
|
|
|
* Check if an angeltype supporter can sign up a user to a shift.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_signup_allowed_angeltype_supporter($angeltype, $needed_angeltype, $shift_entries)
|
|
|
|
{
|
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
|
|
|
if ($free_entries == 0) {
|
|
|
|
return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries);
|
|
|
|
}
|
2016-12-26 23:47:10 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
|
2016-12-26 23:47:10 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 17:00:03 +01:00
|
|
|
/**
|
|
|
|
* Check if an admin can sign up a user to a shift.
|
|
|
|
*
|
|
|
|
* @param Shift $shift
|
|
|
|
* The shift
|
|
|
|
* @param AngelType $angeltype
|
|
|
|
* The angeltype to which the user wants to sign up
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_signup_allowed_admin($angeltype, $needed_angeltype, $shift_entries)
|
|
|
|
{
|
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
2016-12-29 15:24:57 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($free_entries == 0) {
|
|
|
|
// User shift admins may join anybody in every shift
|
2016-12-24 17:00:03 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-12-24 17:00:03 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::FREE, $free_entries);
|
2016-12-24 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_signup_allowed($signup_user, $shift, $angeltype, $user_angeltype = null, $user_shifts = null, $needed_angeltype, $shift_entries)
|
|
|
|
{
|
|
|
|
global $user, $privileges;
|
2016-12-24 17:00:03 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges)) {
|
|
|
|
return Shift_signup_allowed_admin($angeltype, $needed_angeltype, $shift_entries);
|
|
|
|
}
|
2016-12-24 17:00:03 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (in_array('shiftentry_edit_angeltype_supporter', $privileges) && User_is_AngelType_supporter($user, $angeltype)) {
|
|
|
|
return Shift_signup_allowed_angeltype_supporter($angeltype, $needed_angeltype, $shift_entries);
|
|
|
|
}
|
2016-12-24 17:00:03 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return Shift_signup_allowed_angel($signup_user, $shift, $angeltype, $user_angeltype, $user_shifts, $needed_angeltype, $shift_entries);
|
2016-12-24 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:48:35 +01:00
|
|
|
/**
|
|
|
|
* Delete a shift by its external id.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_delete_by_psid($shift_psid)
|
|
|
|
{
|
|
|
|
return sql_query("DELETE FROM `Shifts` WHERE `PSID`='" . sql_escape($shift_psid) . "'");
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a shift.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_delete($shift_id)
|
|
|
|
{
|
|
|
|
mail_shift_delete(Shift($shift_id));
|
2014-12-17 17:22:35 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'");
|
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error('Unable to delete shift.');
|
|
|
|
}
|
|
|
|
return $result;
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
/**
|
|
|
|
* Update a shift.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_update($shift)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
$shift['name'] = ShiftType($shift['shifttype_id'])['name'];
|
|
|
|
mail_shift_change(Shift($shift['SID']), $shift);
|
2014-12-17 17:22:35 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return sql_query("UPDATE `Shifts` SET
|
2014-12-28 13:44:56 +01:00
|
|
|
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
|
|
|
|
`start`='" . sql_escape($shift['start']) . "',
|
|
|
|
`end`='" . sql_escape($shift['end']) . "',
|
|
|
|
`RID`='" . sql_escape($shift['RID']) . "',
|
2014-12-17 17:22:35 +01:00
|
|
|
`title`=" . sql_null($shift['title']) . ",
|
2014-12-07 17:41:40 +01:00
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
2015-08-15 23:36:38 +02:00
|
|
|
`PSID`=" . sql_null($shift['PSID']) . ",
|
|
|
|
`edited_by_user_id`='" . sql_escape($user['UID']) . "',
|
|
|
|
`edited_at_timestamp`=" . time() . "
|
2014-12-28 13:44:56 +01:00
|
|
|
WHERE `SID`='" . sql_escape($shift['SID']) . "'");
|
2014-12-07 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a shift by its external id.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_update_by_psid($shift)
|
|
|
|
{
|
|
|
|
$shift_source = sql_select("SELECT `SID` FROM `Shifts` WHERE `PSID`=" . $shift['PSID']);
|
|
|
|
if ($shift_source === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count($shift_source) == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$shift['SID'] = $shift_source[0]['SID'];
|
|
|
|
return Shift_update($shift);
|
2014-12-07 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:34:29 +01:00
|
|
|
/**
|
|
|
|
* Create a new shift.
|
2014-12-07 17:41:40 +01:00
|
|
|
*
|
2014-12-07 17:34:29 +01:00
|
|
|
* @return new shift id or false
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_create($shift)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
$result = sql_query("INSERT INTO `Shifts` SET
|
2014-12-28 13:44:56 +01:00
|
|
|
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
|
|
|
|
`start`='" . sql_escape($shift['start']) . "',
|
|
|
|
`end`='" . sql_escape($shift['end']) . "',
|
|
|
|
`RID`='" . sql_escape($shift['RID']) . "',
|
2014-12-17 17:22:35 +01:00
|
|
|
`title`=" . sql_null($shift['title']) . ",
|
2014-12-28 13:57:50 +01:00
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
2015-08-15 23:36:38 +02:00
|
|
|
`PSID`=" . sql_null($shift['PSID']) . ",
|
|
|
|
`created_by_user_id`='" . sql_escape($user['UID']) . "',
|
|
|
|
`created_at_timestamp`=" . time());
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($result === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return sql_id();
|
2014-12-07 17:34:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return users shifts.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shifts_by_user($user, $include_freeload_comments = false)
|
|
|
|
{
|
|
|
|
$result = sql_select("
|
2016-11-25 13:38:16 +01:00
|
|
|
SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`,
|
|
|
|
`ShiftEntry`.`id`, `ShiftEntry`.`SID`, `ShiftEntry`.`TID`, `ShiftEntry`.`UID`, `ShiftEntry`.`freeloaded`, `ShiftEntry`.`Comment`,
|
2016-11-25 14:27:47 +01:00
|
|
|
" . ($include_freeload_comments ? "`ShiftEntry`.`freeload_comment`, " : "") . "
|
2016-11-25 13:38:16 +01:00
|
|
|
`Shifts`.*, `Room`.*
|
2014-08-23 01:55:18 +02:00
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
|
2014-12-17 17:22:35 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2014-08-23 01:55:18 +02:00
|
|
|
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
|
2014-12-28 13:44:56 +01:00
|
|
|
WHERE `UID`='" . sql_escape($user['UID']) . "'
|
2014-08-23 01:55:18 +02:00
|
|
|
ORDER BY `start`
|
|
|
|
");
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($result === false) {
|
|
|
|
engelsystem_error('Unable to load users shifts.');
|
|
|
|
}
|
|
|
|
return $result;
|
2014-08-23 01:55:18 +02:00
|
|
|
}
|
2013-12-09 17:10:07 +01:00
|
|
|
|
2013-12-29 15:08:21 +01:00
|
|
|
/**
|
|
|
|
* Returns Shift by id.
|
|
|
|
*
|
2016-09-29 12:45:06 +02:00
|
|
|
* @param $shift_id Shift
|
2014-08-23 01:55:18 +02:00
|
|
|
* ID
|
2013-12-29 15:08:21 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift($shift_id)
|
|
|
|
{
|
|
|
|
$shifts_source = sql_select("
|
2014-12-17 17:22:35 +01:00
|
|
|
SELECT `Shifts`.*, `ShiftTypes`.`name`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2016-09-29 12:45:06 +02:00
|
|
|
WHERE `SID`='" . sql_escape($shift_id) . "'");
|
2017-01-02 03:57:23 +01:00
|
|
|
$shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift_id) . "'");
|
2014-08-23 01:55:18 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($shifts_source === false) {
|
|
|
|
engelsystem_error('Unable to load shift.');
|
|
|
|
}
|
2016-10-04 21:20:38 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (empty($shifts_source)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-04 21:20:38 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = $shifts_source[0];
|
2016-10-04 21:20:38 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result['ShiftEntry'] = $shiftsEntry_source;
|
|
|
|
$result['NeedAngels'] = [];
|
2016-10-04 21:20:38 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$temp = NeededAngelTypes_by_shift($shift_id);
|
|
|
|
foreach ($temp as $e) {
|
|
|
|
$result['NeedAngels'][] = [
|
2016-10-04 21:20:38 +02:00
|
|
|
'TID' => $e['angel_type_id'],
|
|
|
|
'count' => $e['count'],
|
|
|
|
'restricted' => $e['restricted'],
|
2017-01-02 03:57:23 +01:00
|
|
|
'taken' => $e['taken']
|
2016-10-04 21:20:38 +02:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-10-04 21:20:38 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $result;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
/**
|
|
|
|
* Returns all shifts with needed angeltypes and count of subscribed jobs.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shifts()
|
|
|
|
{
|
|
|
|
$shifts_source = sql_select("
|
2014-12-17 17:22:35 +01:00
|
|
|
SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
|
2013-12-09 17:10:07 +01:00
|
|
|
FROM `Shifts`
|
2014-12-17 17:22:35 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2013-12-09 17:10:07 +01:00
|
|
|
JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID`
|
|
|
|
");
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($shifts_source === false) {
|
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
|
|
|
|
foreach ($shifts_source as &$shift) {
|
|
|
|
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
|
|
|
|
if ($needed_angeltypes === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-23 01:55:18 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift['angeltypes'] = $needed_angeltypes;
|
|
|
|
}
|
2013-12-09 17:17:23 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $shifts_source;
|
2013-12-09 17:10:07 +01:00
|
|
|
}
|