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
{
12 function definition () {
13 global $CFG, $COURSE, $USER;
15 $mform =& $this->_form
;
16 $editoroptions = null;
19 if (is_array($this->_customdata
)) {
20 if (array_key_exists('editoroptions', $this->_customdata
)) {
21 $editoroptions = $this->_customdata
['editoroptions'];
23 if (array_key_exists('userid', $this->_customdata
)) {
24 $userid = $this->_customdata
['userid'];
27 //Accessibility: "Required" is bad legend text.
28 $strgeneral = get_string('general');
29 $strrequired = get_string('required');
31 /// Add some extra hidden fields
32 $mform->addElement('hidden', 'id');
33 $mform->setType('id', PARAM_INT
);
34 $mform->addElement('hidden', 'course', $COURSE->id
);
35 $mform->setType('course', PARAM_INT
);
37 /// Print the required moodle fields first
38 $mform->addElement('header', 'moodle', $strgeneral);
41 useredit_shared_definition($mform, $editoroptions);
44 if (!empty($CFG->gdversion
) and !empty($CFG->disableuserimages
)) {
45 $mform->removeElement('deletepicture');
46 $mform->removeElement('imagefile');
47 $mform->removeElement('imagealt');
50 /// Next the customisable profile fields
51 profile_definition($mform, $userid);
53 $this->add_action_buttons(false, get_string('updatemyprofile'));
56 function definition_after_data() {
57 global $CFG, $DB, $OUTPUT;
59 $mform =& $this->_form
;
60 $userid = $mform->getElementValue('id');
62 // if language does not exist, use site default lang
63 if ($langsel = $mform->getElementValue('lang')) {
64 $lang = reset($langsel);
66 if (!get_string_manager()->translation_exists($lang, false)) {
67 $lang_el =& $mform->getElement('lang');
68 $lang_el->setValue($CFG->lang
);
73 if ($user = $DB->get_record('user', array('id'=>$userid))) {
76 if (empty($user->description
) && !empty($CFG->profilesforenrolledusersonly
) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
77 $mform->removeElement('description_editor');
81 if (!empty($CFG->gdversion
)) {
82 $context = get_context_instance(CONTEXT_USER
, $user->id
, MUST_EXIST
);
83 $fs = get_file_storage();
84 $hasuploadedpicture = ($fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.png') ||
$fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.jpg'));
85 if (!empty($user->picture
) && $hasuploadedpicture) {
86 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID
, 'size'=>64));
88 $imagevalue = get_string('none');
90 $imageelement = $mform->getElement('currentpicture');
91 $imageelement->setValue($imagevalue);
93 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
94 $mform->removeElement('deletepicture');
98 /// disable fields that are locked by auth plugins
99 $fields = get_user_fieldnames();
100 $authplugin = get_auth_plugin($user->auth
);
101 foreach ($fields as $field) {
102 if (!$mform->elementExists($field)) {
105 $configvariable = 'field_lock_' . $field;
106 if (isset($authplugin->config
->{$configvariable})) {
107 if ($authplugin->config
->{$configvariable} === 'locked') {
108 $mform->hardFreeze($field);
109 $mform->setConstant($field, $user->$field);
110 } else if ($authplugin->config
->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
111 $mform->hardFreeze($field);
112 $mform->setConstant($field, $user->$field);
117 /// Next the customisable profile fields
118 profile_definition_after_data($mform, $user->id
);
121 profile_definition_after_data($mform, 0);
125 function validation($usernew, $files) {
128 $errors = parent
::validation($usernew, $files);
130 $usernew = (object)$usernew;
131 $user = $DB->get_record('user', array('id'=>$usernew->id
));
134 if (!isset($usernew->email
)) {
135 // mail not confirmed yet
136 } else if (!validate_email($usernew->email
)) {
137 $errors['email'] = get_string('invalidemail');
138 } else if (($usernew->email
!== $user->email
) and $DB->record_exists('user', array('email'=>$usernew->email
, 'mnethostid'=>$CFG->mnet_localhost_id
))) {
139 $errors['email'] = get_string('emailexists');
142 if (isset($usernew->email
) and $usernew->email
=== $user->email
and over_bounce_threshold($user)) {
143 $errors['email'] = get_string('toomanybounces');
146 if (isset($usernew->email
) and !empty($CFG->verifychangedemail
) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM
))) {
147 $errorstr = email_is_not_allowed($usernew->email
);
148 if ($errorstr !== false) {
149 $errors['email'] = $errorstr;
153 /// Next the customisable profile fields
154 $errors +
= profile_validation($usernew, $files);