Skip to content

Commit

Permalink
pkp#10480 Record only latest recommendation & fix canCurrentUserChang…
Browse files Browse the repository at this point in the history
…eMetadata
  • Loading branch information
Vitaliy-1 committed Jan 16, 2025
1 parent 479d779 commit 72db9ed
Showing 1 changed file with 78 additions and 24 deletions.
102 changes: 78 additions & 24 deletions classes/submission/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,7 @@ protected function mapByProperties(array $props, Submission $submission, bool|Co
break;
case 'canCurrentUserChangeMetadata':
// Identify if current user can change metadata. Consider roles in the active stage.
$output[$prop] = !empty(array_intersect(
PKPApplication::getWorkflowTypeRoles()[PKPApplication::WORKFLOW_TYPE_EDITORIAL],
$stages[$submission->getData('stageId')]['currentUserAssignedRoles']
));
$output[$prop] = $this->canChangeMetadata($this->stageAssignments, $submission);
break;
case 'editorAssigned':
$output[$prop] = $this->getPropertyStageAssignments($this->stageAssignments);
Expand Down Expand Up @@ -487,6 +484,43 @@ protected function mapByProperties(array $props, Submission $submission, bool|Co
return $output;
}

/**
* Determine whether current user is able to change metadata
*/
protected function canChangeMetadata(Enumerable $stageAssignments, Submission $submission): bool
{
$currentUser = Application::get()->getRequest()->getUser();
$isAssigned = false;
$canChangeMetadata = false;

// Check if stage assignment is associated with the current user and edit metadata flag
foreach ($stageAssignments as $stageAssignment) {
if ($stageAssignment->userId === $currentUser->getId()) {
$isAssigned = true;
if ($stageAssignment->canChangeMetadata) {
$canChangeMetadata = true;
break;
}
}
}

if ($canChangeMetadata) {
return true;
}

// If user is assigned, check editorial global roles, journal admin and managers should have access for editing metadata
if (!$isAssigned) {
if (!empty(array_intersect(
PKPApplication::getWorkflowTypeRoles()[PKPApplication::WORKFLOW_TYPE_EDITORIAL],
$stages[$submission->getData('stageId')]['currentUserAssignedRoles']
))) {
return true;
}
}

return false;
}

/**
* Get details about the review assignments for a submission
*/
Expand Down Expand Up @@ -602,7 +636,6 @@ protected function getPropertyStages(Enumerable $stageAssignments, Submission $s

// values false by default, to be determined later
'editorAssigned' => false,
'isCurrentUserDecidingEditor' => false,
'currentUserAssignedRoles' => [],
];
}
Expand Down Expand Up @@ -663,7 +696,7 @@ protected function getPropertyStages(Enumerable $stageAssignments, Submission $s
}

if (!$stageAssignment->recommendOnly) {
$isCurrentUserDecidingEditor = $stages[$groupStage->stageId]['isCurrentUserDecidingEditor'] = true;
$isCurrentUserDecidingEditor = true;
}

// if the user is assigned several times in the editorial role, and
Expand All @@ -683,19 +716,22 @@ protected function getPropertyStages(Enumerable $stageAssignments, Submission $s
if (!empty($globalRoles)) {
foreach ($stageIds as $stageId) {
$stages[$stageId]['currentUserAssignedRoles'] = $globalRoles;
$isCurrentUserDecidingEditor = $stages[$stageId]['isCurrentUserDecidingEditor'] = true;
if ($hasRecommendingEditors) {
$isCurrentUserDecidingEditor = $stages[$stageId]['isCurrentUserDecidingEditor'] = true;
}
}
}
}

// Set recommendation related props
// Retrieve recommendations for the review stage
$reviewRecommendations = collect();
if (
isset($currentReviewRound) &&
isset($decisions) && $decisions->isNotEmpty()
) {
foreach ($decisions as $decision) {

// Get only recommendations
// Get only recommendation decisions
$decisionType = Repo::decision()->getDecisionType($decision->getData('decision'));
if (!Repo::decision()->isRecommendation($decisionType->getDecision())) {
continue;
Expand All @@ -706,27 +742,45 @@ protected function getPropertyStages(Enumerable $stageAssignments, Submission $s
continue;
}

$recommendation = [
'decision' => $decision->getData('decision'),
'label' => $decisionType->getRecommendationLabel(),
];
$reviewRecommendations->push($decision);
}
}

// Set recommendations for the deciding editor
if ($isCurrentUserDecidingEditor) {
$stages[$decision->getData('stageId')]['recommendations'][] = $recommendation;
}
$latestRecommendation = $reviewRecommendations->sortByDesc(
fn (Decision $recommendation) =>
strtotime($recommendation->getData('dateDecided'))
)->first();

// Set own recommendations of the current user
if ($currentUser->getId() == $decision->getData('editorId')) {
$stages[$decision->getData('stageId')]['currentUserRecommendation'] = $recommendation;
}
if ($latestRecommendation) {

$recommendationData = [
'decision' => $latestRecommendation->getData('decision'),
'label' => Repo::decision()->getDecisionType($latestRecommendation->getData('decision'))->getRecommendationLabel(),
];

// Set recommendations for the deciding editor
if ($isCurrentUserDecidingEditor) {
$stages[$decision->getData('stageId')]['recommendations'] = $recommendationData;
}

// Set own recommendations of the current user
if ($currentUser->getId() == $decision->getData('editorId')) {
$stages[$decision->getData('stageId')]['currentUserRecommendation'] = $recommendationData;
}
}

foreach ($stages as $stageId => $stage) {
// Determine if deciding editor is assigned
if ($hasDecidingEditor && $hasRecommendingEditors) {
$stages[$stageId]['isDecidingEditorAssigned'] = true;
if ($hasRecommendingEditors) {

// Determine if deciding editor is assigned
if ($hasDecidingEditor) {
$stages[$stageId]['isDecidingEditorAssigned'] = true;
}

// We need to expose isCurrentUserDecidingEditor prop only when recommending editor is assigned
if ($isCurrentUserDecidingEditor) {
$stages[$stageId]['isCurrentUserDecidingEditor'] = true;
}
}
}

Expand Down

0 comments on commit 72db9ed

Please sign in to comment.