From d55aeee10ca8c9c0104d34d7a88c2519e8da27af Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 16 Jul 2021 13:48:18 +0800 Subject: [PATCH] MDL-72163 admin: Create interface for settings with a url --- .../local/settings/linkable_settings_page.php | 37 ++++++++++++++++++++++ lib/adminlib.php | 33 ++++++++++++++++--- lib/classes/plugininfo/base.php | 20 +++--------- lib/navigationlib.php | 13 ++++---- 4 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 admin/classes/local/settings/linkable_settings_page.php diff --git a/admin/classes/local/settings/linkable_settings_page.php b/admin/classes/local/settings/linkable_settings_page.php new file mode 100644 index 00000000000..0b551f34804 --- /dev/null +++ b/admin/classes/local/settings/linkable_settings_page.php @@ -0,0 +1,37 @@ +. + +/** + * A settings page which can be viewed with a link directly. + * + * @package core_admin + * @copyright 2021 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_admin\local\settings; + +use moodle_url; + +interface linkable_settings_page { + + /** + * Get the URL to view this settings page. + * + * @return moodle_url + */ + public function get_settings_page_url(): moodle_url; +} diff --git a/lib/adminlib.php b/lib/adminlib.php index c38965edba9..135538fa233 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -102,6 +102,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core_admin\local\settings\linkable_settings_page; + defined('MOODLE_INTERNAL') || die(); /// Add libraries @@ -769,7 +771,7 @@ interface parentable_part_of_admin_tree extends part_of_admin_tree { * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class admin_category implements parentable_part_of_admin_tree { +class admin_category implements parentable_part_of_admin_tree, linkable_settings_page { /** @var part_of_admin_tree[] An array of part_of_admin_tree objects that are this object's children */ protected $children; @@ -811,7 +813,7 @@ class admin_category implements parentable_part_of_admin_tree { } /** - * Get the URL to view this page. + * Get the URL to view this settings page. * * @return moodle_url */ @@ -1205,7 +1207,7 @@ class admin_root extends admin_category { * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class admin_externalpage implements part_of_admin_tree { +class admin_externalpage implements part_of_admin_tree, linkable_settings_page { /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ public $name; @@ -1256,6 +1258,15 @@ class admin_externalpage implements part_of_admin_tree { } /** + * Get the URL to view this settings page. + * + * @return moodle_url + */ + public function get_settings_page_url(): moodle_url { + return new moodle_url($this->url); + } + + /** * Returns a reference to the part_of_admin_tree object with internal name $name. * * @param string $name The internal name of the object we want. @@ -1426,7 +1437,7 @@ class admin_settingdependency { * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class admin_settingpage implements part_of_admin_tree { +class admin_settingpage implements part_of_admin_tree, linkable_settings_page { /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ public $name; @@ -1479,6 +1490,20 @@ class admin_settingpage implements part_of_admin_tree { } /** + * Get the URL to view this page. + * + * @return moodle_url + */ + public function get_settings_page_url(): moodle_url { + return new moodle_url( + '/admin/settings.php', + [ + 'section' => $this->name, + ] + ); + } + + /** * see admin_category * * @param string $name diff --git a/lib/classes/plugininfo/base.php b/lib/classes/plugininfo/base.php index f32498f40b6..1e0c7c8f5f8 100644 --- a/lib/classes/plugininfo/base.php +++ b/lib/classes/plugininfo/base.php @@ -503,27 +503,17 @@ abstract class base { * * @return null|moodle_url */ - public function get_settings_url() { + public function get_settings_url(): ?moodle_url { $section = $this->get_settings_section_name(); if ($section === null) { return null; } - $settings = admin_get_root()->locate($section); - if ($settings) { - if ($settings instanceof \admin_settingpage) { - return new moodle_url('/admin/settings.php', [ - 'section' => $section, - ]); - } - - if ($settings instanceof \admin_externalpage) { - return new moodle_url($settings->url); - } - if ($settings instanceof \admin_category) { - return $settings->get_settings_page_url(); - } + $settings = admin_get_root()->locate($section); + if ($settings && $settings instanceof \core_admin\local\settings\linkable_settings_page) { + return $settings->get_settings_page_url(); } + return null; } diff --git a/lib/navigationlib.php b/lib/navigationlib.php index de3f0bd75a5..6df89107bcb 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -4377,12 +4377,13 @@ class settings_navigation extends navigation_node { // Now we need to display it and any children it may have $url = null; $icon = null; - if ($adminbranch instanceof admin_settingpage) { - $url = new moodle_url('/'.$CFG->admin.'/settings.php', array('section'=>$adminbranch->name)); - } else if ($adminbranch instanceof admin_externalpage) { - $url = $adminbranch->url; - } else if (!empty($CFG->linkadmincategories) && $adminbranch instanceof admin_category) { - $url = new moodle_url('/'.$CFG->admin.'/category.php', array('category' => $adminbranch->name)); + + if ($adminbranch instanceof \core_admin\local\settings\linkable_settings_page) { + if (empty($CFG->linkadmincategories) && $adminbranch instanceof admin_category) { + $url = null; + } else { + $url = $adminbranch->get_settings_page_url(); + } } // Add the branch -- 2.11.4.GIT