From 032d6f856ce5af0a21cb0c75dddc9d13376910c8 Mon Sep 17 00:00:00 2001 From: Touhidur Rahman Date: Mon, 2 Sep 2024 13:42:41 +0600 Subject: [PATCH] pkp/pkp-lib#4789 refactored cauculating due dates to traits with added validations --- .../form/ResendRequestReviewerForm.php | 15 ++++- .../grid/users/reviewer/form/ReviewerForm.php | 27 ++------ .../form/ReviewerNotifyActionForm.php | 1 + .../reviewer/form/traits/HasReviewDueDate.php | 63 +++++++++++++++++++ 4 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 controllers/grid/users/reviewer/form/traits/HasReviewDueDate.php diff --git a/controllers/grid/users/reviewer/form/ResendRequestReviewerForm.php b/controllers/grid/users/reviewer/form/ResendRequestReviewerForm.php index 29a189cb37d..d8232e82a6a 100644 --- a/controllers/grid/users/reviewer/form/ResendRequestReviewerForm.php +++ b/controllers/grid/users/reviewer/form/ResendRequestReviewerForm.php @@ -21,6 +21,7 @@ use APP\notification\NotificationManager; use APP\submission\Submission; use PKP\context\Context; +use PKP\controllers\grid\users\reviewer\form\traits\HasReviewDueDate; use PKP\core\Core; use PKP\core\PKPApplication; use PKP\log\event\PKPSubmissionEventLogEntry; @@ -33,6 +34,8 @@ class ResendRequestReviewerForm extends ReviewerNotifyActionForm { + use HasReviewDueDate; + /** * Constructor */ @@ -48,6 +51,16 @@ public function __construct(ReviewAssignment $reviewAssignment, ReviewRound $rev // Validation checks for this form $this->addCheck(new \PKP\form\validation\FormValidator($this, 'responseDueDate', 'required', 'editor.review.errorAddingReviewer')); $this->addCheck(new \PKP\form\validation\FormValidator($this, 'reviewDueDate', 'required', 'editor.review.errorAddingReviewer')); + $this->addCheck( + new \PKP\form\validation\FormValidatorDateCompare( + $this, + 'reviewDueDate', + \Carbon\Carbon::parse(Application::get()->getRequest()->getUserVar('responseDueDate')), + \PKP\validation\enums\DateComparisonRule::GREATER_OR_EQUAL, + 'required', + 'editor.review.errorAddingReviewer.dateValidationFailed' + ) + ); } /** @@ -73,7 +86,7 @@ public function initData() { parent::initData(); - [$reviewDueDate, $responseDueDate] = ReviewerForm::getDueDates(Application::get()->getRequest()->getContext()); + [$reviewDueDate, $responseDueDate] = $this->getDueDates(Application::get()->getRequest()->getContext()); $this->setData('responseDueDate', $responseDueDate); $this->setData('reviewDueDate', $reviewDueDate); diff --git a/controllers/grid/users/reviewer/form/ReviewerForm.php b/controllers/grid/users/reviewer/form/ReviewerForm.php index 2f8203551f1..b677a3d2d7d 100644 --- a/controllers/grid/users/reviewer/form/ReviewerForm.php +++ b/controllers/grid/users/reviewer/form/ReviewerForm.php @@ -24,6 +24,7 @@ use APP\submission\Submission; use APP\template\TemplateManager; use PKP\context\Context; +use PKP\controllers\grid\users\reviewer\form\traits\HasReviewDueDate; use PKP\controllers\grid\users\reviewer\PKPReviewerGridHandler; use PKP\core\Core; use PKP\db\DAORegistry; @@ -45,6 +46,8 @@ class ReviewerForm extends Form { + use HasReviewDueDate; + /** @var Submission The submission associated with the review assignment */ public $_submission; @@ -88,26 +91,6 @@ public function __construct($submission, $reviewRound) $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); } - /** - * Get the review submit and response due dates - */ - public static function getDueDates(Context $context): array - { - $numWeeks = (int) $context->getData('numWeeksPerReview'); - if ($numWeeks <= 0) { - $numWeeks = 4; - } - $reviewDueDate = strtotime('+' . $numWeeks . ' week'); - - $numWeeks = (int) $context->getData('numWeeksPerResponse'); - if ($numWeeks <= 0) { - $numWeeks = 3; - } - $responseDueDate = strtotime('+' . $numWeeks . ' week'); - - return [$reviewDueDate, $responseDueDate]; - } - // // Getters and Setters // @@ -231,7 +214,7 @@ public function initData() $reviewFormId = null; } - [$reviewDueDate, $responseDueDate] = static::getDueDates($context); + [$reviewDueDate, $responseDueDate] = $this->getDueDates($context); // Get the currently selected reviewer selection type to show the correct tab if we're re-displaying the form $selectionType = (int) $request->getUserVar('selectionType'); @@ -267,7 +250,7 @@ public function fetch($request, $template = null, $display = false) $reviewFormDao = DAORegistry::getDAO('ReviewFormDAO'); /** @var ReviewFormDAO $reviewFormDao */ $reviewFormsIterator = $reviewFormDao->getActiveByAssocId(Application::getContextAssocType(), $context->getId()); $reviewForms = []; - while ($reviewForm = $reviewFormsIterator->next()) { + while ($reviewForm = $reviewFormsIterator->next()) { /** @var \PKP\reviewForm\ReviewForm $reviewForm */ $reviewForms[$reviewForm->getId()] = $reviewForm->getLocalizedTitle(); } diff --git a/controllers/grid/users/reviewer/form/ReviewerNotifyActionForm.php b/controllers/grid/users/reviewer/form/ReviewerNotifyActionForm.php index 5b27a63240b..c4be1fd30e5 100644 --- a/controllers/grid/users/reviewer/form/ReviewerNotifyActionForm.php +++ b/controllers/grid/users/reviewer/form/ReviewerNotifyActionForm.php @@ -50,6 +50,7 @@ public function __construct($reviewAssignment, $reviewRound, $submission, $templ $this->setReviewRound($reviewRound); $this->setSubmission($submission); + $this->addCheck(new \PKP\form\validation\FormValidatorPost($this)); $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); parent::__construct($template); diff --git a/controllers/grid/users/reviewer/form/traits/HasReviewDueDate.php b/controllers/grid/users/reviewer/form/traits/HasReviewDueDate.php new file mode 100644 index 00000000000..af7200fbbfe --- /dev/null +++ b/controllers/grid/users/reviewer/form/traits/HasReviewDueDate.php @@ -0,0 +1,63 @@ +getData('numWeeksPerReview'); + + if ($numWeeks <= 0) { + $numWeeks = static::REVIEW_SUBMIT_DEFAULT_DUE_WEEKS; + } + + return Carbon::today()->endOfDay()->addWeeks($numWeeks); + } + + /** + * Get the review response due dates + */ + public function getReviewResponseDueDate(Context $context): Carbon + { + $numWeeks = (int) $context->getData('numWeeksPerResponse'); + + if ($numWeeks <= 0) { + $numWeeks = static::RESPONSE_RESPONSE_DEFAULT_DUE_WEEKS; + } + + return Carbon::today()->endOfDay()->addWeeks($numWeeks); + } + + /** + * Get the review submit and response due dates + */ + public function getDueDates(Context $context): array + { + return [ + $this->getReviewSubmitDueDate($context)->getTimestamp(), + $this->getReviewResponseDueDate($context)->getTimestamp(), + ]; + } +}