engelsystem/includes/model/Shifts_model.php

574 lines
18 KiB
PHP
Raw Normal View History

2013-12-09 17:10:07 +01:00
<?php
use Engelsystem\Database\Db;
2023-01-03 22:19:03 +01:00
use Engelsystem\Helpers\Carbon;
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
2023-01-18 13:02:11 +01:00
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\Shifts\ShiftSignupStatus;
2018-10-10 03:10:28 +02:00
use Engelsystem\Models\User\User;
2022-12-03 00:57:04 +01:00
use Engelsystem\Models\UserAngelType;
2016-10-02 23:00:01 +02:00
use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftSignupState;
2023-01-18 13:02:11 +01:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;
2016-10-02 23:00:01 +02:00
2017-11-24 12:01:19 +01:00
/**
2022-11-09 00:02:30 +01:00
* @param AngelType $angeltype
2017-11-24 12:01:19 +01:00
* @return array
*/
2022-11-09 00:02:30 +01:00
function Shifts_by_angeltype(AngelType $angeltype)
2017-12-25 23:12:52 +01:00
{
2022-06-16 22:50:52 +02:00
return Db::select('
2023-01-03 22:19:03 +01:00
SELECT DISTINCT `shifts`.* FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`shift_id` = `shifts`.`id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
2023-01-22 18:43:09 +01:00
WHERE `needed_angel_types`.`angel_type_id` = ?
AND s.shift_id IS NULL
2020-05-13 18:26:32 +02:00
2017-11-24 12:01:19 +01:00
UNION
2020-05-13 18:26:32 +02:00
2023-01-03 22:19:03 +01:00
SELECT DISTINCT `shifts`.* FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`room_id` = `shifts`.`room_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
2023-01-22 18:43:09 +01:00
WHERE `needed_angel_types`.`angel_type_id` = ?
AND NOT s.shift_id IS NULL
2022-11-09 00:02:30 +01:00
', [$angeltype->id, $angeltype->id]);
2017-11-24 12:01:19 +01:00
}
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.
*
2020-10-27 17:13:46 +01:00
* @param int $start timestamp
2022-11-09 00:02:30 +01:00
* @param int $end timestamp
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2023-01-03 22:19:03 +01:00
* @return Collection|Shift[]
2017-12-12 21:57:57 +01:00
*/
2020-10-27 17:13:46 +01:00
function Shifts_free($start, $end, ShiftsFilter $filter = null)
2017-12-12 21:57:57 +01:00
{
2023-01-03 22:19:03 +01:00
$start = Carbon::createFromTimestamp($start);
$end = Carbon::createFromTimestamp($end);
2020-10-27 17:13:46 +01:00
$shifts = Db::select('
2023-01-03 22:19:03 +01:00
SELECT *
FROM (
SELECT id, start
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE (`end` > ? AND `start` < ?)
2023-01-22 18:43:09 +01:00
AND (SELECT SUM(`count`) FROM `needed_angel_types` WHERE `needed_angel_types`.`shift_id`=`shifts`.`id`' . ($filter ? ' AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2023-01-18 13:02:11 +01:00
> (SELECT COUNT(*) FROM `shift_entries` WHERE `shift_entries`.`shift_id`=`shifts`.`id` AND shift_entries.`freeloaded`=0' . ($filter ? ' AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
AND s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2020-05-13 18:26:32 +02:00
UNION
2020-05-13 18:26:32 +02:00
2023-01-03 22:19:03 +01:00
SELECT id, start
FROM `shifts`
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE (`end` > ? AND `start` < ?)
2023-01-22 18:43:09 +01:00
AND (SELECT SUM(`count`) FROM `needed_angel_types` WHERE `needed_angel_types`.`room_id`=`shifts`.`room_id`' . ($filter ? ' AND needed_angel_types.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
2023-01-18 13:02:11 +01:00
> (SELECT COUNT(*) FROM `shift_entries` WHERE `shift_entries`.`shift_id`=`shifts`.`id` AND `freeloaded`=0' . ($filter ? ' AND shift_entries.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ')
AND NOT s.shift_id IS NULL
2023-01-03 22:19:03 +01:00
' . ($filter ? 'AND shifts.room_id IN (' . implode(',', $filter->getRooms()) . ')' : '') . '
2017-12-25 23:12:52 +01:00
) AS `tmp`
ORDER BY `tmp`.`start`
2020-10-27 17:13:46 +01:00
', [
2017-12-25 23:12:52 +01:00
$start,
$end,
$start,
$end,
2017-12-25 23:12:52 +01:00
]);
2017-12-12 21:57:57 +01:00
2023-01-03 22:19:03 +01:00
$shifts = collect($shifts);
return Shift::query()
->whereIn('id', $shifts->pluck('id')->toArray())
->get();
}
2017-01-03 03:22:48 +01:00
/**
* @param ShiftsFilter $shiftsFilter
2023-01-03 22:19:03 +01:00
* @return Shift[]|Collection
2017-01-03 03:22:48 +01:00
*/
2017-01-02 03:57:23 +01:00
function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
{
2020-05-13 18:26:32 +02:00
$sql = '
SELECT * FROM (
2023-01-03 22:19:03 +01:00
SELECT DISTINCT `shifts`.*, `shift_types`.`name`, `rooms`.`name` AS `room_name`
FROM `shifts`
JOIN `rooms` ON `shifts`.`room_id` = `rooms`.`id`
JOIN `shift_types` ON `shift_types`.`id` = `shifts`.`shift_type_id`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`shift_id` = `shifts`.`id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`room_id` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
2020-05-13 18:26:32 +02:00
AND `start` BETWEEN ? AND ?
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
2020-05-13 18:26:32 +02:00
AND s.shift_id IS NULL
UNION
2023-01-03 22:19:03 +01:00
SELECT DISTINCT `shifts`.*, `shift_types`.`name`, `rooms`.`name` AS `room_name`
FROM `shifts`
JOIN `rooms` ON `shifts`.`room_id` = `rooms`.`id`
JOIN `shift_types` ON `shift_types`.`id` = `shifts`.`shift_type_id`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`room_id`=`shifts`.`room_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`room_id` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
2020-05-13 18:26:32 +02:00
AND `start` BETWEEN ? AND ?
2023-01-22 18:43:09 +01:00
AND `needed_angel_types`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
2020-05-13 18:26:32 +02:00
AND NOT s.shift_id IS NULL
) AS tmp_shifts
ORDER BY `room_name`, `start`
';
2017-08-30 00:07:01 +02:00
2023-01-03 22:19:03 +01:00
$shiftsData = Db::select(
2017-01-22 01:16:00 +01:00
$sql,
[
2023-01-03 22:19:03 +01:00
$shiftsFilter->getStart(),
$shiftsFilter->getEnd(),
$shiftsFilter->getStart(),
$shiftsFilter->getEnd(),
2017-01-22 01:16:00 +01:00
]
);
2023-01-03 22:19:03 +01:00
$shifts = [];
foreach ($shiftsData as $shift) {
$shifts[] = (new Shift())->forceFill($shift);
}
return collect($shifts);
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
* @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 = '
2020-05-13 18:26:32 +02:00
SELECT
2023-01-22 18:43:09 +01:00
`needed_angel_types`.*,
2023-01-03 22:19:03 +01:00
`shifts`.`id` AS shift_id,
2022-11-09 00:02:30 +01:00
`angel_types`.`id`,
`angel_types`.`name`,
`angel_types`.`restricted`,
`angel_types`.`no_self_signup`
2023-01-03 22:19:03 +01:00
FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`shift_id`=`shifts`.`id`
JOIN `angel_types` ON `angel_types`.`id`= `needed_angel_types`.`angel_type_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`room_id` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND shifts.`start` BETWEEN ? AND ?
2020-05-13 18:26:32 +02:00
AND s.shift_id IS NULL
UNION
SELECT
2023-01-22 18:43:09 +01:00
`needed_angel_types`.*,
2023-01-03 22:19:03 +01:00
`shifts`.`id` AS shift_id,
2022-11-09 00:02:30 +01:00
`angel_types`.`id`,
`angel_types`.`name`,
`angel_types`.`restricted`,
`angel_types`.`no_self_signup`
2023-01-03 22:19:03 +01:00
FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`room_id`=`shifts`.`room_id`
JOIN `angel_types` ON `angel_types`.`id`= `needed_angel_types`.`angel_type_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`room_id` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND shifts.`start` BETWEEN ? AND ?
2020-05-13 18:26:32 +02:00
AND NOT s.shift_id IS NULL
';
2022-06-16 22:50:52 +02:00
return Db::select(
2017-01-22 01:16:00 +01:00
$sql,
[
2023-01-03 22:19:03 +01:00
$shiftsFilter->getStart(),
$shiftsFilter->getEnd(),
$shiftsFilter->getStart(),
$shiftsFilter->getEnd(),
2017-01-22 01:16:00 +01:00
]
);
2016-12-27 23:02:05 +01:00
}
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2022-11-09 00:02:30 +01:00
* @param AngelType $angeltype
* @return array|null
2017-01-03 03:22:48 +01:00
*/
2023-01-03 22:19:03 +01:00
function NeededAngeltype_by_Shift_and_Angeltype(Shift $shift, AngelType $angeltype)
2017-01-02 03:57:23 +01:00
{
2022-10-18 19:15:22 +02:00
return Db::selectOne(
'
2020-05-13 18:26:32 +02:00
SELECT
2023-01-22 18:43:09 +01:00
`needed_angel_types`.*,
2023-01-03 22:19:03 +01:00
`shifts`.`id` AS shift_id,
2022-11-09 00:02:30 +01:00
`angel_types`.`id`,
`angel_types`.`name`,
`angel_types`.`restricted`,
`angel_types`.`no_self_signup`
2023-01-03 22:19:03 +01:00
FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`shift_id`=`shifts`.`id`
JOIN `angel_types` ON `angel_types`.`id`= `needed_angel_types`.`angel_type_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`id`=?
2022-11-09 00:02:30 +01:00
AND `angel_types`.`id`=?
2020-05-13 18:26:32 +02:00
AND s.shift_id IS NULL
UNION
SELECT
2023-01-22 18:43:09 +01:00
`needed_angel_types`.*,
2023-01-03 22:19:03 +01:00
`shifts`.`id` AS shift_id,
2022-11-09 00:02:30 +01:00
`angel_types`.`id`,
`angel_types`.`name`,
`angel_types`.`restricted`,
`angel_types`.`no_self_signup`
2023-01-03 22:19:03 +01:00
FROM `shifts`
2023-01-22 18:43:09 +01:00
JOIN `needed_angel_types` ON `needed_angel_types`.`room_id`=`shifts`.`room_id`
JOIN `angel_types` ON `angel_types`.`id`= `needed_angel_types`.`angel_type_id`
2023-01-03 22:19:03 +01:00
LEFT JOIN schedule_shift AS s on shifts.id = s.shift_id
WHERE `shifts`.`id`=?
2022-11-09 00:02:30 +01:00
AND `angel_types`.`id`=?
2020-05-13 18:26:32 +02:00
AND NOT s.shift_id IS NULL
',
[
2023-01-03 22:19:03 +01:00
$shift->id,
2022-11-09 00:02:30 +01:00
$angeltype->id,
2023-01-03 22:19:03 +01:00
$shift->id,
$angeltype->id,
]
);
2016-12-27 23:02:05 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @param ShiftsFilter $shiftsFilter
2023-01-18 13:02:11 +01:00
* @return ShiftEntry[]|Collection
2017-01-03 03:22:48 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
{
2023-01-18 13:02:11 +01:00
return ShiftEntry::with('user')
->join('shifts', 'shifts.id', 'shift_entries.shift_id')
->whereIn('shifts.room_id', $shiftsFilter->getRooms())
->whereBetween('start', [$shiftsFilter->getStart(), $shiftsFilter->getEnd()])
->get();
2016-12-27 23:02:05 +01:00
}
/**
* Check if a shift collides with other shifts (in time).
2016-10-02 23:00:01 +02:00
*
2023-01-18 13:02:11 +01:00
* @param Shift $shift
2023-01-03 22:19:03 +01:00
* @param Shift[]|Collection $shifts
2017-01-03 03:22:48 +01:00
* @return bool
*/
2023-01-03 22:19:03 +01:00
function Shift_collides(Shift $shift, $shifts)
2017-01-02 03:57:23 +01:00
{
foreach ($shifts as $other_shift) {
2023-01-03 22:19:03 +01:00
if ($shift->id != $other_shift->id) {
if (
!(
2023-01-18 13:02:11 +01:00
$shift->start->timestamp >= $other_shift->end->timestamp
|| $shift->end->timestamp <= $other_shift->start->timestamp
2023-01-03 22:19:03 +01:00
)
) {
2017-01-02 03:57:23 +01:00
return true;
}
}
}
2017-01-02 03:57:23 +01:00
return false;
}
/**
* Returns the number of needed angels/free shift entries for an angeltype.
2017-01-03 03:22:48 +01:00
*
2023-01-18 13:02:11 +01:00
* @param AngelType $needed_angeltype
* @param ShiftEntry[]|Collection $shift_entries
2017-01-03 03:22:48 +01:00
* @return int
*/
2022-11-09 00:02:30 +01:00
function Shift_free_entries(AngelType $needed_angeltype, $shift_entries)
2017-01-02 03:57:23 +01:00
{
$taken = 0;
foreach ($shift_entries as $shift_entry) {
2023-01-18 13:02:11 +01:00
if (!$shift_entry->freeloaded) {
2017-01-02 15:43:36 +01:00
$taken++;
2017-01-02 03:57:23 +01:00
}
}
2018-08-29 23:10:05 +02:00
2022-11-09 00:02:30 +01:00
$neededAngels = $needed_angeltype->count ?: 0;
2018-08-29 23:10:05 +02:00
return max(0, $neededAngels - $taken);
}
/**
* Check if shift signup is allowed from the end users point of view (no admin like privileges)
*
2023-01-03 22:19:03 +01:00
* @param User $user
* @param Shift $shift The shift
* @param AngelType $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype
2023-01-24 17:40:20 +01:00
* @param Shift[]|Collection|null $user_shifts List of the users shifts
2023-01-03 22:19:03 +01:00
* @param AngelType $needed_angeltype
2023-01-18 13:02:11 +01:00
* @param ShiftEntry[]|Collection $shift_entries
2017-01-03 03:22:48 +01:00
* @return ShiftSignupState
*/
2017-01-02 15:43:36 +01:00
function Shift_signup_allowed_angel(
$user,
2023-01-03 22:19:03 +01:00
Shift $shift,
2022-11-09 00:02:30 +01:00
AngelType $angeltype,
2017-01-02 15:43:36 +01:00
$user_angeltype,
$user_shifts,
2022-11-09 00:02:30 +01:00
AngelType $needed_angeltype,
2017-01-02 15:43:36 +01:00
$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) {
return new ShiftSignupState(ShiftSignupStatus::NOT_ARRIVED, $free_entries);
}
2023-01-03 22:19:03 +01:00
if (config('signup_advance_hours') && $shift->start->timestamp > time() + config('signup_advance_hours') * 3600) {
return new ShiftSignupState(ShiftSignupStatus::NOT_YET, $free_entries);
}
2023-01-24 17:40:20 +01:00
if (is_null($user_shifts) || $user_shifts->isEmpty()) {
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) {
2023-01-03 22:19:03 +01:00
if ($user_shift->id == $shift->id) {
2017-01-02 03:57:23 +01:00
$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
return new ShiftSignupState(ShiftSignupStatus::SIGNED_UP, $free_entries);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2022-11-09 00:02:30 +01:00
$shift_post_signup_total_allowed_seconds =
2023-01-03 22:19:03 +01:00
(config('signup_post_fraction') * ($shift->end->timestamp - $shift->start->timestamp))
2022-11-09 00:02:30 +01:00
+ (config('signup_post_minutes') * 60);
2023-01-03 22:19:03 +01:00
if (time() > $shift->start->timestamp + $shift_post_signup_total_allowed_seconds) {
2017-01-02 03:57:23 +01:00
// you can only join if the shift is in future
return new ShiftSignupState(ShiftSignupStatus::SHIFT_ENDED, $free_entries);
2017-01-02 03:57:23 +01:00
}
if ($free_entries == 0) {
// you cannot join if shift is full
return new ShiftSignupState(ShiftSignupStatus::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)) {
2022-12-03 00:57:04 +01:00
$user_angeltype = UserAngelType::whereUserId($user->id)->where('angel_type_id', $angeltype->id)->first();
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)
2022-11-09 00:02:30 +01:00
|| $angeltype->no_self_signup == 1
|| ($angeltype->restricted == 1 && !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(ShiftSignupStatus::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)) {
2023-01-03 22:19:03 +01:00
// you cannot join if user already joined a parallel of this shift
return new ShiftSignupState(ShiftSignupStatus::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(ShiftSignupStatus::FREE, $free_entries);
}
/**
* Check if an angeltype supporter can sign up a user to a shift.
2017-01-03 03:22:48 +01:00
*
2023-01-18 13:02:11 +01:00
* @param AngelType $needed_angeltype
* @param ShiftEntry[]|Collection $shift_entries
2017-01-03 03:22:48 +01:00
* @return ShiftSignupState
*/
2022-11-09 00:02:30 +01:00
function Shift_signup_allowed_angeltype_supporter(AngelType $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(ShiftSignupStatus::OCCUPIED, $free_entries);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
return new ShiftSignupState(ShiftSignupStatus::FREE, $free_entries);
}
/**
* Check if an admin can sign up a user to a shift.
*
2023-01-18 13:02:11 +01:00
* @param AngelType $needed_angeltype
* @param ShiftEntry[]|Collection $shift_entries
2017-01-03 03:22:48 +01:00
* @return ShiftSignupState
*/
2022-11-09 00:02:30 +01:00
function Shift_signup_allowed_admin(AngelType $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
return new ShiftSignupState(ShiftSignupStatus::ADMIN, $free_entries);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
return new ShiftSignupState(ShiftSignupStatus::FREE, $free_entries);
}
/**
2023-01-03 22:19:03 +01:00
* Check if an angel can sign out from a shift.
2017-12-25 23:12:52 +01:00
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift The shift
2022-11-09 00:02:30 +01:00
* @param AngelType $angeltype The angeltype
* @param int $signout_user_id The user that was signed up for the shift
* @return bool
*/
2023-01-03 22:19:03 +01:00
function Shift_signout_allowed(Shift $shift, AngelType $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
// user shifts admin can sign out any user at any time
if (auth()->can('user_shifts_admin')) {
return true;
}
2017-12-25 23:12:52 +01:00
// angeltype supporter can sign out any user at any time from their supported angeltype
if (
auth()->can('shiftentry_edit_angeltype_supporter')
2022-12-03 00:57:04 +01:00
&& ($user->isAngelTypeSupporter($angeltype) || auth()->can('admin_user_angeltypes'))
2017-12-25 23:12:52 +01:00
) {
return true;
}
2017-12-25 23:12:52 +01:00
2023-01-03 22:19:03 +01:00
if ($signout_user_id == $user->id && $shift->start->timestamp > time() + config('last_unsubscribe') * 3600) {
return true;
}
2017-12-25 23:12:52 +01:00
return false;
}
/**
* Check if an angel can sign up for given shift.
*
2023-01-03 22:19:03 +01:00
* @param User $signup_user
* @param Shift $shift The shift
* @param AngelType $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype
* @param Shift[]|Collection|null $user_shifts List of the users shifts
* @param AngelType $needed_angeltype
2023-01-18 13:02:11 +01:00
* @param ShiftEntry[]|Collection $shift_entries
2017-01-03 03:22:48 +01:00
* @return ShiftSignupState
*/
2017-01-02 15:43:36 +01:00
function Shift_signup_allowed(
$signup_user,
2023-01-03 22:19:03 +01:00
Shift $shift,
2022-11-09 00:02:30 +01:00
AngelType $angeltype,
2017-03-03 08:31:25 +01:00
$user_angeltype,
$user_shifts,
2022-11-09 00:02:30 +01:00
AngelType $needed_angeltype,
2017-01-02 15:43:36 +01:00
$shift_entries
) {
if (auth()->can('user_shifts_admin')) {
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 (
auth()->can('shiftentry_edit_angeltype_supporter')
2022-12-03 00:57:04 +01:00
&& (auth()->user()->isAngelTypeSupporter($angeltype) || auth()->can('admin_user_angeltypes'))
2017-01-02 15:43:36 +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
);
}
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
2023-01-18 13:02:11 +01:00
* @param bool $include_freeloaded_comments
* @return SupportCollection|Shift[]
2014-12-07 17:34:29 +01:00
*/
2023-01-18 13:02:11 +01:00
function Shifts_by_user($userId, $include_freeloaded_comments = false)
2017-01-02 03:57:23 +01:00
{
2023-01-03 22:19:03 +01:00
$shiftsData = Db::select(
2022-10-18 19:15:22 +02:00
'
2020-05-13 18:26:32 +02:00
SELECT
2020-09-06 23:50:36 +02:00
`rooms`.*,
`rooms`.name AS Name,
`shift_types`.`id` AS `shifttype_id`,
`shift_types`.`name`,
2023-01-18 13:02:11 +01:00
`shift_entries`.`id` as shift_entry_id,
`shift_entries`.`shift_id`,
`shift_entries`.`angel_type_id`,
`shift_entries`.`user_id`,
`shift_entries`.`freeloaded`,
`shift_entries`.`user_comment`,
' . ($include_freeloaded_comments ? '`shift_entries`.`freeloaded_comment`, ' : '') . '
2023-01-03 22:19:03 +01:00
`shifts`.*
2023-01-18 13:02:11 +01:00
FROM `shift_entries`
JOIN `shifts` ON (`shift_entries`.`shift_id` = `shifts`.`id`)
2023-01-03 22:19:03 +01:00
JOIN `shift_types` ON (`shift_types`.`id` = `shifts`.`shift_type_id`)
JOIN `rooms` ON (`shifts`.`room_id` = `rooms`.`id`)
2023-01-18 13:02:11 +01:00
WHERE shift_entries.`user_id` = ?
2020-05-13 18:26:32 +02:00
ORDER BY `start`
',
[
$userId,
]
);
2023-01-03 22:19:03 +01:00
$shifts = [];
foreach ($shiftsData as $data) {
$shifts[] = (new Shift())->forceFill($data);
}
return collect($shifts);
2014-08-23 01:55:18 +02:00
}
2013-12-09 17:10:07 +01:00
2013-12-29 15:08:21 +01:00
/**
2023-01-03 22:19:03 +01:00
* Returns Shift by id or extends existing Shift
2013-12-29 15:08:21 +01:00
*
2023-01-03 22:19:03 +01:00
* @param int|Shift $shift Shift ID or shift model
* @return Shift|null
2013-12-29 15:08:21 +01:00
*/
2023-01-03 22:19:03 +01:00
function Shift($shift)
2017-01-02 03:57:23 +01:00
{
2023-01-03 22:19:03 +01:00
if (!$shift instanceof Shift) {
$shift = Shift::find($shift);
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
if (!$shift) {
2017-01-02 03:57:23 +01:00
return null;
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
$neededAngels = [];
$angelTypes = NeededAngelTypes_by_shift($shift->id);
foreach ($angelTypes as $type) {
2023-01-03 22:19:03 +01:00
$neededAngels[] = [
2023-01-18 13:02:11 +01:00
'angel_type_id' => $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
}
2023-01-03 22:19:03 +01:00
$shift->neededAngels = $neededAngels;
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
return $shift;
2013-12-29 15:08:21 +01:00
}