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

MDL-72826 reportbuilder: per-report option to show only unique rows.

This option allows report creators to avoid duplicate data being
displayed in their report. It is applied only when no columns in the
report currently have aggregation methods applied.
parent eab63d2c
......@@ -187,6 +187,8 @@ $string['switchpreview'] = 'Switch to preview mode';
$string['timeadded'] = 'Time added';
$string['timecreated'] = 'Time created';
$string['timemodified'] = 'Time modified';
$string['uniquerows'] = 'Show unique rows';
$string['uniquerows_help'] = 'Show only unique rows in the report. Note this setting has no effect if any report columns are being aggregated';
$string['userfullnamewithlink'] = 'Full name with link';
$string['userfullnamewithpicture'] = 'Full name with picture';
$string['userfullnamewithpicturelink'] = 'Full name with picture and link';
......
......@@ -4386,6 +4386,7 @@
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="source" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="type" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="uniquerows" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="conditiondata" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="settingsdata" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
......
......@@ -3145,5 +3145,20 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021110800.03);
}
if ($oldversion < 2021111600.01) {
// Define field uniquerows to be added to reportbuilder_report.
$table = new xmldb_table('reportbuilder_report');
$field = new xmldb_field('uniquerows', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'type');
// Conditionally launch add field uniquerows.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021111600.01);
}
return true;
}
......@@ -111,6 +111,9 @@ class report extends dynamic_form {
$mform->setDefault('includedefaultsetup', 1);
$mform->addHelpButton('includedefaultsetup', 'includedefaultsetup', 'core_reportbuilder');
}
$mform->addElement('advcheckbox', 'uniquerows', get_string('uniquerows', 'core_reportbuilder'));
$mform->addHelpButton('uniquerows', 'uniquerows', 'core_reportbuilder');
}
/**
......
......@@ -75,8 +75,10 @@ class report {
throw new invalid_parameter_exception('Invalid report');
}
$report->set('name', trim($data->name))
->update();
$report->set_many([
'name' => trim($data->name),
'uniquerows' => $data->uniquerows,
])->update();
return $report;
}
......
......@@ -60,6 +60,10 @@ class report extends persistent {
base::TYPE_SYSTEM_REPORT,
],
],
'uniquerows' => [
'type' => PARAM_BOOL,
'default' => false,
],
'conditiondata' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
......
......@@ -62,7 +62,7 @@ class custom_report_table extends base_report_table {
$this->define_baseurl(new moodle_url('/reportbuilder/edit.php', ['id' => $matches['id']]));
// Load the report persistent, and accompanying system report instance.
// Load the report persistent, and accompanying report instance.
$this->persistent = new report($matches['id']);
$this->report = manager::get_report_from_persistent($this->persistent);
......@@ -90,7 +90,10 @@ class custom_report_table extends base_report_table {
$aggregatedcolumns = array_filter($columns, static function(column $column): bool {
return !empty($column->get_aggregation());
});
// Also take account of the report setting to show unique rows (only if no columns are being aggregated).
$hasaggregatedcolumns = !empty($aggregatedcolumns);
$showuniquerows = !$hasaggregatedcolumns && $this->persistent->get('uniquerows');
$columnheaders = [];
$columnsattributes = [];
......@@ -98,8 +101,9 @@ class custom_report_table extends base_report_table {
$columnheading = $column->get_persistent()->get_formatted_heading($this->report->get_context());
$columnheaders[$column->get_column_alias()] = $columnheading !== '' ? $columnheading : $column->get_title();
// We need to determine for each column whether we should group by it's fields, to support aggregation.
$columnaggregation = $column->get_aggregation();
if ($hasaggregatedcolumns && empty($columnaggregation)) {
if ($showuniquerows || ($hasaggregatedcolumns && empty($columnaggregation))) {
$groupby = array_merge($groupby, $column->get_groupby_sql());
}
......
......@@ -65,9 +65,9 @@ Feature: Manage custom report columns aggregation
Then I should see "Aggregated column 'Last access'"
And I should see "<output>" in the "Richie" "table_row"
Examples:
| aggregation | output |
| Count | 3 |
| Count distinct | 2 |
| aggregation | output |
| Count | 3 |
| Count distinct | 2 |
| Maximum | ##2 days ago##%A, %d %B %Y## |
| Minimum | ##3 days ago##%A, %d %B %Y## |
......@@ -94,3 +94,25 @@ Feature: Manage custom report columns aggregation
| Minimum | No |
| Percentage | 66.7% |
| Sum | 2 |
Scenario: Show unique report rows
Given the following "core_reportbuilder > Reports" exist:
| name | source | default | uniquerows |
| My report | core_user\reportbuilder\datasource\users | 0 | 1 |
And the following "core_reportbuilder > Columns" exist:
| report | uniqueidentifier |
| My report | user:firstname |
| My report | user:lastname |
When I am on the "My report" "reportbuilder > Editor" page logged in as "admin"
Then the following should exist in the "reportbuilder-table" table:
| -1- | -2- |
| Admin | User |
| Ben | Richie |
| Bill | Richie |
# Assert there is no 4th row (duplicate Bill Richie) because we're showing unique rows.
And "//table[@data-region='reportbuilder-table']/tbody/tr[not(@class = 'emptyrow')][4]" "xpath_element" should not exist
And I set the "First name" column aggregation to "Comma separated values"
And the following should exist in the "reportbuilder-table" table:
| -1- | -2- |
| Admin | User |
| Ben, Bill, Bill | Richie |
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2021111600.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2021111600.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.0dev+ (Build: 20211116)'; // Human-friendly version name
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment