diff --git a/course/externallib.php b/course/externallib.php index 55a7f49e4fd..39098bf0bc5 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -2793,6 +2793,8 @@ class core_course_external extends external_api { ), 'Additional options for particular course format.', VALUE_OPTIONAL ), + 'communicationroomname' => new external_value(PARAM_TEXT, 'Communication tool room name.', VALUE_OPTIONAL), + 'communicationroomurl' => new external_value(PARAM_RAW, 'Communication tool room URL.', VALUE_OPTIONAL), ); $coursestructure = array_merge($coursestructure, $extra); } @@ -3273,6 +3275,8 @@ class core_course_external extends external_api { } } + $iscommapiavailable = \core_communication\api::is_available(); + $coursesdata = array(); foreach ($courses as $course) { $context = context_course::instance($course->id); @@ -3334,6 +3338,19 @@ class core_course_external extends external_api { 'value' => $value ); } + + // Communication tools for the course. + if ($iscommapiavailable) { + $communication = \core_communication\api::load_by_instance( + context: $context, + component: 'core_course', + instancetype: 'coursecommunication', + instanceid: $course->id + ); + $coursesdata[$course->id]['communicationroomname'] = \core_external\util::format_string($communication->get_room_name(), $context); + // This will be usually an URL, however, it is better to consider that can be anything a plugin might return, this is why we will use PARAM_RAW. + $coursesdata[$course->id]['communicationroomurl'] = $communication->get_communication_room_url(); + } } return array( diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 04f48008bbc..649d85e5079 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -3029,6 +3029,41 @@ class externallib_test extends externallib_advanced_testcase { ], reset($course['customfields'])); } + /** + * Test retrieving courses by field returning communication tools. + */ + public function test_get_courses_by_field_communication(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create communication tool in course. + set_config('enablecommunicationsubsystem', 1); + + $roomname = 'Course chat'; + $telegramlink = 'https://my.telegram.chat/120'; + $record = [ + 'selectedcommunication' => 'communication_customlink', + 'communicationroomname' => $roomname, + 'customlinkurl' => $telegramlink, + ]; + $course = $this->getDataGenerator()->create_course($record); + $communication = \core_communication\api::load_by_instance( + context: \core\context\course::instance($course->id), + component: 'core_course', + instancetype: 'coursecommunication', + instanceid: $course->id, + ); + + $result = external_api::clean_returnvalue( + core_course_external::get_courses_by_field_returns(), + core_course_external::get_courses_by_field('id', $course->id) + ); + + $course = reset($result['courses']); + $this->assertEquals($roomname, $course['communicationroomname']); + $this->assertEquals($telegramlink, $course['communicationroomurl']); + } + public function test_get_courses_by_field_invalid_field() { $this->expectException('invalid_parameter_exception'); $result = core_course_external::get_courses_by_field('zyx', 'x'); diff --git a/course/upgrade.txt b/course/upgrade.txt index c3da86e8780..77bd2f07842 100644 --- a/course/upgrade.txt +++ b/course/upgrade.txt @@ -5,6 +5,9 @@ information provided here is intended especially for developers. * set_coursemodule_visible() has a new $rebuildcache parameter. If this is being called multiple times in the same request, consider passing `false` for this parameter and rebuilding the cache once after all the course modules have been updated. See course_update_section() for an example. +* The external function core_course::get_courses_by_field now returns the communication tool configuration for the course. + - communicationroomname: the room name + - communicationroomurl: the tool url === 4.3 === * The `core_course_renderer::course_section_cm_completion` method has been removed, and can no longer be used