Merge branch 'MDL-87736-501' of https://github.com/yerairogo/moodle into MOODLE_501_STABLE

This commit is contained in:
Paul Holden
2026-01-28 14:45:07 +00:00
2 changed files with 143 additions and 1 deletions
+7 -1
View File
@@ -447,7 +447,13 @@ abstract class handler {
* In the last case data_controller::get_value() and export_value() functions will return default values.
*/
public function get_instances_data(array $instanceids, bool $returnall = false): array {
$result = api::get_instances_fields_data($this->get_fields(), $instanceids);
$result = api::get_instances_fields_data(
fields: $this->get_fields(),
instanceids: $instanceids,
component: $this->get_component(),
area: $this->get_area(),
itemid: $this->get_itemid(),
);
if (!$returnall) {
// Filter only by visible fields (list of visible fields may be different for each instance).
+136
View File
@@ -48,4 +48,140 @@ final class handler_test extends advanced_testcase {
$this->expectExceptionMessage('Unable to find handler for custom fields for component core_blimey and area test');
handler::get_handler('core_blimey', 'test');
}
/**
* Test getting instances data
*/
public function test_get_instances_data(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$sharedcustomfieldcategory = $this->getDataGenerator()->create_custom_field_category([
'component' => 'core_customfield',
'area' => 'shared',
]);
$sharedfield = $this->getDataGenerator()->create_custom_field([
'categoryid' => $sharedcustomfieldcategory->get('id'),
'name' => 'Color',
'type' => 'text',
'shortname' => 'color',
]);
// Enable the shared custom field category for both courses and cohorts.
$shared = new shared(0, (object)[
'categoryid' => $sharedcustomfieldcategory->get('id'),
'component' => 'core_course',
'area' => 'course',
'itemid' => 0,
]);
$shared->create();
$shared = new shared(0, (object)[
'categoryid' => $sharedcustomfieldcategory->get('id'),
'component' => 'core_cohort',
'area' => 'cohort',
'itemid' => 0,
]);
$shared->create();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$sharedid1 = $course1->id;
$sharedid2 = $course2->id;
// Manually insert cohorts in the database to have both courses and cohorts using the same IDs.
$now = time();
$DB->insert_record_raw('cohort', [
'id' => $sharedid1,
'contextid' => 1,
'name' => 'Cohort 1',
'descriptionformat' => 0,
'visible' => 1,
'component' => '',
'timecreated' => $now,
'timemodified' => $now,
]);
$DB->insert_record_raw('cohort', [
'id' => $sharedid2,
'contextid' => 1,
'name' => 'Cohort 2',
'descriptionformat' => 0,
'visible' => 1,
'component' => '',
'timecreated' => $now,
'timemodified' => $now,
]);
$fieldid = $sharedfield->get('id');
// Manually insert customfield_data records as well.
$DB->insert_record_raw('customfield_data', [
'fieldid' => $fieldid,
'instanceid' => $sharedid1,
'charvalue' => 'orange',
'value' => 'orange',
'valueformat' => 0,
'valuetrust' => 0,
'timecreated' => $now,
'timemodified' => $now,
'component' => 'core_course',
'area' => 'course',
'itemid' => 0,
]);
$DB->insert_record_raw('customfield_data', [
'fieldid' => $fieldid,
'instanceid' => $sharedid2,
'charvalue' => 'red',
'value' => 'red',
'valueformat' => 0,
'valuetrust' => 0,
'timecreated' => $now,
'timemodified' => $now,
'component' => 'core_course',
'area' => 'course',
'itemid' => 0,
]);
$DB->insert_record_raw('customfield_data', [
'fieldid' => $fieldid,
'instanceid' => $sharedid1,
'charvalue' => 'blue',
'value' => 'blue',
'valueformat' => 0,
'valuetrust' => 0,
'timecreated' => $now,
'timemodified' => $now,
'component' => 'core_cohort',
'area' => 'cohort',
'itemid' => 0,
]);
$DB->insert_record_raw('customfield_data', [
'fieldid' => $fieldid,
'instanceid' => $sharedid2,
'charvalue' => 'green',
'value' => 'green',
'valueformat' => 0,
'valuetrust' => 0,
'timecreated' => $now,
'timemodified' => $now,
'component' => 'core_cohort',
'area' => 'cohort',
'itemid' => 0,
]);
// Verify courses retrieve their own custom field values despite sharing IDs with cohorts.
$coursehandler = handler::get_handler('core_course', 'course');
$coursedata = $coursehandler->get_instances_data([$sharedid1, $sharedid2], true);
$this->assertArrayHasKey($sharedid1, $coursedata);
$this->assertArrayHasKey($sharedid2, $coursedata);
$this->assertEquals('orange', $coursedata[$sharedid1][$fieldid]->get_value());
$this->assertEquals('red', $coursedata[$sharedid2][$fieldid]->get_value());
// Verify cohorts retrieve their own custom field values despite sharing IDs with courses.
$cohorthandler = handler::get_handler('core_cohort', 'cohort');
$cohortdata = $cohorthandler->get_instances_data([$sharedid1, $sharedid2], true);
$this->assertArrayHasKey($sharedid1, $cohortdata);
$this->assertArrayHasKey($sharedid2, $cohortdata);
$this->assertEquals('blue', $cohortdata[$sharedid1][$fieldid]->get_value());
$this->assertEquals('green', $cohortdata[$sharedid2][$fieldid]->get_value());
}
}