forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Repository.php
273 lines (233 loc) · 9.17 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* @file classes/submission/reviewAssignment/Repository.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2000-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Repository
*
* @brief A repository to find and manage review assignment.
*/
namespace PKP\submission\reviewAssignment;
use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
use Illuminate\Support\Collection;
use PKP\context\Context;
use PKP\db\DAORegistry;
use PKP\notification\NotificationDAO;
use PKP\plugins\Hook;
use PKP\reviewForm\ReviewFormResponseDAO;
use PKP\security\Role;
use PKP\security\RoleDAO;
use PKP\services\PKPSchemaService;
use PKP\submission\ReviewFilesDAO;
use PKP\submission\reviewRound\ReviewRoundDAO;
use PKP\validation\ValidatorFactory;
class Repository
{
public DAO $dao;
/** @var string $schemaMap The name of the class to map this entity to its schema */
public string $schemaMap = maps\Schema::class;
protected Request $request;
/** @var PKPSchemaService<ReviewAssignment> $schemaService */
protected PKPSchemaService $schemaService;
public function __construct(DAO $dao, Request $request, PKPSchemaService $schemaService)
{
$this->dao = $dao;
$this->request = $request;
$this->schemaService = $schemaService;
}
/** @copydoc DAO::newDataObject() */
public function newDataObject(array $params = []): ReviewAssignment
{
$object = $this->dao->newDataObject();
if (!empty($params)) {
$object->setAllData($params);
}
return $object;
}
/** @copydoc DAO::get() */
public function get(int $id, ?int $submissionId = null): ?ReviewAssignment
{
return $this->dao->get($id, $submissionId);
}
/** @copydoc DAO::exists() */
public function exists(int $id, ?int $submissionId = null): bool
{
return $this->dao->exists($id, $submissionId);
}
/** @copydoc DAO::getCollector() */
public function getCollector(): Collector
{
return app(Collector::class);
}
/**
* Get an instance of the map class for mapping
* announcements to their schema
*/
public function getSchemaMap(): maps\Schema
{
return app('maps')->withExtensions($this->schemaMap);
}
/**
* Validate properties for a category
*
* Perform validation checks on data used to add or edit a review assignment.
*
* @param array $props A key/value array with the new data to validate
*
* @return array A key/value array with validation errors. Empty if no errors
*
* @hook ReviewAssignment::validate [&$errors, $object, $props, $allowedLocales, $primaryLocale]
*/
public function validate(?ReviewAssignment $object, array $props, Context $context): array
{
$primaryLocale = $context->getData('primaryLocale');
$allowedLocales = $context->getData('supportedFormLocales');
$validator = ValidatorFactory::make(
$props,
$this->schemaService->getValidationRules($this->dao->schema, $allowedLocales),
[]
);
// Check required fields
ValidatorFactory::required(
$validator,
$object,
$this->schemaService->getRequiredProps($this->dao->schema),
$this->schemaService->getMultilingualProps($this->dao->schema),
$allowedLocales,
$primaryLocale
);
// Check if submission exists
if (isset($props['submissionId'])) {
$validator->after(function ($validator) use ($props) {
if (!$validator->errors()->get('submissionId')) {
$submission = Repo::submission()->get($props['submissionId']);
if (!$submission) {
$validator->errors()->add('submissionId', __('api.reviews.assignments.invalidSubmission'));
}
}
});
}
// Check if reviewer exists
if (isset($props['reviewerId'])) {
$validator->after(function ($validator) use ($props, $context) {
if (!$validator->errors()->get('reviewerId')) {
$reviewer = Repo::user()->get($props['reviewerId']);
if (!$reviewer) {
$validator->errors()->add('reviewerId', __('api.reviews.assignments.invalidReviewer'));
}
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
$isReviewer = $roleDao->userHasRole($context->getId(), $reviewer->getId(), Role::ROLE_ID_REVIEWER);
if (!$isReviewer) {
$validator->errors()->add('reviewerId', __('api.reviews.assignments.invalidReviewer'));
}
}
});
}
// Check for input from disallowed locales
ValidatorFactory::allowedLocales($validator, $this->schemaService->getMultilingualProps($this->dao->schema), $allowedLocales);
$errors = [];
if ($validator->fails()) {
$errors = $this->schemaService->formatValidationErrors($validator->errors());
}
Hook::run('ReviewAssignment::validate', [&$errors, $object, $props, $allowedLocales, $primaryLocale]);
return $errors;
}
/**
* Add a new review assignment
*
* @hook ReviewAssignment::add [[$reviewAssignment]]
*/
public function add(ReviewAssignment $reviewAssignment): int
{
$id = $this->dao->insert($reviewAssignment);
$reviewAssignment->stampModified();
Hook::call('ReviewAssignment::add', [$reviewAssignment]);
$this->updateReviewRoundStatus($reviewAssignment);
return $id;
}
/**
* Edit a review assignment
*
* @hook ReviewAssignment::edit [[$newReviewAssignment, $reviewAssignment, $params]]
*/
public function edit(ReviewAssignment $reviewAssignment, array $params)
{
$newReviewAssignment = clone $reviewAssignment;
$newReviewAssignment->setAllData(array_merge($newReviewAssignment->_data, $params));
$newReviewAssignment->stampModified();
Hook::call('ReviewAssignment::edit', [$newReviewAssignment, $reviewAssignment, $params]);
$this->dao->update($newReviewAssignment);
$this->updateReviewRoundStatus($newReviewAssignment);
}
/**
* Delete a review assignment
*
* @hook ReviewAssignment::delete::before [[$reviewAssignment]]
*/
public function delete(ReviewAssignment $reviewAssignment)
{
Hook::call('ReviewAssignment::delete::before', [$reviewAssignment]);
$reviewFormResponseDao = DAORegistry::getDAO('ReviewFormResponseDAO'); /** @var ReviewFormResponseDAO $reviewFormResponseDao */
$reviewFormResponseDao->deleteByReviewId($reviewAssignment->getId());
$reviewFilesDao = DAORegistry::getDAO('ReviewFilesDAO'); /** @var ReviewFilesDAO $reviewFilesDao */
$reviewFilesDao->revokeByReviewId($reviewAssignment->getId());
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
$notificationDao->deleteByAssoc(Application::ASSOC_TYPE_REVIEW_ASSIGNMENT, $reviewAssignment->getId());
$this->dao->delete($reviewAssignment);
$this->updateReviewRoundStatus($reviewAssignment);
Hook::call('ReviewAssignment::delete', [$reviewAssignment]);
}
/**
* Delete a collection of announcements
*/
public function deleteMany(Collector $collector)
{
foreach ($collector->getMany() as $reviewAssignment) {
$this->delete($reviewAssignment);
}
}
/**
* Return the review methods translation keys.
*/
public function getReviewMethodsTranslationKeys(): array
{
return [
ReviewAssignment::SUBMISSION_REVIEW_METHOD_DOUBLEANONYMOUS => 'editor.submissionReview.doubleAnonymous',
ReviewAssignment::SUBMISSION_REVIEW_METHOD_ANONYMOUS => 'editor.submissionReview.anonymous',
ReviewAssignment::SUBMISSION_REVIEW_METHOD_OPEN => 'editor.submissionReview.open',
];
}
/**
* Update the status of the review round an assignment is attached to. This
* should be fired whenever a reviewer assignment is modified.
*/
protected function updateReviewRoundStatus(ReviewAssignment $reviewAssignment): bool
{
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO'); /** @var ReviewRoundDAO $reviewRoundDao */
$reviewRound = $reviewRoundDao->getReviewRound(
$reviewAssignment->getSubmissionId(),
$reviewAssignment->getStageId(),
$reviewAssignment->getRound()
);
// Review round may not exist if submission is being deleted
if ($reviewRound) {
$reviewRoundDao->updateStatus($reviewRound);
return true;
}
return false;
}
/**
* @copydoc DAO::getCompletedByYear()
*
* @return Collection<int,int>
*/
public function getCompletedByYear(int $contextId, string $year): Collection
{
return $this->dao->getCompletedByYear($contextId, $year);
}
}