Skip to content

Commit

Permalink
pkp#4787 Reviewer suggestions 3 case consider WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Dec 2, 2024
1 parent e709b00 commit 60b1612
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function toArray(Request $request)
'affiliation' => $this->affiliation,
'suggestionReason' => $this->suggestionReason,
'approvedAt' => $this->approvedAt,
'existingUserId' => $this->existingUser?->getId(),
'existingReviewerRole' => $this->existingReviewerRole,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1237,10 +1237,15 @@ protected function getReviewerFrom(int $selectionType, Request $request = null):
$request ??= Application::get()->getRequest();
$formClassName = $this->_getReviewerFormClassName($selectionType);

if ($selectionType == static::REVIEWER_SELECT_CREATE && $request->getUserVar('reviewerSuggestionId')) {
if ($request->getUserVar('reviewerSuggestionId')) {

$reviewerSuggestion = ReviewerSuggestion::find($request->getUserVar('reviewerSuggestionId'));

if ($reviewerSuggestion?->hasApproved()) {
if (!$reviewerSuggestion) {
throw new Exception('Given reviewer suggestion ID is invalid');
}

if ($reviewerSuggestion->hasApproved()) {
throw new Exception('Not allowed to add reviewer suggestion as reviewer that has already been approved');
}

Expand Down
35 changes: 28 additions & 7 deletions classes/submission/reviewer/suggestion/ReviewerSuggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use PKP\core\traits\ModelWithSettings;
use PKP\security\Role;

class ReviewerSuggestion extends Model
{
Expand Down Expand Up @@ -115,13 +116,33 @@ public function hasApproved(): ?Carbon
protected function fullName(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
$familyName = $this->familyName;
return collect($this->givenName)
->map(fn ($givenName, $locale) => $givenName. ' ' . $familyName[$locale])
->toArray();
}
);
get: fn () => collect($this->givenName)
->map(fn ($givenName, $locale) => $givenName . ' ' . $this->familyName[$locale])
->toArray()
)->shouldCache();
}

/**
* Get the existing user for this suggestion if exists
*/
protected function existingUser(): Attribute
{
return Attribute::make(
get: fn () => Repo::user()->getByEmail($this->email)
)->shouldCache();
}

/**
* If this suggestion already has a review role when already there is an existing user association
*/
protected function existingReviewerRole(): Attribute
{
return Attribute::make(
get: fn () => (bool)$this->existingUser?->hasRole(
[Role::ROLE_ID_REVIEWER],
$this->submission->getData('contextId')
)
)->shouldCache();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace PKP\controllers\grid\users\reviewer\form;

use APP\core\Application;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;

use APP\facades\Repo;
use APP\submission\Submission;
use APP\template\TemplateManager;
Expand All @@ -43,10 +45,13 @@ class AdvancedSearchReviewerForm extends ReviewerForm
*
* @param Submission $submission
* @param ReviewRound $reviewRound
* @param ReviewerSuggestion|null $reviewerSuggestion
*/
public function __construct($submission, $reviewRound)
public function __construct($submission, $reviewRound, $reviewerSuggestion = null)
{
parent::__construct($submission, $reviewRound);
$this->reviewerSuggestion = $reviewerSuggestion;

$this->setTemplate('controllers/grid/users/reviewer/form/advancedSearchReviewerForm.tpl');

$this->addCheck(new \PKP\form\validation\FormValidator($this, 'reviewerId', 'required', 'editor.review.mustSelect'));
Expand All @@ -61,7 +66,13 @@ public function readInputData()
{
parent::readInputData();

$this->readUserVars(['reviewerId']);
$inputData = ['reviewerId'];

if ($this->reviewerSuggestion) {
array_push($inputData, 'reviewerSuggestionId');
}

$this->readUserVars($inputData);
}

/**
Expand All @@ -84,6 +95,11 @@ public function initData()

$this->setData('personalMessage', '');
$this->setData('reviewerMessages', $templates->toArray());

if ($this->reviewerSuggestion?->existingReviewerRole) {
$this->setData('reviewerSuggestionId', $this->reviewerSuggestion->id);
$this->setData('reviewerId', $this->reviewerSuggestion->existingUser->getId());
}
}

/**
Expand Down
23 changes: 13 additions & 10 deletions controllers/grid/users/reviewer/form/CreateReviewerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
namespace PKP\controllers\grid\users\reviewer\form;

use APP\core\Application;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;
use APP\facades\Repo;
use APP\notification\NotificationManager;
use APP\submission\Submission;
use APP\template\TemplateManager;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorEmail;
use PKP\form\validation\FormValidatorUsername;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorLocale;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;
use PKP\core\Core;
use PKP\mail\mailables\ReviewerRegister;
use PKP\notification\Notification;
Expand All @@ -34,8 +39,6 @@

class CreateReviewerForm extends ReviewerForm
{
public ?ReviewerSuggestion $reviewerSuggestion = null;

/**
* Constructor.
*
Expand All @@ -56,8 +59,8 @@ public function __construct($submission, $reviewRound, $reviewerSuggestion = nul
$this->addSupportedFormLocale($site->getPrimaryLocale());

$form = $this;
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $site->getPrimaryLocale()));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function ($familyName) use ($form) {
$this->addCheck(new FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $site->getPrimaryLocale()));
$this->addCheck(new FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function ($familyName) use ($form) {
$givenNames = $form->getData('givenName');
foreach ($familyName as $locale => $value) {
if (!empty($value) && empty($givenNames[$locale])) {
Expand All @@ -66,13 +69,13 @@ public function __construct($submission, $reviewRound, $reviewerSuggestion = nul
}
return true;
}));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', [Repo::user(), 'getByUsername'], [true], true));
$this->addCheck(new \PKP\form\validation\FormValidatorUsername($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
$this->addCheck(new \PKP\form\validation\FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', function ($email) {
$this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', [Repo::user(), 'getByUsername'], [true], true));
$this->addCheck(new FormValidatorUsername($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
$this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
$this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', function ($email) {
return !Repo::user()->getByEmail($email, true);
}));
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'userGroupId', 'required', 'user.profile.form.usergroupRequired'));
$this->addCheck(new FormValidator($this, 'userGroupId', 'required', 'user.profile.form.usergroupRequired'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
namespace PKP\controllers\grid\users\reviewer\form;

use APP\core\Application;
use Carbon\Carbon;

use APP\facades\Repo;
use Illuminate\Support\Facades\Mail;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;
use PKP\submission\reviewRound\ReviewRound;
use APP\submission\Submission;
use PKP\form\validation\FormValidator;
use PKP\db\DAORegistry;
use PKP\security\Role;
use PKP\security\RoleDAO;
Expand All @@ -27,14 +33,20 @@ class EnrollExistingReviewerForm extends ReviewerForm
{
/**
* Constructor.
*
* @param Submission $submission
* @param ReviewRound $reviewRound
* @param ReviewerSuggestion|null $reviewerSuggestion
*/
public function __construct($submission, $reviewRound)
public function __construct($submission, $reviewRound, $reviewerSuggestion = null)
{
parent::__construct($submission, $reviewRound);
$this->reviewerSuggestion = $reviewerSuggestion;

$this->setTemplate('controllers/grid/users/reviewer/form/enrollExistingReviewerForm.tpl');

$this->addCheck(new \PKP\form\validation\FormValidator($this, 'userGroupId', 'required', 'user.profile.form.usergroupRequired'));
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'userId', 'required', 'manager.people.existingUserRequired'));
$this->addCheck(new FormValidator($this, 'userGroupId', 'required', 'user.profile.form.usergroupRequired'));
$this->addCheck(new FormValidator($this, 'userId', 'required', 'manager.people.existingUserRequired'));
}

/**
Expand All @@ -48,6 +60,13 @@ public function initData()
$context = Application::get()->getRequest()->getContext();
$template = Repo::emailTemplate()->getByKey($context->getId(), $mailable::getEmailTemplateKey());
$this->setData('personalMessage', Mail::compileParams($template->getLocalizedData('body'), $mailable->viewData));

if ($this->reviewerSuggestion && $this->reviewerSuggestion->existingUser) {
$existingUser = $this->reviewerSuggestion->existingUser; /** @var \PKP\user\User $existingUser */
$this->setData('reviewerSuggestionId', $this->reviewerSuggestion->id);
$this->setData('userId', $existingUser->getId());
$this->setData('selectedUser', $existingUser->getFullName().' (' . $existingUser->getData('email') . ')');
}
}

/**
Expand All @@ -72,7 +91,13 @@ public function readInputData()
{
parent::readInputData();

$this->readUserVars(['userId', 'userGroupId']);
$inputData = ['userId', 'userGroupId'];

if ($this->reviewerSuggestion) {
array_push($inputData, 'reviewerSuggestionId');
}

$this->readUserVars($inputData);
}

/**
Expand All @@ -94,6 +119,17 @@ public function execute(...$functionArgs)
// Set the reviewerId in the Form for the parent class to use
$this->setData('reviewerId', $userId);

if ($this->reviewerSuggestion?->existingUser
&& $this->reviewerSuggestion->existingUser->getId() == $userId) {

$request = Application::get()->getRequest();
$this->reviewerSuggestion->markAsApprove(
Carbon::now(),
$userId,
$request->getUser()->getId()
);
}

return parent::execute(...$functionArgs);
}

Expand Down
4 changes: 4 additions & 0 deletions controllers/grid/users/reviewer/form/ReviewerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use PKP\security\RoleDAO;
use PKP\submission\action\EditorAction;
use PKP\submission\reviewAssignment\ReviewAssignment;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;
use PKP\submission\ReviewFilesDAO;
use PKP\submission\reviewRound\ReviewRound;
use PKP\submissionFile\SubmissionFile;
Expand All @@ -60,6 +61,9 @@ class ReviewerForm extends Form
/** @var array An array with all current user roles */
public $_userRoles;

/** @var ReviewerSuggestion|null The The suggested reviewer */
public ?ReviewerSuggestion $reviewerSuggestion = null;

/**
* Constructor.
*
Expand Down
Loading

0 comments on commit 60b1612

Please sign in to comment.