MDL-72163 admin: Create interface for settings with a url

This commit is contained in:
Andrew Nicols
2021-07-16 13:48:18 +08:00
parent 78c96c6f44
commit d55aeee10c
4 changed files with 78 additions and 25 deletions

View File

@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A settings page which can be viewed with a link directly.
*
* @package core_admin
* @copyright 2021 Andrew Lyons <andrew@nicols.co.uk>
* @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;
}

View File

@@ -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;
@@ -1255,6 +1257,15 @@ class admin_externalpage implements part_of_admin_tree {
$this->context = $context;
}
/**
* 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.
*
@@ -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;
@@ -1478,6 +1489,20 @@ class admin_settingpage implements part_of_admin_tree {
$this->context = $context;
}
/**
* 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
*

View File

@@ -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();
}
if ($settings && $settings instanceof \core_admin\local\settings\linkable_settings_page) {
return $settings->get_settings_page_url();
}
return null;
}

View File

@@ -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