Skip to content

Commit

Permalink
Merge branch 'adaptToOps340' into 'main'
Browse files Browse the repository at this point in the history
Adapta plugin para OPS 3.4.0

See merge request softwares-pkp/plugins_ojs/pre-endorsement-plaudit!22
  • Loading branch information
JhonathanLepidus committed Mar 6, 2024
2 parents 563252e + 0a0d735 commit 3d894ec
Show file tree
Hide file tree
Showing 48 changed files with 1,046 additions and 1,019 deletions.
3 changes: 2 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ include:
ref: main
file:
- 'templates/groups/pkp_plugin.yml'
- 'templates/groups/ops_plugins_unit_tests_model.yml'
- 'templates/groups/ops_3_4_plugins_unit_tests_model.yml'
- 'templates/groups/ops_3_4_plugins_cypress_tests_model.yml'
441 changes: 0 additions & 441 deletions PlauditPreEndorsementPlugin.inc.php

This file was deleted.

393 changes: 393 additions & 0 deletions PlauditPreEndorsementPlugin.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @file PlauditPreEndorsementSettingsForm.inc.php
*
* Copyright (c) 2022 Lepidus Tecnologia
* Copyright (c) 2022 - 2024 Lepidus Tecnologia
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PlauditPreEndorsementSettingsForm
Expand All @@ -12,9 +12,17 @@
* @brief Form for site admins to modify Plaudit Pre-Endorsement plugin settings
*/

namespace APP\plugins\generic\plauditPreEndorsement;

import('lib.pkp.classes.form.Form');
import('plugins.generic.plauditPreEndorsement.classes.OrcidCredentialsValidator');
use PKP\form\Form;
use APP\template\TemplateManager;
use APP\core\Application;
use PKP\config\Config;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use APP\plugins\generic\plauditPreEndorsement\classes\OrcidCredentialsValidator;

class PlauditPreEndorsementSettingsForm extends Form
{
Expand All @@ -39,7 +47,7 @@ public function __construct($plugin, $contextId)
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));

if (!$this->plugin->orcidIsGloballyConfigured()) {
if (!$this->orcidIsGloballyConfigured()) {
$this->addCheck(new FormValidator($this, 'orcidAPIPath', 'required', 'plugins.generic.plauditPreEndorsement.settings.orcidAPIPathRequired'));
$this->addCheck(new FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.plauditPreEndorsement.settings.orcidClientIdError', function ($clientId) {
return $this->validator->validateClientId($clientId);
Expand Down Expand Up @@ -68,7 +76,7 @@ public function readInputData()
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('globallyConfigured', $this->plugin->orcidIsGloballyConfigured());
$templateMgr->assign('globallyConfigured', $this->orcidIsGloballyConfigured());
$templateMgr->assign('pluginName', $this->plugin->getName());
$templateMgr->assign('applicationName', Application::get()->getName());
return parent::fetch($request, $template, $display);
Expand Down Expand Up @@ -106,4 +114,13 @@ public function _checkPrerequisites()
}
return $messages;
}

public function orcidIsGloballyConfigured(): bool
{
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This plugin allows authors to pre-endorse their manuscripts before the posting t

This plugin is compatible with the following PKP applications:

- OPS 3.3.0-x
- OPS 3.4.0-x

## Installation

Expand All @@ -26,7 +26,7 @@ After taking this configuration process, the plugin is ready for use.
## License
__This plugin is licensed under the GNU General Public License v3.0__

__Copyright (c) 2022 Lepidus Tecnologia__
__Copyright (c) 2022 - 2024 Lepidus Tecnologia__

__Copyright (c) 2022 SciELO__
__Copyright (c) 2022 - 2024 SciELO__

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace APP\plugins\generic\plauditPreEndorsement\classes;

class CrossrefClient
{
public function doiIsIndexed(string $doi): bool
{
$crossrefApiUrl = "https://api.crossref.org/works/".$doi;
$crossrefApiUrl = "https://api.crossref.org/works/$doi";

$headers = get_headers($crossrefApiUrl);
$statusCode = $this->getStatusCode($headers);
Expand Down
13 changes: 13 additions & 0 deletions classes/Endorsement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace APP\plugins\generic\plauditPreEndorsement\classes;

class Endorsement
{
public const STATUS_NOT_CONFIRMED = 0;
public const STATUS_CONFIRMED = 1;
public const STATUS_DENIED = 2;
public const STATUS_COMPLETED = 3;
public const STATUS_COULDNT_COMPLETE = 4;

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

use GuzzleHttp\Exception\ClientException;
namespace APP\plugins\generic\plauditPreEndorsement\classes;

import('plugins.generic.plauditPreEndorsement.classes.PlauditClient');
import('plugins.generic.plauditPreEndorsement.classes.CrossrefClient');
import('plugins.generic.plauditPreEndorsement.classes.OrcidClient');
use GuzzleHttp\Exception\ClientException;
use APP\core\Application;
use APP\facades\Repo;
use PKP\core\Core;
use APP\plugins\generic\plauditPreEndorsement\classes\Endorsement;
use APP\plugins\generic\plauditPreEndorsement\classes\PlauditClient;
use APP\plugins\generic\plauditPreEndorsement\classes\CrossrefClient;
use APP\plugins\generic\plauditPreEndorsement\classes\OrcidClient;

class EndorsementService
{
private $plugin;
private $contextId;
private $orcidClient;
private $crossrefClient;

public function __construct($contextId, $plugin)
Expand Down Expand Up @@ -39,15 +45,15 @@ public function sendEndorsement($publication, $needCheckMessageWasLoggedToday =
} else {
$submissionId = $publication->getData('submissionId');
if(!$needCheckMessageWasLoggedToday or !$this->messageWasAlreadyLoggedToday($submissionId, $validationResult)) {
$submission = DAORegistry::getDAO('SubmissionDAO')->getById($submissionId);
$submission = Repo::submission()->get($submissionId);
$this->plugin->writeOnActivityLog($submission, $validationResult);
}
}
}

public function validateEndorsementSending($publication): string
{
$doi = $publication->getData('pub-id::doi');
$doi = $publication->getDoi();
$secretKey = $this->plugin->getSetting($this->contextId, 'plauditAPISecret');

if(empty($doi)) {
Expand All @@ -67,8 +73,8 @@ public function validateEndorsementSending($publication): string

public function sendEndorsementToPlaudit($publication)
{
$submission = DAORegistry::getDAO('SubmissionDAO')->getById($publication->getData('submissionId'));
$this->plugin->writeOnActivityLog($submission, 'plugins.generic.plauditPreEndorsement.log.attemptSendingEndorsement', ['doi' => $publication->getData('pub-id::doi'), 'orcid' => $publication->getData('endorserOrcid')]);
$submission = Repo::submission()->get($publication->getData('submissionId'));
$this->plugin->writeOnActivityLog($submission, 'plugins.generic.plauditPreEndorsement.log.attemptSendingEndorsement', ['doi' => $publication->getDoi(), 'orcid' => $publication->getData('endorserOrcid')]);

$plauditClient = new PlauditClient();

Expand All @@ -82,12 +88,12 @@ public function sendEndorsementToPlaudit($publication)
$responseBody = print_r($response->getBody()->getContents(), true);

$this->plugin->writeOnActivityLog($submission, 'plugins.generic.plauditPreEndorsement.log.failedSendingEndorsement', ['code' => $responseCode, 'body' => $responseBody]);
$newEndorsementStatus = ENDORSEMENT_STATUS_COULDNT_COMPLETE;
$newEndorsementStatus = Endorsement::STATUS_COULDNT_COMPLETE;
}

$publication->setData('endorsementStatus', $newEndorsementStatus);
$publicationDao = DAORegistry::getDAO('PublicationDAO');
$publicationDao->updateObject($publication);
Repo::publication()->edit($publication, [
'endorsementStatus' => $newEndorsementStatus
]);
}

public function updateEndorserNameFromOrcid($publication, $orcid)
Expand All @@ -97,15 +103,17 @@ public function updateEndorserNameFromOrcid($publication, $orcid)
$fullName = $this->orcidClient->getFullNameFromRecord($orcidRecord);

$publication->setData('endorserName', $fullName);
DAORegistry::getDAO('PublicationDAO')->updateObject($publication);
$publicationDao = Repo::publication()->dao;
$publicationDao->update($publication);

return $publication;
}

public function messageWasAlreadyLoggedToday(int $submissionId, string $message): bool
{
$submissionEventLogDao = DAORegistry::getDAO('SubmissionEventLogDAO');
$submissionLogEntries = $submissionEventLogDao->getBySubmissionId($submissionId);
$submissionLogEntries = Repo::eventLog()->getCollector()
->filterByAssoc(Application::ASSOC_TYPE_SUBMISSION, [$submissionId])
->getMany();
$today = Core::getCurrentDate();

foreach($submissionLogEntries->toArray() as $logEntry) {
Expand Down
63 changes: 63 additions & 0 deletions classes/OrcidClient.inc.php → classes/OrcidClient.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
<?php

namespace APP\plugins\generic\plauditPreEndorsement\classes;

use APP\core\Application;

class OrcidClient
{
public const ORCID_URL = 'https://orcid.org/';
public const ORCID_URL_SANDBOX = 'https://sandbox.orcid.org/';
public const ORCID_API_URL_PUBLIC = 'https://pub.orcid.org/';
public const ORCID_API_URL_PUBLIC_SANDBOX = 'https://pub.sandbox.orcid.org/';
public const ORCID_API_URL_MEMBER = 'https://api.orcid.org/';
public const ORCID_API_URL_MEMBER_SANDBOX = 'https://api.sandbox.orcid.org/';
public const ORCID_API_SCOPE_PUBLIC = '/authenticate';
public const ORCID_API_SCOPE_MEMBER = '/activities/update';

private $plugin;
private $contextId;

Expand Down Expand Up @@ -89,4 +102,54 @@ public function getFullNameFromRecord(array $record): string

return "$givenName $familyName";
}

public function buildOAuthUrl($redirectParams)
{
$request = Application::get()->getRequest();

if ($this->isMemberApiEnabled($this->contextId)) {
$scope = self::ORCID_API_SCOPE_MEMBER;
} else {
$scope = self::ORCID_API_SCOPE_PUBLIC;
}

$redirectUrl = $request->getDispatcher()->url(
$request,
Application::ROUTE_PAGE,
null,
$this->plugin::HANDLER_PAGE,
'orcidVerify',
null,
$redirectParams
);

return $this->getOauthPath() . 'authorize?' . http_build_query(
array(
'client_id' => $this->plugin->getSetting($this->contextId, 'orcidClientId'),
'response_type' => 'code',
'scope' => $scope,
'redirect_uri' => $redirectUrl)
);
}

private function isMemberApiEnabled()
{
$apiUrl = $this->plugin->getSetting($this->contextId, 'orcidAPIPath');
return ($apiUrl == self::ORCID_API_URL_MEMBER || $apiUrl == self::ORCID_API_URL_MEMBER_SANDBOX);
}

private function getOauthPath()
{
return $this->getOrcidUrl() . 'oauth/';
}

private function getOrcidUrl()
{
$apiPath = $this->plugin->getSetting($this->contextId, 'orcidAPIPath');
if ($apiPath == self::ORCID_API_URL_PUBLIC || $apiPath == self::ORCID_API_URL_MEMBER) {
return self::ORCID_URL;
} else {
return self::ORCID_URL_SANDBOX;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace APP\plugins\generic\plauditPreEndorsement\classes;

class OrcidCredentialsValidator
{
public function validateClientId($str): bool
Expand Down
17 changes: 10 additions & 7 deletions classes/PlauditClient.inc.php → classes/PlauditClient.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

import('plugins.generic.plauditPreEndorsement.PlauditPreEndorsementPlugin');
namespace APP\plugins\generic\plauditPreEndorsement\classes;

define('PLAUDIT_API_URL', 'https://plaudit.pub/api/v1/endorsements');
use APP\core\Application;
use APP\plugins\generic\plauditPreEndorsement\classes\Endorsement;

class PlauditClient
{
private const PLAUDIT_API_URL = 'https://plaudit.pub/api/v1/endorsements';

public function filterOrcidNumbers(string $orcid): string
{
preg_match("~\d{4}-\d{4}-\d{4}-\d{3}(\d|X)~", $orcid, $matches);
Expand All @@ -21,12 +24,12 @@ public function requestEndorsementCreation($publication, $secretKey)
$postData = [
'secret_key' => $secretKey,
'orcid' => $orcid,
'doi' => $publication->getData('pub-id::doi')
'doi' => $publication->getDoi()
];

$response = $httpClient->request(
'POST',
PLAUDIT_API_URL,
self::PLAUDIT_API_URL,
[
'headers' => $headers,
'json' => $postData,
Expand All @@ -44,14 +47,14 @@ public function getEndorsementStatusByResponse($response, $publication)
$endorsementData = $body['endorsements'][0];
$responseDoi = $endorsementData['doi'];
$responseOrcid = $endorsementData['orcid'];
$publicationDoi = strtolower($publication->getData('pub-id::doi'));
$publicationDoi = strtolower($publication->getDoi());
$publicationOrcid = $this->filterOrcidNumbers($publication->getData('endorserOrcid'));

if ($responseDoi == $publicationDoi && $responseOrcid == $publicationOrcid) {
return ENDORSEMENT_STATUS_COMPLETED;
return Endorsement::STATUS_COMPLETED;
}
}

return ENDORSEMENT_STATUS_COULDNT_COMPLETE;
return Endorsement::STATUS_COULDNT_COMPLETE;
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
<?php

use Illuminate\Database\Capsule\Manager as Capsule;
namespace APP\plugins\generic\plauditPreEndorsement\classes;

import('lib.pkp.classes.db.DAO');
use PKP\db\DAO;
use Illuminate\Support\Facades\DB;
use APP\facades\Repo;
use APP\submission\Submission;
use APP\plugins\generic\plauditPreEndorsement\classes\Endorsement;

class PlauditPreEndorsementDAO extends DAO
{
public function getPublicationsWithEndorsementReadyToSend(int $contextId): array
{
$result = Capsule::table('submissions as sub')
$result = DB::table('submissions as sub')
->leftJoin('publications as pub', 'sub.current_publication_id', '=', 'pub.publication_id')
->leftJoin('publication_settings as ps', 'pub.publication_id', '=', 'ps.publication_id')
->where('ps.setting_name', 'endorsementStatus')
->where('ps.setting_value', ENDORSEMENT_STATUS_CONFIRMED)
->where('sub.status', STATUS_PUBLISHED)
->where('ps.setting_value', Endorsement::STATUS_CONFIRMED)
->where('sub.status', Submission::STATUS_PUBLISHED)
->where('sub.context_id', $contextId)
->select('pub.publication_id')
->get();

$publications = [];
$publicationDao = DAORegistry::getDAO('PublicationDAO');

foreach ($result as $row) {
$row = (array) $row;
$publications[] = $publicationDao->getById($row['publication_id']);
$publications[] = Repo::publication()->get($row['publication_id']);
}

return $publications;
Expand Down
Loading

0 comments on commit 3d894ec

Please sign in to comment.