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
 * Form for editing a users profile
19
 *
20
 * @copyright 1999 Martin Dougiamas  http://dougiamas.com
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @package core_user
23
 */
24
 
25
if (!defined('MOODLE_INTERNAL')) {
26
    die('Direct access to this script is forbidden.');    //  It must be included from a Moodle page.
27
}
28
 
29
require_once($CFG->dirroot.'/lib/formslib.php');
30
require_once($CFG->dirroot.'/user/lib.php');
31
 
32
/**
33
 * Class user_editadvanced_form.
34
 *
35
 * @copyright 1999 Martin Dougiamas  http://dougiamas.com
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class user_editadvanced_form extends moodleform {
39
 
40
    /**
41
     * Define the form.
42
     */
43
    public function definition() {
44
        global $USER, $CFG, $COURSE;
45
 
46
        $mform = $this->_form;
47
        $editoroptions = null;
48
        $filemanageroptions = null;
49
 
50
        if (!is_array($this->_customdata)) {
51
            throw new coding_exception('invalid custom data for user_edit_form');
52
        }
53
        $editoroptions = $this->_customdata['editoroptions'];
54
        $filemanageroptions = $this->_customdata['filemanageroptions'];
55
        $user = $this->_customdata['user'];
56
        $userid = $user->id;
57
 
58
        // Accessibility: "Required" is bad legend text.
59
        $strgeneral  = get_string('general');
60
        $strrequired = get_string('required');
61
 
62
        // Add some extra hidden fields.
63
        $mform->addElement('hidden', 'id');
64
        $mform->setType('id', core_user::get_property_type('id'));
65
        $mform->addElement('hidden', 'course', $COURSE->id);
66
        $mform->setType('course', PARAM_INT);
67
 
68
        // Print the required moodle fields first.
69
        $mform->addElement('header', 'moodle', $strgeneral);
70
 
71
        $auths = core_component::get_plugin_list('auth');
72
        $enabled = get_string('pluginenabled', 'core_plugin');
73
        $disabled = get_string('plugindisabled', 'core_plugin');
74
        $authoptions = array($enabled => array(), $disabled => array());
75
        $cannotchangepass = array();
76
        $cannotchangeusername = array();
77
        foreach ($auths as $auth => $unused) {
78
            $authinst = get_auth_plugin($auth);
79
 
80
            if (!$authinst->is_internal()) {
81
                $cannotchangeusername[] = $auth;
82
            }
83
 
84
            $passwordurl = $authinst->change_password_url();
85
            if (!($authinst->can_change_password() && empty($passwordurl))) {
86
                if ($userid < 1 and $authinst->is_internal()) {
87
                    // This is unlikely but we can not create account without password
88
                    // when plugin uses passwords, we need to set it initially at least.
89
                } else {
90
                    $cannotchangepass[] = $auth;
91
                }
92
            }
93
            if (is_enabled_auth($auth)) {
94
                $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
95
            } else {
96
                $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
97
            }
98
        }
99
 
100
        $purpose = user_edit_map_field_purpose($userid, 'username');
101
        $mform->addElement('text', 'username', get_string('username'), 'size="20"' . $purpose);
102
        $mform->addHelpButton('username', 'username', 'auth');
103
        $mform->setType('username', PARAM_RAW);
104
 
105
        if ($userid !== -1) {
106
            $mform->disabledIf('username', 'auth', 'in', $cannotchangeusername);
107
        }
108
 
109
        $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
110
        $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
111
 
112
        $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
113
        $mform->addHelpButton('suspended', 'suspended', 'auth');
114
 
115
        $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
116
        $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
117
 
118
        if (!empty($CFG->passwordpolicy)) {
119
            $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
120
        }
121
 
122
        $purpose = user_edit_map_field_purpose($userid, 'password');
123
        $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'),
124
            'maxlength="'.MAX_PASSWORD_CHARACTERS.'" size="20"' . $purpose);
125
        $mform->addRule('newpassword', get_string('maximumchars', '', MAX_PASSWORD_CHARACTERS),
126
            'maxlength', MAX_PASSWORD_CHARACTERS, 'client');
127
        $mform->addHelpButton('newpassword', 'newpassword');
128
        $mform->setType('newpassword', core_user::get_property_type('password'));
129
        $mform->disabledIf('newpassword', 'createpassword', 'checked');
130
 
131
        $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
132
 
133
        // Check if the user has active external tokens.
134
        if ($userid and empty($CFG->passwordchangetokendeletion)) {
135
            if ($tokens = webservice::get_active_tokens($userid)) {
136
                $services = '';
137
                foreach ($tokens as $token) {
138
                    $services .= format_string($token->servicename) . ',';
139
                }
140
                $services = get_string('userservices', 'webservice', rtrim($services, ','));
141
                $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'), $services);
142
                $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
143
                $mform->disabledIf('signoutofotherservices', 'newpassword', 'eq', '');
144
                $mform->setDefault('signoutofotherservices', 1);
145
            }
146
        }
147
 
148
        $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
149
        $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
150
        $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
151
 
152
        // Shared fields.
153
        useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
154
 
155
        // Next the customisable profile fields.
156
        profile_definition($mform, $userid);
157
 
158
        if ($userid == -1) {
159
            $btnstring = get_string('createuser');
160
        } else {
161
            $btnstring = get_string('updatemyprofile');
162
        }
163
 
164
        $this->add_action_buttons(true, $btnstring);
165
 
166
        $this->set_data($user);
167
    }
168
 
169
    /**
170
     * Extend the form definition after data has been parsed.
171
     */
172
    public function definition_after_data() {
173
        global $USER, $CFG, $DB, $OUTPUT;
174
 
175
        $mform = $this->_form;
176
 
177
        // Trim required name fields.
178
        foreach (useredit_get_required_name_fields() as $field) {
179
            $mform->applyFilter($field, 'trim');
180
        }
181
 
182
        if ($userid = $mform->getElementValue('id')) {
183
            $user = $DB->get_record('user', array('id' => $userid));
184
        } else {
185
            $user = false;
186
        }
187
 
188
        // User can not change own auth method.
189
        if ($userid == $USER->id) {
190
            $mform->hardFreeze('auth');
191
            $mform->hardFreeze('preference_auth_forcepasswordchange');
192
        }
193
 
194
        // Admin must choose some password and supply correct email.
195
        if (!empty($USER->newadminuser)) {
196
            $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
197
            if ($mform->elementExists('suspended')) {
198
                $mform->removeElement('suspended');
199
            }
200
        }
201
 
202
        // Require password for new users.
203
        if ($userid > 0) {
204
            if ($mform->elementExists('createpassword')) {
205
                $mform->removeElement('createpassword');
206
            }
207
        }
208
 
209
        if ($user and is_mnet_remote_user($user)) {
210
            // Only local accounts can be suspended.
211
            if ($mform->elementExists('suspended')) {
212
                $mform->removeElement('suspended');
213
            }
214
        }
215
        if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
216
            // Prevent self and admin mess ups.
217
            if ($mform->elementExists('suspended')) {
218
                $mform->hardFreeze('suspended');
219
            }
220
        }
221
 
222
        // Print picture.
223
        if (empty($USER->newadminuser)) {
224
            if ($user) {
225
                $context = context_user::instance($user->id, MUST_EXIST);
226
                $fs = get_file_storage();
227
                $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
228
                if (!empty($user->picture) && $hasuploadedpicture) {
229
                    $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
230
                } else {
231
                    $imagevalue = get_string('none');
232
                }
233
            } else {
234
                $imagevalue = get_string('none');
235
            }
236
            $imageelement = $mform->getElement('currentpicture');
237
            $imageelement->setValue($imagevalue);
238
 
239
            if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
240
                $mform->removeElement('deletepicture');
241
            }
242
        }
243
 
244
        // User changing their preferred theme will delete the cache for this theme.
245
        if ($mform->elementExists('theme') && $mform->isSubmitted()) {
246
            $theme = $mform->getSubmitValue('theme');
247
            if (!empty($user) && ($theme != $user->theme)) {
248
                theme_delete_used_in_context_cache($theme, $user->theme);
249
            }
250
        }
251
 
252
        // Next the customisable profile fields.
253
        profile_definition_after_data($mform, $userid);
254
    }
255
 
256
    /**
257
     * Validate the form data.
258
     * @param array $usernew
259
     * @param array $files
260
     * @return array|bool
261
     */
262
    public function validation($usernew, $files) {
263
        global $CFG, $DB;
264
 
265
        $usernew = (object)$usernew;
266
        $usernew->username = trim($usernew->username);
267
 
268
        $user = $DB->get_record('user', array('id' => $usernew->id));
269
        $err = array();
270
 
271
        if (!$user and !empty($usernew->createpassword)) {
272
            if ($usernew->suspended) {
273
                // Show some error because we can not mail suspended users.
274
                $err['suspended'] = get_string('error');
275
            }
276
        } else {
277
            if (!empty($usernew->newpassword)) {
278
                $errmsg = ''; // Prevent eclipse warning.
279
                if (!check_password_policy($usernew->newpassword, $errmsg, $usernew)) {
280
                    $err['newpassword'] = $errmsg;
281
                }
282
            } else if (!$user) {
283
                $auth = get_auth_plugin($usernew->auth);
284
                if ($auth->is_internal()) {
285
                    // Internal accounts require password!
286
                    $err['newpassword'] = get_string('required');
287
                }
288
            }
289
        }
290
 
291
        if (empty($usernew->username)) {
292
            // Might be only whitespace.
293
            $err['username'] = get_string('required');
294
        } else if (!$user or $user->username !== $usernew->username) {
295
            // Check new username does not exist.
296
            if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
297
                $err['username'] = get_string('usernameexists');
298
            }
299
            // Check allowed characters.
300
            if ($usernew->username !== core_text::strtolower($usernew->username)) {
301
                $err['username'] = get_string('usernamelowercase');
302
            } else {
303
                if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
304
                    $err['username'] = get_string('invalidusername');
305
                }
306
            }
307
        }
308
 
309
        if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
310
            if (!validate_email($usernew->email)) {
311
                $err['email'] = get_string('invalidemail');
312
            } else if (empty($CFG->allowaccountssameemail)) {
313
                // Make a case-insensitive query for the given email address.
314
                $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
315
                $params = array(
316
                    'email' => $usernew->email,
317
                    'mnethostid' => $CFG->mnet_localhost_id,
318
                    'userid' => $usernew->id
319
                );
320
                // If there are other user(s) that already have the same email, show an error.
321
                if ($DB->record_exists_select('user', $select, $params)) {
322
                    $err['email'] = get_string('emailexists');
323
                }
324
            }
325
        }
326
 
327
        // Next the customisable profile fields.
328
        $err += profile_validation($usernew, $files);
329
 
330
        if (count($err) == 0) {
331
            return true;
332
        } else {
333
            return $err;
334
        }
335
    }
336
}
337
 
338