Updated the 19 build version to 20081220
[moodle.git] / user / edit_form.php
blobff3adb41836cefe9ce6c0a0d9ae2cc0d8138d650
1 <?php //$Id$
3 require_once($CFG->dirroot.'/lib/formslib.php');
5 class user_edit_form extends moodleform {
7 // Define the form
8 function definition () {
9 global $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 /// shared fields
25 useredit_shared_definition($mform);
27 /// extra settigs
28 $mform->addRule('description', $strrequired, 'required', null, 'client');
29 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
30 $mform->removeElement('deletepicture');
31 $mform->removeElement('imagefile');
32 $mform->removeElement('imagealt');
35 /// Next the customisable profile fields
36 profile_definition($mform);
38 $this->add_action_buttons(false, get_string('updatemyprofile'));
41 function definition_after_data() {
42 global $CFG;
44 $mform =& $this->_form;
45 $userid = $mform->getElementValue('id');
47 // if language does not exist, use site default lang
48 if ($langsel = $mform->getElementValue('lang')) {
49 $lang = reset($langsel);
50 // missing _utf8 in language, add it before further processing. MDL-11829 MDL-16845
51 if (strpos($lang, '_utf8') === false) {
52 $lang = $lang . '_utf8';
53 $lang_el =& $mform->getElement('lang');
54 $lang_el->setValue($lang);
56 // check lang exists
57 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
58 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
59 $lang_el =& $mform->getElement('lang');
60 $lang_el->setValue($CFG->lang);
64 // remove description
65 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !record_exists('role_assignments', 'userid', $userid)) {
66 $mform->removeElement('description');
69 if ($user = get_record('user', 'id', $userid)) {
71 // print picture
72 if (!empty($CFG->gdversion)) {
73 $image_el =& $mform->getElement('currentpicture');
74 if ($user and $user->picture) {
75 $image_el->setValue(print_user_picture($user, SITEID, $user->picture, 64,true,false,'',true));
76 } else {
77 $image_el->setValue(get_string('none'));
81 /// disable fields that are locked by auth plugins
82 $fields = get_user_fieldnames();
83 $authplugin = get_auth_plugin($user->auth);
84 foreach ($fields as $field) {
85 if (!$mform->elementExists($field)) {
86 continue;
88 $configvariable = 'field_lock_' . $field;
89 if (isset($authplugin->config->{$configvariable})) {
90 if ($authplugin->config->{$configvariable} === 'locked') {
91 $mform->hardFreeze($field);
92 $mform->setConstant($field, $user->$field);
93 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
94 $mform->hardFreeze($field);
95 $mform->setConstant($field, $user->$field);
102 /// Next the customisable profile fields
103 profile_definition_after_data($mform);
107 function validation($usernew, $files) {
108 global $CFG;
110 $errors = parent::validation($usernew, $files);
112 $usernew = (object)$usernew;
113 $user = get_record('user', 'id', $usernew->id);
115 // validate email
116 if (!isset($usernew->email)) {
117 // mail not confirmed yet
118 } else if (!validate_email($usernew->email)) {
119 $errors['email'] = get_string('invalidemail');
120 } else if (($usernew->email !== $user->email) and record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
121 $errors['email'] = get_string('emailexists');
124 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
125 $errors['email'] = get_string('toomanybounces');
128 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
129 $errorstr = email_is_not_allowed($usernew->email);
130 if ($errorstr !== false) {
131 $errors['email'] = $errorstr;
135 /// Next the customisable profile fields
136 $errors += profile_validation($usernew, $files);
138 return $errors;
141 function get_um() {
142 return $this->_upload_manager;