summaryrefslogtreecommitdiff
path: root/includes/model/UserWorkLog_model.php
blob: e785067d97a20365b9fa835872e5a928a4cb8f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

use Carbon\Carbon;
use Engelsystem\Database\Db;
use Engelsystem\Models\User\User;

/**
 * Load a single work log entry.
 *
 * @param int $user_worklog_id
 * @return array|null
 */
function UserWorkLog($user_worklog_id)
{
    $workLog = Db::selectOne("SELECT * FROM `UserWorkLog` WHERE `id`=?", [
        $user_worklog_id
    ]);

    return empty($workLog) ? null : $workLog;
}

/**
 * Returns all work log entries for a user.
 *
 * @param int         $userId
 * @param Carbon|null $sinceTime
 * @return array[]
 */
function UserWorkLogsForUser($userId, Carbon $sinceTime = null)
{
    return Db::select(
        '
                SELECT *
                FROM `UserWorkLog`
                WHERE `user_id`=?
                ' . ($sinceTime ? 'AND work_timestamp >= ' . $sinceTime->getTimestamp() : '') . '
                ORDER BY `created_timestamp`
              ',
        [
            $userId
        ]
    );
}

/**
 * Delete a work log entry.
 *
 * @param $userWorkLog
 * @return int
 */
function UserWorkLog_delete($userWorkLog)
{
    $user_source = User::find($userWorkLog['user_id']);
    $result = Db::delete("DELETE FROM `UserWorkLog` WHERE `id`=?", [
        $userWorkLog['id']
    ]);

    engelsystem_log(sprintf(
        'Delete work log for %s, %s hours, %s',
        User_Nick_render($user_source, true),
        $userWorkLog['work_hours'],
        $userWorkLog['comment']
    ));

    return $result;
}

/**
 * Update work log entry (only work hours and comment)
 *
 * @param $userWorkLog
 * @return int
 */
function UserWorkLog_update($userWorkLog)
{
    $user_source = User::find($userWorkLog['user_id']);

    $result = Db::update("UPDATE `UserWorkLog` SET
        `work_timestamp`=?,
        `work_hours`=?,
        `comment`=?
        WHERE `id`=?", [
        $userWorkLog['work_timestamp'],
        $userWorkLog['work_hours'],
        $userWorkLog['comment'],
        $userWorkLog['id']
    ]);

    engelsystem_log(sprintf(
            'Updated work log for %s, %s hours, %s',
            User_Nick_render($user_source, true),
            $userWorkLog['work_hours'],
            $userWorkLog['comment'])
    );

    return $result;
}

/**
 * Create a new work log entry
 *
 * @param $userWorkLog
 * @return bool
 */
function UserWorkLog_create($userWorkLog)
{
    $user = auth()->user();

    $user_source = User::find($userWorkLog['user_id']);

    $result = Db::insert("INSERT INTO `UserWorkLog` (
            `user_id`,
            `work_timestamp`,
            `work_hours`,
            `comment`,
            `created_user_id`,
            `created_timestamp`
        )
        VALUES (?, ?, ?, ?, ?, ?)", [
        $userWorkLog['user_id'],
        $userWorkLog['work_timestamp'],
        $userWorkLog['work_hours'],
        $userWorkLog['comment'],
        $user->id,
        time()
    ]);

    engelsystem_log(sprintf('Added work log entry for %s, %s hours, %s', User_Nick_render($user_source, true),
        $userWorkLog['work_hours'], $userWorkLog['comment']));

    return $result;
}

/**
 * @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
 * @return array
 */
function UserWorkLog_new($userId)
{
    $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));

    /** @var Carbon $buildup */
    $buildup = config('buildup_start');
    if (!empty($buildup)) {
        $work_date = $buildup->format('Y-m-d H:i');
    }

    return [
        'user_id'        => $userId,
        'work_timestamp' => $work_date,
        'work_hours'     => 0,
        'comment'        => ''
    ];
}