Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
  • /admin/ -> settings.php (source)

    Differences Between: [Versions 310 and 34] [Versions 310 and 35] [Versions 310 and 36] [Versions 34 and 310] [Versions 35 and 310] [Versions 36 and 310]

       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  /// WRITING SUBMITTED DATA (IF ANY) -------------------------------------------------------------------------------
      37  
      38  $statusmsg = '';
      39  $errormsg  = '';
      40  
      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') {
      43  
      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);
      50          }
      51          // We didn't change a setting.
      52          switch ($return) {
      53              case 'site': redirect("$CFG->wwwroot/");
      54              case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
      55          }
      56          redirect($PAGE->url);
      57      } else {
      58          $errormsg = get_string('errorwithsettings', 'admin');
      59          $firsterror = reset($adminroot->errors);
      60      }
      61      $settingspage = $adminroot->locate($section, true);
      62  }
      63  
      64  if ($PAGE->user_allowed_editing() && $adminediting != -1) {
      65      $USER->editing = $adminediting;
      66  }
      67  
      68  /// print header stuff ------------------------------------------------------------
      69  if (empty($SITE->fullname)) {
      70      $PAGE->set_title($settingspage->visiblename);
      71      $PAGE->set_heading($settingspage->visiblename);
      72  
      73      echo $OUTPUT->header();
      74      echo $OUTPUT->box(get_string('configintrosite', 'admin'));
      75  
      76      if ($errormsg !== '') {
      77          echo $OUTPUT->notification($errormsg);
      78  
      79      } else if ($statusmsg !== '') {
      80          echo $OUTPUT->notification($statusmsg, 'notifysuccess');
      81      }
      82  
      83      // ---------------------------------------------------------------------------------------------------------------
      84  
      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]
      92              ];
      93          }, array_keys($pageparams)),
      94          'sesskey' => sesskey(),
      95          'return' => $return,
      96          'title' => null,
      97          'settings' => $settingspage->output_html(),
      98          'showsave' => true
      99      ];
     100  
     101      echo $OUTPUT->render_from_template('core_admin/settings', $context);
     102  
     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');
     112          }
     113          $buttons = $OUTPUT->single_button($url, $caption, 'get');
     114          $PAGE->set_button($buttons);
     115      }
     116  
     117      $visiblepathtosection = array_reverse($settingspage->visiblepath);
     118  
     119      $PAGE->set_title("$SITE->shortname: " . implode(": ",$visiblepathtosection));
     120      $PAGE->set_heading($SITE->fullname);
     121      echo $OUTPUT->header();
     122  
     123      if ($errormsg !== '') {
     124          echo $OUTPUT->notification($errormsg);
     125  
     126      } else if ($statusmsg !== '') {
     127          echo $OUTPUT->notification($statusmsg, 'notifysuccess');
     128      }
     129  
     130      // ---------------------------------------------------------------------------------------------------------------
     131  
     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]
     139              ];
     140          }, array_keys($pageparams)),
     141          'sesskey' => sesskey(),
     142          'return' => $return,
     143          'title' => $settingspage->visiblename,
     144          'settings' => $settingspage->output_html(),
     145          'showsave' => $settingspage->show_save()
     146      ];
     147  
     148      echo $OUTPUT->render_from_template('core_admin/settings', $context);
     149  }
     150  
     151  $PAGE->requires->yui_module('moodle-core-formchangechecker',
     152          'M.core_formchangechecker.init',
     153          array(array(
     154              'formid' => 'adminsettings'
     155          ))
     156  );
     157  $PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
     158  
     159  if ($settingspage->has_dependencies()) {
     160      $opts = [
     161          'dependencies' => $settingspage->get_dependencies_for_javascript()
     162      ];
     163      $PAGE->requires->js_call_amd('core/showhidesettings', 'init', [$opts]);
     164  }
     165  
     166  echo $OUTPUT->footer();