2013-12-09 17:10:07 +01:00
|
|
|
<?php
|
|
|
|
|
2019-12-25 16:26:59 +01:00
|
|
|
use Carbon\Carbon;
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
2018-10-09 21:47:31 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2016-11-17 14:35:04 +01:00
|
|
|
/**
|
|
|
|
* Returns an array with the attributes of shift entries.
|
|
|
|
* FIXME! Needs entity object.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2016-11-17 14:35:04 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntry_new()
|
|
|
|
{
|
|
|
|
return [
|
2017-01-02 15:43:36 +01:00
|
|
|
'id' => null,
|
|
|
|
'SID' => null,
|
|
|
|
'TID' => null,
|
|
|
|
'UID' => null,
|
|
|
|
'Comment' => null,
|
|
|
|
'freeloaded_comment' => null,
|
|
|
|
'freeloaded' => false
|
|
|
|
];
|
2016-11-17 14:35:04 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 15:01:02 +02:00
|
|
|
/**
|
|
|
|
* Counts all freeloaded shifts.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return int
|
2014-09-28 15:01:02 +02:00
|
|
|
*/
|
2017-12-25 23:12:52 +01:00
|
|
|
function ShiftEntries_freeloaded_count()
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
if (empty($result)) {
|
2017-01-21 13:58:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:07:19 +01:00
|
|
|
/**
|
2017-12-25 23:12:52 +01:00
|
|
|
* List users subscribed to a given shift.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $shift_id
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array
|
2014-12-07 17:07:19 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntries_by_shift($shift_id)
|
|
|
|
{
|
2017-01-21 19:37:42 +01:00
|
|
|
return DB::select('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT
|
2018-10-14 18:24:42 +02:00
|
|
|
`users`.*,
|
2017-01-21 13:58:53 +01:00
|
|
|
`ShiftEntry`.`UID`,
|
|
|
|
`ShiftEntry`.`TID`,
|
|
|
|
`ShiftEntry`.`SID`,
|
|
|
|
`AngelTypes`.`name` AS `angel_type_name`,
|
|
|
|
`ShiftEntry`.`Comment`,
|
|
|
|
`ShiftEntry`.`freeloaded`
|
|
|
|
FROM `ShiftEntry`
|
2018-10-14 18:24:42 +02:00
|
|
|
JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id`
|
2017-01-21 13:58:53 +01:00
|
|
|
JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id`
|
2017-01-21 19:37:42 +01:00
|
|
|
WHERE `ShiftEntry`.`SID` = ?',
|
2017-01-21 13:58:53 +01:00
|
|
|
[$shift_id]
|
|
|
|
);
|
2014-12-07 16:54:00 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 16:45:09 +01:00
|
|
|
/**
|
|
|
|
* Create a new shift entry.
|
2014-12-07 16:54:00 +01:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift_entry
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return bool
|
2014-12-07 16:45:09 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntry_create($shift_entry)
|
|
|
|
{
|
2018-10-09 21:47:31 +02:00
|
|
|
$user = User::find($shift_entry['UID']);
|
2017-12-20 00:42:58 +01:00
|
|
|
$shift = Shift($shift_entry['SID']);
|
2019-08-21 01:20:46 +02:00
|
|
|
$shifttype = ShiftType($shift['shifttype_id']);
|
|
|
|
$room = Room($shift['RID']);
|
|
|
|
$angeltype = AngelType($shift_entry['TID']);
|
2017-12-20 00:42:58 +01:00
|
|
|
$result = DB::insert('
|
2017-01-21 13:58:53 +01:00
|
|
|
INSERT INTO `ShiftEntry` (
|
|
|
|
`SID`,
|
|
|
|
`TID`,
|
|
|
|
`UID`,
|
|
|
|
`Comment`,
|
|
|
|
`freeload_comment`,
|
|
|
|
`freeloaded`
|
|
|
|
)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?)
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$shift_entry['SID'],
|
|
|
|
$shift_entry['TID'],
|
|
|
|
$shift_entry['UID'],
|
|
|
|
$shift_entry['Comment'],
|
|
|
|
$shift_entry['freeload_comment'],
|
2017-11-19 13:48:34 +01:00
|
|
|
(int)$shift_entry['freeloaded'],
|
2017-01-21 13:58:53 +01:00
|
|
|
]
|
|
|
|
);
|
2017-12-20 00:42:58 +01:00
|
|
|
engelsystem_log(
|
2019-05-31 04:03:19 +02:00
|
|
|
'User ' . User_Nick_render($user, true)
|
2017-12-20 00:42:58 +01:00
|
|
|
. ' signed up for shift ' . $shift['name']
|
2019-08-21 01:20:46 +02:00
|
|
|
. ' (' . $shifttype['name'] . ')'
|
|
|
|
. ' at ' . $room['Name']
|
2017-12-20 00:42:58 +01:00
|
|
|
. ' from ' . date('Y-m-d H:i', $shift['start'])
|
|
|
|
. ' to ' . date('Y-m-d H:i', $shift['end'])
|
2019-08-21 01:20:46 +02:00
|
|
|
. ' as ' . $angeltype['name']
|
2017-12-25 23:12:52 +01:00
|
|
|
);
|
2018-10-11 01:26:34 +02:00
|
|
|
mail_shift_assign($user, $shift);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-20 00:42:58 +01:00
|
|
|
return $result;
|
2014-12-07 16:45:09 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:13:14 +01:00
|
|
|
/**
|
|
|
|
* Update a shift entry.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param array $shift_entry
|
2014-12-07 17:13:14 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntry_update($shift_entry)
|
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::update('
|
|
|
|
UPDATE `ShiftEntry`
|
|
|
|
SET
|
|
|
|
`Comment` = ?,
|
|
|
|
`freeload_comment` = ?,
|
|
|
|
`freeloaded` = ?
|
|
|
|
WHERE `id` = ?',
|
|
|
|
[
|
|
|
|
$shift_entry['Comment'],
|
|
|
|
$shift_entry['freeload_comment'],
|
2017-11-19 13:48:34 +01:00
|
|
|
(int)$shift_entry['freeloaded'],
|
2017-01-21 13:58:53 +01:00
|
|
|
$shift_entry['id']
|
|
|
|
]
|
|
|
|
);
|
2014-12-07 17:13:14 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 20:29:01 +01:00
|
|
|
/**
|
|
|
|
* Get a shift entry.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $shift_entry_id
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null
|
2014-12-07 20:29:01 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntry($shift_entry_id)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$shiftEntry = DB::selectOne('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]);
|
|
|
|
|
|
|
|
return empty($shiftEntry) ? null : $shiftEntry;
|
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
|
|
|
*
|
2017-12-19 20:58:01 +01:00
|
|
|
* @param array $shiftEntry
|
2014-12-07 17:07:19 +01:00
|
|
|
*/
|
2017-12-19 20:58:01 +01:00
|
|
|
function ShiftEntry_delete($shiftEntry)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-12-19 22:24:06 +01:00
|
|
|
DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shiftEntry['id']]);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2018-10-09 21:47:31 +02:00
|
|
|
$signout_user = User::find($shiftEntry['UID']);
|
2017-12-19 20:58:01 +01:00
|
|
|
$shift = Shift($shiftEntry['SID']);
|
|
|
|
$shifttype = ShiftType($shift['shifttype_id']);
|
|
|
|
$room = Room($shift['RID']);
|
|
|
|
$angeltype = AngelType($shiftEntry['TID']);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
engelsystem_log(
|
2019-08-21 01:20:46 +02:00
|
|
|
'Shift signout: ' . User_Nick_render($signout_user, true)
|
|
|
|
. ' from shift ' . $shift['name']
|
|
|
|
. ' (' . $shifttype['name'] . ')'
|
2017-12-19 20:58:01 +01:00
|
|
|
. ' at ' . $room['Name']
|
|
|
|
. ' from ' . date('Y-m-d H:i', $shift['start'])
|
|
|
|
. ' to ' . date('Y-m-d H:i', $shift['end'])
|
|
|
|
. ' as ' . $angeltype['name']
|
2017-12-25 23:12:52 +01:00
|
|
|
);
|
2018-10-11 01:26:34 +02:00
|
|
|
|
|
|
|
mail_shift_removed(User::find($shiftEntry['UID']), Shift($shiftEntry['SID']));
|
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
|
|
|
*
|
2018-10-09 21:47:31 +02:00
|
|
|
* @param int $userId
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array
|
2014-08-23 01:55:18 +02:00
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function ShiftEntries_upcoming_for_user($userId)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
return DB::select('
|
|
|
|
SELECT *
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`)
|
|
|
|
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
|
|
|
|
WHERE `ShiftEntry`.`UID` = ?
|
|
|
|
AND `Shifts`.`end` > ?
|
|
|
|
ORDER BY `Shifts`.`end`
|
|
|
|
',
|
|
|
|
[
|
2018-10-09 21:47:31 +02:00
|
|
|
$userId,
|
2017-01-21 13:58:53 +01:00
|
|
|
time(),
|
|
|
|
]
|
|
|
|
);
|
2014-08-23 01:55:18 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 22:57:57 +01:00
|
|
|
/**
|
|
|
|
* Returns shifts completed by the given user.
|
|
|
|
*
|
2019-12-25 16:26:59 +01:00
|
|
|
* @param int $userId
|
|
|
|
* @param Carbon|null $sinceTime
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array
|
2016-02-05 22:57:57 +01:00
|
|
|
*/
|
2019-12-25 16:26:59 +01:00
|
|
|
function ShiftEntries_finished_by_user($userId, Carbon $sinceTime = null)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
return DB::select('
|
|
|
|
SELECT *
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`)
|
|
|
|
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
|
|
|
|
WHERE `ShiftEntry`.`UID` = ?
|
|
|
|
AND `Shifts`.`end` < ?
|
|
|
|
AND `ShiftEntry`.`freeloaded` = 0
|
2019-12-25 16:26:59 +01:00
|
|
|
' . ($sinceTime ? 'AND Shifts.start >= ' . $sinceTime->getTimestamp() : '') . '
|
2019-01-13 16:04:01 +01:00
|
|
|
ORDER BY `Shifts`.`end` desc
|
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
|
|
|
time(),
|
|
|
|
]
|
|
|
|
);
|
2016-02-05 22:57:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
/**
|
|
|
|
* Returns all shift entries in given shift for given angeltype.
|
2014-08-23 01:55:18 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param int $shift_id
|
|
|
|
* @param int $angeltype_id
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array
|
2013-12-09 17:10:07 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id)
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT *
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
WHERE `SID` = ?
|
|
|
|
AND `TID` = ?
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$shift_id,
|
|
|
|
$angeltype_id,
|
|
|
|
]
|
|
|
|
);
|
2013-12-09 17:10:07 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:45:27 +01:00
|
|
|
/**
|
|
|
|
* Returns all freeloaded shifts for given user.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2018-10-09 21:47:31 +02:00
|
|
|
* @param int $userId
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return array
|
2013-12-27 18:45:27 +01:00
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function ShiftEntries_freeloaded_by_user($userId)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
return DB::select('
|
|
|
|
SELECT *
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
WHERE `freeloaded` = 1
|
|
|
|
AND `UID` = ?
|
|
|
|
',
|
|
|
|
[
|
2018-10-09 21:47:31 +02:00
|
|
|
$userId
|
2017-01-21 13:58:53 +01:00
|
|
|
]
|
|
|
|
);
|
2013-12-27 18:45:27 +01:00
|
|
|
}
|