Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Select site administrators.
  19   *
  20   * @package    core_role
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../config.php');
  26  require_once($CFG->libdir.'/adminlib.php');
  27  
  28  $addusersaction = optional_param('add', false, PARAM_BOOL);
  29  $addusers = optional_param('addusers', '', PARAM_SEQUENCE);
  30  $removeusersaction = optional_param('remove', false, PARAM_BOOL);
  31  $removeusers = optional_param('removeusers', '', PARAM_SEQUENCE);
  32  
  33  $PAGE->set_url('/admin/roles/admins.php');
  34  
  35  admin_externalpage_setup('admins');
  36  if (!is_siteadmin()) {
  37      die;
  38  }
  39  
  40  $admisselector = new core_role_admins_existing_selector();
  41  $potentialadmisselector = new core_role_admins_potential_selector();
  42  
  43  if ($addusersaction) {
  44      if ($userstoadd = $potentialadmisselector->get_selected_users()) {
  45          $usernames = array_map(static function(stdClass $user) use ($potentialadmisselector): string {
  46              return $potentialadmisselector->output_user($user);
  47          }, $userstoadd);
  48  
  49          $userids = implode(',', array_keys($usernames));
  50  
  51          echo $OUTPUT->header();
  52          echo $OUTPUT->confirm(get_string('confirmaddadmins', 'core_role') . html_writer::alist($usernames),
  53              new moodle_url('/admin/roles/admins.php', ['addusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url);
  54          echo $OUTPUT->footer();
  55          die;
  56      }
  57  
  58  } else if ($removeusersaction) {
  59      if ($userstoremove = $admisselector->get_selected_users()) {
  60  
  61          // Can not remove self.
  62          $userstoremove = array_filter($userstoremove, static function(int $userid): bool {
  63              global $USER;
  64              return $userid != $USER->id;
  65          }, ARRAY_FILTER_USE_KEY);
  66  
  67          if ($userstoremove) {
  68              $usernames = array_map(static function(stdClass $user) use ($admisselector): string {
  69                  return $admisselector->output_user($user);
  70              }, $userstoremove);
  71  
  72              $userids = implode(',', array_keys($usernames));
  73  
  74              echo $OUTPUT->header();
  75              echo $OUTPUT->confirm(get_string('confirmremoveadmins', 'core_role') . html_writer::alist($usernames),
  76                  new moodle_url('/admin/roles/admins.php', ['removeusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url);
  77              echo $OUTPUT->footer();
  78              die;
  79          }
  80      }
  81  
  82  } else if (optional_param('main', false, PARAM_BOOL) && confirm_sesskey()) {
  83      // Setting main administrator will choose the first selected user in the case of multiple selections.
  84      if ($newmain = $admisselector->get_selected_users()) {
  85          $newmain = reset($newmain);
  86          $newmain = $newmain->id;
  87          $admins = array();
  88          foreach (explode(',', $CFG->siteadmins) as $admin) {
  89              $admin = (int)$admin;
  90              if ($admin) {
  91                  $admins[$admin] = $admin;
  92              }
  93          }
  94  
  95          if (isset($admins[$newmain])) {
  96              $logstringold = implode(', ', $admins);
  97  
  98              unset($admins[$newmain]);
  99              array_unshift($admins, $newmain);
 100  
 101              $logstringnew = implode(', ', $admins);
 102  
 103              set_config('siteadmins', implode(',', $admins));
 104              add_to_config_log('siteadmins', $logstringold, $logstringnew, null);
 105  
 106              redirect($PAGE->url);
 107          }
 108      }
 109  
 110  } else if ($addusers && confirm_sesskey()) {
 111      $admins = array();
 112      foreach (explode(',', $CFG->siteadmins) as $admin) {
 113          $admin = (int)$admin;
 114          if ($admin) {
 115              $admins[$admin] = $admin;
 116          }
 117      }
 118  
 119      $logstringold = implode(', ', $admins);
 120  
 121      foreach (explode(',', $addusers) as $userid) {
 122          $admins[$userid] = $userid;
 123      }
 124  
 125      $logstringnew = implode(', ', $admins);
 126  
 127      set_config('siteadmins', implode(',', $admins));
 128      add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
 129  
 130      redirect($PAGE->url);
 131  
 132  } else if ($removeusers && confirm_sesskey()) {
 133      $admins = array();
 134      foreach (explode(',', $CFG->siteadmins) as $admin) {
 135          $admin = (int)$admin;
 136          if ($admin) {
 137              $admins[$admin] = $admin;
 138          }
 139      }
 140  
 141      $logstringold = implode(', ', $admins);
 142  
 143      // Can not remove self.
 144      foreach (explode(',', $removeusers) as $userid) {
 145          if ($userid != $USER->id) {
 146              unset($admins[$userid]);
 147          }
 148      }
 149  
 150      $logstringnew = implode(', ', $admins);
 151  
 152      set_config('siteadmins', implode(',', $admins));
 153      add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
 154  
 155      redirect($PAGE->url);
 156  }
 157  
 158  // Print header.
 159  echo $OUTPUT->header();
 160  ?>
 161  
 162  <div id="addadmisform">
 163      <h3 class="main"><?php print_string('manageadmins', 'core_role'); ?></h3>
 164  
 165      <form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
 166      <div>
 167      <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
 168  
 169      <table class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
 170      <tr>
 171        <td id='existingcell'>
 172            <p>
 173              <label for="removeselect"><?php print_string('existingadmins', 'core_role'); ?></label>
 174            </p>
 175            <?php $admisselector->display(); ?>
 176            </td>
 177        <td id="buttonscell">
 178          <p class="arrow_button">
 179              <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
 180                     title="<?php print_string('add'); ?>" class="btn btn-secondary"/><br />
 181              <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
 182                     title="<?php print_string('remove'); ?>" class="btn btn-secondary"/><br />
 183              <input name="main" id="main" type="submit" value="<?php echo get_string('mainadminset', 'core_role'); ?>"
 184                     title="<?php print_string('mainadminset', 'core_role'); ?>" class="btn btn-secondary"/>
 185          </p>
 186        </td>
 187        <td id="potentialcell">
 188            <p>
 189              <label for="addselect"><?php print_string('users'); ?></label>
 190            </p>
 191            <?php $potentialadmisselector->display(); ?>
 192        </td>
 193      </tr>
 194      </table>
 195      </div>
 196      </form>
 197  </div>
 198  
 199  <?php
 200  
 201  echo $OUTPUT->footer();