-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFidusWriterPlugin.inc.php
executable file
·604 lines (534 loc) · 20.3 KB
/
FidusWriterPlugin.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?php
/**
* Copyright 2016-17, Afshin Sadeghi ([email protected]) of the OSCOSS
* Project.
* License: GNU GPL v2. See LICENSE.md for details.
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class FidusWriterPlugin extends GenericPlugin {
/**
* @param $category
* @param $path
* @return bool
*/
function register($category, $path) {
if (parent::register($category, $path)) {
/* Note: it looks counterintuitive that only the first listener checks
whether the plugin is enabled, but the way OJS is set up, if one
moves the other listeners inside the check, they stop working.
*/
if ($this->getEnabled()) {
HookRegistry::register('PluginRegistry::loadCategory', array($this, 'callbackLoadCategory'));
}
HookRegistry::register('reviewassignmentdao::_insertobject', array($this, 'callbackAddReviewer'));
HookRegistry::register('reviewassignmentdao::_deletebyid', array($this, 'callbackRemoveReviewer'));
HookRegistry::register('reviewrounddao::_insertobject', array($this, 'callbackNewReviewRound'));
HookRegistry::register('reviewrounddao::_updatestatus', array($this, 'callbackUpdateReviewRound'));
HookRegistry::register('TemplateManager::fetch', array($this, 'templateFetchCallback'));
// Add fields fidusId and fidusUrl to submissions
HookRegistry::register('articledao::getAdditionalFieldNames', array($this, 'callbackAdditionalFieldNames'));
return true;
}
return false;
}
// BEGIN STANDARD PLUGIN FUNCTIONS
/**
* Get the name of the settings file to be installed on new context
* creation.
* @return string
*/
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
/**
* Override the builtin to get the correct template path.
* @return string
*/
function getTemplatePath($inCore = false) {
return parent::getTemplatePath($inCore) . 'templates/';
}
/**
* Get the display name for this plugin.
*
* @return string
*/
function getDisplayName() {
return __('plugins.generic.fidusWriter.displayName');
}
/**
* Get a description of this plugin.
*
* @return string
*/
function getDescription() {
return __('plugins.generic.fidusWriter.description');
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $verb) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->register_function('plugin_url', array($this, 'smartyPluginUrl'));
$this->import('FidusWriterSettingsForm');
$form = new FidusWriterSettingsForm($this);
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));
break;
}
return parent::manage($args, $request);
}
/**
* @see Plugin::isSitePlugin()
*/
function isSitePlugin() {
return true;
}
// END STANDARD PLUGIN FUNCTIONS
function getGatewayPluginUrl() {
$request =& Registry::get('request');
return $request->getBaseUrl() . '/index.php/index/gateway/plugin/FidusWriterGatewayPlugin';
}
function getApiKey() {
return $this->getSetting(CONTEXT_ID_NONE, 'apiKey');
}
/**
* Retrieve a submission setting from the DB. We use this to get fidusUrl and
* fidusId.
* @param $hookName
* @param $args
* @return bool
*/
function getSubmissionSetting($submissionId, $settingName) {
$submissionDao = Application::getSubmissionDAO();
$submission = $submissionDao->getById($submissionId);
return $submission->getData($settingName);
}
/**
* Add fieldnames to link submissions to revisions in Fidus Writer
* instances.
* @see DAO::getAdditionalFieldNames()
*/
function callbackAdditionalFieldNames($hookName, $args) {
$returner =& $args[1];
$returner[] = 'fidusUrl';
$returner[] = 'fidusId';
}
/**
* We override the template for the submission file grid in case of a Fidus
* based submission. If the submission is connected to a Fidus Writer instance,
* we instead show a login link to get to the fidus writer instance (via the
* Fidus Writer Gateway plugin).
* @param $hookName
* @param $args
* @return bool
*/
public function templateFetchCallback($hookName, $args) {
$templateManager = $args[0];
$templateName = $args[1];
if ($templateName == 'controllers/grid/grid.tpl') {
$grid = $templateManager->get_template_vars('grid');
$title = $grid->getTitle();
if (
$title==='submission.submit.submissionFiles' ||
$title==='reviewer.submission.reviewFiles' ||
$title==='editor.submission.revisions'
) {
// Not sure if there is another way to find this information,
// but the submissionId is part of the URL of this page.
$submissionId = intval($_GET['submissionId']);
$fidusId = $this->getSubmissionSetting($submissionId, 'fidusId');
if ($fidusId != false) {
// This submission is linked to a Fidus Writer instance, so present
// link rather the file overview.
// If the submission file section is requested, we override the
// entire grid with a link to the file in Fidus Writer. This way
// there are no surprises of users accidentally trying to add
// more files or similar.
$stageId = intval($_GET['stageId']);
$round = 0;
$reviewRoundId = $_GET['reviewRoundId'];
if (isset($reviewRoundId)) {
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
$reviewRound = $reviewRoundDao->getById($reviewRoundId);
$round = $reviewRound->getRound();
$status = $reviewRound->getStatus();
if(
$status != REVIEW_ROUND_STATUS_REVISIONS_REQUESTED &&
$status != REVIEW_ROUND_STATUS_RESUBMITTED &&
$title === 'editor.submission.revisions'
) {
// The review round has not reached a status where there
// would be any author to show in terms of upload files.
// So we show an empty author field.
$result =& $args[4];
$result = '
<div class="pkp_controllers_grid">
<div class="header">
<h4>' . __($title) . '</h4>
</div>
<div style="text-align: center;">
' . __('grid.noItems') . '
</div>
</div>';
return true;
}
}
$revisionType = ($title==='editor.submission.revisions' ? 'Author' : 'Reviewer');
$versionString = $this->stageToVersion($stageId, $round, $revisionType);
$result =& $args[4];
$result = '
<div class="pkp_controllers_grid">
<div class="header">
<h4>' . __($title) . '</h4>
</div>
<div style="text-align: center;">
<a
href="' . $this->getGatewayPluginUrl() . '/documentReview?submissionId=' . $submissionId . '&stageId=' . $stageId . '&version=' . $versionString . '"
target="FidusWriter"
>
' . __('plugins.generic.fidusWriter.linkText') . '
</a>
</div>
</div>';
return true;
}
}
}
}
/**
* Sends information about a newly registered reviewer for a specific submission
* to Fidus Writer, if the submission is of a document in Fidus Writer.
* @param $hookName
* @param $args
* @return bool
*/
function callbackAddReviewer($hookName, $args) {
$row =& $args[1];
$submissionId = $row[0];
$round = $row[4];
$stageId = $row[2];
$versionString = $this->stageToVersion($stageId, $round, 'Reviewer');
$fidusId = $this->getSubmissionSetting($submissionId, 'fidusId');
if ($fidusId === false) {
// The article was not connected with Fidus Writer, so we send no
// notification.
return false;
}
$reviewerId = $row[1];
$reviewer = $this->getUser($reviewerId);
$dataArray = [
'email' => $reviewer->getEmail(),
'username' => $reviewer->getUserName(),
'user_id' => $reviewerId,
'key' => $this->getApiKey()
];
$fidusUrl = $this->getSubmissionSetting($submissionId, 'fidusUrl');
$url = $fidusUrl . '/ojs/add_reviewer/' . $fidusId . '/' . $versionString . '/';
$this->sendPostRequest($url, $dataArray);
return false;
}
/**
* Sends information to Fidus Writer that a given reviewer has been removed
* from a submission so that Fidus Writer also removes the access the reviewer
* has had to the document in question.
* @param $hookName
* @param $args
* @return bool
*/
function callbackRemoveReviewer($hookName, $args) {
$reviewAssignmentId =& $args[1];
$reviewAssignment = $this->getReviewAssignmentByReviewId($reviewAssignmentId);
$submissionId = $reviewAssignment->getSubmissionId();
$round = $reviewAssignment->getRound();
$stageId = $reviewAssignment->getStageId();
$versionString = $this->stageToVersion($stageId, $round, 'Reviewer');
$fidusId = $this->getSubmissionSetting($submissionId, 'fidusId');
if ($fidusId === false) {
// The article was not connected with Fidus Writer, so we send no
// notification.
return false;
}
$dataArray = [
'user_id' => $reviewAssignment->getReviewerId(),
'key' => $this->getApiKey()
];
// Then send the email address of reviewer to Fidus Writer.
$fidusUrl = $this->getSubmissionSetting($submissionId, 'fidusUrl');
$url = $fidusUrl. '/ojs/remove_reviewer/' . $fidusId . '/' . $versionString . '/';
$this->sendPostRequest($url, $dataArray);
return false;
}
/**
* Creates new SubmissionRevision in Fidus Writer
* @param $hookname
* @param $args
*/
function callbackNewReviewRound($hookname, $args) {
$row =& $args[1];
$submissionId = intval($row[0]);
$stageId = intval($row[1]);
$round = intval($row[2]);
$fidusId = $this->getSubmissionSetting($submissionId, 'fidusId');
if ($fidusId == false) {
// Not connected to Fidus Writer
return false;
}
if($round == 1) {
$oldVersionString = "1.0.0";
// TODO: What happens if there is a stage 2?
} else {
$oldRound = $round - 1;
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
$oldReviewRound = $reviewRoundDao->getReviewRound($submissionId, $stageId, $oldRound);
// We need to copy a file from the previous revision round. If the author has
// submitted something for the round, we use that version.
// Otherwise, we use the Reviewer's version.
if($oldReviewRound->getStatus()===REVIEW_ROUND_STATUS_RESUBMITTED) {
$oldRevisionType = 'Author';
} else {
$oldRevisionType = 'Reviewer';
}
$oldVersionString = $this->stageToVersion($stageId, $oldRound, $oldRevisionType);
}
$newVersionString = $this->stageToVersion($stageId, $round, 'Reviewer');
$dataArray = [
'old_version' => $oldVersionString,
'new_version' => $newVersionString,
'key' => $this->getApiKey(), //shared key between OJS and Editor software
];
$fidusUrl = $this->getSubmissionSetting($submissionId, 'fidusUrl');
$url = $fidusUrl . '/ojs/create_copy/' . $fidusId . '/';
$this->sendPostRequest($url, $dataArray);
return false;
}
/**
* Creates new SubmissionRevision in Fidus Writer if the review round is
* about to let the author post a revised document.
* @param $hookname
* @param $args
*/
function callbackUpdateReviewRound($hookname, $args) {
$row =& $args[1];
$reviewRoundId = intval($row[1]);
$newStatus = intval($row[0]);
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
$reviewRound = $reviewRoundDao->getById($reviewRoundId);
$submissionId = $reviewRound->getSubmissionId();
$fidusId = $this->getSubmissionSetting($submissionId, 'fidusId');
if ($fidusId == false) {
// Not connected to Fidus Writer
return false;
}
$oldStatus = $reviewRound->getStatus();
// Status codes for which just the reviewer document is required.
$reviewerStates = array(
REVIEW_ROUND_STATUS_PENDING_REVIEWERS,
REVIEW_ROUND_STATUS_PENDING_REVIEWS,
REVIEW_ROUND_STATUS_REVIEWS_READY,
REVIEW_ROUND_STATUS_REVIEWS_COMPLETED
);
// Status codes for which there will both an author and a reviewer
// document.
$authorStates = array(
REVIEW_ROUND_STATUS_REVISIONS_REQUESTED,
REVIEW_ROUND_STATUS_RESUBMITTED,
REVIEW_ROUND_STATUS_SENT_TO_EXTERNAL,
REVIEW_ROUND_STATUS_ACCEPTED,
REVIEW_ROUND_STATUS_DECLINED
);
if (
in_array($oldStatus, $reviewerStates) &&
in_array($newStatus, $authorStates)
) {
// We need to create the author SubmissionRevision for the round.
$stageId = $reviewRound->getStageId();
$round = $reviewRound->getRound();
$oldVersionString = $this->stageToVersion($stageId, $round, 'Reviewer');
$newVersionString = $this->stageToVersion($stageId, $round, 'Author');
$dataArray = [
'old_version' => $oldVersionString,
'new_version' => $newVersionString,
'key' => $this->getApiKey(), //shared key between OJS and Editor software
];
$fidusUrl = $this->getSubmissionSetting($submissionId, 'fidusUrl');
$url = $fidusUrl . '/ojs/create_copy/' . $fidusId . '/';
$this->sendPostRequest($url, $dataArray);
}
return false;
}
/**
* @param $hookName string
* @param $args array
* @return bool
**/
function callbackLoadCategory($hookName, $args) {
$category =& $args[0];
$plugins =& $args[1];
switch ($category) {
case 'gateways':
$this->import('FidusWriterGatewayPlugin');
$gatewayPlugin = new FidusWriterGatewayPlugin($this->getName());
$plugins[$gatewayPlugin->getSeq()][$gatewayPlugin->getPluginPath()] = $gatewayPlugin;
break;
}
return false;
}
function getUser($userId) {
$userDao = DAORegistry::getDAO('UserDAO');
return $userDao->getById($userId);
}
/**
* @param $userId
* @return string
*/
function getUserEmail($userId) {
/** @var UserDAO $userDao */
$userDao = DAORegistry::getDAO('UserDAO');
return $userDao->getUserEmail($userId);
}
/**
* @param $userId
* @return string
*/
function getUserName($userId) {
/** @var UserDAO $userDao */
$userDao = DAORegistry::getDAO('UserDAO');
/** @var ReviewAssignment $reviewAssignment */
$user = $userDao->getById($userId);
/** @var User $user */
return $user->getUsername($userId);
}
/**
* @param $reviewId
* @return $reviewAssignment
*/
function getReviewAssignmentByReviewId($reviewId) {
/** @var ReviewAssignmentDAO $RADao */
$RADao = DAORegistry::getDAO('ReviewAssignmentDAO');
$reviewAssignmentArray = $RADao->getById($reviewId);
// TODO: Find out if there are any problems here if this assignment
// contains more than one reviewer.
if (is_array($reviewAssignmentArray)) {
$reviewAssignment = $reviewAssignmentArray[0];
} else {
$reviewAssignment = $reviewAssignmentArray;
}
return $reviewAssignment;
}
/**
* This function converts from the kind of versioning information of a document
* as it's stored to the versioning information as it's stored on the FW side.
* The main difference is this:
* On OJS, a stageId is used to determine whether the document is in
* submission (1), internal review (2), external review (3), copyediting (4),
* production (5) stage.
* Within the review stage, one has to know the round.
* Each round allows for the upload of files, first of the reviewer, then of
* the author. So we choose to match two version numbers as used in FW to each
* round, the first one using $revisionType 'Reviewer' (reviewer), the second 'Author'
* (author).
* In FW, we have a version string, similar to a software version number with
* three parts divided by dots, such as: 1.0.0 or 3.1.5 . These numbers are:
* - The first number represents the stage ID, so it is 1-5.
* - The second number represents the round if there is one. Otherwise it is 0.
* - The third number is 0 for the 'Reviewer' version within a round, and 5 for the
* 'Author' version.
* @param $stageId
* @param $reviewRound
* @param $revisionType
* @return int
*/
function stageToVersion($stageId, $round = 0, $revisionType = 'Reviewer') {
switch ($stageId) {
case 1:
// submission
return '1.0.0';
break;
case 2:
// internal review
// TODO: does this also operate with review rounds? Couldn't
// find it how to do internal reviews.
if ($revisionType=='Reviewer') {
return '2.' . $round . '.0';
} else {
return '2.' . $round . '.5';
}
break;
case 3:
if ($revisionType=='Reviewer') {
return '3.' . $round . '.0';
} else {
return '3.' . $round . '.5';
}
break;
case 4:
return '4.0.0';
break;
case 5:
return '5.0.0';
break;
}
}
/**
* @param $requestType
* @param $url
* @param $dataArray
* @return string
*/
function sendRequest($requestType, $url, $dataArray) {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => $requestType,
'content' => http_build_query($dataArray)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === false) { /* Handle error */
echo $result;
}
return $result;
}
/**
* @param $url
* @param $dataArray
* @return string
*/
function sendPostRequest($url, $dataArray) {
$result = $this->sendRequest('POST', $url, $dataArray);
return $result;
}
}