Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/admin/ -> settings.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  require_once('../config.php');
   4  require_once($CFG->libdir.'/adminlib.php');
   5  
   6  $section = required_param('section', PARAM_SAFEDIR);
   7  $return = optional_param('return','', PARAM_ALPHA);
   8  $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
   9  
  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();
  18  
  19  $adminroot = admin_get_root(); // need all settings
  20  $settingspage = $adminroot->locate($section, true);
  21  
  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          throw new \moodle_exception('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
  27      }
  28      die;
  29  }
  30  
  31  if (!($settingspage->check_access())) {
  32      throw new \moodle_exception('accessdenied', 'admin');
  33      die;
  34  }
  35  
  36  // If the context in the admin_settingpage object is explicitly defined and it is not system, reset the current
  37  // page context and use that one instead. This ensures that the proper navigation is displayed and highlighted.
  38  if ($settingspage->context && !$settingspage->context instanceof \context_system) {
  39      $PAGE->set_context($settingspage->context);
  40  }
  41  
  42  $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
  43  // Display the admin search input element in the page header if the user has the capability to change the site
  44  // configuration and the current page context is system.
  45  if ($hassiteconfig && $PAGE->context instanceof \context_system) {
  46      $PAGE->add_header_action($OUTPUT->render_from_template('core_admin/header_search_input', [
  47          'action' => new moodle_url('/admin/search.php'),
  48      ]));
  49  }
  50  
  51  /// WRITING SUBMITTED DATA (IF ANY) -------------------------------------------------------------------------------
  52  
  53  $statusmsg = '';
  54  $errormsg  = '';
  55  
  56  // Form is submitted with changed settings. Do not want to execute when modifying a block.
  57  if ($data = data_submitted() and confirm_sesskey() and isset($data->action) and $data->action == 'save-settings') {
  58  
  59      $count = admin_write_settings($data);
  60      // Regardless of whether any setting change was written (a positive count), check validation errors for those that didn't.
  61      if (empty($adminroot->errors)) {
  62          // No errors. Did we change any setting? If so, then redirect with success.
  63          if ($count) {
  64              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  65          }
  66          // We didn't change a setting.
  67          switch ($return) {
  68              case 'site': redirect("$CFG->wwwroot/");
  69              case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
  70          }
  71          redirect($PAGE->url);
  72      } else {
  73          $errormsg = get_string('errorwithsettings', 'admin');
  74          $firsterror = reset($adminroot->errors);
  75      }
  76      $settingspage = $adminroot->locate($section, true);
  77  }
  78  
  79  if ($PAGE->user_allowed_editing() && $adminediting != -1) {
  80      $USER->editing = $adminediting;
  81  }
  82  
  83  /// print header stuff ------------------------------------------------------------
  84  if (empty($SITE->fullname)) {
  85      $PAGE->set_title($settingspage->visiblename);
  86      $PAGE->set_heading($settingspage->visiblename);
  87  
  88      echo $OUTPUT->header();
  89      echo $OUTPUT->box(get_string('configintrosite', 'admin'));
  90  
  91      if ($errormsg !== '') {
  92          echo $OUTPUT->notification($errormsg);
  93  
  94      } else if ($statusmsg !== '') {
  95          echo $OUTPUT->notification($statusmsg, 'notifysuccess');
  96      }
  97  
  98      // ---------------------------------------------------------------------------------------------------------------
  99  
 100      $pageparams = $PAGE->url->params();
 101      $context = [
 102          'actionurl' => $PAGE->url->out(false),
 103          'params' => array_map(function($param) use ($pageparams) {
 104              return [
 105                  'name' => $param,
 106                  'value' => $pageparams[$param]
 107              ];
 108          }, array_keys($pageparams)),
 109          'sesskey' => sesskey(),
 110          'return' => $return,
 111          'title' => null,
 112          'settings' => $settingspage->output_html(),
 113          'showsave' => true
 114      ];
 115  
 116      echo $OUTPUT->render_from_template('core_admin/settings', $context);
 117  
 118  } else {
 119      if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
 120          $url = clone($PAGE->url);
 121          if ($PAGE->user_is_editing()) {
 122              $caption = get_string('blockseditoff');
 123              $url->param('adminedit', 'off');
 124          } else {
 125              $caption = get_string('blocksediton');
 126              $url->param('adminedit', 'on');
 127          }
 128          $buttons = $OUTPUT->single_button($url, $caption, 'get');
 129          $PAGE->set_button($buttons);
 130      }
 131  
 132      $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $settingspage->visiblepath));
 133      $PAGE->set_heading($SITE->fullname);
 134      echo $OUTPUT->header();
 135  
 136      if ($errormsg !== '') {
 137          echo $OUTPUT->notification($errormsg);
 138  
 139      } else if ($statusmsg !== '') {
 140          echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 141      }
 142  
 143      // ---------------------------------------------------------------------------------------------------------------
 144  
 145      $pageparams = $PAGE->url->params();
 146      $context = [
 147          'actionurl' => $PAGE->url->out(false),
 148          'params' => array_map(function($param) use ($pageparams) {
 149              return [
 150                  'name' => $param,
 151                  'value' => $pageparams[$param]
 152              ];
 153          }, array_keys($pageparams)),
 154          'sesskey' => sesskey(),
 155          'return' => $return,
 156          'title' => $settingspage->visiblename,
 157          'settings' => $settingspage->output_html(),
 158          'showsave' => $settingspage->show_save()
 159      ];
 160  
 161      echo $OUTPUT->render_from_template('core_admin/settings', $context);
 162  }
 163  
 164  // Add the form change checker.
 165  $PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['adminsettings']);
 166  
 167  if ($settingspage->has_dependencies()) {
 168      $opts = [
 169          'dependencies' => $settingspage->get_dependencies_for_javascript()
 170      ];
 171      $PAGE->requires->js_call_amd('core/showhidesettings', 'init', [$opts]);
 172  }
 173  
 174  echo $OUTPUT->footer();