MDL-45242 Lib: Allow custom profile fields in showuseridentity

This commit is contained in:
sam marshall 2020-10-12 15:24:07 +01:00
parent 9ddb51b07e
commit 677e1c6248
9 changed files with 1353 additions and 92 deletions

View file

@ -848,6 +848,36 @@ function profile_save_custom_fields($userid, $profilefields) {
}
}
/**
* Gets basic data about custom profile fields. This is minimal data that is cached within the
* current request for all fields so that it can be used quickly.
*
* @param string $shortname Shortname of custom profile field
* @return array Array with id, name, and visible fields
*/
function profile_get_custom_field_data_by_shortname(string $shortname): array {
global $DB;
$cache = \cache::make_from_params(cache_store::MODE_REQUEST, 'core_profile', 'customfields',
[], ['simplekeys' => true, 'simpledata' => true]);
$data = $cache->get($shortname);
if (!$data) {
// If we don't have data, we get and cache it for all fields to avoid multiple DB requests.
$fields = $DB->get_records('user_info_field', null, '', 'id, shortname, name, visible');
foreach ($fields as $field) {
$cache->set($field->shortname, (array)$field);
if ($field->shortname === $shortname) {
$data = (array)$field;
}
}
if (!$data) {
throw new \coding_exception('Unknown custom field: ' . $shortname);
}
}
return $data;
}
/**
* Trigger a user profile viewed event.
*

View file

@ -240,4 +240,48 @@ class core_user_profilelib_testcase extends advanced_testcase {
$this->assertObjectHasAttribute('house', $profilefields2);
$this->assertNull($profilefields2->house);
}
/**
* Tests the profile_get_custom_field_data_by_shortname function when working normally.
*/
public function test_profile_get_custom_field_data_by_shortname_normal() {
global $DB, $CFG;
require_once($CFG->dirroot . '/user/profile/lib.php');
$this->resetAfterTest();
// Create 3 profile fields.
$generator = $this->getDataGenerator();
$field1 = $generator->create_custom_profile_field(['datatype' => 'text',
'shortname' => 'speciality', 'name' => 'Speciality',
'visible' => PROFILE_VISIBLE_ALL]);
$field2 = $generator->create_custom_profile_field(['datatype' => 'menu',
'shortname' => 'veggie', 'name' => 'Vegetarian',
'visible' => PROFILE_VISIBLE_PRIVATE]);
// Get the first field data and check it is correct.
$data = profile_get_custom_field_data_by_shortname('speciality');
$this->assertEquals('Speciality', $data['name']);
$this->assertEquals(PROFILE_VISIBLE_ALL, $data['visible']);
$this->assertEquals($field1->id, $data['id']);
// Get the second field data, checking there is no database query this time.
$before = $DB->perf_get_queries();
$data = profile_get_custom_field_data_by_shortname('veggie');
$this->assertEquals($before, $DB->perf_get_queries());
$this->assertEquals('Vegetarian', $data['name']);
$this->assertEquals(PROFILE_VISIBLE_PRIVATE, $data['visible']);
$this->assertEquals($field2->id, $data['id']);
}
/**
* Tests the profile_get_custom_field_data_by_shortname function with a field that doesn't exist.
*/
public function test_profile_get_custom_field_data_by_shortname_missing() {
global $CFG;
require_once($CFG->dirroot . '/user/profile/lib.php');
$this->expectExceptionMessage('Unknown custom field: speciality');
profile_get_custom_field_data_by_shortname('speciality');
}
}