Skip to content

Commit

Permalink
pkp/pkp-lib#8248 COUNTER R5 TSV reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bozana committed Jan 9, 2024
1 parent 9a29c1d commit 6a2c21d
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 9 deletions.
52 changes: 52 additions & 0 deletions classes/components/forms/counter/CounterReportForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\PR;
use APP\sushi\PR_P1;
use APP\sushi\TR;
use APP\sushi\TR_B3;
use PKP\components\forms\counter\PKPCounterReportForm;

class CounterReportForm extends PKPCounterReportForm
{
public function getReportFields(): 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_B3 = TR_B3::getReportSettingsFormFields();
$this->reportFields['TR_B3'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsTR_B3);
}
}
139 changes: 134 additions & 5 deletions classes/sushi/PR.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace APP\sushi;

use APP\core\Services;
use Illuminate\Support\Collection;
use PKP\components\forms\FieldOptions;
use PKP\statistics\PKPStatisticsHelper;
use PKP\sushi\CounterR5Report;

Expand Down Expand Up @@ -133,10 +135,9 @@ public function getSupportedAttributes(): array
];
}

/**
* 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;
Expand Down Expand Up @@ -168,6 +169,13 @@ public function getReportItems(): array
'Data' => __('sushi.exception.3030', ['beginDate' => $this->beginDate, 'endDate' => $this->endDate])
]);
}
return $results;
}

/** Get report items */
public function getReportItems(): array
{
$results = $this->getQueryResults();

// There is only one platform, so there will be only one report item
$item['Platform'] = $this->platformName;
Expand Down Expand Up @@ -201,7 +209,6 @@ public function getReportItems(): array
$metrics['Unique_Item_Investigations'] = $result->metric_book_investigations_unique + $result->metric_chapter_investigations_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Item_Requests'] = $result->metric_book_requests_unique + $result->metric_chapter_requests_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Title_Investigations'] = $result->metric_title_investigations_unique;
$metrics['Unique_Title_Requests'] = $result->metric_title_requests_unique;
// filter here by requested metric types
Expand All @@ -220,4 +227,126 @@ public function getReportItems(): array
$items = [$item];
return $items;
}

/** Get TSV report column names */
public function getTSVColumnNames(): array
{
$columnRow = ['Platform'];
if (in_array('Data_Type', $this->attributesToShow)) {
array_push($columnRow, 'Data_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();

// get total numbers for every metric type
$metricsTotal['Total_Item_Investigations'] = $results->pluck('metric_book_investigations')->sum() + $results->pluck('metric_chapter_investigations')->sum();
$metricsTotal['Unique_Item_Investigations'] = $results->pluck('metric_book_investigations_unique')->sum() + $results->pluck('metric_chapter_investigations_unique')->sum();
$metricsTotal['Total_Item_Requests'] = $results->pluck('metric_book_requests')->sum() + $results->pluck('metric_chapter_requests')->sum();
$metricsTotal['Unique_Item_Requests'] = $results->pluck('metric_book_requests_unique')->sum() + $results->pluck('metric_chapter_requests_unique')->sum();
$metricsTotal['Unique_Title_Investigations'] = $results->pluck('metric_title_investigations_unique')->sum();
$metricsTotal['Unique_Title_Requests'] = $results->pluck('metric_title_requests_unique')->sum();

$resultRows = [];
// filter here by requested metric types
foreach ($this->metricTypes as $metricType) {
// if the total numbers for the given metric type > 0
if ($metricsTotal[$metricType] > 0) {
// construct the result row
$resultRow = [];
array_push($resultRow, $this->platformName); // Platform
if (in_array('Data_Type', $this->attributesToShow)) {
array_push($resultRow, self::DATA_TYPE); // Data_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 = $results->firstWhere('month', '=', $month);
if ($result === null) {
array_push($resultRow, '0');
} else {
$metrics['Total_Item_Investigations'] = $result->metric_book_investigations + $result->metric_chapter_investigations;
$metrics['Unique_Item_Investigations'] = $result->metric_book_investigations_unique + $result->metric_chapter_investigations_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Item_Requests'] = $result->metric_book_requests_unique + $result->metric_chapter_requests_unique;
$metrics['Unique_Title_Investigations'] = $result->metric_title_investigations_unique;
$metrics['Unique_Title_Requests'] = $result->metric_title_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',
'Unique_Title_Investigations',
'Unique_Title_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 = ['Data_Type', 'Access_Method'];
$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 FieldOptions('granularity', [
'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails'),
'options' => [
['value' => true, 'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails')],
],
'value' => false,
'groupId' => 'default',
]);

return $formFields;
}
}
6 changes: 6 additions & 0 deletions classes/sushi/PR_P1.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ public function setAttributes(array $attributes): void
{
$this->attributes = [];
}

/** Get report specific form fields */
public static function getReportSettingsFormFields(): array
{
return parent::getCommonReportSettingsFormFields();
}
}
Loading

0 comments on commit 6a2c21d

Please sign in to comment.