From 1c411bc6a3734cea2d60d22f6e1592f517276440 Mon Sep 17 00:00:00 2001 From: blackcoder87 Date: Wed, 18 Dec 2024 16:18:01 +0100 Subject: [PATCH] Add getUserListByGroupIds() and use that function in away module --- application/modules/away/config/config.php | 4 +- .../modules/away/controllers/Index.php | 4 +- .../modules/away/controllers/admin/Index.php | 2 +- application/modules/user/mappers/User.php | 18 +++++++- tests/modules/user/mappers/UserTest.php | 42 +++++++++++++++++++ 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/application/modules/away/config/config.php b/application/modules/away/config/config.php index 246e28907..c68900423 100644 --- a/application/modules/away/config/config.php +++ b/application/modules/away/config/config.php @@ -26,8 +26,8 @@ class Config extends \Ilch\Config\Install 'description' => 'User can enter when they are away (e.g. on holidays). There is an overview of this and the entries can be mananged in the admincenter.', ], ], - 'ilchCore' => '2.2.0', - 'phpVersion' => '7.3' + 'ilchCore' => '2.2.7', + 'phpVersion' => '7.4' ]; public function install() diff --git a/application/modules/away/controllers/Index.php b/application/modules/away/controllers/Index.php index daefcf261..3718caf4c 100644 --- a/application/modules/away/controllers/Index.php +++ b/application/modules/away/controllers/Index.php @@ -91,7 +91,7 @@ public function indexAction() $notifications = []; $userGroups = $awayGroupMapper->getGroups(); - $users = $userMapper->getUserListByGroupId($userGroups, 1); + $users = $userMapper->getUserListByGroupIds($userGroups, 1); // Users might be in several groups. Remove duplicates so each user only gets one notification. $users = array_unique($users, SORT_REGULAR); $currentUserId = $this->getUser()->getId(); @@ -155,7 +155,7 @@ public function updateAction() $notifications = []; $userGroups = $awayGroupMapper->getGroups(); - $users = $userMapper->getUserListByGroupId($userGroups, 1); + $users = $userMapper->getUserListByGroupIds($userGroups, 1); // Users might be in several groups. Remove duplicates so each user only gets one notification. $users = array_unique($users, SORT_REGULAR); $currentUserId = $this->getUser()->getId(); diff --git a/application/modules/away/controllers/admin/Index.php b/application/modules/away/controllers/admin/Index.php index b146854e4..b4f016c30 100644 --- a/application/modules/away/controllers/admin/Index.php +++ b/application/modules/away/controllers/admin/Index.php @@ -79,7 +79,7 @@ public function updateAction() $notifications = []; $userGroups = $awayGroupMapper->getGroups(); - $users = $userMapper->getUserListByGroupId($userGroups, 1); + $users = $userMapper->getUserListByGroupIds($userGroups, 1); // Users might be in several groups. Remove duplicates so each user only gets one notification. $users = array_unique($users, SORT_REGULAR); $currentUserId = $this->getUser()->getId(); diff --git a/application/modules/user/mappers/User.php b/application/modules/user/mappers/User.php index eea8cccc1..69722dd5c 100644 --- a/application/modules/user/mappers/User.php +++ b/application/modules/user/mappers/User.php @@ -112,12 +112,28 @@ public function getUserBySelector(string $selector): ?UserModel * @since 2.1.20 */ public function getUserListByGroupId(int $groupId, int $confirmed = 0, $pagination = null): array + { + return $this->getUserListByGroupIds([$groupId], $confirmed, $pagination); + } + + /** + * Get users (not all fields) by group ids. The users have to be in at least one of the groups. + * This will return duplicated users if a user is in multiple of the groups. + * + * @param int[] $groupIds array of group ids. + * @param int $confirmed + * @param $pagination + * @return array + * @since 2.2.7 + */ + public function getUserListByGroupIds(array $groupIds, int $confirmed = 0, $pagination = null): array { $select = $this->db()->select() ->fields(['u.id', 'u.name', 'u.opt_mail', 'u.date_created', 'u.date_last_activity', 'u.confirmed']) ->from(['u' => 'users']) ->join(['g' => 'users_groups'], 'u.id = g.user_id', 'LEFT', ['group_id' => 'g.group_id']) - ->where(['group_id' => $groupId, 'confirmed' => $confirmed]); + ->where(['group_id' => $groupIds], 'or') + ->andWhere(['confirmed' => $confirmed]); if ($pagination !== null) { $select->limit($pagination->getLimit()) ->useFoundRows(); diff --git a/tests/modules/user/mappers/UserTest.php b/tests/modules/user/mappers/UserTest.php index 90bf01a43..9a88a3b7f 100644 --- a/tests/modules/user/mappers/UserTest.php +++ b/tests/modules/user/mappers/UserTest.php @@ -42,6 +42,48 @@ public function testGetAdministratorCount() self::assertEquals(1, $this->out->getAdministratorCount()); } + /** + * Test getUserListByGroupId to see if the correct user will be returned. + * + * @return void + */ + public function testGetUserListByGroupId() + { + $userMapper = new UserMapper(); + + /** @var \Modules\User\Models\User[] $users */ + $users = $userMapper->getUserListByGroupId(1, 1); + self::assertNotEmpty($users); + self::assertEquals(1, $users[0]->getId()); + self::assertEquals('Testuser1', $users[0]->getName()); + self::assertEquals(1, $users[0]->getConfirmed()); + } + + /** + * Test getUserListByGroupIds to see if the correct users will be returned. + * + * @return void + */ + public function testGetUserListByGroupIds() + { + $userMapper = new UserMapper(); + + /** @var \Modules\User\Models\User[] $users */ + $users = $userMapper->getUserListByGroupIds([1, 3], 1); + self::assertNotEmpty($users); + self::assertEquals(1, $users[0]->getId()); + self::assertEquals('Testuser1', $users[0]->getName()); + self::assertEquals(1, $users[0]->getConfirmed()); + + self::assertEquals(1, $users[1]->getId()); + self::assertEquals('Testuser1', $users[1]->getName()); + self::assertEquals(1, $users[1]->getConfirmed()); + + self::assertEquals(2, $users[2]->getId()); + self::assertEquals('Testuser2', $users[2]->getName()); + self::assertEquals(1, $users[2]->getConfirmed()); + } + /** * Returns database schema sql statements to initialize database *