From b3a50f4a8255c3332dcafdd8559d2df3f01dca5d Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 6 Jan 2023 12:01:54 +0000 Subject: [PATCH] MDL-76815 reportbuilder: avoid duplication getting audience component. The component the audience belongs to is used by the cards exporter for rendering the editing interface. Avoid duplication in determining this value by defining appropriate method in the base class. --- .../custom_report_audience_cards_exporter.php | 10 +----- .../classes/local/audiences/base.php | 32 ++++++++++++++----- .../classes/local/helpers/audience.php | 10 +----- reportbuilder/upgrade.txt | 1 + 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/reportbuilder/classes/external/custom_report_audience_cards_exporter.php b/reportbuilder/classes/external/custom_report_audience_cards_exporter.php index 561e140f117..9a72e02b9e7 100644 --- a/reportbuilder/classes/external/custom_report_audience_cards_exporter.php +++ b/reportbuilder/classes/external/custom_report_audience_cards_exporter.php @@ -20,7 +20,6 @@ namespace core_reportbuilder\external; use core_collator; use core_component; -use core_plugin_manager; use renderer_base; use core_reportbuilder\local\audiences\base; @@ -52,15 +51,8 @@ class custom_report_audience_cards_exporter extends custom_report_menu_cards_exp continue; } - // The name of each card will be the component the audience belongs to. - [$component] = explode('\\', $class); - if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) { - $componentname = $plugininfo->displayname; - } else { - $componentname = get_string('site'); - } - // New menu card per component. + $componentname = $audience->get_component_displayname(); if (!array_key_exists($componentname, $menucards)) { $menucards[$componentname] = [ 'name' => $componentname, diff --git a/reportbuilder/classes/local/audiences/base.php b/reportbuilder/classes/local/audiences/base.php index 834e24971a1..ffecfe62532 100644 --- a/reportbuilder/classes/local/audiences/base.php +++ b/reportbuilder/classes/local/audiences/base.php @@ -22,6 +22,7 @@ use core_plugin_manager; use MoodleQuickForm; use stdClass; use core\output\notification; +use core_reportbuilder\local\helpers\database; use core_reportbuilder\local\models\audience; use core_reportbuilder\report_access_exception; @@ -89,11 +90,27 @@ abstract class base { return $instance; } + /** + * Return display name of the component the audience belongs to + * + * @return string + */ + final public function get_component_displayname(): string { + [$component] = explode('\\', get_class($this)); + + if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) { + return $plugininfo->displayname; + } + + // Return generic site text for those audiences belonging to core subsystems. + return get_string('site'); + } + /** * Helps to build SQL to retrieve users that matches the current audience * - * Implementations must use api::generate_alias() for table/column aliases - * and api::generate_param_name() for named parameters + * Implementations must use {@see database::generate_alias} and {@see database::generate_param_name} for table/column + * aliases and parameter names * * @param string $usertablealias * @return array array of three elements [$join, $where, $params] @@ -104,15 +121,14 @@ abstract class base { * Returns string for audience category. * * @return string + * + * @deprecated since Moodle 4.2 - please do not use this function any more, {@see get_component_displayname} */ final public function get_category(): string { - [$component] = explode('\\', get_class($this)); + debugging('The function ' . __FUNCTION__ . '() is deprecated, please do not use it any more. ' . + 'See class \'get_component_displayname\' method for replacement', DEBUG_DEVELOPER); - if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) { - return $plugininfo->displayname; - } - - return get_string('site'); + return $this->get_component_displayname(); } /** diff --git a/reportbuilder/classes/local/helpers/audience.php b/reportbuilder/classes/local/helpers/audience.php index 6019c4675f0..b0dd62ed9a4 100644 --- a/reportbuilder/classes/local/helpers/audience.php +++ b/reportbuilder/classes/local/helpers/audience.php @@ -23,7 +23,6 @@ use context; use context_system; use core_collator; use core_component; -use core_plugin_manager; use core_reportbuilder\local\audiences\base; use core_reportbuilder\local\models\audience as audience_model; @@ -242,14 +241,7 @@ class audience { foreach ($audiences as $class => $path) { $audienceclass = $class::instance(); if (is_subclass_of($class, base::class) && $audienceclass->user_can_add()) { - [$component] = explode('\\', $class); - - if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) { - $componentname = $plugininfo->displayname; - } else { - $componentname = get_string('site'); - } - + $componentname = $audienceclass->get_component_displayname(); $sources[$componentname][$class] = $audienceclass->get_name(); } } diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index da502fc9e4d..394df967e69 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -14,6 +14,7 @@ Information provided here is intended especially for developers. during tests * The `datasource_stress_test_columns` test helper now enables sorting on those columns that support it * The `create_[column|filter|condition]` test generator methods now allow for setting all persistent properties +* The `get_category` method of the base audience class has been deprecated, callers should instead use `get_component_displayname` === 4.1 ===