Skip to content

Commit

Permalink
Merge branch 'optimizeReminderGeneration-703' into 'stable-3_3_0'
Browse files Browse the repository at this point in the history
Corrige e otimiza funcionalidade de envio manual de lembretes

See merge request softwares-pkp/plugins_ojs/scieloModerationStages!20
  • Loading branch information
JhonathanLepidus committed Oct 7, 2024
2 parents 6ea9b65 + 5ebecd9 commit 5a3259b
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 155 deletions.
48 changes: 2 additions & 46 deletions classes/ModerationReminderHelper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,18 @@ public function getResponsiblesUserGroup(int $contextId)
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')) {
if ($userId != $assignment['userId']) {
continue;
}

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

if (isset($usersMap[$userId])) {
$usersMap[$userId] = array_merge($usersMap[$userId], [$submission]);
Expand Down
24 changes: 24 additions & 0 deletions classes/ModerationStageDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ public function getPreModerationIsOverdue(int $submissionId, int $timeLimit): bo

return $dateSubmitted < $limitDaysAgo;
}

public function getAssignmentsByUserGroupAndModerationStage(int $userGroupId, int $moderationStage, ?int $userId = null): array
{
$query = Capsule::table('stage_assignments AS sa')
->leftJoin('submission_settings AS sub_s', 'sa.submission_id', '=', 'sub_s.submission_id')
->where('sub_s.setting_name', 'currentModerationStage')
->where('sub_s.setting_value', '=', $moderationStage)
->where('sa.user_group_id', '=', $userGroupId)
->select('sa.user_id', 'sa.submission_id');

if ($userId) {
$query = $query->where('sa.user_id', '=', $userId);
}

$result = $query->get();
$assignments = [];

foreach ($result as $row) {
$row = get_object_vars($row);
$assignments[] = ['userId' => $row['user_id'], 'submissionId' => $row['submission_id']];
}

return $assignments;
}
}
24 changes: 14 additions & 10 deletions classes/tasks/SendModerationReminders.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ public function executeActions()
$context = Application::get()->getRequest()->getContext();
$moderationReminderHelper = new ModerationReminderHelper();
$responsiblesUserGroup = $moderationReminderHelper->getResponsiblesUserGroup($context->getId());
$responsibleAssignments = $moderationReminderHelper->getResponsibleAssignments($responsiblesUserGroup, $context->getId());
$preModerationAssignments = $moderationReminderHelper->filterAssignmentsOfSubmissionsOnPreModeration($responsibleAssignments);

if (empty($preModerationAssignments)) {
$moderationStageDao = new ModerationStageDAO();
$responsibleAssignments = $moderationStageDao->getAssignmentsByUserGroupAndModerationStage(
$responsiblesUserGroup->getId(),
SCIELO_MODERATION_STAGE_CONTENT
);

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

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

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

private function getUsersWithOverduePreModeration($contextId, $preModerationAssignments): array
private function getUsersWithOverduePreModeration($contextId, $assignments): array
{
$usersIds = [];
$preModerationTimeLimit = $this->plugin->getSetting($contextId, 'preModerationTimeLimit');
$moderationStageDao = new ModerationStageDAO();

foreach ($preModerationAssignments as $assignment) {
$submissionId = $assignment->getData('submissionId');
foreach ($assignments as $assignment) {
$submissionId = $assignment['submissionId'];
$preModerationIsOverdue = $moderationStageDao->getPreModerationIsOverdue($submissionId, $preModerationTimeLimit);

if ($preModerationIsOverdue) {
$usersIds[] = $assignment->getData('userId');
if ($preModerationIsOverdue and !isset($usersIds[$assignment['userId']])) {
$usersIds[$assignment['userId']] = $assignment['userId'];
}
}

Expand Down
17 changes: 10 additions & 7 deletions controllers/ScieloModerationStagesHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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
Expand All @@ -15,17 +14,21 @@ class ScieloModerationStagesHandler extends Handler

public function getReminderBody($args, $request)
{
$responsiblesUserGroupId = (int) $args['responsiblesUserGroup'];
$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();

$moderationStageDao = new ModerationStageDAO();
$assignments = $moderationStageDao->getAssignmentsByUserGroupAndModerationStage(
$responsiblesUserGroupId,
SCIELO_MODERATION_STAGE_CONTENT,
$responsible->getId()
);

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

if ($submission) {
$submissions[] = $submission;
Expand Down
37 changes: 26 additions & 11 deletions form/SendModerationReminderForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import('lib.pkp.classes.form.Form');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderHelper');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderEmailBuilder');
import('plugins.generic.scieloModerationStages.classes.ModerationStageDAO');
import('plugins.generic.scieloModerationStages.classes.ModerationStage');

class SendModerationReminderForm extends Form
{
Expand All @@ -16,37 +18,50 @@ public function __construct($plugin, $contextId)
parent::__construct($plugin->getTemplateResource('sendModerationReminderForm.tpl'));
}

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

return $responsiblesUserGroup->getId();
}

private function getResponsibles(int $responsiblesUserGroupId): array
{
$moderationStageDao = new ModerationStageDAO();
$responsibleAssignments = $moderationStageDao->getAssignmentsByUserGroupAndModerationStage(
$responsiblesUserGroupId,
SCIELO_MODERATION_STAGE_CONTENT
);

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

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

$mappedUsers = [null => null];
foreach ($usersFromAssignments as $userId => $user) {
$responsibles = [null => null];
$userDao = DAORegistry::getDAO('UserDAO');
foreach ($responsibleAssignments as $assignment) {
$user = $userDao->getById($assignment['userId']);
$fullName = $user->getFullName();
$mappedUsers[$userId] = $fullName;
$responsibles[$user->getId()] = $fullName;
}

asort($mappedUsers, SORT_STRING);
asort($responsibles, SORT_STRING);

return $mappedUsers;
return $responsibles;
}

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

$responsiblesUserGroupId = $this->getResponsiblesUserGroupId($contextId);
$responsibles = $this->getResponsibles($responsiblesUserGroupId);

$templateMgr->assign([
'responsibles' => $this->getResponsibles($contextId),
'responsiblesUserGroupId' => $responsiblesUserGroupId,
'responsibles' => $responsibles,
'pluginName' => $this->plugin->getName(),
'applicationName' => Application::get()->getName()
]);
Expand Down
3 changes: 3 additions & 0 deletions templates/sendModerationReminderForm.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{include file="controllers/notification/inPlaceNotification.tpl" notificationId="sendModerationReminderFormNotification"}

{fbvFormSection id="responsibleSection" label="plugins.generic.scieloModerationStages.sendModerationReminder.responsible.title"}
<input type="hidden" id="responsiblesUserGroupId" name="responsiblesUserGroupId" value="{$responsiblesUserGroupId|escape}" />
{fbvElement type="select" id="responsible" name="responsible" from=$responsibles required="true" label="plugins.generic.scieloModerationStages.sendModerationReminder.responsible.description" translate=false size=$fbvStyles.size.MEDIUM}
{/fbvFormSection}

Expand All @@ -34,11 +35,13 @@
$(function(){ldelim}
$('#responsible').change(function () {
let responsiblesUserGroupId = $('#responsiblesUserGroupId').val();
let responsibleId = $('#responsible').val();
$.get(
"{$getReminderBodyUrl}",
{ldelim}
responsible: responsibleId,
responsiblesUserGroup: responsiblesUserGroupId
{rdelim},
updateReminderBody
);
Expand Down
Loading

0 comments on commit 5a3259b

Please sign in to comment.