engelsystem/includes/model/ShiftEntry_model.php

90 lines
2.4 KiB
PHP
Raw Normal View History

2013-12-09 17:10:07 +01:00
<?php
2019-12-25 16:26:59 +01:00
use Carbon\Carbon;
2023-01-18 13:02:11 +01:00
use Engelsystem\Models\Shifts\ShiftEntry;
2018-10-09 21:47:31 +02:00
use Engelsystem\Models\User\User;
2023-01-18 13:02:11 +01:00
use Illuminate\Database\Eloquent\Collection;
2014-12-07 16:45:09 +01:00
/**
* Create a new shift entry.
*/
2023-01-18 13:02:11 +01:00
function ShiftEntry_onCreate(ShiftEntry $shiftEntry): void
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
$shift = $shiftEntry->shift;
engelsystem_log(
2023-01-18 13:02:11 +01:00
'User ' . User_Nick_render($shiftEntry->user, true)
. ' signed up for shift ' . $shiftEntry->shift->title
. ' (' . $shift->shiftType->name . ')'
2023-10-15 19:25:55 +02:00
. ' at ' . $shift->location->name
2023-01-03 22:19:03 +01:00
. ' from ' . $shift->start->format('Y-m-d H:i')
. ' to ' . $shift->end->format('Y-m-d H:i')
2023-01-18 13:02:11 +01:00
. ' as ' . $shiftEntry->angelType->name
);
2023-01-18 13:02:11 +01:00
mail_shift_assign($shiftEntry->user, $shift);
2014-12-07 20:29:01 +01:00
}
2014-12-07 17:07:19 +01:00
/**
* Delete a shift entry.
2017-01-03 03:22:48 +01:00
*
2023-01-18 13:02:11 +01:00
* @param ShiftEntry $shiftEntry
2014-12-07 17:07:19 +01:00
*/
2023-01-18 13:02:11 +01:00
function ShiftEntry_onDelete(ShiftEntry $shiftEntry)
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
$signout_user = $shiftEntry->user;
$shift = Shift($shiftEntry->shift);
2023-01-03 22:19:03 +01:00
$shifttype = $shift->shiftType;
2023-10-15 19:25:55 +02:00
$location = $shift->location;
2023-01-18 13:02:11 +01:00
$angeltype = $shiftEntry->angelType;
2017-12-25 23:12:52 +01:00
engelsystem_log(
2019-08-21 01:20:46 +02:00
'Shift signout: ' . User_Nick_render($signout_user, true)
2023-01-03 22:19:03 +01:00
. ' from shift ' . $shift->title
. ' (' . $shifttype->name . ')'
2023-10-15 19:25:55 +02:00
. ' at ' . $location->name
2023-01-03 22:19:03 +01:00
. ' from ' . $shift->start->format('Y-m-d H:i')
. ' to ' . $shift->end->format('Y-m-d H:i')
2022-11-09 00:02:30 +01:00
. ' as ' . $angeltype->name
2017-12-25 23:12:52 +01:00
);
2023-01-18 13:02:11 +01:00
mail_shift_removed($signout_user, $shift);
2014-12-07 17:07:19 +01:00
}
2014-08-23 01:55:18 +02:00
/**
* Returns next (or current) shifts of given user.
2014-12-07 16:45:09 +01:00
*
2023-01-03 22:19:03 +01:00
* @param User $user
2023-01-18 13:02:11 +01:00
* @return ShiftEntry[]|Collection
2014-08-23 01:55:18 +02:00
*/
2023-01-03 22:19:03 +01:00
function ShiftEntries_upcoming_for_user(User $user)
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
return $user->shiftEntries()
->with(['shift', 'shift.shiftType'])
->join('shifts', 'shift_entries.shift_id', 'shifts.id')
->where('shifts.end', '>', Carbon::now())
->orderBy('shifts.end')
->get();
2014-08-23 01:55:18 +02:00
}
/**
* Returns shifts completed by the given user.
*
2023-01-18 13:02:11 +01:00
* @param User $user
2019-12-25 16:26:59 +01:00
* @param Carbon|null $sinceTime
2023-01-18 13:02:11 +01:00
* @return ShiftEntry[]|Collection
*/
2023-01-03 22:19:03 +01:00
function ShiftEntries_finished_by_user(User $user, Carbon $sinceTime = null)
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
$query = $user->shiftEntries()
->with(['shift', 'shift.shiftType'])
->join('shifts', 'shift_entries.shift_id', 'shifts.id')
->where('shifts.end', '<', Carbon::now())
->where('freeloaded', false)
->orderByDesc('shifts.end');
if ($sinceTime) {
$query = $query->where('shifts.start', '>=', $sinceTime);
}
2013-12-09 17:10:07 +01:00
2023-01-18 13:02:11 +01:00
return $query->get();
2013-12-27 18:45:27 +01:00
}