3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot
.'/lib/formslib.php');
9 class user_editadvanced_form
extends moodleform
{
12 function definition() {
13 global $USER, $CFG, $COURSE;
15 $mform =& $this->_form
;
16 $editoroptions = null;
17 $filemanageroptions = null;
20 if (is_array($this->_customdata
)) {
21 if (array_key_exists('editoroptions', $this->_customdata
)) {
22 $editoroptions = $this->_customdata
['editoroptions'];
24 if (array_key_exists('filemanageroptions', $this->_customdata
)) {
25 $filemanageroptions = $this->_customdata
['filemanageroptions'];
27 if (array_key_exists('userid', $this->_customdata
)) {
28 $userid = $this->_customdata
['userid'];
32 //Accessibility: "Required" is bad legend text.
33 $strgeneral = get_string('general');
34 $strrequired = get_string('required');
36 /// Add some extra hidden fields
37 $mform->addElement('hidden', 'id');
38 $mform->setType('id', PARAM_INT
);
39 $mform->addElement('hidden', 'course', $COURSE->id
);
40 $mform->setType('course', PARAM_INT
);
42 /// Print the required moodle fields first
43 $mform->addElement('header', 'moodle', $strgeneral);
45 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
46 $mform->addRule('username', $strrequired, 'required', null, 'client');
47 $mform->setType('username', PARAM_RAW
);
49 $auths = core_component
::get_plugin_list('auth');
50 $enabled = get_string('pluginenabled', 'core_plugin');
51 $disabled = get_string('plugindisabled', 'core_plugin');
52 $auth_options = array($enabled=>array(), $disabled=>array());
53 $cannotchangepass = array();
54 foreach ($auths as $auth => $unused) {
55 $authinst = get_auth_plugin($auth);
56 $passwordurl = $authinst->change_password_url();
57 if (!($authinst->can_change_password() && empty($passwordurl))) {
58 if (!$userid and $authinst->is_internal()) {
59 // This is unlikely but we can not create account without password
60 // when plugin uses passwords, we need to set it initially at least.
62 $cannotchangepass[] = $auth;
65 if (is_enabled_auth($auth)) {
66 $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
68 $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
71 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
72 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
74 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
75 $mform->addHelpButton('suspended', 'suspended', 'auth');
77 $mform->addElement('checkbox', 'createpassword', get_string('createpassword','auth'));
78 $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
80 if (!empty($CFG->passwordpolicy
)){
81 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
83 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
84 $mform->addHelpButton('newpassword', 'newpassword');
85 $mform->setType('newpassword', PARAM_RAW
);
86 $mform->disabledIf('newpassword', 'createpassword', 'checked');
88 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
90 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
91 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
92 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
95 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
97 /// Next the customisable profile fields
98 profile_definition($mform, $userid);
101 $btnstring = get_string('createuser');
103 $btnstring = get_string('updatemyprofile');
106 $this->add_action_buttons(false, $btnstring);
109 function definition_after_data() {
110 global $USER, $CFG, $DB, $OUTPUT;
112 $mform =& $this->_form
;
113 if ($userid = $mform->getElementValue('id')) {
114 $user = $DB->get_record('user', array('id'=>$userid));
119 // if language does not exist, use site default lang
120 if ($langsel = $mform->getElementValue('lang')) {
121 $lang = reset($langsel);
123 if (!get_string_manager()->translation_exists($lang, false)) {
124 $lang_el =& $mform->getElement('lang');
125 $lang_el->setValue($CFG->lang
);
129 // user can not change own auth method
130 if ($userid == $USER->id
) {
131 $mform->hardFreeze('auth');
132 $mform->hardFreeze('preference_auth_forcepasswordchange');
135 // admin must choose some password and supply correct email
136 if (!empty($USER->newadminuser
)) {
137 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
138 if ($mform->elementExists('suspended')) {
139 $mform->removeElement('suspended');
143 // require password for new users
145 if ($mform->elementExists('createpassword')) {
146 $mform->removeElement('createpassword');
150 if ($user and is_mnet_remote_user($user)) {
151 // only local accounts can be suspended
152 if ($mform->elementExists('suspended')) {
153 $mform->removeElement('suspended');
156 if ($user and ($user->id
== $USER->id
or is_siteadmin($user))) {
157 // prevent self and admin mess ups
158 if ($mform->elementExists('suspended')) {
159 $mform->hardFreeze('suspended');
164 if (empty($USER->newadminuser
)) {
166 $context = context_user
::instance($user->id
, MUST_EXIST
);
167 $fs = get_file_storage();
168 $hasuploadedpicture = ($fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.png') ||
$fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.jpg'));
169 if (!empty($user->picture
) && $hasuploadedpicture) {
170 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID
, 'size'=>64));
172 $imagevalue = get_string('none');
175 $imagevalue = get_string('none');
177 $imageelement = $mform->getElement('currentpicture');
178 $imageelement->setValue($imagevalue);
180 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
181 $mform->removeElement('deletepicture');
185 /// Next the customisable profile fields
186 profile_definition_after_data($mform, $userid);
189 function validation($usernew, $files) {
192 $usernew = (object)$usernew;
193 $usernew->username
= trim($usernew->username
);
195 $user = $DB->get_record('user', array('id'=>$usernew->id
));
198 if (!$user and !empty($usernew->createpassword
)) {
199 if ($usernew->suspended
) {
200 // Show some error because we can not mail suspended users.
201 $err['suspended'] = get_string('error');
204 if (!empty($usernew->newpassword
)) {
205 $errmsg = ''; // Prevent eclipse warning.
206 if (!check_password_policy($usernew->newpassword
, $errmsg)) {
207 $err['newpassword'] = $errmsg;
210 $auth = get_auth_plugin($usernew->auth
);
211 if ($auth->is_internal()) {
212 // Internal accounts require password!
213 $err['newpassword'] = get_string('required');
218 if (empty($usernew->username
)) {
219 //might be only whitespace
220 $err['username'] = get_string('required');
221 } else if (!$user or $user->username
!== $usernew->username
) {
222 //check new username does not exist
223 if ($DB->record_exists('user', array('username'=>$usernew->username
, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
224 $err['username'] = get_string('usernameexists');
226 //check allowed characters
227 if ($usernew->username
!== core_text
::strtolower($usernew->username
)) {
228 $err['username'] = get_string('usernamelowercase');
230 if ($usernew->username
!== clean_param($usernew->username
, PARAM_USERNAME
)) {
231 $err['username'] = get_string('invalidusername');
236 if (!$user or $user->email
!== $usernew->email
) {
237 if (!validate_email($usernew->email
)) {
238 $err['email'] = get_string('invalidemail');
239 } else if ($DB->record_exists('user', array('email'=>$usernew->email
, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
240 $err['email'] = get_string('emailexists');
244 /// Next the customisable profile fields
245 $err +
= profile_validation($usernew, $files);
247 if (count($err) == 0){