MDL-21676 user: minor gravatar fixes
[moodle.git] / user / edit_form.php
CommitLineData
8bdc9cac 1<?php
c9c74c47 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
d8734783 7require_once($CFG->dirroot.'/lib/formslib.php');
c9c74c47 8
9class user_edit_form extends moodleform {
10
11 // Define the form
12 function definition () {
b0c90e6e 13 global $CFG, $COURSE;
c9c74c47 14
d8734783 15 $mform =& $this->_form;
8bdc9cac
SH
16 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
17 $editoroptions = $this->_customdata['editoroptions'];
18 } else {
19 $editoroptions = null;
20 }
c8149f0f 21 //Accessibility: "Required" is bad legend text.
22 $strgeneral = get_string('general');
c9c74c47 23 $strrequired = get_string('required');
24
c9c74c47 25 /// Add some extra hidden fields
3c19571f 26 $mform->addElement('hidden', 'id');
d18e0fe6 27 $mform->setType('id', PARAM_INT);
d8734783 28 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 29 $mform->setType('course', PARAM_INT);
acc9c3e0 30
c9c74c47 31 /// Print the required moodle fields first
c8149f0f 32 $mform->addElement('header', 'moodle', $strgeneral);
c9c74c47 33
14a6b7e1 34 /// shared fields
8bdc9cac 35 useredit_shared_definition($mform, $editoroptions);
c9c74c47 36
d8734783 37 /// extra settigs
d8734783 38 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
39 $mform->removeElement('deletepicture');
40 $mform->removeElement('imagefile');
41 $mform->removeElement('imagealt');
c9c74c47 42 }
acc9c3e0 43
bb6d3d34 44 /// Next the customisable profile fields
45 profile_definition($mform);
46
d8734783 47 $this->add_action_buttons(false, get_string('updatemyprofile'));
48 }
c9c74c47 49
d8734783 50 function definition_after_data() {
f2f085ee 51 global $CFG, $DB, $OUTPUT;
c9c74c47 52
d8734783 53 $mform =& $this->_form;
54 $userid = $mform->getElementValue('id');
acc9c3e0 55
1545d7f0 56 // if language does not exist, use site default lang
57 if ($langsel = $mform->getElementValue('lang')) {
58 $lang = reset($langsel);
eb309bed 59 // check lang exists
96de38c3 60 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 61 $lang_el =& $mform->getElement('lang');
62 $lang_el->setValue($CFG->lang);
63 }
64 }
65
35f0660a 66
5d910388 67 if ($user = $DB->get_record('user', array('id'=>$userid))) {
c9c74c47 68
35f0660a 69 // remove description
6eaf2e37 70 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
8bdc9cac 71 $mform->removeElement('description_editor');
35f0660a 72 }
73
3eff8146 74 // print picture
75 if (!empty($CFG->gdversion)) {
4125bdc1
SH
76 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
77 $fs = get_file_storage();
ed9e0cb6 78 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
79 if (!empty($user->picture) && $hasuploadedpicture) {
80 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
3eff8146 81 } else {
4125bdc1
SH
82 $imagevalue = get_string('none');
83 }
84 $imageelement = $mform->getElement('currentpicture');
85 $imageelement->setValue($imagevalue);
86
87 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
88 $mform->removeElement('deletepicture');
3eff8146 89 }
c9c74c47 90 }
acc9c3e0 91
d8734783 92 /// disable fields that are locked by auth plugins
93 $fields = get_user_fieldnames();
d8734783 94 $authplugin = get_auth_plugin($user->auth);
95 foreach ($fields as $field) {
96 if (!$mform->elementExists($field)) {
97 continue;
98 }
99 $configvariable = 'field_lock_' . $field;
100 if (isset($authplugin->config->{$configvariable})) {
101 if ($authplugin->config->{$configvariable} === 'locked') {
cc444336 102 $mform->hardFreeze($field);
103 $mform->setConstant($field, $user->$field);
d8734783 104 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
cc444336 105 $mform->hardFreeze($field);
106 $mform->setConstant($field, $user->$field);
d8734783 107 }
108 }
68e9d368 109 }
f2f085ee 110
63466095 111 /// Next the customisable profile fields
112 profile_definition_after_data($mform, $user->id);
c9c74c47 113
63466095 114 } else {
115 profile_definition_after_data($mform, 0);
116 }
c9c74c47 117 }
118
a78890d5 119 function validation($usernew, $files) {
5d910388 120 global $CFG, $DB;
c9c74c47 121
a78890d5 122 $errors = parent::validation($usernew, $files);
123
acc9c3e0 124 $usernew = (object)$usernew;
5d910388 125 $user = $DB->get_record('user', array('id'=>$usernew->id));
acc9c3e0 126
d8734783 127 // validate email
d0b2acde 128 if (!isset($usernew->email)) {
129 // mail not confirmed yet
130 } else if (!validate_email($usernew->email)) {
a78890d5 131 $errors['email'] = get_string('invalidemail');
5d910388 132 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
a78890d5 133 $errors['email'] = get_string('emailexists');
c9c74c47 134 }
14a6b7e1 135
d0b2acde 136 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
a78890d5 137 $errors['email'] = get_string('toomanybounces');
c9c74c47 138 }
139
d0b2acde 140 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
bff34a67 141 $errorstr = email_is_not_allowed($usernew->email);
142 if ($errorstr !== false) {
143 $errors['email'] = $errorstr;
144 }
145 }
146
bb6d3d34 147 /// Next the customisable profile fields
a78890d5 148 $errors += profile_validation($usernew, $files);
bb6d3d34 149
a78890d5 150 return $errors;
c9c74c47 151 }
152}
153
aa6c1ced 154