Skip to content

Commit

Permalink
#10292 refactored interest manager to repo and removed it
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Oct 8, 2024
1 parent 3e91b7a commit 3e03ed6
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 126 deletions.
91 changes: 0 additions & 91 deletions classes/user/InterestManager.php

This file was deleted.

4 changes: 2 additions & 2 deletions classes/user/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PKP\user;

use APP\facades\Repo;
use Illuminate\Contracts\Auth\Authenticatable;
use PKP\db\DAORegistry;
use PKP\identity\Identity;
Expand Down Expand Up @@ -192,8 +193,7 @@ public function setBillingAddress($billingAddress)
*/
public function getInterestString()
{
$interestManager = new InterestManager();
return $interestManager->getInterestsString($this);
return Repo::userInterest()->getInterestsString($this);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions classes/user/form/RegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use PKP\security\Role;
use PKP\security\Validation;
use PKP\site\Site;
use PKP\user\InterestManager;
use PKP\user\User;

class RegistrationForm extends Form
Expand Down Expand Up @@ -323,8 +322,7 @@ public function execute(...$functionArgs)
}

// Insert the user interests
$interestManager = new InterestManager();
$interestManager->setInterestsForUser($user, $this->getData('interests'));
Repo::userInterest()->setInterestsForUser($user, $this->getData('interests'));

return $userId;
}
Expand Down
8 changes: 2 additions & 6 deletions classes/user/form/RolesForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use APP\core\Application;
use APP\facades\Repo;
use APP\template\TemplateManager;
use PKP\user\InterestManager;
use PKP\user\User;

class RolesForm extends BaseProfileForm
Expand Down Expand Up @@ -61,12 +60,10 @@ public function fetch($request, $template = null, $display = false)
*/
public function initData()
{
$interestManager = new InterestManager();

$user = $this->getUser();

$this->_data = [
'interests' => $interestManager->getInterestsForUser($user),
'interests' => Repo::userInterest()->getInterestsForUser($user),
];
}

Expand Down Expand Up @@ -98,8 +95,7 @@ public function execute(...$functionArgs)
$userFormHelper->saveRoleContent($this, $user);

// Insert the user interests
$interestManager = new InterestManager();
$interestManager->setInterestsForUser($user, $this->getData('interests'));
Repo::userInterest()->setInterestsForUser($user, $this->getData('interests'));

parent::execute(...$functionArgs);
}
Expand Down
65 changes: 61 additions & 4 deletions classes/user/interest/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,78 @@

use APP\facades\Repo;
use Illuminate\Support\Facades\DB;
use PKP\user\User;
use PKP\user\interest\UserInterest;
use PKP\controlledVocab\ControlledVocabEntry;
use Throwable;

class Repository
{
/**
* Update a user's set of interests
* Get all interests for all users in the system
*/
public function setUserInterests(array $interests, int $userId): void
public function getAllInterests(?string $filter = null): array
{
$controlledVocab = Repo::controlledVocab()->build(
UserInterest::CONTROLLED_VOCAB_INTEREST
);

return ControlledVocabEntry::query()
->withControlledVocabId($controlledVocab->id)
->when(
$filter,
fn($query) => $query->withSetting(
UserInterest::CONTROLLED_VOCAB_INTEREST,
$filter
)
)
->get()
->sortBy(UserInterest::CONTROLLED_VOCAB_INTEREST)
->pluck(UserInterest::CONTROLLED_VOCAB_INTEREST)
->toArray();
}

/**
* Get user reviewing interests. (Cached in memory for batch fetches.)
*/
public function getInterestsForUser(User $user): array
{
return ControlledVocabEntry::query()
->whereHas(
"controlledVocab",
fn($query) => $query
->withSymbolic(UserInterest::CONTROLLED_VOCAB_INTEREST)
->withAssoc(0, 0)
)
->whereHas("userInterest", fn($query) => $query->withUserId($user->getId()))
->get()
->pluck(UserInterest::CONTROLLED_VOCAB_INTEREST, 'id')
->toArray();
}

/**
* Returns a comma separated string of a user's interests
*/
public function getInterestsString(User $user): string
{
$interests = $this->getInterestsForUser($user);

return implode(', ', $interests);
}

/**
* Set a user's interests
*/
public function setInterestsForUser(User $user, string|array|null $interests = null): void
{
$interests = is_array($interests)
? $interests
: (empty($interests) ? [] : explode(',', $interests));

$controlledVocab = Repo::controlledVocab()->build(
UserInterest::CONTROLLED_VOCAB_INTEREST
);

$currentInterests = ControlledVocabEntry::query()
->whereHas(
'controlledVocab',
Expand All @@ -47,7 +104,7 @@ public function setUserInterests(array $interests, int $userId): void
DB::beginTransaction();

// Delete the existing interests association.
UserInterest::query()->withUserId($userId)->delete();
UserInterest::query()->withUserId($user->getId())->delete();

$newInterestIds = collect(
array_diff(
Expand All @@ -70,7 +127,7 @@ public function setUserInterests(array $interests, int $userId): void
collect($currentInterests->pluck('id'))
->merge($newInterestIds)
->each(fn ($interestId) => UserInterest::create([
'userId' => $userId,
'userId' => $user->getId(),
'controlledVocabEntryId' => $interestId,
]));

Expand Down
5 changes: 1 addition & 4 deletions classes/user/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
namespace PKP\user\maps;

use APP\facades\Repo;
use PKP\user\InterestManager;

use APP\submission\Submission;
use Illuminate\Support\Enumerable;
use PKP\user\interest\UserInterest;
use PKP\db\DAORegistry;
use PKP\plugins\Hook;
use PKP\security\Role;
Expand Down Expand Up @@ -175,7 +172,7 @@ protected function mapByProperties(array $props, User $user, array $auxiliaryDat
case 'interests':
$output[$prop] = [];
if ($this->context) {
$interests = collect((new InterestManager())->getInterestsForUser($user))
$interests = collect(Repo::userInterest()->getInterestsForUser($user))
->map(fn($value, $index) => ['id' => $index, 'interest' => $value])
->values()
->toArray();
Expand Down
7 changes: 2 additions & 5 deletions controllers/grid/settings/user/form/UserDetailsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use PKP\mail\mailables\UserCreated;
use PKP\notification\Notification;
use PKP\security\Validation;
use PKP\user\InterestManager;
use PKP\user\User;
use Symfony\Component\Mailer\Exception\TransportException;

Expand Down Expand Up @@ -154,7 +153,6 @@ public function initData()
if (isset($this->user)) {
$user = $this->user;
$templateMgr->assign('user', $user);
$interestManager = new InterestManager();

$data = [
'username' => $user->getUsername(),
Expand All @@ -169,7 +167,7 @@ public function initData()
'mailingAddress' => $user->getMailingAddress(),
'country' => $user->getCountry(),
'biography' => $user->getBiography(null), // Localized
'interests' => $interestManager->getInterestsForUser($user),
'interests' => Repo::userInterest()->getInterestsForUser($user),
'locales' => $user->getLocales(),
];
$data['canCurrentUserGossip'] = Repo::user()->canCurrentUserGossip($user->getId());
Expand Down Expand Up @@ -392,8 +390,7 @@ public function execute(...$functionParams)
}
}

$interestManager = new InterestManager();
$interestManager->setInterestsForUser($this->user, $this->getData('interests'));
Repo::userInterest()->setInterestsForUser($this->user, $this->getData('interests'));

return $this->user;
}
Expand Down
4 changes: 1 addition & 3 deletions controllers/grid/users/reviewer/form/CreateReviewerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use PKP\notification\Notification;
use PKP\security\Validation;
use PKP\submission\reviewRound\ReviewRound;
use PKP\user\InterestManager;
use Symfony\Component\Mailer\Exception\TransportException;

class CreateReviewerForm extends ReviewerForm
Expand Down Expand Up @@ -143,8 +142,7 @@ public function execute(...$functionArgs)
$this->setData('reviewerId', $reviewerId);

// Insert the user interests
$interestManager = new InterestManager();
$interestManager->setInterestsForUser($user, $this->getData('interests'));
Repo::userInterest()->setInterestsForUser($user, $this->getData('interests'));

// Assign the selected user group ID to the user
$userGroupId = (int) $this->getData('userGroupId');
Expand Down
4 changes: 2 additions & 2 deletions pages/user/PKPUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
namespace PKP\pages\user;

use APP\core\Request;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\core\PKPRequest;
use PKP\security\Validation;
use PKP\user\InterestManager;

class PKPUserHandler extends Handler
{
Expand All @@ -46,7 +46,7 @@ public function getInterests($args, $request)
{
return new JSONMessage(
true,
(new InterestManager())->getAllInterests($request->getUserVar('term'))
Repo::userInterest()->getAllInterests($request->getUserVar('term'))
);
}

Expand Down
4 changes: 1 addition & 3 deletions plugins/importexport/users/filter/PKPUserUserXmlFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use PKP\filter\FilterDAO;
use PKP\filter\FilterGroup;
use PKP\plugins\importexport\native\filter\NativeExportFilter;
use PKP\user\InterestManager;
use PKP\user\User;

class PKPUserUserXmlFilter extends NativeExportFilter
Expand Down Expand Up @@ -141,8 +140,7 @@ public function createPKPUserNode($doc, $user)
}

// Add Reviewing Interests, if any.
$interestManager = new InterestManager();
$interests = $interestManager->getInterestsString($user);
$interests = Repo::userInterest()->getInterestsString($user);
$this->createOptionalNode($doc, $userNode, 'review_interests', $interests);

return $userNode;
Expand Down
4 changes: 1 addition & 3 deletions plugins/importexport/users/filter/UserXmlPKPUserFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use PKP\plugins\importexport\users\PKPUserImportExportDeployment;
use PKP\security\Validation;
use PKP\site\SiteDAO;
use PKP\user\InterestManager;
use PKP\user\User;

class UserXmlPKPUserFilter extends \PKP\plugins\importexport\native\filter\NativeImportFilter
Expand Down Expand Up @@ -264,8 +263,7 @@ public function parseUser($node)
$n = $interestNodeList->item(0);
if ($n) {
$interests = preg_split('/,\s*/', $n->textContent);
$interestManager = new InterestManager();
$interestManager->setInterestsForUser($user, $interests);
Repo::userInterest()->setInterestsForUser($user, $interests);
}
}

Expand Down

0 comments on commit 3e03ed6

Please sign in to comment.