Skip to content

Commit

Permalink
Merge branch 'MDL-72163-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Aug 24, 2021
2 parents 640c045 + d55aeee commit 103027b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
37 changes: 37 additions & 0 deletions admin/classes/local/settings/linkable_settings_page.php
@@ -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;
}
45 changes: 42 additions & 3 deletions lib/adminlib.php
Expand Up @@ -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
Expand Down Expand Up @@ -770,7 +772,7 @@ public function add($destinationname, $something, $beforesibling = null);
*
* @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;
Expand Down Expand Up @@ -811,6 +813,20 @@ public function __construct($name, $visiblename, $hidden=false) {
$this->hidden = $hidden;
}

/**
* Get the URL to view this settings page.
*
* @return moodle_url
*/
public function get_settings_page_url(): moodle_url {
return new moodle_url(
'/admin/category.php',
[
'category' => $this->name,
]
);
}

/**
* Returns a reference to the part_of_admin_tree object with internal name $name.
*
Expand Down Expand Up @@ -1192,7 +1208,7 @@ public function purge_children($requirefulltree) {
*
* @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;
Expand Down Expand Up @@ -1242,6 +1258,15 @@ public function __construct($name, $visiblename, $url, $req_capability='moodle/s
$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.
*
Expand Down Expand Up @@ -1413,7 +1438,7 @@ public static function prepare_for_javascript($dependencies) {
*
* @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;
Expand Down Expand Up @@ -1465,6 +1490,20 @@ public function __construct($name, $visiblename, $req_capability='moodle/site:co
$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
*
Expand Down
13 changes: 6 additions & 7 deletions lib/classes/plugininfo/base.php
Expand Up @@ -503,19 +503,18 @@ public function get_settings_section_name() {
*
* @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 && $settings instanceof \admin_settingpage) {
return new moodle_url('/admin/settings.php', array('section' => $section));
} else if ($settings && $settings instanceof \admin_externalpage) {
return new moodle_url($settings->url);
} else {
return null;
if ($settings && $settings instanceof \core_admin\local\settings\linkable_settings_page) {
return $settings->get_settings_page_url();
}

return null;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions lib/navigationlib.php
Expand Up @@ -4377,12 +4377,13 @@ protected function load_administration_settings(navigation_node $referencebranch
// 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
Expand Down

0 comments on commit 103027b

Please sign in to comment.