Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/admin/ -> settings.php (source)

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

   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          print_error('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
  27      }
  28      die;
  29  }
  30  
  31  if (!($settingspage->check_access())) {
  32      print_error('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      $visiblepathtosection = array_reverse($settingspage->visiblepath);
 133  
 134      $PAGE->set_title("$SITE->shortname: " . implode(": ",$visiblepathtosection));
 135      $PAGE->set_heading($SITE->fullname);
 136      echo $OUTPUT->header();
 137  
 138      if ($errormsg !== '') {
 139          echo $OUTPUT->notification($errormsg);
 140  
 141      } else if ($statusmsg !== '') {
 142          echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 143      }
 144  
 145      // ---------------------------------------------------------------------------------------------------------------
 146  
 147      $pageparams = $PAGE->url->params();
 148      $context = [
 149          'actionurl' => $PAGE->url->out(false),
 150          'params' => array_map(function($param) use ($pageparams) {
 151              return [
 152                  'name' => $param,
 153                  'value' => $pageparams[$param]
 154              ];
 155          }, array_keys($pageparams)),
 156          'sesskey' => sesskey(),
 157          'return' => $return,
 158          'title' => $settingspage->visiblename,
 159          'settings' => $settingspage->output_html(),
 160          'showsave' => $settingspage->show_save()
 161      ];
 162  
 163      echo $OUTPUT->render_from_template('core_admin/settings', $context);
 164  }
 165  
 166  // Add the form change checker.
 167  $PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['adminsettings']);
 168  
 169  if ($settingspage->has_dependencies()) {
 170      $opts = [
 171          'dependencies' => $settingspage->get_dependencies_for_javascript()
 172      ];
 173      $PAGE->requires->js_call_amd('core/showhidesettings', 'init', [$opts]);
 174  }
 175  
 176  echo $OUTPUT->footer();