2013-12-09 17:10:07 +01:00
|
|
|
<?php
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
use Engelsystem\Database\DB;
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
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-11-24 12:01:19 +01:00
|
|
|
/**
|
|
|
|
* @param array $angeltype
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-12-25 23:12:52 +01:00
|
|
|
function Shifts_by_angeltype($angeltype)
|
|
|
|
{
|
2017-11-24 12:01:19 +01:00
|
|
|
return DB::select('
|
|
|
|
SELECT DISTINCT `Shifts`.* FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID`
|
|
|
|
WHERE `NeededAngelTypes`.`angel_type_id` = ?
|
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
|
|
|
AND `Shifts`.`PSID` IS NULL
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT DISTINCT `Shifts`.* FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id` = `Shifts`.`RID`
|
|
|
|
WHERE `NeededAngelTypes`.`angel_type_id` = ?
|
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
|
|
|
AND NOT `Shifts`.`PSID` IS NULL
|
|
|
|
', [$angeltype['id'], $angeltype['id']]);
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:57:57 +01:00
|
|
|
/**
|
2017-12-25 23:12:52 +01:00
|
|
|
* Returns every shift with needed angels in the given time range.
|
|
|
|
*
|
|
|
|
* @param int $start timestamp
|
|
|
|
* @param int $end timestamp
|
|
|
|
* @return array
|
2017-12-12 21:57:57 +01:00
|
|
|
*/
|
|
|
|
function Shifts_free($start, $end)
|
|
|
|
{
|
|
|
|
$shifts = Db::select("
|
2017-12-14 19:18:08 +01:00
|
|
|
SELECT * FROM (
|
|
|
|
SELECT *
|
|
|
|
FROM `Shifts`
|
|
|
|
WHERE (`end` > ? AND `start` < ?)
|
|
|
|
AND (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
|
|
|
|
> (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
|
2017-12-14 19:39:47 +01:00
|
|
|
AND `Shifts`.`PSID` IS NULL
|
2017-12-14 19:18:08 +01:00
|
|
|
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
FROM `Shifts`
|
|
|
|
WHERE (`end` > ? AND `start` < ?)
|
|
|
|
AND (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`)
|
|
|
|
> (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
|
2017-12-14 19:39:47 +01:00
|
|
|
AND NOT `Shifts`.`PSID` IS NULL
|
2017-12-25 23:12:52 +01:00
|
|
|
) AS `tmp`
|
2017-12-14 19:18:08 +01:00
|
|
|
ORDER BY `tmp`.`start`
|
2017-12-12 21:57:57 +01:00
|
|
|
", [
|
2017-12-25 23:12:52 +01:00
|
|
|
$start,
|
|
|
|
$end,
|
|
|
|
$start,
|
|
|
|
$end
|
|
|
|
]);
|
2017-12-12 21:57:57 +01:00
|
|
|
$free_shifts = [];
|
|
|
|
foreach ($shifts as $shift) {
|
|
|
|
$free_shifts[] = Shift($shift['SID']);
|
|
|
|
}
|
|
|
|
return $free_shifts;
|
|
|
|
}
|
|
|
|
|
2017-12-10 18:56:40 +01:00
|
|
|
/**
|
|
|
|
* Returns all shifts with a PSID (from frab import)
|
2017-12-25 23:12:52 +01:00
|
|
|
*
|
|
|
|
* @return array[]
|
2017-12-10 18:56:40 +01:00
|
|
|
*/
|
2017-12-25 23:12:52 +01:00
|
|
|
function Shifts_from_frab()
|
|
|
|
{
|
2017-12-10 18:56:40 +01:00
|
|
|
return DB::select('SELECT * FROM `Shifts` WHERE `PSID` IS NOT NULL ORDER BY `start`');
|
|
|
|
}
|
2017-11-24 12:01:19 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param array $room
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shifts_by_room($room)
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]);
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param ShiftsFilter $shiftsFilter
|
|
|
|
* @return array[]
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
2017-01-21 19:37:42 +01:00
|
|
|
$sql = 'SELECT * FROM (
|
2017-01-02 15:43:36 +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`
|
2017-11-24 12:01:19 +01:00
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID`
|
2017-01-21 19:37:42 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
|
2017-01-22 01:16:00 +01:00
|
|
|
AND `start` BETWEEN ? AND ?
|
2017-01-21 19:37:42 +01:00
|
|
|
AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
|
2016-12-29 14:16:00 +01:00
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
|
|
|
AND `Shifts`.`PSID` IS NULL
|
2017-01-22 01:02:52 +01:00
|
|
|
|
2016-12-29 14:16:00 +01:00
|
|
|
UNION
|
2017-01-22 01:02:52 +01:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` AS `room_name`
|
2016-12-29 14:16:00 +01:00
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `Room` USING (`RID`)
|
|
|
|
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
|
2017-01-21 19:37:42 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
|
2017-01-22 01:16:00 +01:00
|
|
|
AND `start` BETWEEN ? AND ?
|
2017-01-21 19:37:42 +01:00
|
|
|
AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
|
2016-12-29 14:16:00 +01:00
|
|
|
AND `NeededAngelTypes`.`count` > 0
|
2017-01-02 15:43:36 +01:00
|
|
|
AND NOT `Shifts`.`PSID` IS NULL) AS tmp_shifts
|
2017-01-22 01:02:52 +01:00
|
|
|
|
2018-12-28 15:36:45 +01:00
|
|
|
ORDER BY `room_name`, `start`';
|
2017-08-30 00:07:01 +02:00
|
|
|
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select(
|
2017-01-22 01:16:00 +01:00
|
|
|
$sql,
|
|
|
|
[
|
|
|
|
$shiftsFilter->getStartTime(),
|
|
|
|
$shiftsFilter->getEndTime(),
|
|
|
|
$shiftsFilter->getStartTime(),
|
|
|
|
$shiftsFilter->getEndTime(),
|
|
|
|
]
|
|
|
|
);
|
2016-10-02 23:00:01 +02:00
|
|
|
}
|
2014-12-07 17:07:19 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param ShiftsFilter $shiftsFilter
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array[]
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
2017-01-21 19:37:42 +01:00
|
|
|
$sql = '
|
2017-01-02 15:43:36 +01:00
|
|
|
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`
|
2017-01-21 19:37:42 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
|
2017-01-22 01:16:00 +01:00
|
|
|
AND `start` BETWEEN ? AND ?
|
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
|
|
|
|
2017-01-02 15:43:36 +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`
|
2017-01-21 19:37:42 +01:00
|
|
|
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
|
2017-01-22 01:16:00 +01:00
|
|
|
AND `start` BETWEEN ? AND ?
|
2017-01-21 19:37:42 +01:00
|
|
|
AND NOT `Shifts`.`PSID` IS NULL';
|
2017-07-28 19:15:52 +02:00
|
|
|
|
|
|
|
return DB::select(
|
2017-01-22 01:16:00 +01:00
|
|
|
$sql,
|
|
|
|
[
|
|
|
|
$shiftsFilter->getStartTime(),
|
|
|
|
$shiftsFilter->getEndTime(),
|
|
|
|
$shiftsFilter->getStartTime(),
|
|
|
|
$shiftsFilter->getEndTime(),
|
|
|
|
]
|
|
|
|
);
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param array $shift
|
|
|
|
* @param array $angeltype
|
2018-08-29 18:08:45 +02:00
|
|
|
* @return array|null
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
return DB::selectOne('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT
|
|
|
|
`NeededAngelTypes`.*,
|
|
|
|
`Shifts`.`SID`,
|
|
|
|
`AngelTypes`.`id`,
|
|
|
|
`AngelTypes`.`name`,
|
|
|
|
`AngelTypes`.`restricted`,
|
|
|
|
`AngelTypes`.`no_self_signup`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
|
|
|
WHERE `Shifts`.`SID`=?
|
|
|
|
AND `AngelTypes`.`id`=?
|
|
|
|
AND `Shifts`.`PSID` IS NULL
|
2017-01-22 01:02:52 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
UNION
|
2017-01-22 01:02:52 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT
|
|
|
|
`NeededAngelTypes`.*,
|
|
|
|
`Shifts`.`SID`,
|
|
|
|
`AngelTypes`.`id`,
|
|
|
|
`AngelTypes`.`name`,
|
|
|
|
`AngelTypes`.`restricted`,
|
|
|
|
`AngelTypes`.`no_self_signup`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
|
|
|
|
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
|
|
|
|
WHERE `Shifts`.`SID`=?
|
|
|
|
AND `AngelTypes`.`id`=?
|
|
|
|
AND NOT `Shifts`.`PSID` IS NULL
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$shift['SID'],
|
|
|
|
$angeltype['id'],
|
|
|
|
$shift['SID'],
|
|
|
|
$angeltype['id']
|
|
|
|
]
|
|
|
|
);
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param ShiftsFilter $shiftsFilter
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
|
|
|
|
{
|
2018-10-17 01:30:10 +02:00
|
|
|
$sql = sprintf('
|
|
|
|
SELECT
|
|
|
|
users.*,
|
|
|
|
`ShiftEntry`.`UID`,
|
|
|
|
`ShiftEntry`.`TID`,
|
|
|
|
`ShiftEntry`.`SID`,
|
|
|
|
`ShiftEntry`.`Comment`,
|
|
|
|
`ShiftEntry`.`freeloaded`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID`
|
|
|
|
JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
|
|
|
|
WHERE `Shifts`.`RID` IN (%s)
|
|
|
|
AND `start` BETWEEN ? AND ?
|
|
|
|
ORDER BY `Shifts`.`start`
|
|
|
|
',
|
|
|
|
implode(',', $shiftsFilter->getRooms())
|
|
|
|
);
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select(
|
2017-01-22 01:16:00 +01:00
|
|
|
$sql,
|
|
|
|
[
|
|
|
|
$shiftsFilter->getStartTime(),
|
|
|
|
$shiftsFilter->getEndTime(),
|
|
|
|
]
|
|
|
|
);
|
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-03 03:22:48 +01:00
|
|
|
* @param array $shift
|
|
|
|
* @param array $shifts
|
|
|
|
* @return bool
|
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']) {
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) {
|
2017-01-02 03:57:23 +01:00
|
|
|
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.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param array $needed_angeltype
|
|
|
|
* @param array[] $shift_entries
|
|
|
|
* @return int
|
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) {
|
2017-01-02 15:43:36 +01:00
|
|
|
$taken++;
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 12:50:52 +01:00
|
|
|
}
|
2018-08-29 23:10:05 +02:00
|
|
|
|
|
|
|
$neededAngels = !empty($needed_angeltype) ? $needed_angeltype['count'] : 0;
|
|
|
|
return max(0, $neededAngels - $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
|
|
|
*
|
2018-10-17 01:30:10 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift The shift
|
|
|
|
* @param array $angeltype The angeltype to which the user wants to sign up
|
|
|
|
* @param array|null $user_angeltype
|
|
|
|
* @param array|null $user_shifts List of the users shifts
|
|
|
|
* @param array $needed_angeltype
|
|
|
|
* @param array[] $shift_entries
|
|
|
|
* @return ShiftSignupState
|
2014-12-25 22:23:18 +01:00
|
|
|
*/
|
2017-01-02 15:43:36 +01:00
|
|
|
function Shift_signup_allowed_angel(
|
|
|
|
$user,
|
|
|
|
$shift,
|
|
|
|
$angeltype,
|
|
|
|
$user_angeltype,
|
|
|
|
$user_shifts,
|
|
|
|
$needed_angeltype,
|
|
|
|
$shift_entries
|
|
|
|
) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
if (config('signup_requires_arrival') && !$user->state->arrived) {
|
2017-12-20 23:05:46 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::NOT_ARRIVED, $free_entries);
|
2017-08-31 12:25:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:47:26 +01:00
|
|
|
if (empty($user_shifts)) {
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_shifts = Shifts_by_user($user->id);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +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 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($signed_up) {
|
2017-12-25 21:29:00 +01:00
|
|
|
// you cannot join if you already signed up for this shift
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (time() > $shift['start']) {
|
|
|
|
// you can only join if the shift is in future
|
2017-01-02 15:43:36 +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
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-01-14 17:47:26 +01:00
|
|
|
if (empty($user_angeltype)) {
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (
|
2018-01-14 17:47:26 +01:00
|
|
|
empty($user_angeltype)
|
|
|
|
|| ($angeltype['no_self_signup'] == 1 && !empty($user_angeltype))
|
|
|
|
|| ($angeltype['restricted'] == 1 && !empty($user_angeltype) && !isset($user_angeltype['confirm_user_id']))
|
2017-01-02 15:43:36 +01:00
|
|
|
) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// you cannot join if user is not of this angel type
|
2017-01-02 15:43:36 +01:00
|
|
|
// you cannot join if you are not confirmed
|
|
|
|
// you cannot join if angeltype has no self signup
|
|
|
|
|
|
|
|
return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +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
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +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-03 03:22:48 +01:00
|
|
|
*
|
2017-01-03 14:19:38 +01:00
|
|
|
* @param array $needed_angeltype
|
|
|
|
* @param array[] $shift_entries
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return ShiftSignupState
|
2016-12-26 23:47:10 +01:00
|
|
|
*/
|
2017-01-03 14:19:38 +01:00
|
|
|
function Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
|
|
|
if ($free_entries == 0) {
|
|
|
|
return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries);
|
|
|
|
}
|
2017-01-02 15:43:36 +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.
|
|
|
|
*
|
2017-01-03 14:19:38 +01:00
|
|
|
* @param array $needed_angeltype
|
|
|
|
* @param array[] $shift_entries
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return ShiftSignupState
|
2016-12-24 17:00:03 +01:00
|
|
|
*/
|
2017-01-03 14:19:38 +01:00
|
|
|
function Shift_signup_allowed_admin($needed_angeltype, $shift_entries)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
|
|
|
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($free_entries == 0) {
|
|
|
|
// User shift admins may join anybody in every shift
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +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
|
|
|
}
|
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
/**
|
|
|
|
* Check if an angel can signout from a shift.
|
2017-12-25 23:12:52 +01:00
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param array $shift The shift
|
|
|
|
* @param array $angeltype The angeltype
|
|
|
|
* @param int $signout_user_id The user that was signed up for the shift
|
2017-12-19 20:58:01 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-10 03:10:28 +02:00
|
|
|
function Shift_signout_allowed($shift, $angeltype, $signout_user_id)
|
2017-12-25 23:12:52 +01:00
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
$user = auth()->user();
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
// user shifts admin can sign out any user at any time
|
2018-11-12 14:41:23 +01:00
|
|
|
if (auth()->can('user_shifts_admin')) {
|
2017-12-19 20:58:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
// angeltype supporter can sign out any user at any time from their supported angeltype
|
|
|
|
if (
|
2018-11-12 14:41:23 +01:00
|
|
|
auth()->can('shiftentry_edit_angeltype_supporter')
|
2017-12-19 20:58:01 +01:00
|
|
|
&& User_is_AngelType_supporter($user, $angeltype)
|
2017-12-25 23:12:52 +01:00
|
|
|
) {
|
2017-12-19 20:58:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
if ($signout_user_id == $user->id && $shift['start'] > time() + config('last_unsubscribe') * 3600) {
|
2017-12-19 20:58:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-24 17:00:03 +01:00
|
|
|
/**
|
|
|
|
* Check if an angel can sign up for given shift.
|
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $signup_user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift The shift
|
|
|
|
* @param array $angeltype The angeltype to which the user wants to sign up
|
|
|
|
* @param array|null $user_angeltype
|
|
|
|
* @param array|null $user_shifts List of the users shifts
|
|
|
|
* @param array $needed_angeltype
|
|
|
|
* @param array[] $shift_entries
|
|
|
|
* @return ShiftSignupState
|
2016-12-24 17:00:03 +01:00
|
|
|
*/
|
2017-01-02 15:43:36 +01:00
|
|
|
function Shift_signup_allowed(
|
|
|
|
$signup_user,
|
|
|
|
$shift,
|
|
|
|
$angeltype,
|
2017-03-03 08:31:25 +01:00
|
|
|
$user_angeltype,
|
|
|
|
$user_shifts,
|
2017-01-02 15:43:36 +01:00
|
|
|
$needed_angeltype,
|
|
|
|
$shift_entries
|
|
|
|
) {
|
2018-11-12 14:41:23 +01:00
|
|
|
if (auth()->can('user_shifts_admin')) {
|
2017-01-03 14:19:38 +01:00
|
|
|
return Shift_signup_allowed_admin($needed_angeltype, $shift_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (
|
2018-11-12 14:41:23 +01:00
|
|
|
auth()->can('shiftentry_edit_angeltype_supporter')
|
2018-10-10 03:10:28 +02:00
|
|
|
&& User_is_AngelType_supporter(auth()->user(), $angeltype)
|
2017-01-02 15:43:36 +01:00
|
|
|
) {
|
2017-01-03 14:19:38 +01:00
|
|
|
return Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +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-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $shift_psid
|
2014-12-07 17:48:35 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_delete_by_psid($shift_psid)
|
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::delete('DELETE FROM `Shifts` WHERE `PSID`=?', [$shift_psid]);
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a shift.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $shift_id
|
2014-12-07 17:48:35 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_delete($shift_id)
|
|
|
|
{
|
2017-07-28 18:50:00 +02:00
|
|
|
DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]);
|
2017-12-25 23:12:52 +01:00
|
|
|
mail_shift_delete(Shift($shift_id));
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
/**
|
|
|
|
* Update a shift.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param array $shift
|
2017-07-23 11:46:54 +02:00
|
|
|
* @return int Updated row count
|
2014-12-07 17:41:40 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_update($shift)
|
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
$user = auth()->user();
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift['name'] = ShiftType($shift['shifttype_id'])['name'];
|
|
|
|
mail_shift_change(Shift($shift['SID']), $shift);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-23 11:46:54 +02:00
|
|
|
return DB::update('
|
2017-01-02 15:43:36 +01:00
|
|
|
UPDATE `Shifts` SET
|
2017-01-21 13:58:53 +01:00
|
|
|
`shifttype_id` = ?,
|
|
|
|
`start` = ?,
|
|
|
|
`end` = ?,
|
|
|
|
`RID` = ?,
|
|
|
|
`title` = ?,
|
|
|
|
`URL` = ?,
|
|
|
|
`PSID` = ?,
|
|
|
|
`edited_by_user_id` = ?,
|
|
|
|
`edited_at_timestamp` = ?
|
|
|
|
WHERE `SID` = ?
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$shift['shifttype_id'],
|
|
|
|
$shift['start'],
|
|
|
|
$shift['end'],
|
|
|
|
$shift['RID'],
|
|
|
|
$shift['title'],
|
|
|
|
$shift['URL'],
|
|
|
|
$shift['PSID'],
|
2018-10-08 21:15:56 +02:00
|
|
|
$user->id,
|
2017-01-21 13:58:53 +01:00
|
|
|
time(),
|
|
|
|
$shift['SID']
|
|
|
|
]
|
|
|
|
);
|
2014-12-07 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a shift by its external id.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param array $shift
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return int
|
2017-08-30 00:07:01 +02:00
|
|
|
* @throws Exception
|
2014-12-07 17:41:40 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_update_by_psid($shift)
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$shift_source = DB::selectOne('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]);
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($shift_source)) {
|
2017-07-28 19:15:52 +02:00
|
|
|
throw new Exception('Shift not found.');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
$shift['SID'] = $shift_source['SID'];
|
2017-01-02 03:57:23 +01:00
|
|
|
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
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift
|
2017-07-28 19:15:52 +02:00
|
|
|
* @return int ID of the new created shift
|
2014-12-07 17:34:29 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift_create($shift)
|
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::insert('
|
|
|
|
INSERT INTO `Shifts` (
|
|
|
|
`shifttype_id`,
|
|
|
|
`start`,
|
|
|
|
`end`,
|
|
|
|
`RID`,
|
|
|
|
`title`,
|
|
|
|
`URL`,
|
|
|
|
`PSID`,
|
|
|
|
`created_by_user_id`,
|
2017-09-25 19:51:45 +02:00
|
|
|
`edited_at_timestamp`,
|
2017-01-21 13:58:53 +01:00
|
|
|
`created_at_timestamp`
|
|
|
|
)
|
2017-09-25 19:51:45 +02:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2017-01-21 13:58:53 +01:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$shift['shifttype_id'],
|
|
|
|
$shift['start'],
|
|
|
|
$shift['end'],
|
|
|
|
$shift['RID'],
|
|
|
|
$shift['title'],
|
|
|
|
$shift['URL'],
|
|
|
|
$shift['PSID'],
|
2018-10-17 01:30:10 +02:00
|
|
|
auth()->user()->id,
|
2017-01-21 13:58:53 +01:00
|
|
|
time(),
|
2017-09-25 19:51:45 +02:00
|
|
|
time(),
|
2017-01-21 13:58:53 +01:00
|
|
|
]
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2017-07-28 19:15:52 +02:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
return DB::getPdo()->lastInsertId();
|
2014-12-07 17:34:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return users shifts.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param int $userId
|
|
|
|
* @param bool $include_freeload_comments
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2014-12-07 17:34:29 +01:00
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function Shifts_by_user($userId, $include_freeload_comments = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select('
|
2017-12-25 23:12:52 +01:00
|
|
|
SELECT
|
|
|
|
`ShiftTypes`.`id` AS `shifttype_id`,
|
|
|
|
`ShiftTypes`.`name`,
|
|
|
|
`ShiftEntry`.`id`,
|
|
|
|
`ShiftEntry`.`SID`,
|
|
|
|
`ShiftEntry`.`TID`,
|
|
|
|
`ShiftEntry`.`UID`,
|
|
|
|
`ShiftEntry`.`freeloaded`,
|
|
|
|
`ShiftEntry`.`Comment`,
|
|
|
|
' . ($include_freeload_comments ? '`ShiftEntry`.`freeload_comment`, ' : '') . '
|
|
|
|
`Shifts`.*,
|
|
|
|
`Room`.*
|
2017-01-22 01:02:52 +01:00
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
|
2017-01-03 03:22:48 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2017-01-22 01:02:52 +01:00
|
|
|
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
|
|
|
|
WHERE `UID` = ?
|
2017-01-03 03:22:48 +01:00
|
|
|
ORDER BY `start`
|
2017-01-21 13:58:53 +01:00
|
|
|
',
|
|
|
|
[
|
2018-10-09 21:47:31 +02:00
|
|
|
$userId
|
2017-01-21 13:58:53 +01:00
|
|
|
]
|
|
|
|
);
|
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.
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param int $shift_id Shift ID
|
|
|
|
* @return array|null
|
2013-12-29 15:08:21 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Shift($shift_id)
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('
|
2014-12-17 17:22:35 +01:00
|
|
|
SELECT `Shifts`.*, `ShiftTypes`.`name`
|
2017-01-22 01:02:52 +01:00
|
|
|
FROM `Shifts`
|
2014-12-17 17:22:35 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `SID`=?', [$shift_id]);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
if (empty($result)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$shiftsEntry_source = DB::select('
|
|
|
|
SELECT `id`, `TID` , `UID` , `freeloaded`
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
WHERE `SID`=?', [$shift_id]);
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result['ShiftEntry'] = $shiftsEntry_source;
|
|
|
|
$result['NeedAngels'] = [];
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$angelTypes = NeededAngelTypes_by_shift($shift_id);
|
|
|
|
foreach ($angelTypes as $type) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$result['NeedAngels'][] = [
|
2017-01-21 13:58:53 +01:00
|
|
|
'TID' => $type['angel_type_id'],
|
|
|
|
'count' => $type['count'],
|
|
|
|
'restricted' => $type['restricted'],
|
|
|
|
'taken' => $type['taken']
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $result;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|