Commit f47e89a9 authored by Paul Holden's avatar Paul Holden
Browse files

MDL-70795 reportbuilder: editor elements to set column aggregation.

Aggregation of report columns allows the report editor to perform
common types of data aggregation (concatenation, sum, count, etc)
on given columns.
parent db7102eb
......@@ -24,7 +24,20 @@
$string['actions'] = 'Actions';
$string['addcolumn'] = 'Add column \'{$a}\'';
$string['aggregatecolumn'] = 'Aggregate column \'{$a}\'';
$string['aggregationavg'] = 'Average';
$string['aggregationcount'] = 'Count';
$string['aggregationcountdistinct'] = 'Count distinct';
$string['aggregationgroupconcat'] = 'Comma separated values';
$string['aggregationgroupconcatdistinct'] = 'Comma separated distinct values';
$string['aggregationmax'] = 'Maximum';
$string['aggregationmin'] = 'Minimum';
$string['aggregationnone'] = 'No aggregation';
$string['aggregationpercent'] = 'Percentage';
$string['aggregationsum'] = 'Sum';
$string['apply'] = 'Apply';
$string['columnadded'] = 'Added column \'{$a}\'';
$string['columnaggregated'] = 'Aggregated column \'{$a}\'';
$string['columnsortdirectionasc'] = 'Sort column \'{$a}\' ascending';
$string['columnsortdirectiondesc'] = 'Sort column \'{$a}\' descending';
$string['columnsortdisable'] = 'Disable sorting for column \'{$a}\'';
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -27,6 +27,7 @@
import $ from 'jquery';
import {dispatchEvent} from 'core/event_dispatcher';
import 'core/inplace_editable';
import {eventTypes as inplaceEditableEvents} from 'core/local/inplace_editable/events';
import Notification from 'core/notification';
import Pending from 'core/pending';
import {publish} from 'core/pubsub';
......@@ -142,4 +143,21 @@ export const init = (reportElement, initialized) => {
.catch(Notification.exception);
}
});
// Initialize inplace editable listeners for column aggregation.
reportElement.addEventListener(inplaceEditableEvents.elementUpdated, event => {
const columnAggregation = event.target.closest('[data-itemtype="columnaggregation"]');
if (columnAggregation) {
const columnHeader = columnAggregation.closest(reportSelectors.regions.columnHeader);
getString('columnaggregated', 'core_reportbuilder', columnHeader.dataset.columnName)
.then(addToast)
.then(() => {
dispatchEvent(reportEvents.tableReload, {}, reportElement);
return;
})
.catch(Notification.exception);
}
});
};
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column average aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class avg extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationavg', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all numeric columns
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
return in_array($columntype, [
column::TYPE_INTEGER,
column::TYPE_FLOAT,
]);
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
return "AVG(1.0 * {$field})";
}
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return sprintf('%.1f', (float) reset($values));
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Base class for column aggregation types
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base {
/**
* Return the class name of the aggregation type
*
* @return string
*/
final public static function get_class_name(): string {
$namespacedclass = explode('\\', get_called_class());
return end($namespacedclass);
}
/**
* Return the display name of the aggregation
*
* @return lang_string
*/
abstract public static function get_name(): lang_string;
/**
* Whether the aggregation is compatible with the given column type
*
* @param int $columntype The type as defined by the {@see column::set_type} method
* @return bool
*/
abstract public static function compatible(int $columntype): bool;
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
abstract public static function get_field_sql(string $field, int $columntype): string;
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
abstract public static function format_value($value, array $values, array $callbacks);
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column count aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class count extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationcount', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all column types
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
return true;
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
global $DB;
if ($columntype === column::TYPE_LONGTEXT && $DB->get_dbfamily() === 'oracle') {
$field = $DB->sql_compare_text($field, 255);
}
return "COUNT({$field})";
}
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return (int) reset($values);
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column count distinct aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class countdistinct extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationcountdistinct', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all column types
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
return true;
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
global $DB;
if ($columntype === column::TYPE_LONGTEXT && $DB->get_dbfamily() === 'oracle') {
$field = $DB->sql_compare_text($field, 255);
}
return "COUNT(DISTINCT {$field})";
}
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return (int) reset($values);
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column group concatenation aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class groupconcat extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationgroupconcat', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all non-timestamp columns
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
return !in_array($columntype, [
column::TYPE_TIMESTAMP,
]);
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
global $DB;
return $DB->sql_group_concat($field);
}
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return $value;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column group concatenation distinct aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class groupconcatdistinct extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationgroupconcatdistinct', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all non-timestamp columns in MySQL and Postgres only
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
global $DB;
$dbsupportedtype = in_array($DB->get_dbfamily(), [
'mysql',
'postgres',
]);
return $dbsupportedtype && !in_array($columntype, [
column::TYPE_TIMESTAMP,
]);
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
global $DB;
// DB limitations mean we only support MySQL and Postgres, and each handle it differently.
if ($DB->get_dbfamily() === 'postgres') {
return "STRING_AGG(DISTINCT CAST({$field} AS VARCHAR), ', ')";
} else {
return $DB->sql_group_concat("DISTINCT {$field}");
}
}
/**
* Return formatted value for column when applying aggregation
*
* @param mixed $value
* @param array $values
* @param array $callbacks
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return $value;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use lang_string;
use core_reportbuilder\local\report\column;
/**
* Column max aggregation type
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class max extends base {
/**
* Return aggregation name
*
* @return lang_string
*/
public static function get_name(): lang_string {
return new lang_string('aggregationmax', 'core_reportbuilder');
}
/**
* This aggregation can be performed on all numeric/date/boolean types
*
* @param int $columntype
* @return bool
*/
public static function compatible(int $columntype): bool {
return in_array($columntype, [
column::TYPE_INTEGER,
column::TYPE_FLOAT,
column::TYPE_TIMESTAMP,
column::TYPE_BOOLEAN,
]);
}
/**
* Return the aggregated field SQL
*
* @param string $field
* @param int $columntype
* @return string
*/
public static function get_field_sql(string $field, int $columntype): string {
return "MAX({$field})";
}