MDL-82529 reportbuilder: Move functions from datasource to base class

- Methods add_columns_from_entity(), add_filters_from_entity() and
report_element_search() have been moved from
\core_reportbuilder\datasource class to \core_reportbuilder\base class
in order to be available also for system reports
This commit is contained in:
David Carrillo 2024-07-18 16:45:41 +02:00
parent 554a790bf0
commit 5c135b614d
No known key found for this signature in database
GPG Key ID: 250DD1C373361672
3 changed files with 103 additions and 94 deletions

View File

@ -0,0 +1,9 @@
issueNumber: MDL-82529
notes:
core_reportbuilder:
- message: >-
Methods add_columns_from_entity(), add_filters_from_entity() and
report_element_search() have been moved from
\core_reportbuilder\datasource class to \core_reportbuilder\base class
in order to be available also for system reports
type: improved

View File

@ -47,41 +47,6 @@ abstract class datasource extends base {
/** @var array $activeconditions */
private $activeconditions;
/**
* Add columns from the given entity name to be available to use in a custom report
*
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
*
* @param string $entityname
* @param string[] $include Include only these columns, if omitted then include all
* @param string[] $exclude Exclude these columns, if omitted then exclude none
* @throws coding_exception If both $include and $exclude are non-empty
*/
final protected function add_columns_from_entity(string $entityname, array $include = [], array $exclude = []): void {
if (!empty($include) && !empty($exclude)) {
throw new coding_exception('Cannot specify columns to include and exclude simultaneously');
}
$entity = $this->get_entity($entityname);
// Retrieve filtered columns from entity, respecting given $include/$exclude parameters.
$columns = array_filter($entity->get_columns(), function(column $column) use ($include, $exclude): bool {
if (!empty($include)) {
return $this->report_element_search($column->get_name(), $include);
}
if (!empty($exclude)) {
return !$this->report_element_search($column->get_name(), $exclude);
}
return true;
});
foreach ($columns as $column) {
$this->add_column($column);
}
}
/**
* Add default datasource columns to the report
*
@ -180,41 +145,6 @@ abstract class datasource extends base {
return $this->activecolumns['values'];
}
/**
* Add filters from the given entity name to be available to use in a custom report
*
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
*
* @param string $entityname
* @param string[] $include Include only these filters, if omitted then include all
* @param string[] $exclude Exclude these filters, if omitted then exclude none
* @throws coding_exception If both $include and $exclude are non-empty
*/
final protected function add_filters_from_entity(string $entityname, array $include = [], array $exclude = []): void {
if (!empty($include) && !empty($exclude)) {
throw new coding_exception('Cannot specify filters to include and exclude simultaneously');
}
$entity = $this->get_entity($entityname);
// Retrieve filtered filters from entity, respecting given $include/$exclude parameters.
$filters = array_filter($entity->get_filters(), function(filter $filter) use ($include, $exclude): bool {
if (!empty($include)) {
return $this->report_element_search($filter->get_name(), $include);
}
if (!empty($exclude)) {
return !$this->report_element_search($filter->get_name(), $exclude);
}
return true;
});
foreach ($filters as $filter) {
$this->add_filter($filter);
}
}
/**
* Add default datasource filters to the report
*
@ -421,28 +351,4 @@ abstract class datasource extends base {
final public static function report_elements_modified(int $reportid): void {
self::$elementsmodified[$reportid] = microtime(true);
}
/**
* Search for given element within list of search items, supporting '*' wildcards
*
* @param string $element
* @param string[] $search
* @return bool
*/
private function report_element_search(string $element, array $search): bool {
foreach ($search as $item) {
// Simple matching.
if ($element === $item) {
return true;
}
// Wildcard matching.
if (strpos($item, '*') !== false) {
$pattern = '/^' . str_replace('\*', '.*', preg_quote($item)) . '$/';
return (bool) preg_match($pattern, $element);
}
}
return false;
}
}

View File

@ -374,6 +374,41 @@ abstract class base {
return $this->add_column($this->get_entity($entityname)->get_column($columnname));
}
/**
* Add columns from the given entity name to be available to use in a custom report
*
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
*
* @param string $entityname
* @param string[] $include Include only these columns, if omitted then include all
* @param string[] $exclude Exclude these columns, if omitted then exclude none
* @throws coding_exception If both $include and $exclude are non-empty
*/
final protected function add_columns_from_entity(string $entityname, array $include = [], array $exclude = []): void {
if (!empty($include) && !empty($exclude)) {
throw new coding_exception('Cannot specify columns to include and exclude simultaneously');
}
$entity = $this->get_entity($entityname);
// Retrieve filtered columns from entity, respecting given $include/$exclude parameters.
$columns = array_filter($entity->get_columns(), function(column $column) use ($include, $exclude): bool {
if (!empty($include)) {
return $this->report_element_search($column->get_name(), $include);
}
if (!empty($exclude)) {
return !$this->report_element_search($column->get_name(), $exclude);
}
return true;
});
foreach ($columns as $column) {
$this->add_column($column);
}
}
/**
* Add given columns to the report from one or more entities
*
@ -636,6 +671,41 @@ abstract class base {
return $this->add_filter($this->get_entity($entityname)->get_filter($filtername));
}
/**
* Add filters from the given entity name to be available to use in a custom report
*
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
*
* @param string $entityname
* @param string[] $include Include only these filters, if omitted then include all
* @param string[] $exclude Exclude these filters, if omitted then exclude none
* @throws coding_exception If both $include and $exclude are non-empty
*/
final protected function add_filters_from_entity(string $entityname, array $include = [], array $exclude = []): void {
if (!empty($include) && !empty($exclude)) {
throw new coding_exception('Cannot specify filters to include and exclude simultaneously');
}
$entity = $this->get_entity($entityname);
// Retrieve filtered filters from entity, respecting given $include/$exclude parameters.
$filters = array_filter($entity->get_filters(), function(filter $filter) use ($include, $exclude): bool {
if (!empty($include)) {
return $this->report_element_search($filter->get_name(), $include);
}
if (!empty($exclude)) {
return !$this->report_element_search($filter->get_name(), $exclude);
}
return true;
});
foreach ($filters as $filter) {
$this->add_filter($filter);
}
}
/**
* Add given filters to the report from one or more entities
*
@ -829,4 +899,28 @@ abstract class base {
public function get_attributes(): array {
return $this->attributes;
}
/**
* Search for given element within list of search items, supporting '*' wildcards
*
* @param string $element
* @param string[] $search
* @return bool
*/
final protected function report_element_search(string $element, array $search): bool {
foreach ($search as $item) {
// Simple matching.
if ($element === $item) {
return true;
}
// Wildcard matching.
if (strpos($item, '*') !== false) {
$pattern = '/^' . str_replace('\*', '.*', preg_quote($item)) . '$/';
return (bool) preg_match($pattern, $element);
}
}
return false;
}
}