summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-10-21 09:12:10 +0200
committerGitHub <noreply@github.com>2019-10-21 09:12:10 +0200
commit9b08b951a2f7036519a9711ff4aa32a8709e261d (patch)
treed3e42cbb043a854c11f11e735447bebb302f9d43 /includes/model
parent4e65e6f73ae13d5d833e37f5a841c375cdde9530 (diff)
parent52b8b6b45c68dff281e101bc27375347f296ba5e (diff)
Merge pull request #651 from MyIgel/shift-deletion-worklog
Save time of past shifts as worklog if they get deleted
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/Shifts_model.php9
-rw-r--r--includes/model/UserWorkLog_model.php50
2 files changed, 56 insertions, 3 deletions
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 6bb17cf2..01295fbc 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -80,12 +80,15 @@ function Shifts_from_frab()
}
/**
- * @param array $room
+ * @param array|int $room
* @return array[]
*/
function Shifts_by_room($room)
{
- return DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]);
+ return DB::select(
+ 'SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`',
+ [is_array($room) ? $room['RID'] : $room]
+ );
}
/**
@@ -508,8 +511,8 @@ function Shift_delete_by_psid($shift_psid)
*/
function Shift_delete($shift_id)
{
- DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]);
mail_shift_delete(Shift($shift_id));
+ DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]);
}
/**
diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php
index 7b766c5e..cc7686cf 100644
--- a/includes/model/UserWorkLog_model.php
+++ b/includes/model/UserWorkLog_model.php
@@ -122,6 +122,56 @@ function UserWorkLog_create($userWorkLog)
}
/**
+ * @param array|int $shift
+ */
+function UserWorkLog_from_shift($shift)
+{
+ $shift = is_array($shift) ? $shift : Shift($shift);
+ $nightShifts = config('night_shifts');
+
+ if ($shift['start'] > time()) {
+ return;
+ }
+
+ $room = Room($shift['RID']);
+ foreach ($shift['ShiftEntry'] as $entry) {
+ if ($entry['freeloaded']) {
+ continue;
+ }
+
+ $type = AngelType($entry['TID']);
+
+ $nightShiftMultiplier = 1;
+ $shiftStart = Carbon::createFromTimestamp($shift['start']);
+ $shiftEnd = Carbon::createFromTimestamp($shift['end']);
+ if (
+ $nightShifts['enabled']
+ && (
+ $shiftStart->hour >= $nightShifts['start'] && $shiftStart->hour < $nightShifts['end']
+ || $shiftEnd->hour >= $nightShifts['start'] && $shiftEnd->hour < $nightShifts['end']
+ )
+ ) {
+ $nightShiftMultiplier = $nightShifts['multiplier'];
+ }
+
+ UserWorkLog_create([
+ 'user_id' => $entry['UID'],
+ 'work_timestamp' => $shift['start'],
+ 'work_hours' => (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier,
+ 'comment' => sprintf(
+ '%s (%s as %s) in %s, %s-%s',
+ $shift['name'],
+ $shift['title'],
+ $type['name'],
+ $room['Name'],
+ Carbon::createFromTimestamp($shift['start'])->format(__('m/d/Y h:i a')),
+ Carbon::createFromTimestamp($shift['end'])->format(__('m/d/Y h:i a'))
+ ),
+ ]);
+ }
+}
+
+/**
* New user work log entry
*
* @param int $userId