From 80b00f1811db26ec20325d5da9f1040fb5169e64 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 22 Jan 2020 10:29:40 +0100
Subject: [PATCH] FEAT #11882 TIME 0:50 History filters + count + fixes

---
 apps/maarch_entreprise/cas_connect.php        |  2 +-
 apps/maarch_entreprise/ozwilloConnect.php     |  2 +-
 core/class/class_history.php                  |  5 +++-
 migration/20.03/2003.sql                      |  2 ++
 rest/index.php                                |  2 +-
 .../history/controllers/HistoryController.php | 29 +++++++++++++++----
 6 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/apps/maarch_entreprise/cas_connect.php b/apps/maarch_entreprise/cas_connect.php
index 938b07cb893..d6848fd3999 100755
--- a/apps/maarch_entreprise/cas_connect.php
+++ b/apps/maarch_entreprise/cas_connect.php
@@ -121,7 +121,7 @@ $trace->add(
     _CONNECTION_CAS_OK,
     $_SESSION['config']['databasetype'],
     "ADMIN",
-    false
+    false, 'ok', 'DEBUG', $userId
 );
 
 exit();
diff --git a/apps/maarch_entreprise/ozwilloConnect.php b/apps/maarch_entreprise/ozwilloConnect.php
index 40e87de79e3..88b256dea37 100755
--- a/apps/maarch_entreprise/ozwilloConnect.php
+++ b/apps/maarch_entreprise/ozwilloConnect.php
@@ -50,4 +50,4 @@ unset($_REQUEST['state']);
 
 header("location: log.php");
 $trace = new history();
-$trace->add('users', $idToken->sub, 'LOGIN', 'userlogin', 'Ozwillo Connection', $_SESSION['config']['databasetype'], 'ADMIN', false);
+$trace->add('users', $idToken->sub, 'LOGIN', 'userlogin', 'Ozwillo Connection', $_SESSION['config']['databasetype'], 'ADMIN', false, 'ok', 'DEBUG', $_SESSION['ozwillo']['userId']);
diff --git a/core/class/class_history.php b/core/class/class_history.php
index 84d722595f1..bc5ec4db62a 100755
--- a/core/class/class_history.php
+++ b/core/class/class_history.php
@@ -112,10 +112,13 @@ class history
         }
         $remote_ip = $REMOTE_ADDR;
 
-        $user = '';
+        if (empty($user)) {
+            $user = '';
+        }
         if (isset($_SESSION['user']['UserId'])) {
             $user = $_SESSION['user']['UserId'];
         }
+        \SrcCore\models\ValidatorModel::notEmpty(['user' => $user], ['user']);
 
         $traceInformations = array(
             'WHERE' => $table_name,
diff --git a/migration/20.03/2003.sql b/migration/20.03/2003.sql
index 728e8707d24..99efcccfe4f 100644
--- a/migration/20.03/2003.sql
+++ b/migration/20.03/2003.sql
@@ -730,6 +730,8 @@ DO $$ BEGIN
 END$$;
 UPDATE baskets set basket_clause = replace(basket_clause, 'nature_id' , 'custom_fields->>''1''');
 
+UPDATE history SET user_id = (select user_id from users order by user_id='superadmin' desc limit 1) where user_id = '';
+
 /* users followed resources */
 DROP TABLE IF EXISTS users_followed_resources;
 CREATE TABLE users_followed_resources
diff --git a/rest/index.php b/rest/index.php
index a4c1f333129..6cd90df7a97 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -252,7 +252,7 @@ $app->get('/groups/{id}/privileges/{privilegeId}/parameters', \Group\controllers
 
 //History
 $app->get('/history', \History\controllers\HistoryController::class . ':get');
-$app->get('/history/availableEventTypes', \History\controllers\HistoryController::class . ':getAvailableEventTypes');
+$app->get('/history/availableFilters', \History\controllers\HistoryController::class . ':getAvailableFilters');
 $app->get('/history/users/{userSerialId}', \History\controllers\HistoryController::class . ':getByUserId');
 $app->get('/history/resources/{resId}', \History\controllers\HistoryController::class . ':getByResourceId');
 $app->get('/history/resources/{resId}/workflow', \History\controllers\HistoryController::class . ':getWorkflowByResourceId');
diff --git a/src/app/history/controllers/HistoryController.php b/src/app/history/controllers/HistoryController.php
index 40e4757832f..cdf4e3202d9 100755
--- a/src/app/history/controllers/HistoryController.php
+++ b/src/app/history/controllers/HistoryController.php
@@ -83,7 +83,7 @@ class HistoryController
         $orderBy = !in_array($queryParams['orderBy'], ['event_date', 'user_id', 'info']) ? ['event_date DESC'] : ["{$queryParams['orderBy']} {$order}"];
 
         $history = HistoryModel::get([
-            'select'    => ['event_date', 'user_id', 'info', 'remote_ip'],
+            'select'    => ['event_date', 'user_id', 'info', 'remote_ip', 'count(1) OVER()'],
             'where'     => $where,
             'data'      => $data,
             'orderBy'   => $orderBy,
@@ -91,7 +91,13 @@ class HistoryController
             'limit'     => $limit
         ]);
 
-        return $response->withJson(['history' => $history]);
+        $total = $history[0]['count'] ?? 0;
+        foreach ($history as $key => $value) {
+            $history[$key]['userLabel'] = UserModel::getLabelledUserById(['login' => $value['user_id']]);
+            unset($history[$key]['count']);
+        }
+
+        return $response->withJson(['history' => $history, 'count' => $total]);
     }
 
     public static function add(array $aArgs)
@@ -173,7 +179,7 @@ class HistoryController
         return $response->withJson(['history' => $history]);
     }
 
-    public function getAvailableEventTypes(Request $request, Response $response)
+    public function getAvailableFilters(Request $request, Response $response)
     {
         if (!PrivilegeController::hasPrivilege(['privilegeId' => 'view_history', 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
@@ -188,7 +194,9 @@ class HistoryController
         foreach ($eventTypes as $eventType) {
             if (strpos($eventType['event_type'], 'ACTION#') === 0) {
                 $exp = explode('#', $eventType['event_type']);
-                $action = ActionModel::getById(['select' => ['label_action'], 'id' => $exp[1]]);
+                if (!empty($exp[1])) {
+                    $action = ActionModel::getById(['select' => ['label_action'], 'id' => $exp[1]]);
+                }
                 $label = !empty($action) ? $action['label_action'] : null;
                 $actions[] = ['id' => $exp[1], 'label' => $label];
             } else {
@@ -196,6 +204,17 @@ class HistoryController
             }
         }
 
-        return $response->withJson(['actions' => $actions, 'systemActions' => $systemActions]);
+        $usersInHistory = HistoryModel::get([
+            'select'    => ['DISTINCT(user_id)']
+        ]);
+
+        $users = [];
+        foreach ($usersInHistory as $value) {
+            $user = UserModel::getByLogin(['login' => $value['user_id'], 'select' => ['id', 'firstname', 'lastname']]);
+
+            $users[] = ['id' => $user['id'] ?? null, 'login' => $value['user_id'], 'label' => !empty($user['id']) ? "{$user['firstname']} {$user['lastname']}" : null];
+        }
+
+        return $response->withJson(['actions' => $actions, 'systemActions' => $systemActions, 'users' => $users]);
     }
 }
-- 
GitLab