1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Form to edit a users profile
|
|
|
19 |
*
|
|
|
20 |
* @copyright 1999 Martin Dougiamas http://dougiamas.com
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @package core_user
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
if (!defined('MOODLE_INTERNAL')) {
|
|
|
26 |
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
require_once($CFG->dirroot.'/lib/formslib.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Class user_edit_form.
|
|
|
33 |
*
|
|
|
34 |
* @copyright 1999 Martin Dougiamas http://dougiamas.com
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class user_edit_form extends moodleform {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Define the form.
|
|
|
41 |
*/
|
|
|
42 |
public function definition() {
|
|
|
43 |
global $CFG, $COURSE, $USER;
|
|
|
44 |
|
|
|
45 |
$mform = $this->_form;
|
|
|
46 |
$editoroptions = null;
|
|
|
47 |
$filemanageroptions = null;
|
|
|
48 |
$usernotfullysetup = user_not_fully_set_up($USER);
|
|
|
49 |
|
|
|
50 |
if (!is_array($this->_customdata)) {
|
|
|
51 |
throw new coding_exception('invalid custom data for user_edit_form');
|
|
|
52 |
}
|
|
|
53 |
$editoroptions = $this->_customdata['editoroptions'];
|
|
|
54 |
$filemanageroptions = $this->_customdata['filemanageroptions'];
|
|
|
55 |
$user = $this->_customdata['user'];
|
|
|
56 |
$userid = $user->id;
|
|
|
57 |
|
|
|
58 |
if (empty($user->country)) {
|
|
|
59 |
// We must unset the value here so $CFG->country can be used as default one.
|
|
|
60 |
unset($user->country);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Accessibility: "Required" is bad legend text.
|
|
|
64 |
$strgeneral = get_string('general');
|
|
|
65 |
$strrequired = get_string('required');
|
|
|
66 |
|
|
|
67 |
// Add some extra hidden fields.
|
|
|
68 |
$mform->addElement('hidden', 'id');
|
|
|
69 |
$mform->setType('id', PARAM_INT);
|
|
|
70 |
$mform->addElement('hidden', 'course', $COURSE->id);
|
|
|
71 |
$mform->setType('course', PARAM_INT);
|
|
|
72 |
|
|
|
73 |
// Print the required moodle fields first.
|
|
|
74 |
$mform->addElement('header', 'moodle', $strgeneral);
|
|
|
75 |
|
|
|
76 |
// Shared fields.
|
|
|
77 |
useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
|
|
|
78 |
|
|
|
79 |
// Extra settigs.
|
|
|
80 |
if (!empty($CFG->disableuserimages) || $usernotfullysetup) {
|
|
|
81 |
$mform->removeElement('deletepicture');
|
|
|
82 |
$mform->removeElement('imagefile');
|
|
|
83 |
$mform->removeElement('imagealt');
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
// If the user isn't fully set up, let them know that they will be able to change
|
|
|
87 |
// their profile picture once their profile is complete.
|
|
|
88 |
if ($usernotfullysetup) {
|
|
|
89 |
$userpicturewarning = $mform->createElement('warning', 'userpicturewarning', 'notifymessage', get_string('newpictureusernotsetup'));
|
|
|
90 |
$enabledusernamefields = useredit_get_enabled_name_fields();
|
|
|
91 |
if ($mform->elementExists('moodle_additional_names')) {
|
|
|
92 |
$mform->insertElementBefore($userpicturewarning, 'moodle_additional_names');
|
|
|
93 |
} else if ($mform->elementExists('moodle_interests')) {
|
|
|
94 |
$mform->insertElementBefore($userpicturewarning, 'moodle_interests');
|
|
|
95 |
} else {
|
|
|
96 |
$mform->insertElementBefore($userpicturewarning, 'moodle_optional');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// This is expected to exist when the form is submitted.
|
|
|
100 |
$imagefile = $mform->createElement('hidden', 'imagefile');
|
|
|
101 |
$mform->insertElementBefore($imagefile, 'userpicturewarning');
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Next the customisable profile fields.
|
|
|
105 |
profile_definition($mform, $userid);
|
|
|
106 |
|
|
|
107 |
$this->add_action_buttons(true, get_string('updatemyprofile'));
|
|
|
108 |
|
|
|
109 |
$this->set_data($user);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Extend the form definition after the data has been parsed.
|
|
|
114 |
*/
|
|
|
115 |
public function definition_after_data() {
|
|
|
116 |
global $CFG, $DB, $OUTPUT;
|
|
|
117 |
|
|
|
118 |
$mform = $this->_form;
|
|
|
119 |
$userid = $mform->getElementValue('id');
|
|
|
120 |
|
|
|
121 |
// Trim required name fields.
|
|
|
122 |
foreach (useredit_get_required_name_fields() as $field) {
|
|
|
123 |
$mform->applyFilter($field, 'trim');
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if ($user = $DB->get_record('user', array('id' => $userid))) {
|
|
|
127 |
|
|
|
128 |
// Remove description.
|
|
|
129 |
if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
|
|
|
130 |
$mform->removeElement('description_editor');
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// Print picture.
|
|
|
134 |
$context = context_user::instance($user->id, MUST_EXIST);
|
|
|
135 |
$fs = get_file_storage();
|
|
|
136 |
$hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
|
|
|
137 |
if (!empty($user->picture) && $hasuploadedpicture) {
|
|
|
138 |
$imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
|
|
|
139 |
} else {
|
|
|
140 |
$imagevalue = get_string('none');
|
|
|
141 |
}
|
|
|
142 |
$imageelement = $mform->getElement('currentpicture');
|
|
|
143 |
$imageelement->setValue($imagevalue);
|
|
|
144 |
|
|
|
145 |
if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
|
|
|
146 |
$mform->removeElement('deletepicture');
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
// Disable fields that are locked by auth plugins.
|
|
|
150 |
$fields = get_user_fieldnames();
|
|
|
151 |
$authplugin = get_auth_plugin($user->auth);
|
|
|
152 |
$customfields = $authplugin->get_custom_user_profile_fields();
|
|
|
153 |
$customfieldsdata = profile_user_record($userid, false);
|
|
|
154 |
$fields = array_merge($fields, $customfields);
|
|
|
155 |
foreach ($fields as $field) {
|
|
|
156 |
if ($field === 'description') {
|
|
|
157 |
// Hard coded hack for description field. See MDL-37704 for details.
|
|
|
158 |
$formfield = 'description_editor';
|
|
|
159 |
} else {
|
|
|
160 |
$formfield = $field;
|
|
|
161 |
}
|
|
|
162 |
if (!$mform->elementExists($formfield)) {
|
|
|
163 |
continue;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Get the original value for the field.
|
|
|
167 |
if (in_array($field, $customfields)) {
|
|
|
168 |
$key = str_replace('profile_field_', '', $field);
|
|
|
169 |
$value = isset($customfieldsdata->{$key}) ? $customfieldsdata->{$key} : '';
|
|
|
170 |
} else {
|
|
|
171 |
$value = $user->{$field};
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$configvariable = 'field_lock_' . $field;
|
|
|
175 |
if (isset($authplugin->config->{$configvariable})) {
|
|
|
176 |
if ($authplugin->config->{$configvariable} === 'locked') {
|
|
|
177 |
$mform->hardFreeze($formfield);
|
|
|
178 |
$mform->setConstant($formfield, $value);
|
|
|
179 |
} else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
|
|
|
180 |
$mform->hardFreeze($formfield);
|
|
|
181 |
$mform->setConstant($formfield, $value);
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// Next the customisable profile fields.
|
|
|
187 |
profile_definition_after_data($mform, $user->id);
|
|
|
188 |
|
|
|
189 |
} else {
|
|
|
190 |
profile_definition_after_data($mform, 0);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Validate incoming form data.
|
|
|
196 |
* @param array $usernew
|
|
|
197 |
* @param array $files
|
|
|
198 |
* @return array
|
|
|
199 |
*/
|
|
|
200 |
public function validation($usernew, $files) {
|
|
|
201 |
global $CFG, $DB;
|
|
|
202 |
|
|
|
203 |
$errors = parent::validation($usernew, $files);
|
|
|
204 |
|
|
|
205 |
$usernew = (object)$usernew;
|
|
|
206 |
$user = $DB->get_record('user', array('id' => $usernew->id));
|
|
|
207 |
|
|
|
208 |
// Validate email.
|
|
|
209 |
if (!isset($usernew->email)) {
|
|
|
210 |
// Mail not confirmed yet.
|
|
|
211 |
} else if (!validate_email($usernew->email)) {
|
|
|
212 |
$errors['email'] = get_string('invalidemail');
|
|
|
213 |
} else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
|
|
|
214 |
// Make a case-insensitive query for the given email address.
|
|
|
215 |
$select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
|
|
|
216 |
$params = array(
|
|
|
217 |
'email' => $usernew->email,
|
|
|
218 |
'mnethostid' => $CFG->mnet_localhost_id,
|
|
|
219 |
'userid' => $usernew->id
|
|
|
220 |
);
|
|
|
221 |
// If there are other user(s) that already have the same email, show an error.
|
|
|
222 |
if ($DB->record_exists_select('user', $select, $params)) {
|
|
|
223 |
$errors['email'] = get_string('emailexists');
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
|
|
|
228 |
$errors['email'] = get_string('toomanybounces');
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
|
|
|
232 |
$errorstr = email_is_not_allowed($usernew->email);
|
|
|
233 |
if ($errorstr !== false) {
|
|
|
234 |
$errors['email'] = $errorstr;
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
// Next the customisable profile fields.
|
|
|
239 |
$errors += profile_validation($usernew, $files);
|
|
|
240 |
|
|
|
241 |
return $errors;
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|