Skip to content

Commit

Permalink
Add getUserListByGroupIds() and use that function in away module
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 committed Dec 18, 2024
1 parent e4ec70d commit 1c411bc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
4 changes: 2 additions & 2 deletions application/modules/away/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions application/modules/away/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion application/modules/away/controllers/admin/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 17 additions & 1 deletion application/modules/user/mappers/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 42 additions & 0 deletions tests/modules/user/mappers/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 1c411bc

Please sign in to comment.