Skip to content

Commit

Permalink
pkp#52 update based on initial PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Feb 12, 2024
1 parent 7f39d43 commit b51046e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
41 changes: 24 additions & 17 deletions IThenticate.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class IThenticate
*
* @var array|null
*/
protected $eualVersionDetails = null;
protected $eulaVersionDetails = null;

/**
* Base API path
Expand Down Expand Up @@ -111,7 +111,7 @@ public function __construct($apiUrl, $apiKey, $integrationName, $integrationVers
}

/**
* Confirm the EUAL on the user's behalf for given version
* Confirm the EULA on the user's behalf for given version
* @see https://developers.turnitin.com/docs/tca#accept-eula-version
*
* @param User $user
Expand Down Expand Up @@ -142,7 +142,7 @@ public function confirmEula($user, $context, $version = self::DEFAULT_EULA_VERSI
'json' => [
'user_id' => $user->getId(),
'accepted_timestamp' => \Carbon\Carbon::now()->toIso8601String(),
'language' => $this->getEualConfirmationLocale($context->getPrimaryLocale()),
'language' => $this->getEulaConfirmationLocale($context->getPrimaryLocale()),
],
'verify' => false,
'exceptions' => false,
Expand All @@ -159,15 +159,22 @@ public function confirmEula($user, $context, $version = self::DEFAULT_EULA_VERSI
* @param Submission $submission
* @param User $user
* @param Author|null $author
* @param Site|null $site
*
* @return string|null if succeed, it will return the created submission UUID at service's end and
* at failure, will return null
*/
public function submitSubmission($submission, $user, $author = null) {
public function submitSubmission($submission, $user, $author = null, $site = null) {

$publication = $submission->getCurrentPublication(); /** @var Publication $publication */
$author ??= $publication->getPrimaryAuthor();

if (!$site) {
import('lib.pkp.classes.db.DAORegistry');
$siteDao = DAORegistry::getDAO("SiteDAO"); /** @var SiteDAO $siteDao */
$site = $siteDao->getSite(); /** @var Site $site */
}

$response = Application::get()->getHttpClient()->request(
'POST',
$this->getApiPath("submissions"),
Expand All @@ -183,15 +190,15 @@ public function submitSubmission($submission, $user, $author = null) {
'owners' => [
[
'id' => $author->getId(),
'given_name' => $author->getLocalizedGivenName(),
'family_name' => $author->getLocalizedFamilyName(),
'given_name' => $author->getGivenName($publication->getData('locale')) ?? $author->getLocalizedGivenName(),
'family_name' => $author->getGivenName($publication->getData('locale')) ?? $author->getLocalizedFamilyName(),
'email' => $author->getEmail(),
]
],
'submitter' => [
'id' => $user->getId(),
'given_name' => $user->getLocalizedGivenName(),
'family_name' => $user->getLocalizedFamilyName(),
'given_name' => $user->getGivenName($site->getPrimaryLocale()) ?? $user->getLocalizedGivenName(),
'family_name' => $user->getFamilyName($site->getPrimaryLocale()) ?? $user->getLocalizedFamilyName(),
'email' => $user->getEmail(),
],
'original_submitted_time' => \Carbon\Carbon::now()->toIso8601String(),
Expand Down Expand Up @@ -274,7 +281,7 @@ public function uploadFile($submissionTacId, $fileName, $fileContent) {
}

/**
* Verify if user has already confirmed the given EUAL version
* Verify if user has already confirmed the given EULA version
* @see https://developers.turnitin.com/docs/tca#get-eula-acceptance-info
*
* @param Author|User $user
Expand All @@ -297,7 +304,7 @@ public function verifyUserEulaAcceptance($user, $version)
}

/**
* Validate/Retrieve the given EUAL version
* Validate/Retrieve the given EULA version
* @see https://developers.turnitin.com/docs/tca#get-eula-version-info
*
* @param string $version
Expand All @@ -315,10 +322,10 @@ public function validateEulaVersion($version) {
);

if ($response->getStatusCode() === 200) {
$this->eualVersionDetails = json_decode($response->getBody()->getContents(), true);
$this->eulaVersionDetails = json_decode($response->getBody()->getContents(), true);

if (!$this->eulaVersion) {
$this->eulaVersion = $this->eualVersionDetails['version'];
$this->eulaVersion = $this->eulaVersionDetails['version'];
}

return true;
Expand Down Expand Up @@ -370,8 +377,8 @@ public function registerWebhook($signingSecret, $url, $events = self::DEFAULT_WE
*
* @return array|null
*/
public function getEualDetails() {
return $this->eualVersionDetails;
public function getEulaDetails() {
return $this->eulaVersionDetails;
}

/**
Expand All @@ -395,12 +402,12 @@ public function getApplicationEulaVersion() {
* @param string $locale
* @return string
*/
protected function getEualConfirmationLocale($locale) {
if (!$this->getEualDetails()) {
protected function getEulaConfirmationLocale($locale) {
if (!$this->getEulaDetails()) {
return static::DEFAULT_EULA_LANGUAGE;
}

$euleLangs = $this->getEualDetails()['available_languages'];
$euleLangs = $this->getEulaDetails()['available_languages'];
$locale = str_replace("_", "-", substr($locale, 0, 5));

return in_array($locale, $euleLangs) ? $locale : static::DEFAULT_EULA_LANGUAGE;
Expand Down
18 changes: 14 additions & 4 deletions PlagiarismPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import('lib.pkp.classes.plugins.GenericPlugin');
import('lib.pkp.classes.db.DAORegistry');

class PlagiarismPlugin extends GenericPlugin {

Expand Down Expand Up @@ -83,7 +84,7 @@ public function getEnabled($contextId = null) {
* @param string $hookName `Schema::get::submission`
* @param array $params
*
* @return void
* @return bool
*/
public function addPlagiarismCheckDataToSubmissionSchema($hookName, $params) {
$schema =& $params[0];
Expand All @@ -94,6 +95,8 @@ public function addPlagiarismCheckDataToSubmissionSchema($hookName, $params) {
'apiSummary' => true,
'validation' => ['nullable'],
];

return false;
}

/**
Expand All @@ -103,7 +106,7 @@ public function addPlagiarismCheckDataToSubmissionSchema($hookName, $params) {
* @param string $hookName `Schema::get::context` or `Schema::get::site`
* @param array $params
*
* @return void
* @return bool
*/
public function addPlagiarismCheckWebhookDataToSchema($hookName, $params) {
$schema =& $params[0];
Expand All @@ -121,6 +124,8 @@ public function addPlagiarismCheckWebhookDataToSchema($hookName, $params) {
'apiSummary' => true,
'validation' => ['nullable'],
];

return false;
}

/**
Expand Down Expand Up @@ -254,7 +259,12 @@ public function submitForPlagiarismCheck($hookName, $args) {
}

// Create the submission at iThenticate's end
$submissionUuid = $ithenticate->submitSubmission($submission, $request->getUser(), $author);
$submissionUuid = $ithenticate->submitSubmission(
$submission,
$request->getUser(),
$author,
$request->getSite()
);

if (!$submissionUuid) {
$this->sendErrorMessage($submission->getId(), 'Could not submit the submission at iThenticate.');
Expand All @@ -263,6 +273,7 @@ public function submitForPlagiarismCheck($hookName, $args) {

// $submission->setData('ithenticate_id', $submissionUuid);
// $submissionDao->updateObject($submission);
import('classes.core.Services');
Services::get("submission")->edit($submission, [
'ithenticate_id' => $submissionUuid,
], $request);
Expand All @@ -279,7 +290,6 @@ public function submitForPlagiarismCheck($hookName, $args) {
if (!$webhookStorable->getData('ithenticate_webhook_id')) {
$signingSecret = \Illuminate\Support\Str::random(12);

// http://ojs-stable-3_3_0.test/index.php/test-01/$$$call$$$/plugins/generic/plagiarism/controllers/plagiarism-webhook/handle
$webhookUrl = $request->getDispatcher()->url(
$request,
ROUTE_COMPONENT,
Expand Down
31 changes: 16 additions & 15 deletions TestIThenticate.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @brief Low-budget mock class for \IThenticate -- Replace the
* constructor in PlagiarismPlugin::submitForPlagiarismCheck with this class name
* to log API usage instead of interacting with the iThenticate service.
* constructor and import in PlagiarismPlugin::submitForPlagiarismCheck with
* this class name to log API usage instead of interacting with the iThenticate service.
*/

class TestIThenticate {
Expand All @@ -26,7 +26,7 @@ class TestIThenticate {
*
* @var array|null
*/
protected $eualVersionDetails = null;
protected $eulaVersionDetails = null;

/**
* The default EULA version placeholder to retrieve the current latest version
Expand Down Expand Up @@ -70,7 +70,7 @@ public function __construct($apiUrl, $apiKey, $integrationName, $integrationVers
}

/**
* Confirm the EUAL on the user's behalf for given version
* Confirm the EULA on the user's behalf for given version
* @see https://developers.turnitin.com/docs/tca#accept-eula-version
*
* @param User $user
Expand All @@ -80,7 +80,7 @@ public function __construct($apiUrl, $apiKey, $integrationName, $integrationVers
* @return bool
*/
public function confirmEula($user, $context, $version = self::DEFAULT_EULA_VERSION) {
error_log("Confirming EULA for user {$user->getId()} with language ".$this->getEualConfirmationLocale($context->getPrimaryLocale())." for version {$this->getApplicationEulaVersion()}");
error_log("Confirming EULA for user {$user->getId()} with language ".$this->getEulaConfirmationLocale($context->getPrimaryLocale())." for version {$this->getApplicationEulaVersion()}");
return true;
}

Expand All @@ -91,11 +91,12 @@ public function confirmEula($user, $context, $version = self::DEFAULT_EULA_VERSI
* @param Submission $submission
* @param User $user
* @param Author|null $author
* @param Site|null $site
*
* @return string|null if succeed, it will return the created submission UUID at service's end and
* at failure, will return null
*/
public function submitSubmission($submission, $user, $author = null) {
public function submitSubmission($submission, $user, $author = null, $site = null) {
error_log("Creating a new submission with id {$submission->getId()} by submitter {$user->getId()} for owner {$author->getId()}");
return \Illuminate\Support\Str::uuid()->__toString();
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public function uploadFile($submissionTacId, $fileName, $fileContent) {
}

/**
* Verify if user has already confirmed the given EUAL version
* Verify if user has already confirmed the given EULA version
* @see https://developers.turnitin.com/docs/tca#get-eula-acceptance-info
*
* @param Author|User $user
Expand All @@ -155,19 +156,19 @@ public function uploadFile($submissionTacId, $fileName, $fileContent) {
* @return bool
*/
public function verifyUserEulaAcceptance($user, $version) {
error_log("Verifying if user with id {$user->getId()} has already confirmed the given EUAL version {$version}");
error_log("Verifying if user with id {$user->getId()} has already confirmed the given EULA version {$version}");
return true;
}

/**
* Validate/Retrieve the given EUAL version
* Validate/Retrieve the given EULA version
* @see https://developers.turnitin.com/docs/tca#get-eula-version-info
*
* @param string $version
* @return bool
*/
public function validateEulaVersion($version) {
error_log("Validating/Retrieving the given EUAL version {$version}");
error_log("Validating/Retrieving the given EULA version {$version}");
return true;
}

Expand Down Expand Up @@ -198,7 +199,7 @@ public function registerWebhook($signingSecret, $url, $events = self::DEFAULT_WE
*
* @return array|null
*/
public function getEualDetails() {
public function getEulaDetails() {
return [
"version" => "v1beta",
"valid_from" => "2018-04-30T17:00:00Z",
Expand Down Expand Up @@ -249,14 +250,14 @@ public function getApplicationEulaVersion() {
* @param string $locale
* @return string
*/
protected function getEualConfirmationLocale($locale) {
if (!$this->getEualDetails()) {
protected function getEulaConfirmationLocale($locale) {
if (!$this->getEulaDetails()) {
return static::DEFAULT_EULA_LANGUAGE;
}

$euleLangs = $this->getEualDetails()['available_languages'];
$euleLangs = $this->getEulaDetails()['available_languages'];
$locale = str_replace("_", "-", substr($locale, 0, 5));

return in_array($locale, $euleLangs) ? $locale : static::DEFAULT_EULA_LANGUAGE;
}
}
}

0 comments on commit b51046e

Please sign in to comment.