MDL-8096 - some more fixes and minor refactoring in custom profile fields
[moodle.git] / user / editadvanced_form.php
CommitLineData
ad6226fb 1<?php //$Id$
2
3require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 4
5class user_editadvanced_form extends moodleform {
6
7 // Define the form
bb6d3d34 8 function definition() {
d8734783 9 global $USER, $CFG, $COURSE;
ad6226fb 10
11 $mform =& $this->_form;
ad6226fb 12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
13 $strrequired = get_string('required');
14
15 /// Add some extra hidden fields
16 $mform->addElement('hidden', 'id');
d8734783 17 $mform->addElement('hidden', 'course', $COURSE->id);
ad6226fb 18
19 /// Print the required moodle fields first
20 $mform->addElement('header', 'moodle', $strrequired);
21
22 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
23 $mform->addRule('username', $strrequired, 'required', null, 'client');
24 $mform->setType('username', PARAM_RAW);
25
26 $modules = get_list_of_plugins('auth');
27 $auth_options = array();
28 foreach ($modules as $module) {
29 $auth_options[$module] = get_string("auth_$module"."title", "auth");
30 }
31 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
32 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
33 $mform->setAdvanced('auth');
34
35 $mform->addElement('text', 'newpassword', get_string('newpassword'), 'size="20"');
36 $mform->setType('newpassword', PARAM_RAW);
37 //TODO: add missing help - empty means no change
38
39 $mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
40 //TODO: add missing help - user will be forced to change password
41
42 require('edit_form_common.php');
43
bb6d3d34 44 /// Next the customisable profile fields
45 profile_definition($mform);
46
ad6226fb 47 $this->add_action_buttons(false, get_string('updatemyprofile'));
48 }
49
50 function definition_after_data() {
51 global $USER, $CFG;
52
53 $mform =& $this->_form;
e8e0bb2d 54 $userid = $mform->getElementValue('id');
55 $user = get_record('user', 'id', $userid);
ad6226fb 56
e8e0bb2d 57 // user can not change own auth method
58 if ($userid == $USER->id) {
59 $mform->hardFreeze('auth');
60 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 61 }
62
63 // admin must choose some password and supply correct email
64 if (!empty($USER->newadminuser)) {
65 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
66
e8e0bb2d 67 $email_el = $mform->getElement('email');
68 if ($email_el->getValue() == 'root@localhost') {
69 $email_el->setValue('');
ad6226fb 70 }
71 }
72
e8e0bb2d 73 // require password for new users
74 if ($userid == -1) {
75 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
76 }
77
78 // print picture
ad6226fb 79 if (!empty($CFG->gdversion)) {
e8e0bb2d 80 $image_el = $mform->getElement('currentpicture');
81 if ($user and $user->picture) {
82 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
83 } else {
84 $image_el->setValue(get_string('none'));
85 }
86 }
bb6d3d34 87
88 /// Next the customisable profile fields
89 profile_definition_after_data($mform);
e8e0bb2d 90 }
91
bb6d3d34 92 function validation($usernew) {
e8e0bb2d 93 global $CFG;
94
95 $usernew = (object)$usernew;
96 $user = get_record('user', 'id', $usernew->id);
97 $err = array();
98
99 if (!$user or $user->username !== $usernew->username) {
100 //check new username does not exist
101 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
102 $err['username'] = get_string('usernameexists');
103 }
104 //check allowed characters
105 if ($usernew->username !== moodle_strtolower($usernew->username)) {
106 $err['username'] = get_string('usernamelowercase');
ad6226fb 107 } else {
e8e0bb2d 108 if (empty($CFG->extendedusernamechars)) {
109 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
110 if ($usernew->username !== $string) {
111 $err['username'] = get_string('alphanumerical');
112 }
113 }
ad6226fb 114 }
115 }
e8e0bb2d 116
117 if (!$user or $user->email !== $usernew->email) {
118 if (!validate_email($usernew->email)) {
119 $err['email'] = get_string('invalidemail');
120 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
121 $err['email'] = get_string('emailexists');
122 }
123 }
124
bb6d3d34 125 /// Next the customisable profile fields
126 $err += profile_validation($usernew);
127
e8e0bb2d 128 if (count($err) == 0){
129 return true;
130 } else {
131 return $err;
132 }
ad6226fb 133 }
134
135 function get_um() {
136 return $this->_upload_manager;
137 }
138}
139
140?>