-
Notifications
You must be signed in to change notification settings - Fork 439
/
PKPWorkflowHandler.inc.php
530 lines (460 loc) · 18.1 KB
/
PKPWorkflowHandler.inc.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* @file pages/workflow/PKPWorkflowHandler.inc.php
*
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class WorkflowHandler
* @ingroup pages_reviewer
*
* @brief Handle requests for the submssion workflow.
*/
import('classes.handler.Handler');
import('lib.pkp.classes.workflow.WorkflowStageDAO');
// import UI base classes
import('lib.pkp.classes.linkAction.LinkAction');
import('lib.pkp.classes.linkAction.request.AjaxModal');
abstract class PKPWorkflowHandler extends Handler {
//
// Implement template methods from PKPHandler
//
/**
* @copydoc PKPHandler::authorize()
*/
function authorize($request, &$args, $roleAssignments) {
$router = $request->getRouter();
$operation = $router->getRequestedOp($request);
if ($operation == 'access') {
// Authorize requested submission.
import('lib.pkp.classes.security.authorization.internal.SubmissionRequiredPolicy');
$this->addPolicy(new SubmissionRequiredPolicy($request, $args, 'submissionId'));
// This policy will deny access if user has no accessible workflow stage.
// Otherwise it will build an authorized object with all accessible
// workflow stages and authorize user operation access.
import('lib.pkp.classes.security.authorization.internal.UserAccessibleWorkflowStageRequiredPolicy');
$this->addPolicy(new UserAccessibleWorkflowStageRequiredPolicy($request));
} else {
import('lib.pkp.classes.security.authorization.WorkflowStageAccessPolicy');
$this->addPolicy(new WorkflowStageAccessPolicy($request, $args, $roleAssignments, 'submissionId', $this->identifyStageId($request, $args)));
}
return parent::authorize($request, $args, $roleAssignments);
}
/**
* @copydoc PKPHandler::initialize()
*/
function initialize($request, $args) {
$router = $request->getRouter();
$operation = $router->getRequestedOp($request);
if ($operation != 'access') {
$this->setupTemplate($request);
}
// Call parent method.
parent::initialize($request, $args);
}
//
// Public handler methods
//
/**
* Redirect users to their most appropriate
* submission workflow stage.
* @param $args array
* @param $request PKPRequest
*/
function access($args, $request) {
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
$currentStageId = $submission->getStageId();
$accessibleWorkflowStages = $this->getAuthorizedContextObject(ASSOC_TYPE_ACCESSIBLE_WORKFLOW_STAGES);
// Get the closest workflow stage that user has an assignment.
$stagePath = null;
$workingStageId = null;
for ($workingStageId = $currentStageId; $workingStageId >= WORKFLOW_STAGE_ID_SUBMISSION; $workingStageId--) {
if (array_key_exists($workingStageId, $accessibleWorkflowStages)) {
break;
}
}
// If no stage was found, user still have access to future stages of the
// submission. Try to get the closest future workflow stage.
if ($workingStageId == null) {
for ($workingStageId = $currentStageId; $workingStageId <= WORKFLOW_STAGE_ID_PRODUCTION; $workingStageId++) {
if (array_key_exists($workingStageId, $accessibleWorkflowStages)) {
break;
}
}
}
assert(isset($workingStageId));
$router = $request->getRouter();
$request->redirectUrl($router->url($request, null, 'workflow', 'index', array($submission->getId(), $workingStageId)));
}
/**
* Show the workflow stage, with the stage path as an #anchor.
* @param $args array
* @param $request PKPRequest
*/
function index($args, $request) {
$this->_displayWorkflow($args, $request);
}
/**
* Show the submission stage.
* @param $args array
* @param $request PKPRequest
*/
function submission($args, $request) {
$this->_redirectToIndex($args, $request);
}
/**
* Show the external review stage.
* @param $args array
* @param $request PKPRequest
*/
function externalReview($args, $request) {
$this->_redirectToIndex($args, $request);
}
/**
* Show the editorial stage
* @param $request PKPRequest
* @param $args array
*/
function editorial(&$args, $request) {
$this->_redirectToIndex($args, $request);
}
/**
* Show the production stage
* @param $request PKPRequest
* @param $args array
*/
function production(&$args, $request) {
$this->_redirectToIndex($args, $request);
}
/**
* Redirect all old stage paths to index
* @param $args array
* @param $request PKPRequest
*/
protected function _redirectToIndex(&$args, $request) {
// Translate the operation to a workflow stage identifier.
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$router = $request->getRouter();
$workflowPath = $router->getRequestedOp($request);
$stageId = WorkflowStageDAO::getIdFromPath($workflowPath);
$request->redirectUrl($router->url($request, null, 'workflow', 'index', array($submission->getId(), $stageId)));
}
/**
* Fetch JSON-encoded editor decision options.
* @param $args array
* @param $request Request
* @return JSONMessage JSON object
*/
function editorDecisionActions($args, $request) {
AppLocale::requireComponents(LOCALE_COMPONENT_APP_EDITOR);
$reviewRoundId = (int) $request->getUserVar('reviewRoundId');
// Prepare the action arguments.
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$stageId = $this->getAuthorizedContextObject(ASSOC_TYPE_WORKFLOW_STAGE);
$actionArgs = array(
'submissionId' => $submission->getId(),
'stageId' => (int) $stageId,
);
// If a review round was specified, include it in the args;
// must also check that this is the last round or decisions
// cannot be recorded.
if ($reviewRoundId) {
$actionArgs['reviewRoundId'] = $reviewRoundId;
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
$lastReviewRound = $reviewRoundDao->getLastReviewRoundBySubmissionId($submission->getId(), $stageId);
$reviewRound = $reviewRoundDao->getById($reviewRoundId);
}
// If a review round was specified,
// If there is an editor assigned, retrieve stage decisions.
$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$editorsStageAssignments = $stageAssignmentDao->getEditorsAssignedToStage($submission->getId(), $stageId);
$dispatcher = $request->getDispatcher();
$user = $request->getUser();
// If it is a review stage, check if the user can make decisions or recommend only
$recommendOnly = $makeDecision = false;
if ($reviewRoundId) {
// if the user is assigned several times in an editorial role, check his/her assignments permissions i.e.
// if the user is assigned with both possibilities: to only recommend as well as make decision
foreach ($editorsStageAssignments as $editorsStageAssignment) {
if ($editorsStageAssignment->getUserId() == $user->getId()) {
if (!$editorsStageAssignment->getRecommendOnly()) {
$makeDecision = true;
} else {
$recommendOnly = true;
}
}
}
}
// If user is not assigned to the submission,
// see if the user is manager or sub-editor, and
// if the group is recommendOnly
if (!$recommendOnly && !$makeDecision) {
$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$userGroups = $userGroupDao->getByUserId($user->getId(), $request->getContext()->getId());
while ($userGroup = $userGroups->next()) {
if (in_array($userGroup->getRoleId(), array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR))) {
if (!$userGroup->getRecommendOnly()) {
$makeDecision = true;
} else {
$recommendOnly = true;
}
}
}
}
import('lib.pkp.classes.linkAction.request.AjaxModal');
$editorActions = array();
$lastRecommendation = $allRecommendations = null;
if (!empty($editorsStageAssignments) && (!$reviewRoundId || $reviewRoundId == $lastReviewRound->getId())) {
import('classes.workflow.EditorDecisionActionsManager');
$editDecisionDao = DAORegistry::getDAO('EditDecisionDAO');
$recommendationOptions = EditorDecisionActionsManager::getRecommendationOptions($stageId);
// If this is a review stage and the user has "recommend only role"
if (($stageId == WORKFLOW_STAGE_ID_EXTERNAL_REVIEW || $stageId == WORKFLOW_STAGE_ID_INTERNAL_REVIEW)) {
if ($recommendOnly) {
// Get the made editorial decisions from the current user
$editorDecisions = $editDecisionDao->getEditorDecisions($submission->getId(), $stageId, $reviewRound->getRound(), $user->getId());
// Get the last recommendation
foreach ($editorDecisions as $editorDecision) {
if (array_key_exists($editorDecision['decision'], $recommendationOptions)) {
if ($lastRecommendation) {
if ($editorDecision['dateDecided'] >= $lastRecommendation['dateDecided']) {
$lastRecommendation = $editorDecision;
}
} else {
$lastRecommendation = $editorDecision;
}
}
}
if ($lastRecommendation) {
$lastRecommendation = __($recommendationOptions[$lastRecommendation['decision']]);
}
// Add the recommend link action.
$editorActions[] =
new LinkAction(
'recommendation',
new AjaxModal(
$dispatcher->url(
$request, ROUTE_COMPONENT, null,
'modals.editorDecision.EditorDecisionHandler',
'sendRecommendation', null, $actionArgs
),
$lastRecommendation ? __('editor.submission.changeRecommendation') : __('editor.submission.makeRecommendation'),
'review_recommendation'
),
$lastRecommendation ? __('editor.submission.changeRecommendation') : __('editor.submission.makeRecommendation')
);
} elseif ($makeDecision) {
// Get the made editorial decisions from all users
$editorDecisions = $editDecisionDao->getEditorDecisions($submission->getId(), $stageId, $reviewRound->getRound());
// Get all recommendations
$recommendations = array();
foreach ($editorDecisions as $editorDecision) {
if (array_key_exists($editorDecision['decision'], $recommendationOptions)) {
if (array_key_exists($editorDecision['editorId'], $recommendations)) {
if ($editorDecision['dateDecided'] >= $recommendations[$editorDecision['editorId']]['dateDecided']) {
$recommendations[$editorDecision['editorId']] = array('dateDecided' => $editorDecision['dateDecided'], 'decision' => $editorDecision['decision']);;
}
} else {
$recommendations[$editorDecision['editorId']] = array('dateDecided' => $editorDecision['dateDecided'], 'decision' => $editorDecision['decision']);
}
}
}
$i = 0;
foreach ($recommendations as $recommendation) {
$allRecommendations .= $i == 0 ? __($recommendationOptions[$recommendation['decision']]) : ', ' . __($recommendationOptions[$recommendation['decision']]);
$i++;
}
}
}
// In non-review stages, the user have to be able to make decisions,
// in order for editor actions to be displayed/available
if ($makeDecision) {
// Get the possible editor decisions for this stage
$decisions = EditorDecisionActionsManager::getStageDecisions($request->getContext(), $stageId);
// Iterate through the editor decisions and create a link action
// for each decision which as an operation associated with it.
foreach($decisions as $decision => $action) {
if (empty($action['operation'])) {
continue;
}
$actionArgs['decision'] = $decision;
$editorActions[] = new LinkAction(
$action['name'],
new AjaxModal(
$dispatcher->url(
$request, ROUTE_COMPONENT, null,
'modals.editorDecision.EditorDecisionHandler',
$action['operation'], null, $actionArgs
),
__($action['title'])
),
__($action['title'])
);
}
}
}
// Assign the actions to the template.
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
'editorActions' => $editorActions,
'editorsAssigned' => count($editorsStageAssignments) > 0,
'stageId' => $stageId,
'lastRecommendation' => $lastRecommendation,
'allRecommendations' => $allRecommendations,
));
return $templateMgr->fetchJson('workflow/editorialLinkActions.tpl');
}
/**
* Fetch the JSON-encoded submission header.
* @param $args array
* @param $request Request
* @return JSONMessage JSON object
*/
function submissionHeader($args, $request) {
$templateMgr = TemplateManager::getManager($request);
return $templateMgr->fetchJson('workflow/submissionHeader.tpl');
}
/**
* Fetch the JSON-encoded submission progress bar.
* @param $args array
* @param $request Request
* @return JSONMessage JSON object
*/
function submissionProgressBar($args, $request) {
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();
$workflowStages = WorkflowStageDAO::getWorkflowStageKeysAndPaths();
$stageNotifications = array();
foreach (array_keys($workflowStages) as $stageId) {
$stageNotifications[$stageId] = $this->notificationOptionsByStage($request->getUser(), $stageId, $context->getId());
}
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$editDecisionDao = DAORegistry::getDAO('EditDecisionDAO'); /* @var $editDecisionDao EditDecisionDAO */
$stageDecisions = $editDecisionDao->getEditorDecisions($submission->getId());
$stagesWithDecisions = array();
foreach ($stageDecisions as $decision) {
$stagesWithDecisions[$decision['stageId']] = $decision['stageId'];
}
$workflowStages = WorkflowStageDAO::getStageStatusesBySubmission($submission, $stagesWithDecisions, $stageNotifications);
$templateMgr->assign('workflowStages', $workflowStages);
if ($this->isSubmissionReady($submission)) {
$templateMgr->assign('submissionIsReady', true);
}
return $templateMgr->fetchJson('workflow/submissionProgressBar.tpl');
}
/**
* Setup variables for the template
* @param $request Request
*/
function setupTemplate($request) {
parent::setupTemplate($request);
AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION, LOCALE_COMPONENT_APP_SUBMISSION, LOCALE_COMPONENT_APP_EDITOR, LOCALE_COMPONENT_PKP_GRID, LOCALE_COMPONENT_PKP_EDITOR);
$router = $request->getRouter();
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$stageId = $this->getAuthorizedContextObject(ASSOC_TYPE_WORKFLOW_STAGE);
// Construct array with workflow stages data.
$workflowStages = WorkflowStageDAO::getWorkflowStageKeysAndPaths();
$templateMgr = TemplateManager::getManager($request);
// Assign the authorized submission.
$templateMgr->assign('submission', $submission);
// Assign workflow stages related data.
$templateMgr->assign('stageId', $stageId);
$templateMgr->assign('submissionStageId', $submission->getStageId());
$templateMgr->assign('workflowStages', $workflowStages);
import('controllers.modals.submissionMetadata.linkAction.SubmissionEntryLinkAction');
$templateMgr->assign(
'submissionEntryAction',
new SubmissionEntryLinkAction($request, $submission->getId(), $stageId)
);
import('lib.pkp.controllers.informationCenter.linkAction.SubmissionInfoCenterLinkAction');
$templateMgr->assign(
'submissionInformationCenterAction',
new SubmissionInfoCenterLinkAction($request, $submission->getId())
);
import('lib.pkp.controllers.modals.documentLibrary.linkAction.SubmissionLibraryLinkAction');
$templateMgr->assign(
'submissionLibraryAction',
new SubmissionLibraryLinkAction($request, $submission->getId())
);
}
//
// Protected helper methods
//
/**
* Displays the workflow tab structure.
* @param $args array
* @param $request Request
*/
private function _displayWorkflow($args, $request) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->display('workflow/workflow.tpl');
}
/**
* Translate the requested operation to a stage id.
* @param $request Request
* @param $args array
* @return integer One of the WORKFLOW_STAGE_* constants.
*/
protected function identifyStageId($request, $args) {
if ($stageId = $request->getUserVar('stageId')) {
return (int) $stageId;
}
// Maintain the old check for previous path urls
$router = $request->getRouter();
$workflowPath = $router->getRequestedOp($request);
$stageId = WorkflowStageDAO::getIdFromPath($workflowPath);
if ($stageId) {
return $stageId;
}
// Finally, retrieve the requested operation, if the stage id is
// passed in via an argument in the URL, like index/submissionId/stageId
$stageId = $args[1];
// Translate the operation to a workflow stage identifier.
assert(WorkflowStageDAO::getPathFromId($stageId) !== null);
return $stageId;
}
/**
* Determine if a particular stage has a notification pending. If so, return true.
* This is used to set the CSS class of the submission progress bar.
* @param $user PKPUser
* @param $stageId integer
* @param $contextId integer
* @return boolean
*/
protected function notificationOptionsByStage($user, $stageId, $contextId) {
$submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
$notificationDao = DAORegistry::getDAO('NotificationDAO');
$editorAssignmentNotificationType = $this->getEditorAssignmentNotificationTypeByStageId($stageId);
$editorAssignments = $notificationDao->getByAssoc(ASSOC_TYPE_SUBMISSION, $submission->getId(), null, $editorAssignmentNotificationType, $contextId);
// if the User has assigned TASKs in this stage check, return true
if (!$editorAssignments->wasEmpty()) {
return true;
}
// check for more specific notifications on those stages that have them.
if ($stageId == WORKFLOW_STAGE_ID_PRODUCTION) {
$submissionApprovalNotification = $notificationDao->getByAssoc(ASSOC_TYPE_SUBMISSION, $submission->getId(), null, NOTIFICATION_TYPE_APPROVE_SUBMISSION, $contextId);
if (!$submissionApprovalNotification->wasEmpty()) {
return true;
}
}
return false;
}
//
// Abstract protected methods.
//
/**
* Return the editor assignment notification type based on stage id.
* @param $stageId int
* @return int
*/
abstract protected function getEditorAssignmentNotificationTypeByStageId($stageId);
/**
* Checks whether or not the submission is ready to appear in catalog.
* @param $submission Submission
* @return boolean
*/
abstract protected function isSubmissionReady($submission);
}
?>