From 4b2bf298f1db6507fdb50ec49b8db418e339aa91 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 6 Sep 2024 18:53:49 +1000 Subject: [PATCH] issue #5: process course_deleted event --- classes/observer.php | 12 +++++++++++ db/events.php | 4 ++++ tests/observer_test.php | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/classes/observer.php b/classes/observer.php index 46b4cbf..ca23a25 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -18,6 +18,7 @@ use core\event\course_category_created; use core\event\course_created; +use core\event\course_deleted; use core\event\tag_added; use core\event\tag_removed; use core\event\tag_deleted; @@ -95,6 +96,17 @@ public static function course_created(course_created $event): void { helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name, $course); } + /** + * Process course_deleted event. + * + * @param course_deleted $event The event. + */ + public static function course_deleted(course_deleted $event): void { + $courseid = $event->courseid; + $coursename = $event->other['fullname']; + helper::remove_item($courseid, helper::ITEM_TYPE_COURSE, $coursename); + } + /** * Process course_category_created event. * diff --git a/db/events.php b/db/events.php index 97c899d..cf3cc4c 100644 --- a/db/events.php +++ b/db/events.php @@ -41,6 +41,10 @@ 'eventname' => '\core\event\course_created', 'callback' => '\tool_enrolprofile\observer::course_created', ], + [ + 'eventname' => '\core\event\course_deleted', + 'callback' => '\tool_enrolprofile\observer::course_deleted', + ], [ 'eventname' => '\core\event\course_category_created', 'callback' => '\tool_enrolprofile\observer::course_category_created', diff --git a/tests/observer_test.php b/tests/observer_test.php index da2a7fe..2d2dce4 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -313,6 +313,53 @@ public function test_course_created(): void { $this->assertNotEmpty($enrol); } + /** + * Check logic when deleting a course. + */ + public function test_course_deleted(): void { + global $DB; + + $coursename = 'Course name'; + $course = $this->getDataGenerator()->create_course(['fullname' => $coursename]); + + // Should be course and category cohorts. + $this->assertCount(2, $DB->get_records('enrol', ['courseid' => $course->id, 'enrol' => 'cohort'])); + + $coursecohort = $DB->get_record('cohort', ['name' => $coursename]); + $this->assertNotEmpty($coursecohort); + + $cohort = cohort_get_cohort($coursecohort->id, context_course::instance($course->id), true); + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id]); + $this->assertNotEmpty($profilefielddata); + $this->assertTrue(in_array($coursename, explode("\n", $profilefielddata))); + + $rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $coursename]); + $this->assertNotEmpty($rule); + $this->assertEquals($cohort->id, $rule->cohortid); + $this->assertEquals(1, $rule->enabled); + $conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]); + $this->assertCount(2, $conditions); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertNotEmpty($enrol); + + delete_course($course->id, false); + + $coursecohort = $DB->get_record('cohort', ['name' => $coursename]); + $this->assertEmpty($coursecohort); + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id]); + $this->assertFalse(in_array($coursename, explode("\n", $profilefielddata))); + + $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $coursename])); + $conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]); + $this->assertCount(0, $conditions); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertEmpty($enrol); + } + /** * Check logic when creating a course category. */