Merge branch 'MDL-81084-main' of https://github.com/andrewnicols/moodle
[moodle.git] / admin / category.php
blobe990383cadbd5d5898af56c4725451a325466250
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is used to display a categories sub categories, external pages, and settings.
20 * @since Moodle 2.3
21 * @package admin
22 * @copyright 2011 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
29 $category = required_param('category', PARAM_SAFEDIR);
30 $return = optional_param('return','', PARAM_ALPHA);
31 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
33 require_login(0, false);
34 $PAGE->set_context(context_system::instance());
35 $PAGE->set_url('/admin/category.php', array('category' => $category));
36 $PAGE->set_pagetype('admin-setting-' . $category);
37 $PAGE->set_pagelayout('admin');
38 $PAGE->navigation->clear_cache();
40 $adminroot = admin_get_root(); // need all settings
41 $settingspage = $adminroot->locate($category, true);
43 if (empty($settingspage) or !($settingspage instanceof admin_category)) {
44 throw new \moodle_exception('categoryerror', 'error', "$CFG->wwwroot/$CFG->admin/");
47 if (!($settingspage->check_access())) {
48 throw new \moodle_exception('accessdenied', 'admin');
51 $hassiteconfig = has_capability('moodle/site:config', $PAGE->context);
52 if ($hassiteconfig) {
53 $PAGE->add_header_action($OUTPUT->render_from_template('core_admin/header_search_input', [
54 'action' => new moodle_url('/admin/search.php'),
55 ]));
58 $statusmsg = '';
59 $errormsg = '';
61 if ($data = data_submitted() and confirm_sesskey()) {
62 $count = admin_write_settings($data);
63 if (empty($adminroot->errors)) {
64 // No errors. Did we change any setting? If so, then indicate success.
65 if ($count) {
66 $statusmsg = get_string('changessaved');
67 } else {
68 switch ($return) {
69 case 'site': redirect("$CFG->wwwroot/");
70 case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
73 } else {
74 $errormsg = get_string('errorwithsettings', 'admin');
75 $firsterror = reset($adminroot->errors);
77 $settingspage = $adminroot->locate($category, true);
80 if ($PAGE->user_allowed_editing() && $adminediting != -1) {
81 $USER->editing = $adminediting;
83 $buttons = null;
84 if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
85 $url = clone($PAGE->url);
86 if ($PAGE->user_is_editing()) {
87 $caption = get_string('blockseditoff');
88 $url->param('adminedit', 'off');
89 } else {
90 $caption = get_string('blocksediton');
91 $url->param('adminedit', 'on');
93 $buttons = $OUTPUT->single_button($url, $caption, 'get');
96 $savebutton = false;
97 $outputhtml = '';
98 foreach ($settingspage->children as $childpage) {
99 if ($childpage->is_hidden() || !$childpage->check_access()) {
100 continue;
102 if ($childpage instanceof admin_externalpage) {
103 $outputhtml .= $OUTPUT->heading(html_writer::link($childpage->url, $childpage->visiblename), 3);
104 } else if ($childpage instanceof admin_settingpage) {
105 $outputhtml .= $OUTPUT->heading(html_writer::link(new moodle_url('/'.$CFG->admin.'/settings.php', array('section' => $childpage->name)), $childpage->visiblename), 3);
106 // If its a settings page and has settings lets display them.
107 if (!empty($childpage->settings)) {
108 $outputhtml .= html_writer::start_tag('fieldset', array('class' => 'adminsettings'));
109 foreach ($childpage->settings as $setting) {
110 if (empty($setting->nosave)) {
111 $savebutton = true;
113 $fullname = $setting->get_full_name();
114 if (array_key_exists($fullname, $adminroot->errors)) {
115 $data = $adminroot->errors[$fullname]->data;
116 } else {
117 $data = $setting->get_setting();
119 $outputhtml .= html_writer::tag('div', '<!-- -->', array('class' => 'clearer'));
120 $outputhtml .= $setting->output_html($data);
122 $outputhtml .= html_writer::end_tag('fieldset');
124 } else if ($childpage instanceof admin_category) {
125 $outputhtml .= $OUTPUT->heading(html_writer::link(new moodle_url('/'.$CFG->admin.'/category.php', array('category' => $childpage->name)), get_string('admincategory', 'admin', $childpage->visiblename)), 3);
128 if ($savebutton) {
129 $outputhtml .= html_writer::start_tag('div', array('class' => 'form-buttons'));
130 $outputhtml .= html_writer::empty_tag('input', array('class' => 'btn btn-primary form-submit', 'type' => 'submit', 'value' => get_string('savechanges','admin')));
131 $outputhtml .= html_writer::end_tag('div');
134 $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $settingspage->visiblepath));
135 $PAGE->set_heading($SITE->fullname);
136 if ($buttons) {
137 $PAGE->set_button($buttons);
140 echo $OUTPUT->header();
142 if ($errormsg !== '') {
143 echo $OUTPUT->notification($errormsg);
144 } else if ($statusmsg !== '') {
145 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
148 echo $OUTPUT->heading(get_string('admincategory', 'admin', $settingspage->visiblename), 2);
150 echo html_writer::start_tag('form', array('action' => '', 'method' => 'post', 'id' => 'adminsettings'));
151 echo html_writer::start_tag('div');
152 echo html_writer::input_hidden_params(new moodle_url($PAGE->url, array('sesskey' => sesskey(), 'return' => $return)));
153 echo html_writer::end_tag('div');
154 echo html_writer::start_tag('fieldset');
155 echo html_writer::tag('div', '<!-- -->', array('class' => 'clearer'));
156 echo $outputhtml;
157 echo html_writer::end_tag('fieldset');
158 echo html_writer::end_tag('form');
160 // Add the form change checker.
161 $PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['adminsettings']);
163 echo $OUTPUT->footer();