forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_form.php
154 lines (123 loc) · 5.63 KB
/
edit_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once($CFG->dirroot.'/lib/formslib.php');
class user_edit_form extends moodleform {
// Define the form
function definition () {
global $CFG, $COURSE;
$mform =& $this->_form;
if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
$editoroptions = $this->_customdata['editoroptions'];
} else {
$editoroptions = null;
}
//Accessibility: "Required" is bad legend text.
$strgeneral = get_string('general');
$strrequired = get_string('required');
/// Add some extra hidden fields
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'course', $COURSE->id);
$mform->setType('course', PARAM_INT);
/// Print the required moodle fields first
$mform->addElement('header', 'moodle', $strgeneral);
/// shared fields
useredit_shared_definition($mform, $editoroptions);
/// extra settigs
$mform->addRule('description_editor', $strrequired, 'required', null, 'client');
if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
$mform->removeElement('deletepicture');
$mform->removeElement('imagefile');
$mform->removeElement('imagealt');
}
/// Next the customisable profile fields
profile_definition($mform);
$this->add_action_buttons(false, get_string('updatemyprofile'));
}
function definition_after_data() {
global $CFG, $DB, $OUTPUT;
$mform =& $this->_form;
$userid = $mform->getElementValue('id');
// if language does not exist, use site default lang
if ($langsel = $mform->getElementValue('lang')) {
$lang = reset($langsel);
// missing _utf8 in language, add it before further processing. MDL-11829 MDL-16845
if (strpos($lang, '_utf8') === false) {
$lang = $lang . '_utf8';
$lang_el =& $mform->getElement('lang');
$lang_el->setValue($lang);
}
// check lang exists
if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
!file_exists($CFG->dirroot .'/lang/'.$lang)) {
$lang_el =& $mform->getElement('lang');
$lang_el->setValue($CFG->lang);
}
}
if ($user = $DB->get_record('user', array('id'=>$userid))) {
// remove description
if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
$mform->removeElement('description_editor');
}
// print picture
if (!empty($CFG->gdversion)) {
$image_el =& $mform->getElement('currentpicture');
if ($user and $user->picture) {
$image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64)));
} else {
$image_el->setValue(get_string('none'));
}
}
/// disable fields that are locked by auth plugins
$fields = get_user_fieldnames();
$authplugin = get_auth_plugin($user->auth);
foreach ($fields as $field) {
if (!$mform->elementExists($field)) {
continue;
}
$configvariable = 'field_lock_' . $field;
if (isset($authplugin->config->{$configvariable})) {
if ($authplugin->config->{$configvariable} === 'locked') {
$mform->hardFreeze($field);
$mform->setConstant($field, $user->$field);
} else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
$mform->hardFreeze($field);
$mform->setConstant($field, $user->$field);
}
}
}
/// Next the customisable profile fields
profile_definition_after_data($mform, $user->id);
} else {
profile_definition_after_data($mform, 0);
}
}
function validation($usernew, $files) {
global $CFG, $DB;
$errors = parent::validation($usernew, $files);
$usernew = (object)$usernew;
$user = $DB->get_record('user', array('id'=>$usernew->id));
// validate email
if (!isset($usernew->email)) {
// mail not confirmed yet
} else if (!validate_email($usernew->email)) {
$errors['email'] = get_string('invalidemail');
} else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
$errors['email'] = get_string('emailexists');
}
if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
$errors['email'] = get_string('toomanybounces');
}
if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
$errorstr = email_is_not_allowed($usernew->email);
if ($errorstr !== false) {
$errors['email'] = $errorstr;
}
}
/// Next the customisable profile fields
$errors += profile_validation($usernew, $files);
return $errors;
}
function get_um() {
return $this->_upload_manager;
}
}