From 86a38a3372557bd72db309e7b24b4a4a95544397 Mon Sep 17 00:00:00 2001
From: msquare <msquare@notrademark.de>
Date: Fri, 29 Dec 2017 17:19:27 +0100
Subject: add user work log entries feature

---
 db/update.sql | 7 +++++++
 1 file changed, 7 insertions(+)

(limited to 'db/update.sql')

diff --git a/db/update.sql b/db/update.sql
index 212cd97e..351cf8b4 100644
--- a/db/update.sql
+++ b/db/update.sql
@@ -49,3 +49,10 @@ ALTER TABLE `Room` ADD `description` TEXT NULL AFTER `map_url`;
 -- Dashboard
 ALTER TABLE `AngelTypes` ADD `show_on_dashboard` BOOLEAN NOT NULL AFTER `contact_email`;
 UPDATE `AngelTypes` SET `show_on_dashboard` = TRUE;
+
+-- Work Log
+CREATE TABLE `engelsystem`.`UserWorkLog` ( `id` INT NOT NULL AUTO_INCREMENT , `user_id` INT NOT NULL , `work_hours` DECIMAL NOT NULL , `comment` VARCHAR(200) NOT NULL , `created_user_id` INT NOT NULL , `created_timestamp` INT NOT NULL , PRIMARY KEY (`id`), INDEX (`user_id`), INDEX (`created_user_id`)) ENGINE = InnoDB;
+ALTER TABLE `UserWorkLog` ADD FOREIGN KEY (`created_user_id`) REFERENCES `User`(`UID`) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE `UserWorkLog` ADD FOREIGN KEY (`user_id`) REFERENCES `User`(`UID`) ON DELETE CASCADE ON UPDATE CASCADE;
+ALTER TABLE `UserWorkLog` ADD INDEX(`created_timestamp`);
+INSERT INTO `Privileges` (`id`, `name`, `desc`) VALUES (NULL, 'admin_user_worklog', 'Manage user work log entries.');
+ALTER TABLE `UserWorkLog` CHANGE `work_hours` `work_hours` DECIMAL(10,2) NOT NULL;
-- 
cgit v1.2.3-54-g00ecf


From be3652b44aa279d7a8ae153d8038959e71eb7ecc Mon Sep 17 00:00:00 2001
From: msquare <msquare@notrademark.de>
Date: Fri, 29 Dec 2017 17:30:54 +0100
Subject: fix work log logs

---
 db/update.sql                        | 2 +-
 includes/model/UserWorkLog_model.php | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

(limited to 'db/update.sql')

diff --git a/db/update.sql b/db/update.sql
index 351cf8b4..f012e319 100644
--- a/db/update.sql
+++ b/db/update.sql
@@ -51,7 +51,7 @@ ALTER TABLE `AngelTypes` ADD `show_on_dashboard` BOOLEAN NOT NULL AFTER `contact
 UPDATE `AngelTypes` SET `show_on_dashboard` = TRUE;
 
 -- Work Log
-CREATE TABLE `engelsystem`.`UserWorkLog` ( `id` INT NOT NULL AUTO_INCREMENT , `user_id` INT NOT NULL , `work_hours` DECIMAL NOT NULL , `comment` VARCHAR(200) NOT NULL , `created_user_id` INT NOT NULL , `created_timestamp` INT NOT NULL , PRIMARY KEY (`id`), INDEX (`user_id`), INDEX (`created_user_id`)) ENGINE = InnoDB;
+CREATE TABLE `UserWorkLog` ( `id` INT NOT NULL AUTO_INCREMENT , `user_id` INT NOT NULL , `work_hours` DECIMAL NOT NULL , `comment` VARCHAR(200) NOT NULL , `created_user_id` INT NOT NULL , `created_timestamp` INT NOT NULL , PRIMARY KEY (`id`), INDEX (`user_id`), INDEX (`created_user_id`)) ENGINE = InnoDB;
 ALTER TABLE `UserWorkLog` ADD FOREIGN KEY (`created_user_id`) REFERENCES `User`(`UID`) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE `UserWorkLog` ADD FOREIGN KEY (`user_id`) REFERENCES `User`(`UID`) ON DELETE CASCADE ON UPDATE CASCADE;
 ALTER TABLE `UserWorkLog` ADD INDEX(`created_timestamp`);
 INSERT INTO `Privileges` (`id`, `name`, `desc`) VALUES (NULL, 'admin_user_worklog', 'Manage user work log entries.');
diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php
index d9628e1d..55e135ca 100644
--- a/includes/model/UserWorkLog_model.php
+++ b/includes/model/UserWorkLog_model.php
@@ -23,7 +23,7 @@ function UserWorkLogsForUser($user) {
  */
 function UserWorkLog_delete($userWorkLog)
 {
-    $user_source = $userWorkLog['user_id'];
+    $user_source = User($userWorkLog['user_id']);
     $result = Db::delete("DELETE FROM `UserWorkLog` WHERE `id`=?", [
         $userWorkLog['id']
     ]);
@@ -42,7 +42,7 @@ function UserWorkLog_delete($userWorkLog)
  */
 function UserWorkLog_update($userWorkLog)
 {
-    $user_source = $userWorkLog['user_id'];
+    $user_source = User($userWorkLog['user_id']);
 
     $result = Db::update("UPDATE `UserWorkLog` SET
         `work_hours`=?,
@@ -69,7 +69,7 @@ function UserWorkLog_create($userWorkLog)
 {
     global $user;
     
-    $user_source = $userWorkLog['user_id'];
+    $user_source = User($userWorkLog['user_id']);
     
     $result = Db::insert("INSERT INTO `UserWorkLog` (
             `user_id`,
-- 
cgit v1.2.3-54-g00ecf


From ee9fbda098b3c608176ced4425765e3029ca8f93 Mon Sep 17 00:00:00 2001
From: msquare <msquare@notrademark.de>
Date: Fri, 29 Dec 2017 18:57:11 +0100
Subject: add work date to work log

---
 db/update.sql                                   |  1 +
 includes/controller/user_worklog_controller.php |  6 ++
 includes/model/UserWorkLog_model.php            | 78 ++++++++++++++-----------
 includes/model/User_model.php                   |  4 +-
 includes/pages/admin_active.php                 |  8 ++-
 includes/view/UserWorkLog_view.php              |  3 +-
 includes/view/User_view.php                     | 15 ++---
 7 files changed, 71 insertions(+), 44 deletions(-)

(limited to 'db/update.sql')

diff --git a/db/update.sql b/db/update.sql
index f012e319..56663071 100644
--- a/db/update.sql
+++ b/db/update.sql
@@ -56,3 +56,4 @@ ALTER TABLE `UserWorkLog` ADD FOREIGN KEY (`created_user_id`) REFERENCES `User`(
 ALTER TABLE `UserWorkLog` ADD INDEX(`created_timestamp`);
 INSERT INTO `Privileges` (`id`, `name`, `desc`) VALUES (NULL, 'admin_user_worklog', 'Manage user work log entries.');
 ALTER TABLE `UserWorkLog` CHANGE `work_hours` `work_hours` DECIMAL(10,2) NOT NULL;
+ALTER TABLE `UserWorkLog` ADD `work_timestamp` INT NOT NULL AFTER `user_id`;
diff --git a/includes/controller/user_worklog_controller.php b/includes/controller/user_worklog_controller.php
index ba91534b..bd62c83c 100644
--- a/includes/controller/user_worklog_controller.php
+++ b/includes/controller/user_worklog_controller.php
@@ -69,6 +69,12 @@ function user_worklog_from_request($userWorkLog)
     
     $valid = true;
     
+    $userWorkLog['work_timestamp'] = parse_date('Y-m-d H:i', $request->input('work_timestamp') . ' 00:00');
+    if ($userWorkLog['work_timestamp'] == null) {
+        $valid = false;
+        error(_('Please enter work date.'));
+    }
+    
     $userWorkLog['work_hours'] = $request->input('work_hours');
     if (! preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
         $valid = false;
diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php
index 55e135ca..36442943 100644
--- a/includes/model/UserWorkLog_model.php
+++ b/includes/model/UserWorkLog_model.php
@@ -3,23 +3,32 @@ use Engelsystem\Database\Db;
 
 /**
  * Load a single work log entry.
- * @param int $user_worklog_id
+ * 
+ * @param int $user_worklog_id            
  */
-function UserWorkLog($user_worklog_id) {
-    return Db::selectOne("SELECT * FROM `UserWorkLog` WHERE `id`=?", [$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
+ * 
+ * @param User $user            
  */
-function UserWorkLogsForUser($user) {
-    return Db::select("SELECT * FROM `UserWorkLog` WHERE `user_id`=? ORDER BY `created_timestamp`", [$user['UID']]);
+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
+ * 
+ * @param UserWorkLog $userWorkLog            
  */
 function UserWorkLog_delete($userWorkLog)
 {
@@ -28,42 +37,40 @@ function UserWorkLog_delete($userWorkLog)
         $userWorkLog['id']
     ]);
     
-    engelsystem_log(sprintf('Delete work log for %s, %s hours, %s',
-        User_Nick_render($user_source),
-        $userWorkLog['work_hours'],
-        $userWorkLog['comment']));
+    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
+ * 
+ * @param UserWorkLog $userWorkLog            
  */
 function UserWorkLog_update($userWorkLog)
 {
     $user_source = User($userWorkLog['user_id']);
-
+    
     $result = Db::update("UPDATE `UserWorkLog` SET
+        `work_timestamp`=?,
         `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_timestamp'],
         $userWorkLog['work_hours'],
-        $userWorkLog['comment']));
+        $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
+ * 
+ * @param UserWorkLog $userWorkLog            
  */
 function UserWorkLog_create($userWorkLog)
 {
@@ -73,23 +80,22 @@ function UserWorkLog_create($userWorkLog)
     
     $result = Db::insert("INSERT INTO `UserWorkLog` (
             `user_id`,
+            `work_timestamp`,
             `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),
+        VALUES (?, ?, ?, ?, ?, ?)", [
+        $userWorkLog['user_id'],
+        $userWorkLog['work_timestamp'],
         $userWorkLog['work_hours'],
-        $userWorkLog['comment']));
+        $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;
 }
@@ -101,8 +107,14 @@ function UserWorkLog_create($userWorkLog)
  */
 function UserWorkLog_new($user)
 {
+    $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
+    $event_config = EventConfig();
+    if (! empty($event_config['buildup_start_date'])) {
+        $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', $event_config['buildup_start_date']));
+    }
     return [
         'user_id' => $user['UID'],
+        'work_timestamp' => $work_date,
         'work_hours' => 0,
         'comment' => ''
     ];
diff --git a/includes/model/User_model.php b/includes/model/User_model.php
index b398cefe..5a31e7b8 100644
--- a/includes/model/User_model.php
+++ b/includes/model/User_model.php
@@ -42,8 +42,10 @@ function User_tshirt_score($user) {
         FROM `User` 
         LEFT JOIN `UserWorkLog` ON `User`.`UID` = `UserWorkLog`.`user_id`
         WHERE `User`.`UID` = ?
+        AND `UserWorkLog`.`work_timestamp` < ?
     ',[
-        $user['UID']
+        $user['UID'],
+        time()
     ]);
     
     return $result_shifts['tshirt_score'] + $result_worklog['tshirt_score'];
diff --git a/includes/pages/admin_active.php b/includes/pages/admin_active.php
index 18553159..f3a28061 100644
--- a/includes/pages/admin_active.php
+++ b/includes/pages/admin_active.php
@@ -59,7 +59,8 @@ function admin_active()
                       `User`.*,
                       COUNT(`ShiftEntry`.`id`) AS `shift_count`,
                       (%s + (
-                          SELECT SUM(`work_hours`) * 3600 FROM `UserWorkLog` WHERE `user_id`=`User`.`UID`
+                          SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`User`.`UID`
+                          AND `work_timestamp` < %s
                       )) AS `shift_length`
                   FROM `User`
                   LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
@@ -71,6 +72,7 @@ function admin_active()
                   %s
                 ',
                 $shift_sum_formula,
+                time(),
                 $limit
             ));
             $user_nicks = [];
@@ -143,7 +145,8 @@ function admin_active()
                 `User`.*,
                 COUNT(`ShiftEntry`.`id`) AS `shift_count`,
                 (%s + (
-                    SELECT SUM(`work_hours`) * 3600 FROM `UserWorkLog` WHERE `user_id`=`User`.`UID`
+                    SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`User`.`UID`
+                    AND `work_timestamp` < %s
                 )) AS `shift_length`
             FROM `User` LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
             LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID` '
@@ -154,6 +157,7 @@ function admin_active()
             %s
         ',
         $shift_sum_formula,
+        time(),
         $limit
     ));
     $matched_users = [];
diff --git a/includes/view/UserWorkLog_view.php b/includes/view/UserWorkLog_view.php
index 86a367ae..79c049b3 100644
--- a/includes/view/UserWorkLog_view.php
+++ b/includes/view/UserWorkLog_view.php
@@ -36,9 +36,10 @@ function UserWorkLog_edit_form($user_source, $userWorkLog)
 {
     return form([
         form_info(_('User'), User_Nick_render($user_source)),
+        form_date('work_timestamp', _('Work date'), $userWorkLog['work_timestamp'], null, time()),
         form_text('work_hours', _('Work hours'), $userWorkLog['work_hours']),
         form_text('comment', _('Comment'), $userWorkLog['comment']),
-        form_submit('submit', _('Add'))
+        form_submit('submit', _('Save'))
     ]);
 }
 
diff --git a/includes/view/User_view.php b/includes/view/User_view.php
index 82df1851..36ac36c3 100644
--- a/includes/view/User_view.php
+++ b/includes/view/User_view.php
@@ -356,7 +356,7 @@ function User_view_myshift($shift, $user_source, $its_me)
             . glyph('time') . date('H:i', $shift['start'])
             . ' - '
             . date('H:i', $shift['end']),
-        'duration'   => round(($shift['end'] - $shift['start']) / 3600, 2) . ' h',
+        'duration'   => sprintf('%.2f', round(($shift['end'] - $shift['start']) / 3600, 2)) . ' h',
         'room'       => Room_name_render($shift),
         'shift_info' => $shift_info,
         'comment'    => ''
@@ -413,7 +413,7 @@ function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshi
     $myshifts_table = [];
     $timesum = 0;
     foreach ($shifts as $shift) {
-        $myshifts_table[] = User_view_myshift($shift, $user_source, $its_me);
+        $myshifts_table[$shift['start']] = User_view_myshift($shift, $user_source, $its_me);
 
         if (!$shift['freeloaded']) {
             $timesum += ($shift['end'] - $shift['start']);
@@ -422,15 +422,16 @@ function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshi
 
     if($its_me || $admin_user_worklog_privilege) {
         foreach($user_worklogs as $worklog) {
-            $myshifts_table[] = User_view_worklog($worklog, $admin_user_worklog_privilege);
+            $myshifts_table[$worklog['work_timestamp']] = User_view_worklog($worklog, $admin_user_worklog_privilege);
             $timesum += $worklog['work_hours'] * 3600;
         }
     }
 
     if (count($myshifts_table) > 0) {
+        ksort($myshifts_table);
         $myshifts_table[] = [
             'date'       => '<b>' . _('Sum:') . '</b>',
-            'duration'   => '<b>' . round($timesum / 3600, 2) . ' h</b>',
+            'duration'   => '<b>' . sprintf('%.2f', round($timesum / 3600, 2)) . ' h</b>',
             'room'       => '',
             'shift_info' => '',
             'comment'    => '',
@@ -439,7 +440,7 @@ function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshi
         if (config('enable_tshirt_size', false) && ($its_me || $tshirt_admin)) {
             $myshifts_table[] = [
                 'date'       => '<b>' . _('Your t-shirt score') . '&trade;:</b>',
-                'duration'   => '<b>' . $tshirt_score . '</b>',
+                'duration'   => '<b>' . sprintf('%.2f', $tshirt_score) . ' h</b>',
                 'room'       => '',
                 'shift_info' => '',
                 'comment'    => '',
@@ -473,8 +474,8 @@ function User_view_worklog($worklog, $admin_user_worklog_privilege) {
     }
     
     return [
-        'date'       => '',
-        'duration'   => '<b>' . $worklog['work_hours'] . ' h</b>',
+        'date'       => glyph('calendar') . date('Y-m-d', $worklog['work_timestamp']),
+        'duration'   => '<b>' . sprintf('%.2f', $worklog['work_hours']) . ' h</b>',
         'room'       => '',
         'shift_info' => _('Work log entry'),
         'comment'    => $worklog['comment'] . '<br>'
-- 
cgit v1.2.3-54-g00ecf

