-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReviewerCredits.inc.php
391 lines (369 loc) · 13.2 KB
/
ReviewerCredits.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
<?php
/**
* @file plugins/generic/reviewerCredits/ReviewerCredits.inc.php
*
* Copyright (c) 2015-2018 University of Pittsburgh
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2003-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class ReviewerCreditsPlugin
* @ingroup plugins_generic_reviewerCredits
*
* @brief ReviewerCredits plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
define('REVIEWER_CREDITS_URL', 'https://www.reviewercredits.com/wp-json');
define('REVIEWER_CREDITS_AUTH_ENDPOINT' , '/jwt-auth/v1/token');
define('REVIEWER_CREDITS_CLAIM_ENDPOINT' , '/reviewer-credits/v10/journal/claim');
//define('REVIEWER_CREDITS_BASIC_AUTH_CRED', 'user:passwd');
class ReviewerCreditsPlugin extends GenericPlugin {
protected $_apiFlag = FALSE;
protected $_consentFlag = TRUE;
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if(!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if($success && $this->getEnabled($mainContextId)){
HookRegistry::register('LoadHandler', Array($this, 'callbackGetInfo'));
HookRegistry::register('TemplateManager::fetch', Array($this, 'handleTemplateDisplay'));
HookRegistry::register('reviewerreviewstep3form::execute', Array($this, 'callbackSendClaim'));
$this->_registerTemplateResource();
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName() {
return __('plugins.generic.reviewerCredits.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription() {
return __('plugins.generic.reviewerCredits.description');
}
/**
* @copydoc PKPPlugin::getTemplatePath
*/
public function getTemplatePath($inCore = false) {
if(method_exists($this, 'getTemplateResourceName')){
//##--> OJS 3.1.1
return $this->getTemplateResourceName() . ':templates/';
}
//##--> OJS 3.1.2 onward
return parent::getTemplatePath($inCore) . '/';
}
/**
* @copydoc Plugin::getActions()
*/
public 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)
);
}
/**
* @see Plugin::manage()
*/
public function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$templateMgr = TemplateManager::getManager();
$templateMgr->register_function('plugin_url', array($this, 'smartyPluginUrl'));
/* Future implementation
$apiOptions = array(
RC_API_REV_ID_TYPE_EMAIL => 'plugins.generic.reviewerCredits.manager.settings.rcReviewerIdType.email',
RC_API_REV_ID_TYPE_ORCID => 'plugins.generic.reviewerCredits.manager.settings.rcReviewerIdType.orcid',
);
$templateMgr->assign('rcReviewerIdType', $apiOptions);*/
$this->import('ReviewerCreditsSettingsForm');
$form = new ReviewerCreditsSettingsForm($this, $contextId);
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));
}
return parent::manage($args, $request);
}
/**
* Check the correct operation to trigger the API Call
*/
public function callbackGetInfo($hookName, $op) {
if($op[0] == 'reviewer' && $op[1] == 'saveStep'){
$request = Application::getRequest();
$args = $request->getUserVars();
if($args['step'] == 3){
$this->_apiFlag = TRUE;
}
}
}
/**
* This method manage and send the Peer Review Claim information to ReviewerCredits
*/
public function callbackSendClaim($hookName, $args) {
if($this->_apiFlag && isset($_POST['confirmSendRC']) && $_POST['confirmSendRC'] == 1){
$request = Application::getRequest();
$context = $request->getContext();
$userDao = DAORegistry::getDAO('UserDAO');
$reviewerSubmission = $args[0]->getReviewerSubmission();
$reviewer = $userDao->getById($reviewerSubmission->getReviewerId());
$tryArray = Array('orcid', 'email');
$claimPayload = new stdClass();
$authPayload = new stdClass();
$notificationManager = new NotificationManager();
$authPayload->username = $this->getSetting($context->getId(), 'reviewerCreditsJournalLogin');
$authPayload->password = $this->getSetting($context->getId(), 'reviewerCreditsJournalPassword');
if(!empty($authPayload->username) && !empty($authPayload->password)){
$apiAuthToken = $this->_getToken($authPayload);
if(!$apiAuthToken->error){
$credentialMessages = Array();
while(count($tryArray) > 0){
if(in_array('orcid', $tryArray)){
$claimPayload->reviewerIdentifier = $reviewer->getOrcid();
if(empty($claimPayload->reviewerIdentifier)){
$claimPayload->reviewerIdentifier = $reviewer->getEmail();
$claimPayload->reviewerIdentifierType = 'email';
$tryArray = Array();
}else{
$claimPayload->reviewerIdentifierType = 'orcid';
$rawOrcid = preg_replace('{[^0-9X]}', '', $claimPayload->reviewerIdentifier);
$hrOrcid = chunk_split($rawOrcid, 4, '-');
$hrOrcid = substr($hrOrcid, 0, -1);
$claimPayload->reviewerIdentifier = $hrOrcid;
unset($tryArray['0']);
}
}else{
$claimPayload->reviewerIdentifier = $reviewer->getEmail();
$claimPayload->reviewerIdentifierType = 'email';
$tryArray = Array();
}
$credentialMessages[] = strtoupper($claimPayload->reviewerIdentifierType).': "'.$claimPayload->reviewerIdentifier.'"';
$claimPayload->dateCompleted = $reviewerSubmission->getDateCompleted();
//##--> if dateCompleted is empty the submission is a new submission
if(empty($claimPayload->dateCompleted)){
$claimPayload->dateCompleted = date('Y/m/d');
$reviewAssignment = $args[0]->getReviewAssignment();
$claimPayload->manuscriptID = 'PR'.$reviewerSubmission->getReviewId().'-S'.$reviewAssignment->getSubmissionId().'-R'.$reviewerSubmission->getRound();
//##--> Not used
// $claimPayload->editorName = '';
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $reviewerSubmission->getDateDue());
$claimPayload->dateDue = $dateTime->format('Y/m/d');
usleep(50000);
$apiClaimResponse = $this->_insertClaim($claimPayload, $apiAuthToken->data);
if($apiClaimResponse->error){
if($apiClaimResponse->noUser && count($tryArray) == 0){
if(count($credentialMessages) > 1){
$credentialString = join(' or ', $credentialMessages);
}else{
$credentialString = $credentialMessages[0];
}
$notificationManager->createTrivialNotification($reviewer->getId(), NOTIFICATION_TYPE_WARNING, Array('contents' => __('plugins.generic.reviewerCredits.notification.noUser', Array('crMessage' => $credentialString))));
}elseif($apiClaimResponse->noUser && count($tryArray) > 0){
continue;
}else{
$notificationManager->createTrivialNotification($reviewer->getId(), NOTIFICATION_TYPE_ERROR, Array('contents' => __('plugins.generic.reviewerCredits.notification.failed')));
$tryArray = Array();
}
}else{
$notificationManager->createTrivialNotification($reviewer->getId(), NOTIFICATION_TYPE_SUCCESS, Array('contents' => __('plugins.generic.reviewerCredits.notification.success')));
$tryArray = Array();
}
}
}
}else{
$notificationManager->createTrivialNotification($reviewer->getId(), NOTIFICATION_TYPE_ERROR, Array('contents' => __('plugins.generic.reviewerCredits.notification.failedAuth')));
}
}
}
}
/**
* Hook callback: register output filter.
* @see TemplateManager::display()
*/
public function handleTemplateDisplay($hookName, $args) {
$templateMgr = $args[0];
$template = $args[1];
$request = PKPApplication::getRequest();
if($template == 'reviewer/review/step3.tpl'){
if(method_exists($templateMgr, 'register_outputfilter')){
//##--> OJS 3.1.1
$templateMgr->register_outputfilter(Array($this, 'profileFilter'));
}else{
//##--> OJS 3.1.2 onward
$templateMgr->registerFilter('output', Array($this, 'profileFilter'));
}
}
return FALSE;
}
/**
* Output filter adds ReviewerCredits checkbox to reviewer submission form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
public function profileFilter($output, $templateMgr) {
if(preg_match('|div.*formButtons|', $output) && $this->_consentFlag == TRUE){
if(!preg_match('|disabled|', $output)){
$this->_consentFlag = FALSE;
if(method_exists($this, 'getTemplateResource')){
$output = $templateMgr->fetch($this->getTemplateResource('confirmRCEdit.tpl')) . $output;
}else{
$output = $templateMgr->fetch($this->getTemplatePath() . 'confirmRCEdit.tpl') . $output;
}
}
}
return $output;
}
/**
* Journal credentials check.
* @param $username string
* @param $password string
* @return bool
*/
public function verifyCredentials($username, $password){
$authPayload = new stdClass();
$authPayload->username = $username;
$authPayload->password = $password;
$apiAuthToken = $this->_getToken($authPayload);
if(!$apiAuthToken->error){
$output = TRUE;
}else{
$output = FALSE;
}
return $output;
}
/**
* This method call the ReviewerCredits API to obtain an authorization token
*/
protected function _getToken($authPayload) {
$output = new stdClass();
$response = $this->_makeApiCall(REVIEWER_CREDITS_AUTH_ENDPOINT, $authPayload);
$outputMessage = '';
if($response->status != 200){
if(property_exists($response, 'payload')){
$outputMessage = $response->payload->message;
}
if(property_exists($response->payload->data, 'json_error_message')){
if(!empty($outputMessage)){
$outputMessage .= ' - ';
}
$outputMessage .= strip_tags($response->payload->data->json_error_message);
}
$output->error = TRUE;
}else{
$outputMessage = $response->payload->token;
$output->error = FALSE;
}
$output->data = $outputMessage;
return $output;
}
/**
* This method call the ReviewerCredits API to create a new Peer Review Claim
*/
protected function _insertClaim($claimPayload, $token) {
$output = new stdClass();
$response = $this->_makeApiCall(REVIEWER_CREDITS_CLAIM_ENDPOINT, $claimPayload, $token);
if($response->status != 200 && $response->status != 422){
if(property_exists($response, 'payload')){
$outputMessage = $response->payload->message;
}
if(property_exists($response->payload, 'payload')){
$arrayMessage = Array();
foreach($response->payload->payload as $key => $element){
$arrayMessage[] = $key.': '.$element;
}
if(count($arrayMessage) > 0){
if(!empty($outputMessage)){
$outputMessage .= ' - ';
}
if(count($arrayMessage) > 1){
$outputMessage .= join('; ', $arrayMessage);
}else{
$outputMessage .= $arrayMessage[0];
}
}
}
$output->noUser = FALSE;
$output->error = TRUE;
}else{
if($response->status != 200){
$outputMessage = 0;
$output->noUser = TRUE;
$output->error = TRUE;
}else{
$outputMessage = $response->payload->claimId;
$output->noUser = FALSE;
$output->error = FALSE;
}
}
$output->data = $outputMessage;
return $output;
}
/**
* This method make the call to the ReviewerCredits API using cURL
*/
protected function _makeApiCall($endpoint, $rawPayload, $token = null) {
$output = new stdClass();
$payload = json_encode($rawPayload);
$headers = Array('Content-Type:application/json',
'Content-Length: ' . strlen($payload)
);
if(defined('REVIEWER_CREDITS_BASIC_AUTH_CRED')){
$headerAuth = 'Authorization: Basic '.base64_encode(REVIEWER_CREDITS_BASIC_AUTH_CRED);
if(!is_null($token)){
$headerAuth .= ', Bearer '.$token;
}
$headers[] = $headerAuth;
}else{
if(!is_null($token)){
$headers[] = 'Authorization: Bearer '.$token;
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, REVIEWER_CREDITS_URL.$endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rawOutput = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($rawOutput === FALSE){
$output->status = 0;
$output->message = curl_error($ch);
}elseif($httpStatus != 200){
$output->status = $httpStatus;
$output->message = 'HTTP Status '.$httpStatus;
$output->payload = json_decode($rawOutput);
}else{
$output->status = 200;
$output->message = 'OK';
$output->payload = json_decode($rawOutput);
}
curl_close($ch);
return $output;
}
}