summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-12-29 17:19:27 +0100
committermsquare <msquare@notrademark.de>2017-12-29 17:19:27 +0100
commit86a38a3372557bd72db309e7b24b4a4a95544397 (patch)
treeb2349773367b95bc1aa78b2f78cfcda6cd277916 /includes/model
parente7b0e9934d2df55a7971eaac42cc6b27a9733a32 (diff)
add user work log entries feature
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/UserWorkLog_model.php111
-rw-r--r--includes/model/User_model.php12
2 files changed, 121 insertions, 2 deletions
diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php
new file mode 100644
index 00000000..d9628e1d
--- /dev/null
+++ b/includes/model/UserWorkLog_model.php
@@ -0,0 +1,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 = $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 = $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 = $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' => ''
+ ];
+}
+
+?> \ No newline at end of file
diff --git a/includes/model/User_model.php b/includes/model/User_model.php
index fa24aac1..b398cefe 100644
--- a/includes/model/User_model.php
+++ b/includes/model/User_model.php
@@ -26,7 +26,7 @@ function User_delete($user_id)
function User_tshirt_score($user) {
$shift_sum_formula = config('shift_sum_formula');
- $result = DB::selectOne('
+ $result_shifts = DB::selectOne('
SELECT ROUND((' . $shift_sum_formula . ') / 3600, 2) AS `tshirt_score`
FROM `User` LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID`
@@ -37,8 +37,16 @@ function User_tshirt_score($user) {
$user['UID'],
time()
]);
+ $result_worklog = DB::selectOne('
+ SELECT SUM(`work_hours`) AS `tshirt_score`
+ FROM `User`
+ LEFT JOIN `UserWorkLog` ON `User`.`UID` = `UserWorkLog`.`user_id`
+ WHERE `User`.`UID` = ?
+ ',[
+ $user['UID']
+ ]);
- return $result['tshirt_score'];
+ return $result_shifts['tshirt_score'] + $result_worklog['tshirt_score'];
}
/**