Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
42
if (array_key_exists('siteadmins', $CFG->config_php_settings)) {
43
    echo $OUTPUT->header();
44
    echo $OUTPUT->heading(get_string('manageadmins', 'core_role'), 3);
45
    echo $OUTPUT->notification(get_string('siteadministratorsconfigphp', 'core_role'), \core\output\notification::NOTIFY_INFO);
46
    echo $OUTPUT->box_start();
47
    echo $OUTPUT->paragraph(get_string('existingadmins', 'core_role'));
48
    $admisselector->display();
49
    echo $OUTPUT->box_end();
50
    echo $OUTPUT->footer();
51
    die();
52
}
53
 
54
$potentialadmisselector = new core_role_admins_potential_selector();
55
 
56
if ($addusersaction) {
57
    if ($userstoadd = $potentialadmisselector->get_selected_users()) {
58
        $usernames = array_map(static function(stdClass $user) use ($potentialadmisselector): string {
59
            return $potentialadmisselector->output_user($user);
60
        }, $userstoadd);
61
 
62
        $userids = implode(',', array_keys($usernames));
63
 
64
        echo $OUTPUT->header();
65
        echo $OUTPUT->confirm(get_string('confirmaddadmins', 'core_role') . html_writer::alist($usernames),
66
            new moodle_url('/admin/roles/admins.php', ['addusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url);
67
        echo $OUTPUT->footer();
68
        die;
69
    }
70
 
71
} else if ($removeusersaction) {
72
    if ($userstoremove = $admisselector->get_selected_users()) {
73
 
74
        // Can not remove self.
75
        $userstoremove = array_filter($userstoremove, static function(int $userid): bool {
76
            global $USER;
77
            return $userid != $USER->id;
78
        }, ARRAY_FILTER_USE_KEY);
79
 
80
        if ($userstoremove) {
81
            $usernames = array_map(static function(stdClass $user) use ($admisselector): string {
82
                return $admisselector->output_user($user);
83
            }, $userstoremove);
84
 
85
            $userids = implode(',', array_keys($usernames));
86
 
87
            echo $OUTPUT->header();
88
            echo $OUTPUT->confirm(get_string('confirmremoveadmins', 'core_role') . html_writer::alist($usernames),
89
                new moodle_url('/admin/roles/admins.php', ['removeusers' => $userids, 'sesskey' => sesskey()]), $PAGE->url);
90
            echo $OUTPUT->footer();
91
            die;
92
        }
93
    }
94
 
95
} else if (optional_param('main', false, PARAM_BOOL) && confirm_sesskey()) {
96
    // Setting main administrator will choose the first selected user in the case of multiple selections.
97
    if ($newmain = $admisselector->get_selected_users()) {
98
        $newmain = reset($newmain);
99
        $newmain = $newmain->id;
100
        $admins = array();
101
        foreach (explode(',', $CFG->siteadmins) as $admin) {
102
            $admin = (int)$admin;
103
            if ($admin) {
104
                $admins[$admin] = $admin;
105
            }
106
        }
107
 
108
        if (isset($admins[$newmain])) {
109
            $logstringold = implode(', ', $admins);
110
 
111
            unset($admins[$newmain]);
112
            array_unshift($admins, $newmain);
113
 
114
            $logstringnew = implode(', ', $admins);
115
 
116
            set_config('siteadmins', implode(',', $admins));
117
            add_to_config_log('siteadmins', $logstringold, $logstringnew, null);
118
 
119
            redirect($PAGE->url);
120
        }
121
    }
122
 
123
} else if ($addusers && confirm_sesskey()) {
124
    $admins = array();
125
    foreach (explode(',', $CFG->siteadmins) as $admin) {
126
        $admin = (int)$admin;
127
        if ($admin) {
128
            $admins[$admin] = $admin;
129
        }
130
    }
131
 
132
    $logstringold = implode(', ', $admins);
133
 
134
    foreach (explode(',', $addusers) as $userid) {
135
        $admins[$userid] = $userid;
136
    }
137
 
138
    $logstringnew = implode(', ', $admins);
139
 
140
    set_config('siteadmins', implode(',', $admins));
141
    add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
142
 
143
    redirect($PAGE->url);
144
 
145
} else if ($removeusers && confirm_sesskey()) {
146
    $admins = array();
147
    foreach (explode(',', $CFG->siteadmins) as $admin) {
148
        $admin = (int)$admin;
149
        if ($admin) {
150
            $admins[$admin] = $admin;
151
        }
152
    }
153
 
154
    $logstringold = implode(', ', $admins);
155
 
156
    // Can not remove self.
157
    foreach (explode(',', $removeusers) as $userid) {
158
        if ($userid != $USER->id) {
159
            unset($admins[$userid]);
160
        }
161
    }
162
 
163
    $logstringnew = implode(', ', $admins);
164
 
165
    set_config('siteadmins', implode(',', $admins));
166
    add_to_config_log('siteadmins', $logstringold, $logstringnew, 'core');
167
 
168
    redirect($PAGE->url);
169
}
170
 
171
// Print header.
172
echo $OUTPUT->header();
173
?>
174
 
175
<div id="addadmisform">
176
    <h3 class="main"><?php print_string('manageadmins', 'core_role'); ?></h3>
177
 
178
    <form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
179
    <div>
180
    <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
181
 
182
    <table class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
183
    <tr>
184
      <td id='existingcell'>
185
          <p>
186
            <label for="removeselect"><?php print_string('existingadmins', 'core_role'); ?></label>
187
          </p>
188
          <?php $admisselector->display(); ?>
189
          </td>
190
      <td id="buttonscell">
191
        <p class="arrow_button">
192
            <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
193
                   title="<?php print_string('add'); ?>" class="btn btn-secondary"/><br />
194
            <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
195
                   title="<?php print_string('remove'); ?>" class="btn btn-secondary"/><br />
196
            <input name="main" id="main" type="submit" value="<?php echo get_string('mainadminset', 'core_role'); ?>"
197
                   title="<?php print_string('mainadminset', 'core_role'); ?>" class="btn btn-secondary"/>
198
        </p>
199
      </td>
200
      <td id="potentialcell">
201
          <p>
202
            <label for="addselect"><?php print_string('users'); ?></label>
203
          </p>
204
          <?php $potentialadmisselector->display(); ?>
205
      </td>
206
    </tr>
207
    </table>
208
    </div>
209
    </form>
210
</div>
211
 
212
<?php
213
 
214
echo $OUTPUT->footer();