Merge branch 'MDL-70327-310' of git://github.com/cescobedo/moodle into MOODLE_310_STABLE
[moodle.git] / admin / settings.php
blob0872e0375cc1a88fb76a1a6335c17af551d50100
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
6 $section = required_param('section', PARAM_SAFEDIR);
7 $return = optional_param('return','', PARAM_ALPHA);
8 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
10 /// no guest autologin
11 require_login(0, false);
12 $PAGE->set_context(context_system::instance());
13 $PAGE->set_url('/admin/settings.php', array('section' => $section));
14 $PAGE->set_pagetype('admin-setting-' . $section);
15 $PAGE->set_pagelayout('admin');
16 $PAGE->navigation->clear_cache();
17 navigation_node::require_admin_tree();
19 $adminroot = admin_get_root(); // need all settings
20 $settingspage = $adminroot->locate($section, true);
22 if (empty($settingspage) or !($settingspage instanceof admin_settingpage)) {
23 if (moodle_needs_upgrading()) {
24 redirect(new moodle_url('/admin/index.php'));
25 } else {
26 print_error('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
28 die;
31 if (!($settingspage->check_access())) {
32 print_error('accessdenied', 'admin');
33 die;
36 /// WRITING SUBMITTED DATA (IF ANY) -------------------------------------------------------------------------------
38 $statusmsg = '';
39 $errormsg = '';
41 // Form is submitted with changed settings. Do not want to execute when modifying a block.
42 if ($data = data_submitted() and confirm_sesskey() and isset($data->action) and $data->action == 'save-settings') {
44 $count = admin_write_settings($data);
45 // Regardless of whether any setting change was written (a positive count), check validation errors for those that didn't.
46 if (empty($adminroot->errors)) {
47 // No errors. Did we change any setting? If so, then redirect with success.
48 if ($count) {
49 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
51 // We didn't change a setting.
52 switch ($return) {
53 case 'site': redirect("$CFG->wwwroot/");
54 case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
56 redirect($PAGE->url);
57 } else {
58 $errormsg = get_string('errorwithsettings', 'admin');
59 $firsterror = reset($adminroot->errors);
61 $settingspage = $adminroot->locate($section, true);
64 if ($PAGE->user_allowed_editing() && $adminediting != -1) {
65 $USER->editing = $adminediting;
68 /// print header stuff ------------------------------------------------------------
69 if (empty($SITE->fullname)) {
70 $PAGE->set_title($settingspage->visiblename);
71 $PAGE->set_heading($settingspage->visiblename);
73 echo $OUTPUT->header();
74 echo $OUTPUT->box(get_string('configintrosite', 'admin'));
76 if ($errormsg !== '') {
77 echo $OUTPUT->notification($errormsg);
79 } else if ($statusmsg !== '') {
80 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
83 // ---------------------------------------------------------------------------------------------------------------
85 $pageparams = $PAGE->url->params();
86 $context = [
87 'actionurl' => $PAGE->url->out(false),
88 'params' => array_map(function($param) use ($pageparams) {
89 return [
90 'name' => $param,
91 'value' => $pageparams[$param]
93 }, array_keys($pageparams)),
94 'sesskey' => sesskey(),
95 'return' => $return,
96 'title' => null,
97 'settings' => $settingspage->output_html(),
98 'showsave' => true
101 echo $OUTPUT->render_from_template('core_admin/settings', $context);
103 } else {
104 if ($PAGE->user_allowed_editing()) {
105 $url = clone($PAGE->url);
106 if ($PAGE->user_is_editing()) {
107 $caption = get_string('blockseditoff');
108 $url->param('adminedit', 'off');
109 } else {
110 $caption = get_string('blocksediton');
111 $url->param('adminedit', 'on');
113 $buttons = $OUTPUT->single_button($url, $caption, 'get');
114 $PAGE->set_button($buttons);
117 $visiblepathtosection = array_reverse($settingspage->visiblepath);
119 $PAGE->set_title("$SITE->shortname: " . implode(": ",$visiblepathtosection));
120 $PAGE->set_heading($SITE->fullname);
121 echo $OUTPUT->header();
123 if ($errormsg !== '') {
124 echo $OUTPUT->notification($errormsg);
126 } else if ($statusmsg !== '') {
127 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
130 // ---------------------------------------------------------------------------------------------------------------
132 $pageparams = $PAGE->url->params();
133 $context = [
134 'actionurl' => $PAGE->url->out(false),
135 'params' => array_map(function($param) use ($pageparams) {
136 return [
137 'name' => $param,
138 'value' => $pageparams[$param]
140 }, array_keys($pageparams)),
141 'sesskey' => sesskey(),
142 'return' => $return,
143 'title' => $settingspage->visiblename,
144 'settings' => $settingspage->output_html(),
145 'showsave' => $settingspage->show_save()
148 echo $OUTPUT->render_from_template('core_admin/settings', $context);
151 $PAGE->requires->yui_module('moodle-core-formchangechecker',
152 'M.core_formchangechecker.init',
153 array(array(
154 'formid' => 'adminsettings'
157 $PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
159 if ($settingspage->has_dependencies()) {
160 $opts = [
161 'dependencies' => $settingspage->get_dependencies_for_javascript()
163 $PAGE->requires->js_call_amd('core/showhidesettings', 'init', [$opts]);
166 echo $OUTPUT->footer();