Skip to content

Commit

Permalink
pkp/pkp-lib#9666 COUNTER R5 TSV reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bozana committed Aug 9, 2024
1 parent b08b8bb commit d9cea3a
Show file tree
Hide file tree
Showing 8 changed files with 687 additions and 30 deletions.
66 changes: 66 additions & 0 deletions classes/components/forms/counter/CounterReportForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @file classes/components/form/counter/CounterReportForm.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CounterReportForm
*
* @ingroup classes_controllers_form
*
* @brief A form for setting a counter report
*/

namespace APP\components\forms\counter;

use APP\sushi\IR;
use APP\sushi\IR_A1;
use APP\sushi\PR;
use APP\sushi\PR_P1;
use APP\sushi\TR;
use APP\sushi\TR_J3;
use PKP\components\forms\counter\PKPCounterReportForm;

class CounterReportForm extends PKPCounterReportForm
{
public function setReportFields(): void
{
$formFieldsPR = PR::getReportSettingsFormFields();
$this->reportFields['PR'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsPR);

$formFieldsPR_P1 = PR_P1::getReportSettingsFormFields();
$this->reportFields['PR_P1'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsPR_P1);

$formFieldsTR = TR::getReportSettingsFormFields();
$this->reportFields['TR'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsTR);

$formFieldsTR_J3 = TR_J3::getReportSettingsFormFields();
$this->reportFields['TR_J3'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsTR_J3);

$formFieldsIR = IR::getReportSettingsFormFields();
$this->reportFields['IR'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsIR);

$formFieldsIR_A1 = IR_A1::getReportSettingsFormFields();
$this->reportFields['IR_A1'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsIR_A1);
}
}
279 changes: 268 additions & 11 deletions classes/sushi/IR.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
namespace APP\sushi;

use APP\facades\Repo;
use Illuminate\Support\Collection;
use PKP\components\forms\FieldOptions;
use PKP\components\forms\FieldText;
use PKP\statistics\PKPStatisticsHelper;
use PKP\sushi\CounterR5Report;

Expand Down Expand Up @@ -93,7 +96,8 @@ public function getSupportedParams(): array
'attributes_to_show',
'include_component_details',
'include_parent_details',
'granularity'
'granularity',
'_', // for ajax requests
];
}

Expand Down Expand Up @@ -203,18 +207,16 @@ public function setAttributes(array $attributes): void
}
}

/**
* Get report items
*/
public function getReportItems(): array
/** Get DB query results for the report */
protected function getQueryResults(): Collection
{
$params['contextIds'] = [$this->context->getId()];
$params['institutionId'] = $this->customerId;
$params['dateStart'] = $this->beginDate;
$params['dateEnd'] = $this->endDate;
$params['yearsOfPublication'] = $this->yearsOfPublication;
if ($this->itemId > 0) {
$allowedParams['submissionIds'] = [$this->itemId];
$params['submissionIds'] = [$this->itemId];
}
// do not consider metric_type filter now, but for display

Expand Down Expand Up @@ -242,11 +244,16 @@ public function getReportItems(): array
'Data' => __('sushi.exception.3030', ['beginDate' => $this->beginDate, 'endDate' => $this->endDate])
]);
}
return $results;
}

$resultsGroupedBySubmission = $items = [];
foreach ($results as $result) {
$resultsGroupedBySubmission[$result->submission_id][] = $result;
}
/** Get report items */
public function getReportItems(): array
{
$results = $this->getQueryResults();

$items = [];
$resultsGroupedBySubmission = $results->groupBy('submission_id');

foreach ($resultsGroupedBySubmission as $submissionId => $submissionResults) {
// Get the submission properties
Expand Down Expand Up @@ -316,7 +323,7 @@ public function getReportItems(): array
$itemContributor['Type'] = 'Author';
$itemContributor['Name'] = $author->getFullName(true, false, $submissionLocale);
$orcid = $author->getOrcid();
if (isset($orcid) && !empty($orcid)) {
if (!empty($orcid)) {
$itemContributor['Identifier'] = $orcid;
}
$itemContributors[] = $itemContributor;
Expand Down Expand Up @@ -370,4 +377,254 @@ public function getReportItems(): array

return $items;
}

/** Get TSV report column names */
public function getTSVColumnNames(): array
{
$columnRow = ['Item', 'Publisher', 'Publisher ID', 'Platform'];

if (in_array('Authors', $this->attributesToShow)) {
array_push($columnRow, 'Authors');
}
if (in_array('Publication_Date', $this->attributesToShow)) {
array_push($columnRow, 'Publication_Date');
}
if (in_array('Article_Version', $this->attributesToShow)) {
array_push($columnRow, 'Article_Version');
}

array_push($columnRow, 'DOI', 'Proprietary_ID', 'ISBN', 'Print_ISSN', 'Online_ISSN', 'URI');

if ($this->includeParentDetails == 'True') {
array_push(
$columnRow,
'Parent_Title',
'Parent_Authors',
'Parent_Publication_Date',
'Parent_Article_Version',
'Parent_Data_Type',
'Parent_DOI',
'Parent_Proprietary_ID',
'Parent_ISBN',
'Parent_Print_ISSN',
'Parent_Online_ISSN',
'Parent_URI'
);
}

if (in_array('Data_Type', $this->attributesToShow)) {
array_push($columnRow, 'Data_Type');
}
if (in_array('YOP', $this->attributesToShow)) {
array_push($columnRow, 'YOP');
}
if (in_array('Access_Type', $this->attributesToShow)) {
array_push($columnRow, 'Access_Type');
}
if (in_array('Access_Method', $this->attributesToShow)) {
array_push($columnRow, 'Access_Method');
}

array_push($columnRow, 'Metric_Type', 'Reporting_Period_Total');

if ($this->granularity == 'Month') {
$period = $this->getMonthlyDatePeriod();
foreach ($period as $dt) {
array_push($columnRow, $dt->format('M-Y'));
}
}

return [$columnRow];
}

/** Get TSV report rows */
public function getTSVReportItems(): array
{
$results = $this->getQueryResults();

$resultRows = [];
$resultsGroupedBySubmission = $results->groupBy('submission_id');

foreach ($resultsGroupedBySubmission as $submissionId => $submissionResults) {
$results = collect($submissionResults);

// get total numbers for every metric type
$metricsTotal['Total_Item_Investigations'] = $results->pluck('metric_investigations')->sum();
$metricsTotal['Unique_Item_Investigations'] = $results->pluck('metric_investigations_unique')->sum();
$metricsTotal['Total_Item_Requests'] = $results->pluck('metric_requests')->sum();
$metricsTotal['Unique_Item_Requests'] = $results->pluck('metric_requests_unique')->sum();

// filter here by requested metric types
foreach ($this->metricTypes as $metricType) {
// if the total numbers for the given metric type > 0,
// construct the result row
if ($metricsTotal[$metricType] > 0) {
$submission = Repo::submission()->get($submissionId);
if (!$submission || !$submission->getOriginalPublication()) {
break;
}
$currentPublication = $submission->getCurrentPublication();
$submissionLocale = $submission->getData('locale');
$datePublished = $submission->getOriginalPublication()->getData('datePublished');

$resultRow = [
$currentPublication->getLocalizedTitle($submissionLocale), // Item
$this->context->getData('publisherInstitution'), // Publisher
'', // Publisher ID
$this->platformName, // Platform
];

if (in_array('Authors', $this->attributesToShow)) {
$authors = $currentPublication->getData('authors');
$authorRowValue = '';
foreach ($authors as $author) {
$authorRowValue = $author->getFullName(true, false, $submissionLocale);
$orcid = $author->getOrcid();
if (!empty($orcid)) {
$authorRowValue .= '(ORCID:' . $orcid . ')';
}
}
array_push($resultRow, $authorRowValue); // Authors
}

if (in_array('Publication_Date', $this->attributesToShow)) {
array_push($resultRow, $datePublished); // Publication_Date
}

if (in_array('Article_Version', $this->attributesToShow)) {
array_push($resultRow, 'VoR'); // Article_Version
}

$doi = $currentPublication->getDoi() ?? '';
array_push($resultRow, $doi); // DOI

array_push($resultRow, $this->platformId . ':' . $submissionId); // Proprietary_ID
array_push($resultRow, '', '', '', ''); // ISBN, Print_ISSN, Online_ISSN, URI

if ($this->includeParentDetails == 'True') {
array_push(
$resultRow,
$this->context->getName($this->context->getPrimaryLocale()), // Parent_Title
'', // Parent_Authors
'', // Parent_Publication_Date
'', // Parent_Article_Version
self::PARENT_DATA_TYPE, // Parent_Data_Type
'', // Parent_DOI
$this->platformId . ':' . $this->context->getId(), // Parent_Proprietary_ID
'', //Parent_ISBN
);
$printIssn = $this->context->getData('printIssn') ?? '';
array_push($resultRow, $printIssn); // Parent_Print_ISSN

$onlineIssn = $this->context->getData('onlineIssn') ?? '';
array_push($resultRow, $onlineIssn); // Parent_Online_ISSN

array_push($resultRow, ''); // Parent_URI
}

if (in_array('Data_Type', $this->attributesToShow)) {
array_push($resultRow, self::DATA_TYPE); // Data_Type
}
if (in_array('YOP', $this->attributesToShow)) {
array_push($resultRow, date('Y', strtotime($datePublished))); // YOP
}
if (in_array('Access_Type', $this->attributesToShow)) {
array_push($resultRow, self::ACCESS_TYPE); // Access_Type
}
if (in_array('Access_Method', $this->attributesToShow)) {
array_push($resultRow, self::ACCESS_METHOD); // Access_Method
}

array_push($resultRow, $metricType); // Metric_Type
array_push($resultRow, $metricsTotal[$metricType]); // Reporting_Period_Total
if ($this->granularity == 'Month') { // metrics for each month in the given period
$period = $this->getMonthlyDatePeriod();
foreach ($period as $dt) {
$month = $dt->format('Ym');
$result = $submissionResults->firstWhere('month', '=', $month);
if ($result === null) {
array_push($resultRow, '0');
} else {
$metrics['Total_Item_Investigations'] = $result->metric_investigations;
$metrics['Unique_Item_Investigations'] = $result->metric_investigations_unique;
$metrics['Total_Item_Requests'] = $result->metric_requests;
$metrics['Unique_Item_Requests'] = $result->metric_requests_unique;
array_push($resultRow, $metrics[$metricType]);
}
}
}
$resultRows[] = $resultRow;
}
}
}

return $resultRows;
}

/** Get report specific form fields */
public static function getReportSettingsFormFields(): array
{
$formFields = parent::getCommonReportSettingsFormFields();

$metricTypes = ['Total_Item_Investigations', 'Unique_Item_Investigations', 'Total_Item_Requests', 'Unique_Item_Requests'];
$metricTypeOptions = [];
foreach ($metricTypes as $metricType) {
$metricTypeOptions[] = ['value' => $metricType, 'label' => $metricType];
}
$formFields[] = new FieldOptions('metric_type', [
'label' => __('manager.statistics.counterR5Report.settings.metricType'),
'options' => $metricTypeOptions,
'value' => $metricTypes,
'groupId' => 'default',
]);

$attributesToShow = ['Article_Version', 'Authors', 'Access_Method', 'Access_Type', 'Data_Type', 'Publication_Date', 'YOP'];
$attributesToShowOptions = [];
foreach ($attributesToShow as $attributeToShow) {
$attributesToShowOptions[] = ['value' => $attributeToShow, 'label' => $attributeToShow];
}
$formFields[] = new FieldOptions('attributes_to_show', [
'label' => __('manager.statistics.counterR5Report.settings.attributesToShow'),
'options' => $attributesToShowOptions,
'value' => [],
'groupId' => 'default',
]);

$formFields[] = new FieldText('yop', [
'label' => __('manager.statistics.counterR5Report.settings.yop'),
'description' => __('manager.statistics.counterR5Report.settings.date.yop.description'),
'size' => 'small',
'isMultilingual' => false,
'isRequired' => false,
'groupId' => 'default',
]);

$formFields[] = new FieldText('item_id', [
'label' => __('manager.statistics.counterR5Report.settings.itemId'),
'size' => 'small',
'isMultilingual' => false,
'isRequired' => false,
'groupId' => 'default',
]);

$formFields[] = new FieldOptions('include_parent_details', [
'label' => __('manager.statistics.counterR5Report.settings.includeParentDetails'),
'options' => [
['value' => true, 'label' => __('manager.statistics.counterR5Report.settings.includeParentDetails')],
],
'value' => false,
'groupId' => 'default',
]);

$formFields[] = new FieldOptions('granularity', [
'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails'),
'options' => [
['value' => true, 'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails')],
],
'value' => false,
'groupId' => 'default',
]);

return $formFields;
}
}
Loading

0 comments on commit d9cea3a

Please sign in to comment.