summaryrefslogtreecommitdiff
path: root/includes/model/UserWorkLog_model.php
blob: 55e135ca1a52ca4aa33e998b4270d2391e2ccf14 (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
<?php
use Engelsystem\Database\Db;

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

/**
 * Returns all work log entries for a user.
 * @param User $user
 */
function UserWorkLogsForUser($user) {
    return Db::select("SELECT * FROM `UserWorkLog` WHERE `user_id`=? ORDER BY `created_timestamp`", [$user['UID']]);
}

/**
 * Delete a work log entry.
 * @param UserWorkLog $userWorkLog
 */
function UserWorkLog_delete($userWorkLog)
{
    $user_source = User($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),
        $userWorkLog['work_hours'],
        $userWorkLog['comment']));
    
    return $result;
}

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

    $result = Db::update("UPDATE `UserWorkLog` SET
        `work_hours`=?,
        `comment`=?
        WHERE `id`=?", [
            $userWorkLog['work_hours'],
            $userWorkLog['comment'],
            $userWorkLog['id']
        ]);
    
    engelsystem_log(sprintf('Updated work log for %s, %s hours, %s',
        User_Nick_render($user_source),
        $userWorkLog['work_hours'],
        $userWorkLog['comment']));
    
    return $result;
}

/**
 * Create a new work log entry
 * @param UserWorkLog $userWorkLog
 */
function UserWorkLog_create($userWorkLog)
{
    global $user;
    
    $user_source = User($userWorkLog['user_id']);
    
    $result = Db::insert("INSERT INTO `UserWorkLog` (
            `user_id`,
            `work_hours`,
            `comment`,
            `created_user_id`,
            `created_timestamp`
        )
        VALUES (?, ?, ?, ?, ?)", [
            $userWorkLog['user_id'],
            $userWorkLog['work_hours'],
            $userWorkLog['comment'],
            $user['UID'],
            time()
        ]);
    
    engelsystem_log(sprintf('Added work log entry for %s, %s hours, %s',
        User_Nick_render($user_source),
        $userWorkLog['work_hours'],
        $userWorkLog['comment']));
    
    return $result;
}

/**
 * New user work log entry
 *
 * @param array[] $user            
 */
function UserWorkLog_new($user)
{
    return [
        'user_id' => $user['UID'],
        'work_hours' => 0,
        'comment' => ''
    ];
}

?>