Merge branch 'wip-mdl-27562' of git://github.com/rajeshtaneja/moodle
[moodle.git] / user / edit_form.php
blob2fdfe1e596e8298009e6a4badbf2ebe787d40695
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_edit_form extends moodleform {
11 // Define the form
12 function definition () {
13 global $CFG, $COURSE;
15 $mform =& $this->_form;
16 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
17 $editoroptions = $this->_customdata['editoroptions'];
18 } else {
19 $editoroptions = null;
21 //Accessibility: "Required" is bad legend text.
22 $strgeneral = get_string('general');
23 $strrequired = get_string('required');
25 /// Add some extra hidden fields
26 $mform->addElement('hidden', 'id');
27 $mform->setType('id', PARAM_INT);
28 $mform->addElement('hidden', 'course', $COURSE->id);
29 $mform->setType('course', PARAM_INT);
31 /// Print the required moodle fields first
32 $mform->addElement('header', 'moodle', $strgeneral);
34 /// shared fields
35 useredit_shared_definition($mform, $editoroptions);
37 /// extra settigs
38 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
39 $mform->removeElement('deletepicture');
40 $mform->removeElement('imagefile');
41 $mform->removeElement('imagealt');
44 /// Next the customisable profile fields
45 profile_definition($mform);
47 $this->add_action_buttons(false, get_string('updatemyprofile'));
50 function definition_after_data() {
51 global $CFG, $DB, $OUTPUT;
53 $mform =& $this->_form;
54 $userid = $mform->getElementValue('id');
56 // if language does not exist, use site default lang
57 if ($langsel = $mform->getElementValue('lang')) {
58 $lang = reset($langsel);
59 // check lang exists
60 if (!get_string_manager()->translation_exists($lang, false)) {
61 $lang_el =& $mform->getElement('lang');
62 $lang_el->setValue($CFG->lang);
67 if ($user = $DB->get_record('user', array('id'=>$userid))) {
69 // remove description
70 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
71 $mform->removeElement('description_editor');
74 // print picture
75 if (!empty($CFG->gdversion)) {
76 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
77 $fs = get_file_storage();
78 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
79 if (!empty($user->picture) && $hasuploadedpicture) {
80 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
81 } else {
82 $imagevalue = get_string('none');
84 $imageelement = $mform->getElement('currentpicture');
85 $imageelement->setValue($imagevalue);
87 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
88 $mform->removeElement('deletepicture');
92 /// disable fields that are locked by auth plugins
93 $fields = get_user_fieldnames();
94 $authplugin = get_auth_plugin($user->auth);
95 foreach ($fields as $field) {
96 if (!$mform->elementExists($field)) {
97 continue;
99 $configvariable = 'field_lock_' . $field;
100 if (isset($authplugin->config->{$configvariable})) {
101 if ($authplugin->config->{$configvariable} === 'locked') {
102 $mform->hardFreeze($field);
103 $mform->setConstant($field, $user->$field);
104 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
105 $mform->hardFreeze($field);
106 $mform->setConstant($field, $user->$field);
111 /// Next the customisable profile fields
112 profile_definition_after_data($mform, $user->id);
114 } else {
115 profile_definition_after_data($mform, 0);
119 function validation($usernew, $files) {
120 global $CFG, $DB;
122 $errors = parent::validation($usernew, $files);
124 $usernew = (object)$usernew;
125 $user = $DB->get_record('user', array('id'=>$usernew->id));
127 // validate email
128 if (!isset($usernew->email)) {
129 // mail not confirmed yet
130 } else if (!validate_email($usernew->email)) {
131 $errors['email'] = get_string('invalidemail');
132 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
133 $errors['email'] = get_string('emailexists');
136 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
137 $errors['email'] = get_string('toomanybounces');
140 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
141 $errorstr = email_is_not_allowed($usernew->email);
142 if ($errorstr !== false) {
143 $errors['email'] = $errorstr;
147 /// Next the customisable profile fields
148 $errors += profile_validation($usernew, $files);
150 return $errors;