Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured Citations #10462

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions api/v1/users/PKPUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\LazyCollection;
use PKP\context\Context;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\facades\Locale;
use PKP\mail\mailables\UserRoleEndNotify;
use PKP\plugins\Hook;
use PKP\security\authorization\ContextAccessPolicy;
use PKP\security\authorization\UserRolesRequiredPolicy;
use PKP\security\Role;
use PKP\userGroup\UserGroup;
use Symfony\Component\HttpFoundation\StreamedResponse;

class PKPUserController extends PKPBaseController
Expand Down Expand Up @@ -76,6 +81,10 @@ public function getGroupRoutes(): void

Route::get('', $this->getMany(...))
->name('user.getManyUsers');

Route::put('{userId}/endRole/{userGroupId}', $this->endRole(...))
->name('user.endRole')
->whereNumber(['userId', 'userGroupId']);
}

/**
Expand Down Expand Up @@ -114,7 +123,7 @@ public function get(Request $request): JsonResponse
*/
public function getMany(Request $request): JsonResponse
{
$context = $request->attributes->get('context'); /** @var \PKP\context\Context $context */
$context = $request->attributes->get('context'); /** @var Context $context */

$params = $this->_processAllowedParams($request->query(null), [
'assignedToCategory',
Expand Down Expand Up @@ -189,7 +198,7 @@ public function getMany(Request $request): JsonResponse
*/
public function getReviewers(Request $request): JsonResponse
{
$context = $request->attributes->get('context'); /** @var \PKP\context\Context $context */
$context = $request->attributes->get('context'); /** @var Context $context */

$params = $this->_processAllowedParams($request->query(), [
'averageCompletion',
Expand Down Expand Up @@ -243,7 +252,7 @@ public function getReviewers(Request $request): JsonResponse
*/
public function getReport(Request $request): StreamedResponse|JsonResponse
{
$context = $request->attributes->get('context'); /** @var \PKP\context\Context $context */
$context = $request->attributes->get('context'); /** @var Context $context */
$params = ['contextIds' => [$context->getId()]];

foreach ($request->query() as $param => $value) {
Expand Down Expand Up @@ -285,6 +294,47 @@ function () use ($report) {
);
}

public function endRole(Request $request): JsonResponse
{
// Ensure user exists
$userId = $request->route('userId');
$user = Repo::user()->get($userId);
if (!$user) {
return response()->json([
'error' => __('api.404.resourceNotFound')
], Response::HTTP_NOT_FOUND);
}

// Ensure user has role
// Will not appear if role has ended or if user never had role to begin with
$userGroupId = $request->route('userGroupId');
$userUserGroups = Repo::userGroup()->userUserGroups($userId); /** @var LazyCollection<UserGroup> $userUserGroups */
$userGroup = $userUserGroups->first(fn (UserGroup $userGroup) => $userGroup->getId() === (int) $userGroupId); /** @var UserGroup $userGroup */
if (!$userGroup) {
return response()->json([
'error' => __('api.404.resourceNotFound')
], Response::HTTP_NOT_FOUND);
}

// Set end date for role and save
$context = $request->attributes->get('context'); /** @var Context $context */
Repo::userGroup()->endAssignments($context->getId(), $userId, $userGroupId);

// Send email notification
$mailable = new UserRoleEndNotify($context, $userGroup);
$emailTemplate = Repo::emailTemplate()->getByKey($context->getId(), $mailable::getEmailTemplateKey());

$mailable->sender($request->user())
->recipients([$user])
->body($emailTemplate->getLocalizedData('body'))
->subject($emailTemplate->getLocalizedData('subject'));
Mail::send($mailable);

// Return updated user model
$user = Repo::user()->get($userId);
return response()->json(Repo::user()->getSchemaMap()->map($user), Response::HTTP_OK);
}

/**
* Convert the query params passed to the end point. Exclude unsupported
* params and coerce the type of those passed.
Expand Down
93 changes: 41 additions & 52 deletions classes/citation/Citation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/**
* @file classes/citation/Citation.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Citation
Expand All @@ -20,97 +20,86 @@

namespace PKP\citation;

class Citation extends \PKP\core\DataObject
use PKP\core\DataObject;

class Citation extends DataObject
{
/**
* Constructor.
*
* @param string $rawCitation an unparsed citation string
* @param string|null $rawCitation an unparsed citation string
*/
public function __construct($rawCitation = null)
public function __construct(string $rawCitation = null)
{
parent::__construct();
$this->setRawCitation($rawCitation);
}

//
// Getters and Setters
//

/**
* Replace URLs through HTML links, if the citation does not already contain HTML links
*
* @return string
* Get publication id.
*/
public function getCitationWithLinks()
public function getPublicationId()
{
$citation = $this->getRawCitation();
if (stripos($citation, '<a href=') === false) {
$citation = preg_replace_callback(
'#(http|https|ftp)://[\d\w\.-]+\.[\w\.]{2,6}[^\s\]\[\<\>]*/?#',
function ($matches) {
$trailingDot = in_array($char = substr($matches[0], -1), ['.', ',']);
$url = rtrim($matches[0], '.,');
return "<a href=\"{$url}\">{$url}</a>" . ($trailingDot ? $char : '');
},
$citation
);
}
return $citation;
return $this->getData('publicationId');
}

/**
* Get the rawCitation
*
* @return string
* Get the rawCitation.
*/
public function getRawCitation()
public function getRawCitation(): string
{
return $this->getData('rawCitation');
}

/**
* Set the rawCitation
*
* @param string $rawCitation
* Set the rawCitation.
*/
public function setRawCitation($rawCitation)
public function setRawCitation(string $rawCitation = null): void
{
$rawCitation = $this->_cleanCitationString($rawCitation);
$rawCitation = $this->cleanCitationString($rawCitation);
$this->setData('rawCitation', $rawCitation);
}

/**
* Get the sequence number
*
* @return int
* Get the sequence number.
*/
public function getSequence()
public function getSequence(): int
{
return $this->getData('seq');
}

/**
* Set the sequence number
*
* @param int $seq
* Set the sequence number.
*/
public function setSequence($seq)
public function setSequence(int $seq): void
{
$this->setData('seq', $seq);
}

//
// Private methods
//
/**
* Take a citation string and clean/normalize it
*
* @param string $citationString
*
* @return string
* Replace URLs through HTML links, if the citation does not already contain HTML links.
*/
public function getCitationWithLinks(): string
{
$citation = $this->getRawCitation();
if (stripos($citation, '<a href=') === false) {
$citation = preg_replace_callback(
'#(http|https|ftp)://[\d\w\.-]+\.[\w\.]{2,6}[^\s\]\[\<\>]*/?#',
function ($matches) {
$trailingDot = in_array($char = substr($matches[0], -1), ['.', ',']);
$url = rtrim($matches[0], '.,');
return "<a href=\"{$url}\">{$url}</a>" . ($trailingDot ? $char : '');
},
$citation
);
}
return $citation;
}

/**
* Take a citation string and clean/normalize it.
*/
public function _cleanCitationString($citationString)
public function cleanCitationString(string $citationString = null): string
{
// 1) Strip slashes and whitespace
$citationString = trim(stripslashes($citationString));
Expand Down
32 changes: 20 additions & 12 deletions classes/citation/CitationDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file classes/citation/CitationDAO.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CitationDAO
Expand Down Expand Up @@ -57,7 +57,7 @@ public function insertObject($citation)
]
);
$citation->setId($this->getInsertId());
$this->_updateObjectMetadata($citation);
$this->updateObjectMetadata($citation);
return $citation->getId();
}

Expand All @@ -75,7 +75,7 @@ public function getById($citationId)
[$citationId]
);
$row = $result->current();
return $row ? $this->_fromRow((array) $row) : null;
return $row ? $this->fromRow((array) $row) : null;
}

/**
Expand Down Expand Up @@ -162,7 +162,7 @@ public function updateObject($citation)
(int) $citation->getId()
]
);
$this->_updateObjectMetadata($citation);
$this->updateObjectMetadata($citation);
}

/**
Expand Down Expand Up @@ -203,15 +203,12 @@ public function deleteByPublicationId($publicationId)
return true;
}

//
// Private helper methods
//
/**
* Construct a new citation object.
*
* @return Citation
*/
public function _newDataObject()
public function newDataObject()
{
return new Citation();
}
Expand All @@ -224,9 +221,9 @@ public function _newDataObject()
*
* @return Citation
*/
public function _fromRow($row)
public function fromRow($row)
{
$citation = $this->_newDataObject();
$citation = $this->newDataObject();
$citation->setId((int)$row['citation_id']);
$citation->setData('publicationId', (int)$row['publication_id']);
$citation->setRawCitation($row['raw_citation']);
Expand All @@ -242,10 +239,21 @@ public function _fromRow($row)
*
* @param Citation $citation
*/
public function _updateObjectMetadata($citation)
public function updateObjectMetadata($citation)
{
$this->updateDataObjectSettings('citation_settings', $citation, ['citation_id' => $citation->getId()]);
}

//todo: remove these after refactoring
public function _newDataObject(){
return $this->newDataObject();
}
public function _fromRow($row){
return $this->fromRow($row);
}
public function _updateObjectMetadata($citation){
return $this->updateObjectMetadata($citation);
}
}

if (!PKP_STRICT_MODE) {
Expand Down
6 changes: 3 additions & 3 deletions classes/citation/CitationListTokenizerFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* @file classes/citation/CitationListTokenizerFilter.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CitationListTokenizerFilter
*
* @ingroup classes_citation
* @ingroup citation
*
* @brief Class that takes an unformatted list of citations
* and returns an array of raw citation strings.
Expand Down
Loading