From a5404839cd258e58f6d9531893c4e9a703dd5f82 Mon Sep 17 00:00:00 2001 From: Markus Date: Mon, 30 Sep 2024 13:15:53 +0200 Subject: [PATCH] refactor to db pattern --- classes/local/db/moodle_core_repository.php | 10 ++++++++++ .../upgrade_3_2_0_to_4_0_0_completionlib.php | 11 +---------- tests/local/db/moodle_core_repository_test.php | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/classes/local/db/moodle_core_repository.php b/classes/local/db/moodle_core_repository.php index 331e149..4a672e3 100644 --- a/classes/local/db/moodle_core_repository.php +++ b/classes/local/db/moodle_core_repository.php @@ -47,4 +47,14 @@ public function update_grade_item_record(int $grade_item_id, array $data): void public function update_course_module_record(int $cmid, array $data): void { $this->db->update_record('course_modules', (object)array_merge($data, ['id' => $cmid])); } + + public function get_cms_with_module_name_by_course_id(int $course_id): array { + return $this->db->get_records_sql( + 'SELECT cm.*, m.name AS modname + FROM {course_modules} cm + JOIN {modules} m ON m.id = cm.module + WHERE cm.course = ?', + [$course_id] + ); + } } \ No newline at end of file diff --git a/classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php b/classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php index 8d1bffb..de5efcc 100644 --- a/classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php +++ b/classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php @@ -82,16 +82,7 @@ private function resetCourseCache(): void { */ public function upgrade_modules(): void { global $DB; - // get_fast_modinfo is not allowed during upgrade and there is no get_modinfo - // TODO: probably always follow this approach - // TODO: repo pattern - $cm_infos = $DB->get_records_sql( - 'SELECT cm.*, m.name AS modname - FROM {course_modules} cm - JOIN {modules} m ON m.id = cm.module - WHERE cm.course = ?', - [$this->course_id] - ); + $cm_infos = $this->moodle_core_repository->get_cms_with_module_name_by_course_id($this->course_id); $transaction = $DB->start_delegated_transaction(); foreach ($cm_infos as $cm_info) { diff --git a/tests/local/db/moodle_core_repository_test.php b/tests/local/db/moodle_core_repository_test.php index 495a78c..3878689 100644 --- a/tests/local/db/moodle_core_repository_test.php +++ b/tests/local/db/moodle_core_repository_test.php @@ -106,4 +106,22 @@ public function test_update_course_module_record() { $result = $DB->get_record('course_modules', ['id' => $course_module->cmid]); $this->assertEquals(2, $result->completion); } + + public function test_get_cms_with_module_name_by_course_id() { + $moodle_core_repository = new moodle_core_repository(); + + // Create a course + $course = $this->getDataGenerator()->create_course(); + + // Create a course module + $course_module = $this->getDataGenerator()->create_module('url', ['course' => $course->id]); + + // Call the function + $result = $moodle_core_repository->get_cms_with_module_name_by_course_id($course->id); + + // Check the result + $this->assertCount(1, $result); + $this->assertEquals($course_module->id, reset($result)->instance); + $this->assertEquals('url', reset($result)->modname); + } } \ No newline at end of file