3 require_once($CFG->dirroot
.'/lib/formslib.php');
5 class user_editadvanced_form
extends moodleform
{
8 function definition() {
9 global $USER, $CFG, $COURSE;
11 $mform =& $this->_form
;
12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
13 //Accessibility: "Required" is bad legend text.
14 $strgeneral = get_string('general');
15 $strrequired = get_string('required');
17 /// Add some extra hidden fields
18 $mform->addElement('hidden', 'id');
19 $mform->setType('id', PARAM_INT
);
20 $mform->addElement('hidden', 'course', $COURSE->id
);
21 $mform->setType('course', PARAM_INT
);
23 /// Print the required moodle fields first
24 $mform->addElement('header', 'moodle', $strgeneral);
26 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
27 $mform->addRule('username', $strrequired, 'required', null, 'client');
28 $mform->setType('username', PARAM_RAW
);
30 $modules = get_list_of_plugins('auth');
31 $auth_options = array();
32 foreach ($modules as $module) {
33 $auth_options[$module] = auth_get_plugin_title ($module);
35 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
36 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
37 $mform->setAdvanced('auth');
39 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
40 $mform->setHelpButton('newpassword',array('newpassword', get_string('leavetokeep')));
41 $mform->setType('newpassword', PARAM_RAW
);
43 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
44 $mform->setHelpButton('preference_auth_forcepasswordchange',array('forcepasswordchange', get_string('forcepasswordchange')));
46 useredit_shared_definition($mform);
48 /// Next the customisable profile fields
49 profile_definition($mform);
51 $this->add_action_buttons(false, get_string('updatemyprofile'));
54 function definition_after_data() {
57 $mform =& $this->_form
;
58 if ($userid = $mform->getElementValue('id')) {
59 $user = get_record('user', 'id', $userid);
64 // if language does not exist, use site default lang
65 if ($langsel = $mform->getElementValue('lang')) {
66 $lang = reset($langsel);
67 // missing _utf8 in language, add it before further processing. MDL-11829 MDL-16845
68 if (strpos($lang, '_utf8') === false) {
69 $lang = $lang . '_utf8';
70 $lang_el =& $mform->getElement('lang');
71 $lang_el->setValue($lang);
74 if (!file_exists($CFG->dataroot
.'/lang/'.$lang) and
75 !file_exists($CFG->dirroot
.'/lang/'.$lang)) {
76 $lang_el =& $mform->getElement('lang');
77 $lang_el->setValue($CFG->lang
);
81 // user can not change own auth method
82 if ($userid == $USER->id
) {
83 $mform->hardFreeze('auth');
84 $mform->hardFreeze('preference_auth_forcepasswordchange');
87 // admin must choose some password and supply correct email
88 if (!empty($USER->newadminuser
)) {
89 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
91 $email_el =& $mform->getElement('email');
92 if ($email_el->getValue() == 'root@localhost') {
93 $email_el->setValue('');
97 // require password for new users
99 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
103 if (!empty($CFG->gdversion
)) {
104 $image_el =& $mform->getElement('currentpicture');
105 if ($user and $user->picture
) {
106 $image_el->setValue(print_user_picture($user, SITEID
, $user->picture
, 64, true, false, '', true));
108 $image_el->setValue(get_string('none'));
112 /// Next the customisable profile fields
113 profile_definition_after_data($mform, $userid);
116 function validation($usernew, $files) {
119 $usernew = (object)$usernew;
120 $usernew->username
= trim($usernew->username
);
122 $user = get_record('user', 'id', $usernew->id
);
125 if (!empty($usernew->newpassword
)) {
126 $errmsg = '';//prevent eclipse warning
127 if (!check_password_policy($usernew->newpassword
, $errmsg)) {
128 $err['newpassword'] = $errmsg;
132 if (empty($usernew->username
)) {
133 //might be only whitespace
134 $err['username'] = get_string('required');
135 } else if (!$user or $user->username
!== stripslashes($usernew->username
)) {
136 //check new username does not exist
137 if (record_exists('user', 'username', $usernew->username
, 'mnethostid', $CFG->mnet_localhost_id
)) {
138 $err['username'] = get_string('usernameexists');
140 //check allowed characters
141 if ($usernew->username
!== moodle_strtolower($usernew->username
)) {
143 $err['username'] = get_string('usernamelowercase');
145 if (empty($CFG->extendedusernamechars
)) {
146 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username
);
147 if ($usernew->username
!== $string) {
148 $err['username'] = get_string('alphanumerical');
154 if (!$user or $user->email
!== stripslashes($usernew->email
)) {
155 if (!validate_email(stripslashes($usernew->email
))) {
156 $err['email'] = get_string('invalidemail');
157 } else if (record_exists('user', 'email', $usernew->email
, 'mnethostid', $CFG->mnet_localhost_id
)) {
158 $err['email'] = get_string('emailexists');
162 /// Next the customisable profile fields
163 $err +
= profile_validation($usernew, $files);
165 if (count($err) == 0){
173 return $this->_upload_manager
;