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->addElement('hidden', 'course', $COURSE->id
);
21 /// Print the required moodle fields first
22 $mform->addElement('header', 'moodle', $strgeneral);
24 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
25 $mform->addRule('username', $strrequired, 'required', null, 'client');
26 $mform->setType('username', PARAM_RAW
);
28 $modules = get_list_of_plugins('auth');
29 $auth_options = array();
30 foreach ($modules as $module) {
31 $auth_options[$module] = get_string("auth_$module"."title", "auth");
33 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
34 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
35 $mform->setAdvanced('auth');
37 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
38 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
39 false, true, false, get_string('leavetokeep')));
40 $mform->setType('newpassword', PARAM_RAW
);
42 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
43 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
44 false, true, false, get_string('forcepasswordchangehelp')));
47 useredit_shared_definition($mform);
49 /// Next the customisable profile fields
50 profile_definition($mform);
52 $this->add_action_buttons(false, get_string('updatemyprofile'));
55 function definition_after_data() {
58 $mform =& $this->_form
;
59 $userid = $mform->getElementValue('id');
60 $user = get_record('user', 'id', $userid);
62 // if language does not exist, use site default lang
63 if ($langsel = $mform->getElementValue('lang')) {
64 $lang = reset($langsel);
65 if (!file_exists($CFG->dataroot
.'/lang/'.$lang) and
66 !file_exists($CFG->dirroot
.'/lang/'.$lang)) {
67 $lang_el =& $mform->getElement('lang');
68 $lang_el->setValue($CFG->lang
);
72 // user can not change own auth method
73 if ($userid == $USER->id
) {
74 $mform->hardFreeze('auth');
75 $mform->hardFreeze('preference_auth_forcepasswordchange');
78 // admin must choose some password and supply correct email
79 if (!empty($USER->newadminuser
)) {
80 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
82 $email_el =& $mform->getElement('email');
83 if ($email_el->getValue() == 'root@localhost') {
84 $email_el->setValue('');
88 // require password for new users
90 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
94 if (!empty($CFG->gdversion
)) {
95 $image_el =& $mform->getElement('currentpicture');
96 if ($user and $user->picture
) {
97 $image_el->setValue(print_user_picture($user, SITEID
, $user->picture
, 64, true, false, '', true));
99 $image_el->setValue(get_string('none'));
103 /// Next the customisable profile fields
104 profile_definition_after_data($mform);
107 function validation($usernew, $files) {
110 $usernew = (object)$usernew;
111 $usernew->username
= trim($usernew->username
);
113 $user = get_record('user', 'id', $usernew->id
);
116 if (!empty($usernew->newpassword
)) {
117 $errmsg = '';//prevent eclipse warning
118 if (!check_password_policy($usernew->newpassword
, $errmsg)) {
119 $err['newpassword'] = $errmsg;
123 if (empty($usernew->username
)) {
124 //might be only whitespace
125 $err['username'] = get_string('required');
126 } else if (!$user or $user->username
!== $usernew->username
) {
127 //check new username does not exist
128 if (record_exists('user', 'username', $usernew->username
, 'mnethostid', $CFG->mnet_localhost_id
)) {
129 $err['username'] = get_string('usernameexists');
131 //check allowed characters
132 if ($usernew->username
!== moodle_strtolower($usernew->username
)) {
133 $err['username'] = get_string('usernamelowercase');
135 if (empty($CFG->extendedusernamechars
)) {
136 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username
);
137 if ($usernew->username
!== $string) {
138 $err['username'] = get_string('alphanumerical');
144 if (!$user or $user->email
!== $usernew->email
) {
145 if (!validate_email($usernew->email
)) {
146 $err['email'] = get_string('invalidemail');
147 } else if (record_exists('user', 'email', $usernew->email
, 'mnethostid', $CFG->mnet_localhost_id
)) {
148 $err['email'] = get_string('emailexists');
152 /// Next the customisable profile fields
153 $err +
= profile_validation($usernew, $files);
155 if (count($err) == 0){
163 return $this->_upload_manager
;