diff --git a/classes/helper.php b/classes/helper.php index adbb414..593870c 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -155,9 +155,10 @@ public static function get_cohort_by_item(int $itemid, string $itemtype): ?stdCl public static function add_enrolment_method(stdClass $course, stdClass $cohort): void { global $DB; - $studentrole = $DB->get_record('role', ['shortname' => self::STUDENT_ROLE]); + $studentrole = self::get_student_role(); $fields = [ + 'enrol' => 'cohort', 'customint1' => $cohort->id, 'roleid' => $studentrole->id, 'courseid' => $course->id, @@ -169,6 +170,41 @@ public static function add_enrolment_method(stdClass $course, stdClass $cohort): } } + /** + * A helper to remove enrolment method from a given course based on item details. + * + * @param stdClass $course Given course. + * @param int $itemid Item ID. + * @param string $itemtype Item type (tag, course, category). + */ + public static function remove_enrolment_method(stdClass $course, int $itemid, string $itemtype): void { + global $DB; + + $cohort = self::get_cohort_by_item($itemid, $itemtype); + + if ($cohort) { + $studentrole = self::get_student_role(); + + $fields = [ + 'enrol' => 'cohort', + 'customint1' => $cohort->id, + 'roleid' => $studentrole->id, + 'courseid' => $course->id, + ]; + + $instances = $DB->get_records('enrol', $fields); + + if ($instances) { + $enrol = enrol_get_plugin('cohort'); + + foreach ($instances as $instance) { + // Remove enrolment method. + $enrol->delete_instance($instance); + } + } + } + } + /** * Update profile field with new item. * @@ -262,4 +298,15 @@ public static function add_rule(stdClass $cohort, string $fieldshortname): void $rule->set('enabled', 1); $rule->save(); } + + /** + * Returns a student role. + * + * @return stdClass + */ + public static function get_student_role(): stdClass { + global $DB; + + return $DB->get_record('role', ['shortname' => helper::STUDENT_ROLE]); + } } diff --git a/classes/observer.php b/classes/observer.php index 094795b..570c9ba 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -19,6 +19,7 @@ use core\event\course_category_created; use core\event\course_created; use core\event\tag_added; +use core\event\tag_removed; /** * Event observer class. @@ -48,6 +49,23 @@ public static function tag_added(tag_added $event): void { helper::set_up_item($tagid, helper::ITEM_TYPE_TAG, $tagname, $course); } + /** + * Process tag_removed event. + * + * @param tag_removed $event The event. + */ + public static function tag_removed(tag_removed $event): void { + // Check context is course context. + $context = $event->get_context(); + if ($context->contextlevel != CONTEXT_COURSE && $event->other['itemtype'] != 'course') { + return; + } + + $tagid = $event->other['tagid']; + $course = get_course($event->other['itemid']); + helper::remove_enrolment_method($course, $tagid, helper::ITEM_TYPE_TAG); + } + /** * Process course_created event. * diff --git a/db/events.php b/db/events.php index 8d5dd1e..b1867ec 100644 --- a/db/events.php +++ b/db/events.php @@ -37,4 +37,8 @@ 'eventname' => '\core\event\course_category_created', 'callback' => '\tool_enrolprofile\observer::course_category_created', ], + [ + 'eventname' => '\core\event\tag_removed', + 'callback' => '\tool_enrolprofile\observer::tag_removed', + ], ]; diff --git a/tests/observer_test.php b/tests/observer_test.php index ee5e586..14f334b 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -174,6 +174,27 @@ public function test_tag_added(): void { $this->assertNotEmpty($enrol); } + /** + * Check logic when removing a tag. + */ + public function test_tag_removed(): void { + global $DB; + + $course = $this->getDataGenerator()->create_course(); + $tagname = 'A tag'; + + core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), [$tagname]); + + $cohort = $DB->get_record('cohort', ['name' => $tagname]); + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertNotEmpty($enrol); + + core_tag_tag::remove_item_tag('core', 'course', $course->id, $tagname); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertEmpty($enrol); + } + /** * Check logic when creating a course. */