Skip to content

Commit

Permalink
pkp#10751 EditorialMasthead page issue with eloquent based user group…
Browse files Browse the repository at this point in the history
… update
  • Loading branch information
touhidurabir committed Jan 3, 2025
1 parent 17d758f commit f888613
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function passes($attribute, $value)
// At this point, we know the user group exists; check if the user has it assigned
if ($user = $this->invitation->getExistingUser()) {
$userUserGroups = UserUserGroup::withUserId($user->getId())
->withUserGroupId($value) // The $value is the userGroupId
->withUserGroupIds([$value]) // The $value is the userGroupId
->withActive()
->get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function setData(?string $locale = null): void

foreach ($userGroups as $userGroup) {
$userUserGroups = UserUserGroup::withUserId($user->getId())
->withUserGroupId($userGroup->id)
->withUserGroupIds([$userGroup->id])
->withActive()
->get();

Expand Down
2 changes: 1 addition & 1 deletion classes/services/PKPContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public function add($context, $request)

$assignmentExists = UserUserGroup::query()
->withUserId($currentUser->getId())
->withUserGroupId($managerUserGroup->id)
->withUserGroupIds([$managerUserGroup->id])
->exists();

if (!$assignmentExists) {
Expand Down
4 changes: 2 additions & 2 deletions classes/user/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function mergeUsers(int $oldUserId, int $newUserId)
$submissionCommentDao = DAORegistry::getDAO('SubmissionCommentDAO'); /** @var SubmissionCommentDAO $submissionCommentDao */
$submissionComments = $submissionCommentDao->getByUserId($oldUserId);

while ($submissionComment = $submissionComments->next()) {
while ($submissionComment = $submissionComments->next()) { /** @var \PKP\submission\SubmissionComment $submissionComment */
$submissionComment->setAuthorId($newUserId);
$submissionCommentDao->updateObject($submissionComment);
}
Expand All @@ -369,7 +369,7 @@ public function mergeUsers(int $oldUserId, int $newUserId)
// Check if the new user is already assigned to this user group
$exists = UserUserGroup::query()
->withUserId($newUserId)
->withUserGroupId($userUserGroup->userGroupId)
->withUserGroupIds([$userUserGroup->userGroupId])
->exists();

if (!$exists) {
Expand Down
2 changes: 1 addition & 1 deletion classes/user/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected function mapByProperties(array $props, User $user, array $auxiliaryDat
'recommendOnly' => (bool) $userGroup->recommendOnly,
'dateStart' => UserUserGroup::withUserId($user->getId())
->withActive()
->withUserGroupId($userGroup->id)
->withUserGroupIds([$userGroup->id])
->pluck('date_start')->first()
];
}
Expand Down
18 changes: 10 additions & 8 deletions classes/userGroup/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Carbon\Carbon;
use DateInterval;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\LazyCollection;
Expand Down Expand Up @@ -218,7 +219,7 @@ public function userInGroup(int $userId, int $userGroupId): bool
{
return UserUserGroup::query()
->withUserId($userId)
->withUserGroupId($userGroupId)
->withUserGroupIds($userGroupId)
->withActive()
->exists();
}
Expand All @@ -242,7 +243,7 @@ public function userOnMasthead(int $userId, ?int $userGroupId = null): bool
->withMasthead();

if ($userGroupId) {
$query->withUserGroupId($userGroupId);
$query->withUserGroupIds([$userGroupId]);
}

return $query->exists();
Expand All @@ -256,7 +257,7 @@ public function getUserUserGroupMastheadStatus(int $userId, int $userGroupId): U
{
$masthead = UserUserGroup::query()
->withUserId($userId)
->withUserGroupId($userGroupId)
->withUserGroupIds([$userGroupId])
->withActive()
->pluck('masthead')
->first();
Expand Down Expand Up @@ -344,7 +345,7 @@ public function deleteAssignmentsByUserId(int $userId, ?int $userGroupId = null)
$query = UserUserGroup::query()->withUserId($userId);

if ($userGroupId) {
$query->withUserGroupId($userGroupId);
$query->withUserGroupIds([$userGroupId]);
$userGroup = $this->get($userGroupId);
if ($userGroup && $userGroup->masthead) {
self::forgetEditorialCache($userGroup->contextId);
Expand Down Expand Up @@ -374,7 +375,7 @@ public function endAssignments(int $contextId, int $userId, ?int $userGroupId =
->withActive();

if ($userGroupId) {
$query->withUserGroupId($userGroupId);
$query->withUserGroupIds([$userGroupId]);
}

$query->update(['date_end' => $dateEnd]);
Expand Down Expand Up @@ -590,12 +591,13 @@ public function getMastheadUserIdsByRoleIds(array $mastheadRoles, int $contextId
$users = UserUserGroup::query()
->withContextId($contextId)
->withUserGroupIds($mastheadRoleIds)
->withUserUserGroupStatus($userUserGroupStatus->value)
->withUserUserGroupMastheadStatus(UserUserGroupMastheadStatus::STATUS_ON->value)
->whereHas('userGroup', fn (Builder $query) => $query->withUserUserGroupStatus($userUserGroupStatus->value))
->withMasthead()
->orderBy('user_groups.role_id', 'asc')
->join('user_groups', 'user_user_groups.user_group_id', '=', 'user_groups.user_group_id')
->join('users', 'user_user_groups.user_id', '=', 'users.user_id')
->orderBy('users.family_name', 'asc')
->join('user_settings', 'user_user_groups.user_id', '=', 'user_settings.user_id')
->orderBy('user_settings.family_name', 'asc')
->get(['user_groups.user_group_id', 'users.user_id']);

// group unique user ids by UserGroup id
Expand Down
10 changes: 5 additions & 5 deletions classes/userGroup/relationships/UserUserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

namespace PKP\userGroup\relationships;

use PKP\core\Core;
use APP\facades\Repo;
use PKP\userGroup\UserGroup;
use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Eloquence\Behaviours\HasCamelCasing;
use PKP\core\Core;
use PKP\userGroup\UserGroup;

class UserUserGroup extends \Illuminate\Database\Eloquent\Model
{
Expand Down Expand Up @@ -52,9 +52,9 @@ public function scopeWithUserId(Builder $query, int $userId): Builder
return $query->where('user_user_groups.user_id', $userId);
}

public function scopeWithUserGroupId(Builder $query, int $userGroupId): Builder
public function scopeWithUserGroupIds(Builder $query, array $userGroupIds): Builder
{
return $query->where('user_user_groups.user_group_id', $userGroupId);
return $query->whereIn('user_user_groups.user_group_id', $userGroupIds);
}

public function scopeWithActive(Builder $query): Builder
Expand Down
2 changes: 1 addition & 1 deletion controllers/grid/settings/user/form/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function saveUserGroupAssignments(Request $request): void
fn ($userGroupId) =>
UserUserGroup::query()
->withUserId($this->userId)
->withUserGroupId($userGroupId)
->withUserGroupIds([$userGroupId])
->withActive()
->update(['date_end' => now()])
);
Expand Down
12 changes: 7 additions & 5 deletions pages/about/AboutContextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use APP\handler\Handler;
use APP\template\TemplateManager;
use DateTime;
use Illuminate\Support\Collection;
use PKP\context\Context;
use PKP\facades\Locale;
use PKP\orcid\OrcidManager;
use PKP\plugins\Hook;
Expand Down Expand Up @@ -61,7 +63,7 @@ public function index($args, $request)
}


private function getSortedMastheadUserGroups($context)
private function getSortedMastheadUserGroups(Context $context): Collection
{
$mastheadUserGroups = UserGroup::withContextIds([$context->getId()])
->masthead(true)
Expand Down Expand Up @@ -95,7 +97,7 @@ public function editorialMasthead($args, $request)

// Get all user IDs grouped by user group ID for the masthead roles
$allUsersIdsGroupedByUserGroupId = Repo::userGroup()->getMastheadUserIdsByRoleIds(
$mastheadRoles,
$mastheadRoles->all(),
$context->getId()
);

Expand All @@ -104,7 +106,7 @@ public function editorialMasthead($args, $request)
foreach ($allUsersIdsGroupedByUserGroupId[$userGroupId] ?? [] as $userId) {
$user = Repo::user()->get($userId);
$userUserGroup = UserUserGroup::withUserId($user->getId())
->withUserGroupId($userGroupId)
->withUserGroupIds([$userGroupId])
->withActive()
->withMasthead()
->first();
Expand Down Expand Up @@ -161,7 +163,7 @@ public function editorialHistory($args, $request)

// get all user IDs grouped by user group ID for the masthead roles with ended status
$allUsersIdsGroupedByUserGroupId = Repo::userGroup()->getMastheadUserIdsByRoleIds(
$mastheadRoles,
$mastheadRoles->all(),
$context->getId(),
UserUserGroupStatus::STATUS_ENDED
);
Expand All @@ -171,7 +173,7 @@ public function editorialHistory($args, $request)
foreach ($allUsersIdsGroupedByUserGroupId[$userGroupId] ?? [] as $userId) {
$user = Repo::user()->get($userId);
$userUserGroups = UserUserGroup::withUserId($user->getId())
->withUserGroupId($userGroupId)
->withUserGroupIds([$userGroupId])
->withEnded()
->withMasthead()
->orderBy('date_start', 'desc')
Expand Down
6 changes: 3 additions & 3 deletions templates/frontend/pages/editorialMasthead.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

<h1>{translate key="common.editorialMasthead"}</h1>
{foreach from=$mastheadRoles item="mastheadRole"}
{if array_key_exists($mastheadRole->getId(), $mastheadUsers)}
<h2>{$mastheadRole->getLocalizedName()|escape}</h2>
{if array_key_exists($mastheadRole->id, $mastheadUsers)}
<h2>{$mastheadRole->getLocalizedData('name')|escape}</h2>
<ul class="user_listing" role="list">
{foreach from=$mastheadUsers[$mastheadRole->getId()] item="mastheadUser"}
{foreach from=$mastheadUsers[$mastheadRole->id] item="mastheadUser"}
<li>
{strip}
<span class="date_start">{translate key="common.fromUntil" from=$mastheadUser['dateStart'] until=""}</span>
Expand Down

0 comments on commit f888613

Please sign in to comment.