diff --git a/classes/tasks/SendModerationReminders.php b/classes/tasks/SendModerationReminders.php index af3aeb6..0ed624d 100644 --- a/classes/tasks/SendModerationReminders.php +++ b/classes/tasks/SendModerationReminders.php @@ -2,6 +2,7 @@ namespace APP\plugins\generic\scieloModerationStages\classes\tasks; +use DateTime; use PKP\scheduledTask\ScheduledTask; use PKP\plugins\PluginRegistry; use APP\core\Application; @@ -22,6 +23,20 @@ public function executeActions() $this->plugin = PluginRegistry::getPlugin('generic', 'scielomoderationstagesplugin'); $context = Application::get()->getRequest()->getContext(); + $locale = $context->getPrimaryLocale(); + $this->plugin->addLocaleData($locale); + + $preModerationTimeLimit = $this->plugin->getSetting($context->getId(), 'preModerationTimeLimit'); + $this->sendResponsiblesReminders($context, $preModerationTimeLimit, $locale); + + $areaModerationTimeLimit = $this->plugin->getSetting($context->getId(), 'areaModerationTimeLimit'); + $this->sendAreaModeratorsReminders($context, $areaModerationTimeLimit, $locale); + + return true; + } + + private function sendResponsiblesReminders($context, $preModerationTimeLimit, $locale) + { $moderationReminderHelper = new ModerationReminderHelper(); $responsiblesUserGroup = $moderationReminderHelper->getResponsiblesUserGroup($context->getId()); @@ -32,16 +47,12 @@ public function executeActions() ); if (empty($responsibleAssignments)) { - return true; + return; } - $usersWithOverduePreModeration = $this->getUsersWithOverduePreModeration($context->getId(), $responsibleAssignments); + $usersWithOverduePreModeration = $this->getUsersWithOverduePreModeration($responsibleAssignments, $preModerationTimeLimit); $mapModeratorsAndOverdueSubmissions = $moderationReminderHelper->mapUsersAndSubmissions($usersWithOverduePreModeration, $responsibleAssignments); - $locale = $context->getPrimaryLocale(); - $this->plugin->addLocaleData($locale); - $preModerationTimeLimit = $this->plugin->getSetting($context->getId(), 'preModerationTimeLimit'); - foreach ($mapModeratorsAndOverdueSubmissions as $userId => $submissions) { $moderator = Repo::user()->get($userId); $moderationReminderEmailBuilder = new ModerationReminderEmailBuilder( @@ -56,14 +67,45 @@ public function executeActions() $reminderEmail = $moderationReminderEmailBuilder->buildEmail(); Mail::send($reminderEmail); } + } - return true; + private function sendAreaModeratorsReminders($context, $areaModerationTimeLimit, $locale) + { + $moderationReminderHelper = new ModerationReminderHelper(); + $areaModeratorsUserGroup = $moderationReminderHelper->getAreaModeratorsUserGroup($context->getId()); + + $moderationStageDao = new ModerationStageDAO(); + $areaModeratorAssignments = $moderationStageDao->getAssignmentsByUserGroupAndModerationStage( + $areaModeratorsUserGroup->getId(), + ModerationStage::SCIELO_MODERATION_STAGE_AREA + ); + + if (empty($areaModeratorAssignments)) { + return; + } + + $usersWithOverdueAreaModeration = $this->getUsersWithOverdueAreaModeration($areaModeratorAssignments, $areaModerationTimeLimit); + $mapModeratorsAndOverdueSubmissions = $moderationReminderHelper->mapUsersAndSubmissions($usersWithOverdueAreaModeration, $areaModeratorAssignments); + + foreach ($mapModeratorsAndOverdueSubmissions as $userId => $submissions) { + $moderator = Repo::user()->get($userId); + $moderationReminderEmailBuilder = new ModerationReminderEmailBuilder( + $context, + $moderator, + $submissions, + $locale, + ModerationReminderEmailBuilder::REMINDER_TYPE_AREA_MODERATION, + $areaModerationTimeLimit + ); + + $reminderEmail = $moderationReminderEmailBuilder->buildEmail(); + Mail::send($reminderEmail); + } } - private function getUsersWithOverduePreModeration($contextId, $assignments): array + private function getUsersWithOverduePreModeration($assignments, $preModerationTimeLimit): array { $usersIds = []; - $preModerationTimeLimit = $this->plugin->getSetting($contextId, 'preModerationTimeLimit'); $moderationStageDao = new ModerationStageDAO(); foreach ($assignments as $assignment) { @@ -77,4 +119,20 @@ private function getUsersWithOverduePreModeration($contextId, $assignments): arr return $usersIds; } + + private function getUsersWithOverdueAreaModeration($assignments, $areaModerationTimeLimit): array + { + $usersIds = []; + $limitDaysAgo = (new DateTime())->modify("-$areaModerationTimeLimit days"); + + foreach ($assignments as $assignment) { + $dateAssigned = new DateTime($assignment['dateAssigned']); + + if ($dateAssigned < $limitDaysAgo and !isset($usersIds[$assignment['userId']])) { + $usersIds[$assignment['userId']] = $assignment['userId']; + } + } + + return $usersIds; + } } diff --git a/scheduledTasks.xml b/scheduledTasks.xml index 16b9dc4..9062ce6 100644 --- a/scheduledTasks.xml +++ b/scheduledTasks.xml @@ -5,6 +5,6 @@ Send moderation reminders to moderators whose moderation is overdue - + \ No newline at end of file