Skip to content

Commit

Permalink
Merge branch 'MDL-77967-main' of https://github.com/rmady/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Apr 11, 2024
2 parents 3ec5b46 + b2af915 commit b64dff6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function user_get_user_details($user, $course = null, array $userfields = array(
foreach ($fields as $formfield) {
if ($formfield->show_field_content()) {
$userdetails['customfields'][] = [
'name' => $formfield->field->name,
'name' => $formfield->display_name(),
'value' => $formfield->data,
'displayvalue' => $formfield->display_data(),
'type' => $formfield->field->datatype,
Expand Down
11 changes: 11 additions & 0 deletions user/profile/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ public function display_data() {
return format_text($this->data, FORMAT_MOODLE, $options);
}

/**
* Display the name of the profile field.
*
* @return string
*/
public function display_name() {
$options = new stdClass();
$options->para = false;
return format_text($this->field->name, FORMAT_MOODLE, $options);
}

/**
* Print out the form field in the edit profile page
* @param MoodleQuickForm $mform instance of the moodleform class
Expand Down
29 changes: 27 additions & 2 deletions user/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,14 @@ public function test_get_users_by_field() {

// Create complex user profile field supporting multi-lang.
filter_set_global_state('multilang', TEXTFILTER_ON);
$name = '<span lang="en" class="multilang">Employment status</span>'.
'<span lang="es" class="multilang">Estado de Empleo</span>';
$statuses = 'UE\nSE\n<span lang="en" class="multilang">Other</span><span lang="es" class="multilang">Otro</span>';
$generator->create_custom_profile_field(
[
'datatype' => 'menu',
'shortname' => 'employmentstatus', 'name' => 'Employment status',
'shortname' => 'employmentstatus',
'name' => $name,
'param1' => $statuses
]
);
Expand Down Expand Up @@ -311,6 +314,7 @@ public function test_get_users_by_field() {
$this->assertCount(1, $returneduser['customfields']);
$dbvalue = explode('\n', $statuses)[2];
$this->assertEquals($dbvalue, $returneduser['customfields'][0]['value']);
$this->assertEquals('Employment status', $returneduser['customfields'][0]['name']);
$this->assertEquals('Other', $returneduser['customfields'][0]['displayvalue']);
}
}
Expand Down Expand Up @@ -346,6 +350,21 @@ public function get_course_user_profiles_setup($capability) {

$return = new \stdClass();

$generator = self::getDataGenerator();
// Create complex user profile field supporting multi-lang.
filter_set_global_state('multilang', TEXTFILTER_ON);
$name = '<span lang="en" class="multilang">Employment status</span>'.
'<span lang="es" class="multilang">Estado de Empleo</span>';
$statuses = 'UE\nSE\n<span lang="en" class="multilang">Other</span><span lang="es" class="multilang">Otro</span>';
$generator->create_custom_profile_field(
[
'datatype' => 'menu',
'shortname' => 'employmentstatus',
'name' => $name,
'param1' => $statuses
]
);

// Create the course and fetch its context.
$return->course = self::getDataGenerator()->create_course();
$return->user1 = array(
Expand All @@ -362,7 +381,8 @@ public function get_course_user_profiles_setup($capability) {
'description' => 'This is a description for user 1',
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'country' => 'AU'
'country' => 'AU',
'profile_field_employmentstatus' => explode('\n', $statuses)[2],
);
$return->user1 = self::getDataGenerator()->create_user($return->user1);
if (!empty($CFG->usetags)) {
Expand Down Expand Up @@ -448,6 +468,11 @@ public function test_get_user_course_profile_as_admin() {
$this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
$this->assertEquals($data->user1->city, $enrolleduser['city']);
$this->assertEquals($data->user1->country, $enrolleduser['country']);
// Default language was used for the user.
$this->assertEquals($CFG->lang, $enrolleduser['lang']);
$this->assertEquals('Employment status', $enrolleduser['customfields'][0]['name']);
$this->assertEquals('Other', $enrolleduser['customfields'][0]['displayvalue']);

if (!empty($CFG->usetags)) {
$this->assertEquals(implode(', ', $data->user1->interests), $enrolleduser['interests']);
}
Expand Down

0 comments on commit b64dff6

Please sign in to comment.