Skip to content

Commit

Permalink
Merge branch 'sendRemindersManually-702' into 'stable-3_3_0'
Browse files Browse the repository at this point in the history
Adiciona envio manual do lembrete de moderadores

See merge request softwares-pkp/plugins_ojs/scieloModerationStages!19
  • Loading branch information
JhonathanLepidus committed Oct 4, 2024
2 parents 48c0d2f + e56f38d commit 6ea9b65
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 85 deletions.
44 changes: 29 additions & 15 deletions ScieloModerationStagesPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,22 @@ public function getActions($request, $actionArgs)
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
[
new LinkAction(
'sendModerationReminder',
new AjaxModal(
$router->url($request, null, null, 'manage', null, ['verb' => 'sendModerationReminder', 'plugin' => $this->getName(), 'category' => 'generic']),
__('plugins.generic.scieloModerationStages.sendModerationReminder')
),
__('plugins.generic.scieloModerationStages.sendModerationReminder')
),
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']),
__('plugins.generic.scieloModerationStages.settings.title')
),
__('manager.plugins.settings'),
null
),
__('manager.plugins.settings')
)
],
parent::getActions($request, $actionArgs)
);
Expand All @@ -118,22 +125,29 @@ public function manage($args, $request)

switch ($request->getUserVar('verb')) {
case 'settings':
$this->import('ScieloModerationStagesSettingsForm');
$form = new ScieloModerationStagesSettingsForm($this, $contextId);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
return $this->handlePluginForm($request, $contextId, 'ScieloModerationStagesSettingsForm');
case 'sendModerationReminder':
return $this->handlePluginForm($request, $contextId, 'SendModerationReminderForm');
}
return parent::manage($args, $request);
}

private function handlePluginForm($request, $contextId, $formClass)
{
$this->import('form.'.$formClass);
$form = new $formClass($this, $contextId);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}

public function setupScieloModerationStagesHandler($hookName, $params)
{
$component = &$params[0];
Expand Down
103 changes: 103 additions & 0 deletions classes/ModerationReminderHelper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

import('plugins.generic.scieloModerationStages.classes.ModerationStageDAO');

class ModerationReminderHelper
{
private $moderationStageDao;

public function __construct()
{
$this->moderationStageDao = new ModerationStageDAO();
}

public function setModerationStageDao($moderationStageDao)
{
$this->moderationStageDao = $moderationStageDao;
}

public function getResponsiblesUserGroup(int $contextId)
{
$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$contextUserGroups = $userGroupDao->getByContextId($contextId)->toArray();

foreach ($contextUserGroups as $userGroup) {
$userGroupAbbrev = strtolower($userGroupDao->getSetting($userGroup->getId(), 'abbrev', 'en_US'));

if ($userGroupAbbrev === 'resp') {
$responsiblesUserGroup = $userGroup;
break;
}
}

return $responsiblesUserGroup;
}

public function getResponsibleAssignments($responsiblesUserGroup, $contextId): array
{
if (!$responsiblesUserGroup) {
return [];
}

$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$responsiblesAssignments = $stageAssignmentDao->getByUserGroupId($responsiblesUserGroup->getId(), $contextId);

return $responsiblesAssignments->toArray();
}

public function filterAssignmentsOfSubmissionsOnPreModeration(array $assignments): array
{
$preModerationAssignments = [];

foreach ($assignments as $assignment) {
$submissionId = $assignment->getData('submissionId');
$submissionModerationStage = $this->moderationStageDao->getSubmissionModerationStage($submissionId);

if ($submissionModerationStage === SCIELO_MODERATION_STAGE_CONTENT) {
$preModerationAssignments[] = $assignment;
}
}

return $preModerationAssignments;
}

public function getUsersFromAssignments(array $assignments): array
{
$users = [];
$userDao = DAORegistry::getDAO('UserDAO');

foreach ($assignments as $assignment) {
$user = $userDao->getById($assignment->getUserId());

if ($user and !isset($users[$user->getId()])) {
$users[$user->getId()] = $user;
}
}

return $users;
}

public function mapUsersAndSubmissions($users, $assignments)
{
$usersMap = [];
$submissionDao = DAORegistry::getDAO('SubmissionDAO');

foreach ($users as $userId) {
foreach ($assignments as $assignment) {
if ($userId != $assignment->getData('userId')) {
continue;
}

$submission = $submissionDao->getById($assignment->getData('submissionId'));

if (isset($usersMap[$userId])) {
$usersMap[$userId] = array_merge($usersMap[$userId], [$submission]);
} else {
$usersMap[$userId] = [$submission];
}
}
}

return $usersMap;
}
}
74 changes: 6 additions & 68 deletions classes/tasks/SendModerationReminders.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import('plugins.generic.scieloModerationStages.classes.ModerationStage');
import('plugins.generic.scieloModerationStages.classes.ModerationStageDAO');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderEmailBuilder');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderHelper');

class SendModerationReminders extends ScheduledTask
{
Expand All @@ -15,15 +16,17 @@ public function executeActions()
$this->plugin = PluginRegistry::getPlugin('generic', 'scielomoderationstagesplugin');

$context = Application::get()->getRequest()->getContext();
$responsiblesAssignments = $this->getResponsiblesAssignments($context->getId());
$preModerationAssignments = $this->filterPreModerationAssignments($responsiblesAssignments);
$moderationReminderHelper = new ModerationReminderHelper();
$responsiblesUserGroup = $moderationReminderHelper->getResponsiblesUserGroup($context->getId());
$responsibleAssignments = $moderationReminderHelper->getResponsibleAssignments($responsiblesUserGroup, $context->getId());
$preModerationAssignments = $moderationReminderHelper->filterAssignmentsOfSubmissionsOnPreModeration($responsibleAssignments);

if (empty($preModerationAssignments)) {
return true;
}

$usersWithOverduePreModeration = $this->getUsersWithOverduePreModeration($context->getId(), $preModerationAssignments);
$mapModeratorsAndOverdueSubmissions = $this->mapModeratorsAndOverdueSubmissions($usersWithOverduePreModeration, $preModerationAssignments);
$mapModeratorsAndOverdueSubmissions = $moderationReminderHelper->mapUsersAndSubmissions($usersWithOverduePreModeration, $preModerationAssignments);

foreach ($mapModeratorsAndOverdueSubmissions as $userId => $submissions) {
$moderator = DAORegistry::getDAO('UserDAO')->getById($userId);
Expand All @@ -36,47 +39,6 @@ public function executeActions()
return true;
}

private function getResponsiblesAssignments(int $contextId)
{
$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$contextUserGroups = $userGroupDao->getByContextId($contextId)->toArray();

foreach ($contextUserGroups as $userGroup) {
$userGroupAbbrev = strtolower($userGroupDao->getSetting($userGroup->getId(), 'abbrev', 'en_US'));

if ($userGroupAbbrev === 'resp') {
$responsiblesUserGroup = $userGroup;
break;
}
}

if (!$responsiblesUserGroup) {
return [];
}

$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$responsiblesAssignments = $stageAssignmentDao->getByUserGroupId($responsiblesUserGroup->getId(), $contextId);

return $responsiblesAssignments->toArray();
}

private function filterPreModerationAssignments($responsiblesAssignments): array
{
$moderationStageDao = new ModerationStageDAO();
$preModerationAssignments = [];

foreach ($responsiblesAssignments as $assignment) {
$submissionId = $assignment->getData('submissionId');
$submissionModerationStage = $moderationStageDao->getSubmissionModerationStage($submissionId);

if ($submissionModerationStage === SCIELO_MODERATION_STAGE_CONTENT) {
$preModerationAssignments[] = $assignment;
}
}

return $preModerationAssignments;
}

private function getUsersWithOverduePreModeration($contextId, $preModerationAssignments): array
{
$usersIds = [];
Expand All @@ -94,28 +56,4 @@ private function getUsersWithOverduePreModeration($contextId, $preModerationAssi

return $usersIds;
}

private function mapModeratorsAndOverdueSubmissions($moderators, $preModerationAssignments)
{
$moderatorsMap = [];
$submissionDao = DAORegistry::getDAO('SubmissionDAO');

foreach ($moderators as $moderatorId) {
foreach ($preModerationAssignments as $assignment) {
if ($moderatorId != $assignment->getData('userId')) {
continue;
}

$submission = $submissionDao->getById($assignment->getData('submissionId'));

if (isset($moderatorsMap[$moderatorId])) {
$moderatorsMap[$moderatorId] = array_merge($moderatorsMap[$moderatorId], [$submission]);
} else {
$moderatorsMap[$moderatorId] = [$submission];
}
}
}

return $moderatorsMap;
}
}
27 changes: 27 additions & 0 deletions controllers/ScieloModerationStagesHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@
import('classes.handler.Handler');
import('classes.workflow.EditorDecisionActionsManager');
import('plugins.generic.scieloModerationStages.classes.ModerationStageDAO');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderHelper');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderEmailBuilder');

class ScieloModerationStagesHandler extends Handler
{
private const SUBMISSION_STAGE_ID = 5;
private const THRESHOLD_TIME_EXHIBITORS = 2;

public function getReminderBody($args, $request)
{
$responsible = DAORegistry::getDAO('UserDAO')->getById((int) $args['responsible']);

$moderationReminderHelper = new ModerationReminderHelper();
$context = $request->getContext();
$responsiblesUserGroup = $moderationReminderHelper->getResponsiblesUserGroup($context->getId());
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$assignments = $stageAssignmentDao->_getByIds(null, null, $responsiblesUserGroup->getId(), $responsible->getId())->toArray();

$submissions = [];
foreach ($assignments as $assignment) {
$submission = DAORegistry::getDAO('SubmissionDAO')->getById($assignment->getSubmissionId());

if ($submission) {
$submissions[] = $submission;
}
}

$moderationReminderEmailBuilder = new ModerationReminderEmailBuilder($context, $responsible, $submissions);
$reminderEmail = $moderationReminderEmailBuilder->buildEmail();

return json_encode(['reminderBody' => $reminderEmail->getBody()]);
}

public function updateSubmissionStageData($args, $request)
{
$submissionDao = DAORegistry::getDAO('SubmissionDAO');
Expand Down
File renamed without changes.
76 changes: 76 additions & 0 deletions form/SendModerationReminderForm.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

import('lib.pkp.classes.form.Form');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderHelper');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderEmailBuilder');

class SendModerationReminderForm extends Form
{
public $contextId;
public $plugin;

public function __construct($plugin, $contextId)
{
$this->contextId = $contextId;
$this->plugin = $plugin;
parent::__construct($plugin->getTemplateResource('sendModerationReminderForm.tpl'));
}

private function getResponsibles(int $contextId): array
{
$moderationReminderHelper = new ModerationReminderHelper();
$responsiblesUserGroup = $moderationReminderHelper->getResponsiblesUserGroup($contextId);
$responsibleAssignments = $moderationReminderHelper->getResponsibleAssignments($responsiblesUserGroup, $contextId);

if (empty($responsibleAssignments)) {
return [];
}

$filteredAssignments = $moderationReminderHelper->filterAssignmentsOfSubmissionsOnPreModeration($responsibleAssignments);
$usersFromAssignments = $moderationReminderHelper->getUsersFromAssignments($filteredAssignments);

$mappedUsers = [null => null];
foreach ($usersFromAssignments as $userId => $user) {
$fullName = $user->getFullName();
$mappedUsers[$userId] = $fullName;
}

asort($mappedUsers, SORT_STRING);

return $mappedUsers;
}

public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$contextId = $request->getContext()->getId();

$templateMgr->assign([
'responsibles' => $this->getResponsibles($contextId),
'pluginName' => $this->plugin->getName(),
'applicationName' => Application::get()->getName()
]);

return parent::fetch($request, $template, $display);
}

public function readInputData()
{
$this->readUserVars(['responsible', 'reminderBody']);
}

public function execute(...$functionArgs)
{
$responsibleUserId = $this->getData('responsible');
$reminderBody = $this->getData('reminderBody');

$responsible = DAORegistry::getDAO('UserDAO')->getById($responsibleUserId);
$context = Application::get()->getRequest()->getContext();

$moderationReminderEmailBuilder = new ModerationReminderEmailBuilder($context, $responsible, []);
$reminderEmail = $moderationReminderEmailBuilder->buildEmail();
$reminderEmail->setBody($reminderBody);

$reminderEmail->send();
}
}
Loading

0 comments on commit 6ea9b65

Please sign in to comment.