diff --git a/local/telconfig/classes/api_client.php b/local/telconfig/classes/api_client.php index 0cf5646ae49..a909b7ba667 100644 --- a/local/telconfig/classes/api_client.php +++ b/local/telconfig/classes/api_client.php @@ -17,4 +17,15 @@ public function post(string $url, array $data): string|false { $context = stream_context_create($options); return @file_get_contents($url, false, $context); } + + public function delete(string $url): string|false { + $options = [ + 'http' => [ + 'method' => 'DELETE', + 'timeout' => 5, + ] + ]; + $context = stream_context_create($options); + return @file_get_contents($url, false, $context); + } } \ No newline at end of file diff --git a/local/telconfig/classes/course_data_builder.php b/local/telconfig/classes/course_data_builder.php new file mode 100644 index 00000000000..e05afa8852f --- /dev/null +++ b/local/telconfig/classes/course_data_builder.php @@ -0,0 +1,69 @@ +. + +/** + * Helper class locally used. + * + * @package local_telconfig + * @copyright + * @license + */ + +namespace local_telconfig; + +defined('MOODLE_INTERNAL') || die(); + +class course_data_builder { + public static function build_course_metadata($course): array { + global $DB, $CFG; + + try { + + // Get course context and teachers (authors) + $context = \context_course::instance($course->id); + $teachers = get_role_users(3, $context); // 3 = editingteacher by default + $authors = array_values(array_map(fn($u) => fullname($u), $teachers)); + + // Extract tags + require_once($CFG->dirroot . '/tag/lib.php'); + $tags = \core_tag_tag::get_item_tags('core', 'course', $course->id); + $keywords = array_map(fn($tag) => $tag->rawname, $tags); + + // Prepare data + $data = [ + '_id' => 'M' . $course->id, + 'course_id' => $course->id, + 'authored_date' => date('Y-m-d', $course->startdate), + 'authors' => $authors, + 'catalogue_ids' => [$course->category], + 'description' => format_text($course->summary, FORMAT_HTML), + 'keywords' => array_values($keywords), + 'location_paths' => [], // category hierarchy if needed + 'publication_date' => date('Y-m-d', $course->startdate), + 'rating' => 0, + 'resource_reference_id' => 0, + 'resource_type' => 'Course', + 'title' => $course->fullname, + ]; + + return $data; + + } catch (\Throwable $e) { + debugging('Error in course_data_builder: ' . $e->getMessage(), DEBUG_DEVELOPER); + return []; // Always return an array + } + } +} diff --git a/local/telconfig/classes/helper.php b/local/telconfig/classes/helper.php index edc9a7bbb2b..22d42b95f2a 100644 --- a/local/telconfig/classes/helper.php +++ b/local/telconfig/classes/helper.php @@ -35,14 +35,14 @@ class helper { * @param array $data * @return void */ - public static function send_findwise_api(array $data, ?api_client $client = null): void { + public static function send_findwise_api(array $data, string $method = 'POST', ?api_client $client = null): void { $indexurl = get_config('local_telconfig', 'findwiseindexurl'); $indexmethod = get_config('local_telconfig', 'findwiseindexmethod'); $collection = get_config('local_telconfig', 'findwisecollection'); $apitoken = get_config('local_telconfig', 'findwiseapitoken'); if (empty($indexurl) || empty($apitoken)) { - return; + return; } $indexurl = rtrim($indexurl, '/') . '/' . $indexmethod . '?token=' . urlencode($apitoken); @@ -51,7 +51,18 @@ public static function send_findwise_api(array $data, ?api_client $client = null $client ??= new api_client(); try { - $response = $client->post($apiurl, $data); + if ($method === 'DELETE') { + // Add logic to construct a deletion URL with course ID + if (isset($data['course_id'])) { + $deleteurl = rtrim($apiurl, '/') . '&id=M' . $data['course_id']; + $response = $client->delete($deleteurl); + } else { + debugging('send_findwise_api: Cannot perform DELETE without course_id in $data.', DEBUG_DEVELOPER); + return; + } + } else { + $response = $client->post($apiurl, $data); // POST or PUT + } if ($response === false) { debugging('send_findwise_api: Failed to send data to findwise API.', DEBUG_DEVELOPER); diff --git a/local/telconfig/classes/observer.php b/local/telconfig/classes/observer.php index b2d128a6a08..0468733a7f1 100644 --- a/local/telconfig/classes/observer.php +++ b/local/telconfig/classes/observer.php @@ -1,6 +1,7 @@ other['enrol']) || $event->other['enrol'] !== 'self') { - return; - } - try { + // Get enrol instance - $enrol = $DB->get_record('enrol', ['id' => $event->objectid], '*', MUST_EXIST); - + $enrol = $DB->get_record('enrol', ['id' => $event->objectid], '*', MUST_EXIST); + + // Only act if it's for 'self' enrolment. + if (!isset($event->other['enrol']) || $event->other['enrol'] !== 'self') { + return; + } + // Get course info $course = $DB->get_record('course', ['id' => $event->courseid], '*', MUST_EXIST); - $data = [ - '_id' => $course->id, - 'event' => $event->eventname, - 'enrolid' => $enrol->id, - 'courseid' => $course->id, - 'coursename' => $course->fullname, - 'shortname' => $course->shortname, - 'summary' => $course->summary, - 'startdate' => $course->startdate, - 'enddate' => $course->enddate, - 'enrolstatus' => $enrol->status, // 0 = enabled, 1 = disabled - 'time' => time() - ]; - - helper::send_findwise_api($data); + if ((int)$enrol->status === ENROL_INSTANCE_ENABLED) { + // Fetch the enrolment instance data. + $data = course_data_builder::build_course_metadata($course); + helper::send_findwise_api($data); + } else { + // Delete from external API when disabled. + $data = ['course_id' => $course->id]; + helper::send_findwise_api($data,'DELETE'); + } + } catch (\dml_exception $e) { debugging("Failed to fetch course/enrol data: " . $e->getMessage(), DEBUG_DEVELOPER); } diff --git a/local/telconfig/lang/en/local_telconfig.php b/local/telconfig/lang/en/local_telconfig.php index be5810c7897..6047a5b9139 100644 --- a/local/telconfig/lang/en/local_telconfig.php +++ b/local/telconfig/lang/en/local_telconfig.php @@ -12,7 +12,7 @@ $string['Findwisesettings_desc'] = 'Findwise API configurations.'; $string['findwiseindexurl'] = 'Findwise index url'; $string['findwiseindexurl_desc'] = 'Findwise index url desc.'; -$string['findwiseindexmethod'] = 'Findwise index url'; +$string['findwiseindexmethod'] = 'Findwise index method'; $string['findwiseindexmethod_desc'] = 'Findwise index method desc.'; $string['findwisecollection'] = 'Findwise collection'; $string['findwisecollection_desc'] = 'Findwise collection desc.'; diff --git a/local/telconfig/tests/course_data_builder_test.php b/local/telconfig/tests/course_data_builder_test.php new file mode 100644 index 00000000000..8d6810c2fe2 --- /dev/null +++ b/local/telconfig/tests/course_data_builder_test.php @@ -0,0 +1,71 @@ +. + +// File: local/telconfig/tests/course_data_builder_test.php + +namespace local_telconfig\tests; + +use advanced_testcase; +use context_course; +use core_tag_tag; +use local_telconfig\course_data_builder; + +defined('MOODLE_INTERNAL') || die(); + +class course_data_builder_test extends advanced_testcase { + + + public function test_build_course_metadata_returns_expected_array() { + global $DB; + + $this->resetAfterTest(true); + + // Create a course using Moodle's generator + $course = $this->getDataGenerator()->create_course([ + 'fullname' => 'Test Course', + 'summary' => 'Test summary', + 'startdate' => strtotime('2022-01-01') + ]); + + // Enrol a teacher in the course + $teacher = $this->getDataGenerator()->create_user(['firstname' => 'Alice', 'lastname' => 'Teacher']); + $roleid = 3; // editingteacher + $context = context_course::instance($course->id); + role_assign($roleid, $teacher->id, $context->id); + + // Add tags to course + core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), ['tag1', 'tag2']); + + // Call the method + $result = course_data_builder::build_course_metadata($course); + + // Assertions + $this->assertIsArray($result); + $this->assertSame('M' . $course->id, $result['_id']); + $this->assertSame($course->id, $result['course_id']); + $this->assertSame(date('Y-m-d', $course->startdate), $result['authored_date']); + $this->assertContains(fullname($teacher), $result['authors']); + $this->assertEquals([$course->category], $result['catalogue_ids']); + $this->assertSame(format_text($course->summary, FORMAT_HTML), $result['description']); + $this->assertEquals(['tag1', 'tag2'], $result['keywords']); + $this->assertIsArray($result['location_paths']); + $this->assertSame(date('Y-m-d', $course->startdate), $result['publication_date']); + $this->assertSame(0, $result['rating']); + $this->assertSame(0, $result['resource_reference_id']); + $this->assertSame('Course', $result['resource_type']); + $this->assertSame($course->fullname, $result['title']); + } +} diff --git a/local/telconfig/tests/helper_test.php b/local/telconfig/tests/helper_test.php index 79d0eb96125..0e60cafbe3a 100644 --- a/local/telconfig/tests/helper_test.php +++ b/local/telconfig/tests/helper_test.php @@ -41,6 +41,6 @@ public function test_send_findwise_api_makes_api_call() { ->willReturn('{"status":"ok"}'); // Call the method with the mock client. - helper::send_findwise_api(['courseid' => 123], $mock); + helper::send_findwise_api(['courseid' => 123], 'POST', $mock); } }