MDL-70795 reportbuilder: custom report filters editor.

Implement module for filters editing, allowing the report
editor to define which filters are available when users
are viewing the report.

These filters allow those users viewing the report to further
limit the data being displayed (after conditions have been
applied), by selecting their own values for each filter.

Co-authored-by: Paul Holden <[email protected]>
This commit is contained in:
David Matamoros
2021-10-19 13:56:04 +01:00
committed by Paul Holden
co-authored by Paul Holden
parent ab596ec11a
commit 09b69d04e3
34 changed files with 1573 additions and 40 deletions
@@ -28,6 +28,7 @@ use core_reportbuilder\local\filters\base;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\base as base_report;
use core_reportbuilder\local\report\filter;
use core\output\notification;
defined('MOODLE_INTERNAL') || die;
@@ -51,6 +52,9 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl
/** @var string $groupbysql */
protected $groupbysql = '';
/** @var bool $applyfilters */
protected $applyfilters = true;
/**
* Initialises table SQL properties
*
@@ -81,19 +85,23 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl
}
// For each filter, we also need to apply their values (will differ according to user viewing the report).
$filtervalues = $this->report->get_filter_values();
foreach ($this->report->get_active_filters() as $filter) {
[$filtersql, $filterparams] = $this->get_filter_sql($filter, $filtervalues);
if ($filtersql !== '') {
$joins = array_merge($joins, $filter->get_joins());
$wheres[] = "({$filtersql})";
$params = array_merge($params, $filterparams);
if ($this->applyfilters) {
$filtervalues = $this->report->get_filter_values();
foreach ($this->report->get_active_filters() as $filter) {
[$filtersql, $filterparams] = $this->get_filter_sql($filter, $filtervalues);
if ($filtersql !== '') {
$joins = array_merge($joins, $filter->get_joins());
$wheres[] = "({$filtersql})";
$params = array_merge($params, $filterparams);
}
}
}
$wheresql = '1=1';
// Join all the filters into a SQL WHERE clause, falling back to all records.
if (!empty($wheres)) {
$wheresql = implode(' AND ', $wheres);
} else {
$wheresql = '1=1';
}
if (!empty($groupby)) {
@@ -115,6 +123,16 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl
) {$counttablealias}", $params);
}
/**
* Whether active filters should be applied to the report, defaults to true except in the case where we are editing a report
* and we do not want filters to be applied to it
*
* @param bool $applyfilters
*/
public function set_filters_applied(bool $applyfilters): void {
$this->applyfilters = $applyfilters;
}
/**
* Return SQL fragments from given filter instance suitable for inclusion in table SQL
*
@@ -169,4 +187,18 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl
public function guess_base_url(): void {
$this->baseurl = new moodle_url('/');
}
/**
* Override print_nothing_to_display to modity the output styles.
*/
public function print_nothing_to_display() {
global $OUTPUT;
echo $this->get_dynamic_table_html_start();
echo $this->render_reset_button();
echo $OUTPUT->render(new notification(get_string('nothingtodisplay'), notification::NOTIFY_INFO));
echo $this->get_dynamic_table_html_end();
}
}