diff --git a/classes/observer.php b/classes/observer.php index ca23a25..61c332f 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -17,6 +17,7 @@ namespace tool_enrolprofile; use core\event\course_category_created; +use core\event\course_category_deleted; use core\event\course_created; use core\event\course_deleted; use core\event\tag_added; @@ -118,4 +119,17 @@ public static function course_category_created(course_category_created $event): $category = $DB->get_record('course_categories', ['id' => $event->objectid]); helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name); } + + /** + * Process course_category_deleted event. + * + * @param course_category_deleted $event The event. + */ + public static function course_category_deleted(course_category_deleted $event): void { + + $categoryid = $event->objectid; + $categoryname = $event->other['name']; + + helper::remove_item($categoryid, helper::ITEM_TYPE_CATEGORY, $categoryname); + } } diff --git a/db/events.php b/db/events.php index cf3cc4c..436ff20 100644 --- a/db/events.php +++ b/db/events.php @@ -49,5 +49,8 @@ 'eventname' => '\core\event\course_category_created', 'callback' => '\tool_enrolprofile\observer::course_category_created', ], - + [ + 'eventname' => '\core\event\course_category_deleted', + 'callback' => '\tool_enrolprofile\observer::course_category_deleted', + ], ]; diff --git a/tests/observer_test.php b/tests/observer_test.php index 2d2dce4..a9cf281 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -398,4 +398,52 @@ public function test_course_category_created(): void { $conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]); $this->assertCount(2, $conditions); } + + /** + * Check logic when deleting a course category. + */ + public function test_course_category_deleted(): void { + global $DB; + + $categoryname = 'Category name'; + $this->assertEmpty($DB->get_record('cohort', ['name' => $categoryname])); + $this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id])); + $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname])); + + $category = $this->getDataGenerator()->create_category(['name' => $categoryname]); + $course = $this->getDataGenerator()->create_course(['category' => $category->id]); + + // Check everything about category cohort. + $categorycohort = $DB->get_record('cohort', ['name' => $category->name]); + $this->assertNotEmpty($categorycohort); + + $cohort = cohort_get_cohort($categorycohort->id, \context_coursecat::instance($category->id), true); + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]); + $this->assertNotEmpty($profilefielddata); + $this->assertTrue(in_array($categoryname, explode("\n", $profilefielddata))); + + $rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname]); + $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); + + $category->delete_full(false); + + $categorycohort = $DB->get_record('cohort', ['name' => $categoryname]); + $this->assertEmpty($categorycohort); + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]); + $this->assertFalse(in_array($categoryname, explode("\n", $profilefielddata))); + $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname])); + $this->assertEmpty($DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id])); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertEmpty($enrol); + } }