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 |
* Profile field API library file.
|
|
|
19 |
*
|
|
|
20 |
* @package core_user
|
|
|
21 |
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Visible to anyone who has the moodle/site:viewuseridentity permission.
|
|
|
27 |
* Editable by the profile owner if they have the moodle/user:editownprofile capability
|
|
|
28 |
* or any user with the moodle/user:update capability.
|
|
|
29 |
*/
|
|
|
30 |
define('PROFILE_VISIBLE_TEACHERS', '3');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Visible to anyone who can view the user.
|
|
|
34 |
* Editable by the profile owner if they have the moodle/user:editownprofile capability
|
|
|
35 |
* or any user with the moodle/user:update capability.
|
|
|
36 |
*/
|
|
|
37 |
define('PROFILE_VISIBLE_ALL', '2');
|
|
|
38 |
/**
|
|
|
39 |
* Visible to the profile owner or anyone with the moodle/user:viewalldetails capability.
|
|
|
40 |
* Editable by the profile owner if they have the moodle/user:editownprofile capability
|
|
|
41 |
* or any user with moodle/user:viewalldetails and moodle/user:update capabilities.
|
|
|
42 |
*/
|
|
|
43 |
define('PROFILE_VISIBLE_PRIVATE', '1');
|
|
|
44 |
/**
|
|
|
45 |
* Only visible to users with the moodle/user:viewalldetails capability.
|
|
|
46 |
* Only editable by users with the moodle/user:viewalldetails and moodle/user:update capabilities.
|
|
|
47 |
*/
|
|
|
48 |
define('PROFILE_VISIBLE_NONE', '0');
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Base class for the customisable profile fields.
|
|
|
52 |
*
|
|
|
53 |
* @package core_user
|
|
|
54 |
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
|
|
|
55 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
56 |
*/
|
|
|
57 |
class profile_field_base {
|
|
|
58 |
|
|
|
59 |
// These 2 variables are really what we're interested in.
|
|
|
60 |
// Everything else can be extracted from them.
|
|
|
61 |
|
|
|
62 |
/** @var int */
|
|
|
63 |
public $fieldid;
|
|
|
64 |
|
|
|
65 |
/** @var int */
|
|
|
66 |
public $userid;
|
|
|
67 |
|
|
|
68 |
/** @var stdClass */
|
|
|
69 |
public $field;
|
|
|
70 |
|
|
|
71 |
/** @var string */
|
|
|
72 |
public $inputname;
|
|
|
73 |
|
|
|
74 |
/** @var mixed */
|
|
|
75 |
public $data;
|
|
|
76 |
|
|
|
77 |
/** @var string */
|
|
|
78 |
public $dataformat;
|
|
|
79 |
|
|
|
80 |
/** @var string name of the user profile category */
|
|
|
81 |
protected $categoryname;
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Constructor method.
|
|
|
85 |
* @param int $fieldid id of the profile from the user_info_field table
|
|
|
86 |
* @param int $userid id of the user for whom we are displaying data
|
|
|
87 |
* @param stdClass $fielddata optional data for the field object plus additional fields 'hasuserdata', 'data' and 'dataformat'
|
|
|
88 |
* with user data. (If $fielddata->hasuserdata is empty, user data is not available and we should use default data).
|
|
|
89 |
* If this parameter is passed, constructor will not call load_data() at all.
|
|
|
90 |
*/
|
|
|
91 |
public function __construct($fieldid=0, $userid=0, $fielddata=null) {
|
|
|
92 |
global $CFG;
|
|
|
93 |
|
|
|
94 |
if ($CFG->debugdeveloper) {
|
|
|
95 |
// In Moodle 3.4 the new argument $fielddata was added to the constructor. Make sure that
|
|
|
96 |
// plugin constructor properly passes this argument.
|
|
|
97 |
$backtrace = debug_backtrace();
|
|
|
98 |
if (isset($backtrace[1]['class']) && $backtrace[1]['function'] === '__construct' &&
|
|
|
99 |
in_array(self::class, class_parents($backtrace[1]['class']))) {
|
|
|
100 |
// If this constructor is called from the constructor of the plugin make sure that the third argument was passed through.
|
|
|
101 |
if (count($backtrace[1]['args']) >= 3 && count($backtrace[0]['args']) < 3) {
|
|
|
102 |
debugging($backtrace[1]['class'].'::__construct() must support $fielddata as the third argument ' .
|
|
|
103 |
'and pass it to the parent constructor', DEBUG_DEVELOPER);
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
$this->set_fieldid($fieldid);
|
|
|
109 |
$this->set_userid($userid);
|
|
|
110 |
if ($fielddata) {
|
|
|
111 |
$this->set_field($fielddata);
|
|
|
112 |
if ($userid > 0 && !empty($fielddata->hasuserdata)) {
|
|
|
113 |
$this->set_user_data($fielddata->data, $fielddata->dataformat);
|
|
|
114 |
}
|
|
|
115 |
} else {
|
|
|
116 |
$this->load_data();
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Old syntax of class constructor. Deprecated in PHP7.
|
|
|
122 |
*
|
|
|
123 |
* @deprecated since Moodle 3.1
|
|
|
124 |
*/
|
|
|
125 |
public function profile_field_base($fieldid=0, $userid=0) {
|
|
|
126 |
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
|
|
|
127 |
self::__construct($fieldid, $userid);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Abstract method: Adds the profile field to the moodle form class
|
|
|
132 |
* @abstract The following methods must be overwritten by child classes
|
|
|
133 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
134 |
*/
|
|
|
135 |
public function edit_field_add($mform) {
|
|
|
136 |
throw new \moodle_exception('mustbeoveride', 'debug', '', 'edit_field_add');
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Display the data for this field
|
|
|
141 |
* @return string
|
|
|
142 |
*/
|
|
|
143 |
public function display_data() {
|
|
|
144 |
$options = new stdClass();
|
|
|
145 |
$options->para = false;
|
|
|
146 |
return format_text($this->data, FORMAT_MOODLE, $options);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Display the name of the profile field.
|
|
|
151 |
*
|
|
|
152 |
* @return string
|
|
|
153 |
*/
|
|
|
154 |
public function display_name(): string {
|
|
|
155 |
return format_text($this->field->name, FORMAT_MOODLE, [
|
|
|
156 |
'para' => false,
|
|
|
157 |
]);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Print out the form field in the edit profile page
|
|
|
162 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
163 |
* @return bool
|
|
|
164 |
*/
|
|
|
165 |
public function edit_field($mform) {
|
|
|
166 |
if (!$this->is_editable()) {
|
|
|
167 |
return false;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
$this->edit_field_add($mform);
|
|
|
171 |
$this->edit_field_set_default($mform);
|
|
|
172 |
$this->edit_field_set_required($mform);
|
|
|
173 |
return true;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Tweaks the edit form
|
|
|
178 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
179 |
* @return bool
|
|
|
180 |
*/
|
|
|
181 |
public function edit_after_data($mform) {
|
|
|
182 |
if (!$this->is_editable()) {
|
|
|
183 |
return false;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
$this->edit_field_set_locked($mform);
|
|
|
187 |
return true;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Saves the data coming from form
|
|
|
192 |
* @param stdClass $usernew data coming from the form
|
|
|
193 |
*/
|
|
|
194 |
public function edit_save_data($usernew) {
|
|
|
195 |
global $DB;
|
|
|
196 |
|
|
|
197 |
if (!isset($usernew->{$this->inputname})) {
|
|
|
198 |
// Field not present in form, probably locked and invisible - skip it.
|
|
|
199 |
return;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
$data = new stdClass();
|
|
|
203 |
|
|
|
204 |
$usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
|
|
|
205 |
if (!isset($usernew->{$this->inputname})) {
|
|
|
206 |
// Field cannot be set to null, set the default value.
|
|
|
207 |
$usernew->{$this->inputname} = $this->field->defaultdata;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
$data->userid = $usernew->id;
|
|
|
211 |
$data->fieldid = $this->field->id;
|
|
|
212 |
$data->data = $usernew->{$this->inputname};
|
|
|
213 |
|
|
|
214 |
if ($dataid = $DB->get_field('user_info_data', 'id', array('userid' => $data->userid, 'fieldid' => $data->fieldid))) {
|
|
|
215 |
$data->id = $dataid;
|
|
|
216 |
$DB->update_record('user_info_data', $data);
|
|
|
217 |
} else {
|
|
|
218 |
$DB->insert_record('user_info_data', $data);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Validate the form field from profile page
|
|
|
224 |
*
|
|
|
225 |
* @param stdClass $usernew
|
|
|
226 |
* @return array error messages for the form validation
|
|
|
227 |
*/
|
|
|
228 |
public function edit_validate_field($usernew) {
|
|
|
229 |
global $DB;
|
|
|
230 |
|
|
|
231 |
$errors = array();
|
|
|
232 |
// Get input value.
|
|
|
233 |
if (isset($usernew->{$this->inputname})) {
|
|
|
234 |
if (is_array($usernew->{$this->inputname}) && isset($usernew->{$this->inputname}['text'])) {
|
|
|
235 |
$value = $usernew->{$this->inputname}['text'];
|
|
|
236 |
} else {
|
|
|
237 |
$value = $usernew->{$this->inputname};
|
|
|
238 |
}
|
|
|
239 |
} else {
|
|
|
240 |
$value = '';
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
// Check for uniqueness of data if required.
|
|
|
244 |
if ($this->is_unique() && (($value !== '') || $this->is_required())) {
|
|
|
245 |
$data = $DB->get_records_sql('
|
|
|
246 |
SELECT id, userid
|
|
|
247 |
FROM {user_info_data}
|
|
|
248 |
WHERE fieldid = ?
|
|
|
249 |
AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
|
|
|
250 |
array($this->field->id, $value));
|
|
|
251 |
if ($data) {
|
|
|
252 |
$existing = false;
|
|
|
253 |
foreach ($data as $v) {
|
|
|
254 |
if ($v->userid == $usernew->id) {
|
|
|
255 |
$existing = true;
|
|
|
256 |
break;
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
if (!$existing) {
|
|
|
260 |
$errors[$this->inputname] = get_string('valuealreadyused');
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
return $errors;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Sets the default data for the field in the form object
|
|
|
269 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
270 |
*/
|
|
|
271 |
public function edit_field_set_default($mform) {
|
|
|
272 |
if (isset($this->field->defaultdata)) {
|
|
|
273 |
$mform->setDefault($this->inputname, $this->field->defaultdata);
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Sets the required flag for the field in the form object
|
|
|
279 |
*
|
|
|
280 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
281 |
*/
|
|
|
282 |
public function edit_field_set_required($mform) {
|
|
|
283 |
global $USER;
|
|
|
284 |
if ($this->is_required() && ($this->userid == $USER->id || isguestuser())) {
|
|
|
285 |
$mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* HardFreeze the field if locked.
|
|
|
291 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
292 |
*/
|
|
|
293 |
public function edit_field_set_locked($mform) {
|
|
|
294 |
if (!$mform->elementExists($this->inputname)) {
|
|
|
295 |
return;
|
|
|
296 |
}
|
|
|
297 |
if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) {
|
|
|
298 |
$mform->hardFreeze($this->inputname);
|
|
|
299 |
$mform->setConstant($this->inputname, $this->data);
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Hook for child classess to process the data before it gets saved in database
|
|
|
305 |
* @param stdClass $data
|
|
|
306 |
* @param stdClass $datarecord The object that will be used to save the record
|
|
|
307 |
* @return mixed
|
|
|
308 |
*/
|
|
|
309 |
public function edit_save_data_preprocess($data, $datarecord) {
|
|
|
310 |
return $data;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Loads a user object with data for this field ready for the edit profile
|
|
|
315 |
* form
|
|
|
316 |
* @param stdClass $user a user object
|
|
|
317 |
*/
|
|
|
318 |
public function edit_load_user_data($user) {
|
|
|
319 |
if ($this->data !== null) {
|
|
|
320 |
$user->{$this->inputname} = $this->data;
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Check if the field data should be loaded into the user object
|
|
|
326 |
* By default it is, but for field types where the data may be potentially
|
|
|
327 |
* large, the child class should override this and return false
|
|
|
328 |
* @return bool
|
|
|
329 |
*/
|
|
|
330 |
public function is_user_object_data() {
|
|
|
331 |
return true;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Accessor method: set the userid for this instance
|
|
|
336 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
337 |
* @param integer $userid id from the user table
|
|
|
338 |
*/
|
|
|
339 |
public function set_userid($userid) {
|
|
|
340 |
$this->userid = $userid;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Accessor method: set the fieldid for this instance
|
|
|
345 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
346 |
* @param integer $fieldid id from the user_info_field table
|
|
|
347 |
*/
|
|
|
348 |
public function set_fieldid($fieldid) {
|
|
|
349 |
$this->fieldid = $fieldid;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
/**
|
|
|
353 |
* Sets the field object and default data and format into $this->data and $this->dataformat
|
|
|
354 |
*
|
|
|
355 |
* This method should be called before {@link self::set_user_data}
|
|
|
356 |
*
|
|
|
357 |
* @param stdClass $field
|
|
|
358 |
* @throws coding_exception
|
|
|
359 |
*/
|
|
|
360 |
public function set_field($field) {
|
|
|
361 |
global $CFG;
|
|
|
362 |
if ($CFG->debugdeveloper) {
|
|
|
363 |
$properties = ['id', 'shortname', 'name', 'datatype', 'description', 'descriptionformat', 'categoryid', 'sortorder',
|
|
|
364 |
'required', 'locked', 'visible', 'forceunique', 'signup', 'defaultdata', 'defaultdataformat', 'param1', 'param2',
|
|
|
365 |
'param3', 'param4', 'param5'];
|
|
|
366 |
foreach ($properties as $property) {
|
|
|
367 |
if (!property_exists($field, $property)) {
|
|
|
368 |
debugging('The \'' . $property . '\' property must be set.', DEBUG_DEVELOPER);
|
|
|
369 |
}
|
|
|
370 |
}
|
|
|
371 |
}
|
|
|
372 |
if ($this->fieldid && $this->fieldid != $field->id) {
|
|
|
373 |
throw new coding_exception('Can not set field object after a different field id was set');
|
|
|
374 |
}
|
|
|
375 |
$this->fieldid = $field->id;
|
|
|
376 |
$this->field = $field;
|
|
|
377 |
$this->inputname = 'profile_field_' . $this->field->shortname;
|
|
|
378 |
$this->data = $this->field->defaultdata;
|
|
|
379 |
$this->dataformat = FORMAT_HTML;
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
/**
|
|
|
383 |
* Sets user id and user data for the field
|
|
|
384 |
*
|
|
|
385 |
* @param mixed $data
|
|
|
386 |
* @param int $dataformat
|
|
|
387 |
*/
|
|
|
388 |
public function set_user_data($data, $dataformat) {
|
|
|
389 |
$this->data = $data;
|
|
|
390 |
$this->dataformat = $dataformat;
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
/**
|
|
|
394 |
* Set the name for the profile category where this field is
|
|
|
395 |
*
|
|
|
396 |
* @param string $categoryname
|
|
|
397 |
*/
|
|
|
398 |
public function set_category_name($categoryname) {
|
|
|
399 |
$this->categoryname = $categoryname;
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
/**
|
|
|
403 |
* Return field short name
|
|
|
404 |
*
|
|
|
405 |
* @return string
|
|
|
406 |
*/
|
|
|
407 |
public function get_shortname(): string {
|
|
|
408 |
return $this->field->shortname;
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* Returns the name of the profile category where this field is
|
|
|
413 |
*
|
|
|
414 |
* @return string
|
|
|
415 |
*/
|
|
|
416 |
public function get_category_name() {
|
|
|
417 |
global $DB;
|
|
|
418 |
if ($this->categoryname === null) {
|
|
|
419 |
$this->categoryname = $DB->get_field('user_info_category', 'name', ['id' => $this->field->categoryid]);
|
|
|
420 |
}
|
|
|
421 |
return $this->categoryname;
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
/**
|
|
|
425 |
* Accessor method: Load the field record and user data associated with the
|
|
|
426 |
* object's fieldid and userid
|
|
|
427 |
*
|
|
|
428 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
429 |
*/
|
|
|
430 |
public function load_data() {
|
|
|
431 |
global $DB;
|
|
|
432 |
|
|
|
433 |
// Load the field object.
|
|
|
434 |
if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id' => $this->fieldid))))) {
|
|
|
435 |
$this->field = null;
|
|
|
436 |
$this->inputname = '';
|
|
|
437 |
} else {
|
|
|
438 |
$this->set_field($field);
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
if (!empty($this->field) && $this->userid > 0) {
|
|
|
442 |
$params = array('userid' => $this->userid, 'fieldid' => $this->fieldid);
|
|
|
443 |
if ($data = $DB->get_record('user_info_data', $params, 'data, dataformat')) {
|
|
|
444 |
$this->set_user_data($data->data, $data->dataformat);
|
|
|
445 |
}
|
|
|
446 |
} else {
|
|
|
447 |
$this->data = null;
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* Check if the field data is visible to the current user
|
|
|
453 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
454 |
*
|
|
|
455 |
* @param context|null $context
|
|
|
456 |
* @return bool
|
|
|
457 |
*/
|
|
|
458 |
public function is_visible(?context $context = null): bool {
|
|
|
459 |
global $USER, $COURSE;
|
|
|
460 |
|
|
|
461 |
if ($context === null) {
|
|
|
462 |
$context = ($this->userid > 0) ? context_user::instance($this->userid) : context_system::instance();
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
switch ($this->field->visible) {
|
|
|
466 |
case PROFILE_VISIBLE_TEACHERS:
|
|
|
467 |
if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
|
|
|
468 |
return true;
|
|
|
469 |
} else if ($this->userid == $USER->id) {
|
|
|
470 |
return true;
|
|
|
471 |
} else if ($this->userid > 0) {
|
|
|
472 |
return has_capability('moodle/user:viewalldetails', $context);
|
|
|
473 |
} else {
|
|
|
474 |
$coursecontext = context_course::instance($COURSE->id);
|
|
|
475 |
return has_capability('moodle/site:viewuseridentity', $coursecontext);
|
|
|
476 |
}
|
|
|
477 |
case PROFILE_VISIBLE_ALL:
|
|
|
478 |
return true;
|
|
|
479 |
case PROFILE_VISIBLE_PRIVATE:
|
|
|
480 |
if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
|
|
|
481 |
return true;
|
|
|
482 |
} else if ($this->userid == $USER->id) {
|
|
|
483 |
return true;
|
|
|
484 |
} else {
|
|
|
485 |
return has_capability('moodle/user:viewalldetails', $context);
|
|
|
486 |
}
|
|
|
487 |
default:
|
|
|
488 |
// PROFILE_VISIBLE_NONE, so let's check capabilities at system level.
|
|
|
489 |
if ($this->userid > 0) {
|
|
|
490 |
$context = context_system::instance();
|
|
|
491 |
}
|
|
|
492 |
return has_capability('moodle/user:viewalldetails', $context);
|
|
|
493 |
}
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
/**
|
|
|
497 |
* Check if the field data is editable for the current user
|
|
|
498 |
* This method should not generally be overwritten by child classes.
|
|
|
499 |
* @return bool
|
|
|
500 |
*/
|
|
|
501 |
public function is_editable() {
|
|
|
502 |
global $USER;
|
|
|
503 |
|
|
|
504 |
if (!$this->is_visible()) {
|
|
|
505 |
return false;
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
|
|
|
509 |
// Allow editing the field on the signup page.
|
|
|
510 |
return true;
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
$systemcontext = context_system::instance();
|
|
|
514 |
|
|
|
515 |
if ($this->userid == $USER->id && has_capability('moodle/user:editownprofile', $systemcontext)) {
|
|
|
516 |
return true;
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
if (has_capability('moodle/user:update', $systemcontext)) {
|
|
|
520 |
return true;
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
// Checking for mentors have capability to edit user's profile.
|
|
|
524 |
if ($this->userid > 0) {
|
|
|
525 |
$usercontext = context_user::instance($this->userid);
|
|
|
526 |
if ($this->userid != $USER->id && has_capability('moodle/user:editprofile', $usercontext, $USER->id)) {
|
|
|
527 |
return true;
|
|
|
528 |
}
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
return false;
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* Check if the field data is considered empty
|
|
|
536 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
537 |
* @return boolean
|
|
|
538 |
*/
|
|
|
539 |
public function is_empty() {
|
|
|
540 |
return ( ($this->data != '0') and empty($this->data));
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
/**
|
|
|
544 |
* Check if the field is required on the edit profile page
|
|
|
545 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
546 |
* @return bool
|
|
|
547 |
*/
|
|
|
548 |
public function is_required() {
|
|
|
549 |
return (boolean)$this->field->required;
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
/**
|
|
|
553 |
* Check if the field is locked on the edit profile page
|
|
|
554 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
555 |
* @return bool
|
|
|
556 |
*/
|
|
|
557 |
public function is_locked() {
|
|
|
558 |
return (boolean)$this->field->locked;
|
|
|
559 |
}
|
|
|
560 |
|
|
|
561 |
/**
|
|
|
562 |
* Check if the field data should be unique
|
|
|
563 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
564 |
* @return bool
|
|
|
565 |
*/
|
|
|
566 |
public function is_unique() {
|
|
|
567 |
return (boolean)$this->field->forceunique;
|
|
|
568 |
}
|
|
|
569 |
|
|
|
570 |
/**
|
|
|
571 |
* Check if the field should appear on the signup page
|
|
|
572 |
* @internal This method should not generally be overwritten by child classes.
|
|
|
573 |
* @return bool
|
|
|
574 |
*/
|
|
|
575 |
public function is_signup_field() {
|
|
|
576 |
return (boolean)$this->field->signup;
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* Return the field settings suitable to be exported via an external function.
|
|
|
581 |
* By default it return all the field settings.
|
|
|
582 |
*
|
|
|
583 |
* @return array all the settings
|
|
|
584 |
* @since Moodle 3.2
|
|
|
585 |
*/
|
|
|
586 |
public function get_field_config_for_external() {
|
|
|
587 |
return (array) $this->field;
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
/**
|
|
|
591 |
* Return the field type and null properties.
|
|
|
592 |
* This will be used for validating the data submitted by a user.
|
|
|
593 |
*
|
|
|
594 |
* @return array the param type and null property
|
|
|
595 |
* @since Moodle 3.2
|
|
|
596 |
*/
|
|
|
597 |
public function get_field_properties() {
|
|
|
598 |
return array(PARAM_RAW, NULL_NOT_ALLOWED);
|
|
|
599 |
}
|
|
|
600 |
|
|
|
601 |
/**
|
|
|
602 |
* Whether to display the field and content to the user
|
|
|
603 |
*
|
|
|
604 |
* @param context|null $context
|
|
|
605 |
* @return bool
|
|
|
606 |
*/
|
|
|
607 |
public function show_field_content(?context $context = null): bool {
|
|
|
608 |
return $this->is_visible($context) && !$this->is_empty();
|
|
|
609 |
}
|
|
|
610 |
|
|
|
611 |
/**
|
|
|
612 |
* Check if the field should convert the raw data into user-friendly data when exporting
|
|
|
613 |
*
|
|
|
614 |
* @return bool
|
|
|
615 |
*/
|
|
|
616 |
public function is_transform_supported(): bool {
|
|
|
617 |
return false;
|
|
|
618 |
}
|
|
|
619 |
}
|
|
|
620 |
|
|
|
621 |
/**
|
|
|
622 |
* Return profile field instance for given type
|
|
|
623 |
*
|
|
|
624 |
* @param string $type
|
|
|
625 |
* @param int $fieldid
|
|
|
626 |
* @param int $userid
|
|
|
627 |
* @param stdClass|null $fielddata
|
|
|
628 |
* @return profile_field_base
|
|
|
629 |
*/
|
|
|
630 |
function profile_get_user_field(string $type, int $fieldid = 0, int $userid = 0, ?stdClass $fielddata = null): profile_field_base {
|
|
|
631 |
global $CFG;
|
|
|
632 |
|
|
|
633 |
require_once("{$CFG->dirroot}/user/profile/field/{$type}/field.class.php");
|
|
|
634 |
|
|
|
635 |
// Return instance of profile field type.
|
|
|
636 |
$profilefieldtype = "profile_field_{$type}";
|
|
|
637 |
return new $profilefieldtype($fieldid, $userid, $fielddata);
|
|
|
638 |
}
|
|
|
639 |
|
|
|
640 |
/**
|
|
|
641 |
* Returns an array of all custom field records with any defined data (or empty data), for the specified user id.
|
|
|
642 |
* @param int $userid
|
|
|
643 |
* @return profile_field_base[]
|
|
|
644 |
*/
|
|
|
645 |
function profile_get_user_fields_with_data(int $userid): array {
|
|
|
646 |
global $DB;
|
|
|
647 |
|
|
|
648 |
// Join any user info data present with each user info field for the user object.
|
|
|
649 |
$sql = 'SELECT uif.*, uic.name AS categoryname ';
|
|
|
650 |
if ($userid > 0) {
|
|
|
651 |
$sql .= ', uind.id AS hasuserdata, uind.data, uind.dataformat ';
|
|
|
652 |
}
|
|
|
653 |
$sql .= 'FROM {user_info_field} uif ';
|
|
|
654 |
$sql .= 'LEFT JOIN {user_info_category} uic ON uif.categoryid = uic.id ';
|
|
|
655 |
if ($userid > 0) {
|
|
|
656 |
$sql .= 'LEFT JOIN {user_info_data} uind ON uif.id = uind.fieldid AND uind.userid = :userid ';
|
|
|
657 |
}
|
|
|
658 |
$sql .= 'ORDER BY uic.sortorder ASC, uif.sortorder ASC ';
|
|
|
659 |
$fields = $DB->get_records_sql($sql, ['userid' => $userid]);
|
|
|
660 |
$data = [];
|
|
|
661 |
foreach ($fields as $field) {
|
|
|
662 |
$field->hasuserdata = !empty($field->hasuserdata);
|
|
|
663 |
$fieldobject = profile_get_user_field($field->datatype, $field->id, $userid, $field);
|
|
|
664 |
$fieldobject->set_category_name($field->categoryname);
|
|
|
665 |
unset($field->categoryname);
|
|
|
666 |
$data[] = $fieldobject;
|
|
|
667 |
}
|
|
|
668 |
return $data;
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
/**
|
|
|
672 |
* Returns an array of all custom field records with any defined data (or empty data), for the specified user id, by category.
|
|
|
673 |
* @param int $userid
|
|
|
674 |
* @return profile_field_base[][]
|
|
|
675 |
*/
|
|
|
676 |
function profile_get_user_fields_with_data_by_category(int $userid): array {
|
|
|
677 |
$fields = profile_get_user_fields_with_data($userid);
|
|
|
678 |
$data = [];
|
|
|
679 |
foreach ($fields as $field) {
|
|
|
680 |
$data[$field->field->categoryid][] = $field;
|
|
|
681 |
}
|
|
|
682 |
return $data;
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
/**
|
|
|
686 |
* Loads user profile field data into the user object.
|
|
|
687 |
* @param stdClass $user
|
|
|
688 |
*/
|
|
|
689 |
function profile_load_data(stdClass $user): void {
|
|
|
690 |
$fields = profile_get_user_fields_with_data($user->id);
|
|
|
691 |
foreach ($fields as $formfield) {
|
|
|
692 |
$formfield->edit_load_user_data($user);
|
|
|
693 |
}
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
/**
|
|
|
697 |
* Print out the customisable categories and fields for a users profile
|
|
|
698 |
*
|
|
|
699 |
* @param MoodleQuickForm $mform instance of the moodleform class
|
|
|
700 |
* @param int $userid id of user whose profile is being edited or 0 for the new user
|
|
|
701 |
*/
|
|
|
702 |
function profile_definition(MoodleQuickForm $mform, int $userid = 0): void {
|
|
|
703 |
$categories = profile_get_user_fields_with_data_by_category($userid);
|
|
|
704 |
foreach ($categories as $categoryid => $fields) {
|
|
|
705 |
// Check first if *any* fields will be displayed.
|
|
|
706 |
$fieldstodisplay = [];
|
|
|
707 |
|
|
|
708 |
foreach ($fields as $formfield) {
|
|
|
709 |
if ($formfield->is_editable()) {
|
|
|
710 |
$fieldstodisplay[] = $formfield;
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
|
|
|
714 |
if (empty($fieldstodisplay)) {
|
|
|
715 |
continue;
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
// Display the header and the fields.
|
|
|
719 |
$mform->addElement('header', 'category_'.$categoryid, format_string($fields[0]->get_category_name()));
|
|
|
720 |
foreach ($fieldstodisplay as $formfield) {
|
|
|
721 |
$formfield->edit_field($mform);
|
|
|
722 |
}
|
|
|
723 |
}
|
|
|
724 |
}
|
|
|
725 |
|
|
|
726 |
/**
|
|
|
727 |
* Adds profile fields to user edit forms.
|
|
|
728 |
* @param MoodleQuickForm $mform
|
|
|
729 |
* @param int $userid
|
|
|
730 |
*/
|
|
|
731 |
function profile_definition_after_data(MoodleQuickForm $mform, int $userid): void {
|
|
|
732 |
$userid = ($userid < 0) ? 0 : (int)$userid;
|
|
|
733 |
|
|
|
734 |
$fields = profile_get_user_fields_with_data($userid);
|
|
|
735 |
foreach ($fields as $formfield) {
|
|
|
736 |
$formfield->edit_after_data($mform);
|
|
|
737 |
}
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
/**
|
|
|
741 |
* Validates profile data.
|
|
|
742 |
* @param stdClass $usernew
|
|
|
743 |
* @param array $files
|
|
|
744 |
* @return array array of errors, same as in {@see moodleform::validation()}
|
|
|
745 |
*/
|
|
|
746 |
function profile_validation(stdClass $usernew, array $files): array {
|
|
|
747 |
$err = array();
|
|
|
748 |
$fields = profile_get_user_fields_with_data($usernew->id);
|
|
|
749 |
foreach ($fields as $formfield) {
|
|
|
750 |
$err += $formfield->edit_validate_field($usernew, $files);
|
|
|
751 |
}
|
|
|
752 |
return $err;
|
|
|
753 |
}
|
|
|
754 |
|
|
|
755 |
/**
|
|
|
756 |
* Saves profile data for a user.
|
|
|
757 |
* @param stdClass $usernew
|
|
|
758 |
*/
|
|
|
759 |
function profile_save_data(stdClass $usernew): void {
|
|
|
760 |
global $CFG;
|
|
|
761 |
|
|
|
762 |
$fields = profile_get_user_fields_with_data($usernew->id);
|
|
|
763 |
foreach ($fields as $formfield) {
|
|
|
764 |
$formfield->edit_save_data($usernew);
|
|
|
765 |
}
|
|
|
766 |
}
|
|
|
767 |
|
|
|
768 |
/**
|
|
|
769 |
* Retrieves a list of profile fields that must be displayed in the sign-up form.
|
|
|
770 |
*
|
|
|
771 |
* @return array list of profile fields info
|
|
|
772 |
* @since Moodle 3.2
|
|
|
773 |
*/
|
|
|
774 |
function profile_get_signup_fields(): array {
|
|
|
775 |
$profilefields = array();
|
|
|
776 |
$fieldobjects = profile_get_user_fields_with_data(0);
|
|
|
777 |
foreach ($fieldobjects as $fieldobject) {
|
|
|
778 |
$field = (object)$fieldobject->get_field_config_for_external();
|
|
|
779 |
if ($fieldobject->get_category_name() !== null && $fieldobject->is_signup_field() && $field->visible <> 0) {
|
|
|
780 |
$profilefields[] = (object) array(
|
|
|
781 |
'categoryid' => $field->categoryid,
|
|
|
782 |
'categoryname' => $fieldobject->get_category_name(),
|
|
|
783 |
'fieldid' => $field->id,
|
|
|
784 |
'datatype' => $field->datatype,
|
|
|
785 |
'object' => $fieldobject
|
|
|
786 |
);
|
|
|
787 |
}
|
|
|
788 |
}
|
|
|
789 |
return $profilefields;
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
/**
|
|
|
793 |
* Adds code snippet to a moodle form object for custom profile fields that
|
|
|
794 |
* should appear on the signup page
|
|
|
795 |
* @param MoodleQuickForm $mform moodle form object
|
|
|
796 |
*/
|
|
|
797 |
function profile_signup_fields(MoodleQuickForm $mform): void {
|
|
|
798 |
|
|
|
799 |
if ($fields = profile_get_signup_fields()) {
|
|
|
800 |
foreach ($fields as $field) {
|
|
|
801 |
// Check if we change the categories.
|
|
|
802 |
if (!isset($currentcat) || $currentcat != $field->categoryid) {
|
|
|
803 |
$currentcat = $field->categoryid;
|
|
|
804 |
$mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
|
|
|
805 |
};
|
|
|
806 |
$field->object->edit_field($mform);
|
|
|
807 |
}
|
|
|
808 |
}
|
|
|
809 |
}
|
|
|
810 |
|
|
|
811 |
/**
|
|
|
812 |
* Returns an object with the custom profile fields set for the given user
|
|
|
813 |
* @param int $userid
|
|
|
814 |
* @param bool $onlyinuserobject True if you only want the ones in $USER.
|
|
|
815 |
* @return stdClass object where properties names are shortnames of custom profile fields
|
|
|
816 |
*/
|
|
|
817 |
function profile_user_record(int $userid, bool $onlyinuserobject = true): stdClass {
|
|
|
818 |
$usercustomfields = new stdClass();
|
|
|
819 |
|
|
|
820 |
$fields = profile_get_user_fields_with_data($userid);
|
|
|
821 |
foreach ($fields as $formfield) {
|
|
|
822 |
if (!$onlyinuserobject || $formfield->is_user_object_data()) {
|
|
|
823 |
$usercustomfields->{$formfield->field->shortname} = $formfield->data;
|
|
|
824 |
}
|
|
|
825 |
}
|
|
|
826 |
|
|
|
827 |
return $usercustomfields;
|
|
|
828 |
}
|
|
|
829 |
|
|
|
830 |
/**
|
|
|
831 |
* Obtains a list of all available custom profile fields, indexed by id.
|
|
|
832 |
*
|
|
|
833 |
* Some profile fields are not included in the user object data (see
|
|
|
834 |
* profile_user_record function above). Optionally, you can obtain only those
|
|
|
835 |
* fields that are included in the user object.
|
|
|
836 |
*
|
|
|
837 |
* To be clear, this function returns the available fields, and does not
|
|
|
838 |
* return the field values for a particular user.
|
|
|
839 |
*
|
|
|
840 |
* @param bool $onlyinuserobject True if you only want the ones in $USER
|
|
|
841 |
* @return array Array of field objects from database (indexed by id)
|
|
|
842 |
* @since Moodle 2.7.1
|
|
|
843 |
*/
|
|
|
844 |
function profile_get_custom_fields(bool $onlyinuserobject = false): array {
|
|
|
845 |
$fieldobjects = profile_get_user_fields_with_data(0);
|
|
|
846 |
$fields = [];
|
|
|
847 |
foreach ($fieldobjects as $fieldobject) {
|
|
|
848 |
if (!$onlyinuserobject || $fieldobject->is_user_object_data()) {
|
|
|
849 |
$fields[$fieldobject->fieldid] = (object)$fieldobject->get_field_config_for_external();
|
|
|
850 |
}
|
|
|
851 |
}
|
|
|
852 |
ksort($fields);
|
|
|
853 |
return $fields;
|
|
|
854 |
}
|
|
|
855 |
|
|
|
856 |
/**
|
|
|
857 |
* Load custom profile fields into user object
|
|
|
858 |
*
|
|
|
859 |
* @param stdClass $user user object
|
|
|
860 |
*/
|
|
|
861 |
function profile_load_custom_fields($user) {
|
|
|
862 |
$user->profile = (array)profile_user_record($user->id);
|
|
|
863 |
}
|
|
|
864 |
|
|
|
865 |
/**
|
|
|
866 |
* Save custom profile fields for a user.
|
|
|
867 |
*
|
|
|
868 |
* @param int $userid The user id
|
|
|
869 |
* @param array $profilefields The fields to save
|
|
|
870 |
*/
|
|
|
871 |
function profile_save_custom_fields($userid, $profilefields) {
|
|
|
872 |
global $DB;
|
|
|
873 |
|
|
|
874 |
$fields = profile_get_user_fields_with_data(0);
|
|
|
875 |
if ($fields) {
|
|
|
876 |
foreach ($fields as $fieldobject) {
|
|
|
877 |
$field = (object)$fieldobject->get_field_config_for_external();
|
|
|
878 |
if (isset($profilefields[$field->shortname])) {
|
|
|
879 |
$conditions = array('fieldid' => $field->id, 'userid' => $userid);
|
|
|
880 |
$id = $DB->get_field('user_info_data', 'id', $conditions);
|
|
|
881 |
$data = $profilefields[$field->shortname];
|
|
|
882 |
if ($id) {
|
|
|
883 |
$DB->set_field('user_info_data', 'data', $data, array('id' => $id));
|
|
|
884 |
} else {
|
|
|
885 |
$record = array('fieldid' => $field->id, 'userid' => $userid, 'data' => $data);
|
|
|
886 |
$DB->insert_record('user_info_data', $record);
|
|
|
887 |
}
|
|
|
888 |
}
|
|
|
889 |
}
|
|
|
890 |
}
|
|
|
891 |
}
|
|
|
892 |
|
|
|
893 |
/**
|
|
|
894 |
* Gets basic data about custom profile fields. This is minimal data that is cached within the
|
|
|
895 |
* current request for all fields so that it can be used quickly.
|
|
|
896 |
*
|
|
|
897 |
* @param string $shortname Shortname of custom profile field
|
|
|
898 |
* @param bool $casesensitive Whether to perform case-sensitive matching of shortname. Note current limitations of custom profile
|
|
|
899 |
* fields allow the same shortname to exist differing only by it's case
|
|
|
900 |
* @return stdClass|null Object with properties id, shortname, name, visible, datatype, categoryid, etc
|
|
|
901 |
*/
|
|
|
902 |
function profile_get_custom_field_data_by_shortname(string $shortname, bool $casesensitive = true): ?stdClass {
|
|
|
903 |
$cache = \cache::make_from_params(cache_store::MODE_REQUEST, 'core_profile', 'customfields',
|
|
|
904 |
[], ['simplekeys' => true, 'simpledata' => true]);
|
|
|
905 |
$data = $cache->get($shortname);
|
|
|
906 |
if ($data === false) {
|
|
|
907 |
// If we don't have data, we get and cache it for all fields to avoid multiple DB requests.
|
|
|
908 |
$fields = profile_get_custom_fields();
|
|
|
909 |
$data = null;
|
|
|
910 |
foreach ($fields as $field) {
|
|
|
911 |
$cache->set($field->shortname, $field);
|
|
|
912 |
|
|
|
913 |
// Perform comparison according to case sensitivity parameter.
|
|
|
914 |
$shortnamematch = $casesensitive
|
|
|
915 |
? strcmp($field->shortname, $shortname) === 0
|
|
|
916 |
: strcasecmp($field->shortname, $shortname) === 0;
|
|
|
917 |
|
|
|
918 |
if ($shortnamematch) {
|
|
|
919 |
$data = $field;
|
|
|
920 |
}
|
|
|
921 |
}
|
|
|
922 |
}
|
|
|
923 |
|
|
|
924 |
return $data;
|
|
|
925 |
}
|
|
|
926 |
|
|
|
927 |
/**
|
|
|
928 |
* Trigger a user profile viewed event.
|
|
|
929 |
*
|
|
|
930 |
* @param stdClass $user user object
|
|
|
931 |
* @param stdClass $context context object (course or user)
|
|
|
932 |
* @param stdClass $course course object
|
|
|
933 |
* @since Moodle 2.9
|
|
|
934 |
*/
|
|
|
935 |
function profile_view($user, $context, $course = null) {
|
|
|
936 |
|
|
|
937 |
$eventdata = array(
|
|
|
938 |
'objectid' => $user->id,
|
|
|
939 |
'relateduserid' => $user->id,
|
|
|
940 |
'context' => $context
|
|
|
941 |
);
|
|
|
942 |
|
|
|
943 |
if (!empty($course)) {
|
|
|
944 |
$eventdata['courseid'] = $course->id;
|
|
|
945 |
$eventdata['other'] = array(
|
|
|
946 |
'courseid' => $course->id,
|
|
|
947 |
'courseshortname' => $course->shortname,
|
|
|
948 |
'coursefullname' => $course->fullname
|
|
|
949 |
);
|
|
|
950 |
}
|
|
|
951 |
|
|
|
952 |
$event = \core\event\user_profile_viewed::create($eventdata);
|
|
|
953 |
$event->add_record_snapshot('user', $user);
|
|
|
954 |
$event->trigger();
|
|
|
955 |
}
|
|
|
956 |
|
|
|
957 |
/**
|
|
|
958 |
* Does the user have all required custom fields set?
|
|
|
959 |
*
|
|
|
960 |
* Internal, to be exclusively used by {@link user_not_fully_set_up()} only.
|
|
|
961 |
*
|
|
|
962 |
* Note that if users have no way to fill a required field via editing their
|
|
|
963 |
* profiles (e.g. the field is not visible or it is locked), we still return true.
|
|
|
964 |
* So this is actually checking if we should redirect the user to edit their
|
|
|
965 |
* profile, rather than whether there is a value in the database.
|
|
|
966 |
*
|
|
|
967 |
* @param int $userid
|
|
|
968 |
* @return bool
|
|
|
969 |
*/
|
|
|
970 |
function profile_has_required_custom_fields_set($userid) {
|
|
|
971 |
$profilefields = profile_get_user_fields_with_data($userid);
|
|
|
972 |
foreach ($profilefields as $profilefield) {
|
|
|
973 |
if ($profilefield->is_required() && !$profilefield->is_locked() &&
|
|
|
974 |
$profilefield->is_empty() && $profilefield->get_field_config_for_external()['visible']) {
|
|
|
975 |
return false;
|
|
|
976 |
}
|
|
|
977 |
}
|
|
|
978 |
|
|
|
979 |
return true;
|
|
|
980 |
}
|
|
|
981 |
|
|
|
982 |
/**
|
|
|
983 |
* Return the list of valid custom profile user fields.
|
|
|
984 |
*
|
|
|
985 |
* @return array array of profile field names
|
|
|
986 |
*/
|
|
|
987 |
function get_profile_field_names(): array {
|
|
|
988 |
$profilefields = profile_get_user_fields_with_data(0);
|
|
|
989 |
$profilefieldnames = [];
|
|
|
990 |
foreach ($profilefields as $field) {
|
|
|
991 |
$profilefieldnames[] = $field->inputname;
|
|
|
992 |
}
|
|
|
993 |
return $profilefieldnames;
|
|
|
994 |
}
|
|
|
995 |
|
|
|
996 |
/**
|
|
|
997 |
* Return the list of profile fields
|
|
|
998 |
* in a format they can be used for choices in a group select menu.
|
|
|
999 |
*
|
|
|
1000 |
* @return array array of category name with its profile fields
|
|
|
1001 |
*/
|
|
|
1002 |
function get_profile_field_list(): array {
|
|
|
1003 |
$customfields = profile_get_user_fields_with_data_by_category(0);
|
|
|
1004 |
$data = [];
|
|
|
1005 |
foreach ($customfields as $category) {
|
|
|
1006 |
foreach ($category as $field) {
|
|
|
1007 |
$categoryname = $field->get_category_name();
|
|
|
1008 |
if (!isset($data[$categoryname])) {
|
|
|
1009 |
$data[$categoryname] = [];
|
|
|
1010 |
}
|
|
|
1011 |
$data[$categoryname][$field->inputname] = $field->field->name;
|
|
|
1012 |
}
|
|
|
1013 |
}
|
|
|
1014 |
return $data;
|
|
|
1015 |
}
|