diff --git a/classes/helper.php b/classes/helper.php index 9f84ea4..adbb414 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -78,12 +78,13 @@ class helper { /** * Set up configuration item. * - * @param stdClass $course Course to set up it for. * @param int $itemid Item ID number - * @param string $itemtype Item type (tag, course, category)/ + * @param string $itemtype Item type (tag, course, category). * @param string $itemname Item name. + * @param stdClass|null $course Course to set up enrolment method. If not set, the no enrolment method will be created. + * @return void */ - public static function set_up_item(stdClass $course, int $itemid, string $itemtype, string $itemname): void { + public static function set_up_item(int $itemid, string $itemtype, string $itemname, ?stdClass $course = null): void { $cohort = self::get_cohort_by_item($itemid, $itemtype); if (empty($cohort)) { @@ -105,8 +106,11 @@ public static function set_up_item(stdClass $course, int $itemid, string $itemty self::add_rule($cohort, $itemtype); // Add a tag to a custom profile field. self::update_profile_field($itemtype, $itemname); - // If yes, create enrolment method for the cohort for a given course. - self::add_enrolment_method($course, $cohort); + + // Create enrolment method for the cohort for a given course. + if (!empty($course)) { + self::add_enrolment_method($course, $cohort); + } } /** diff --git a/classes/observer.php b/classes/observer.php index e3ad72f..094795b 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -16,6 +16,7 @@ namespace tool_enrolprofile; +use core\event\course_category_created; use core\event\course_created; use core\event\tag_added; @@ -44,7 +45,7 @@ public static function tag_added(tag_added $event): void { $tagname = $event->other['tagrawname']; $course = get_course($event->other['itemid']); - helper::set_up_item($course, $tagid, 'tag', $tagname); + helper::set_up_item($tagid, helper::ITEM_TYPE_TAG, $tagname, $course); } /** @@ -56,9 +57,21 @@ public static function course_created(course_created $event): void { global $DB; $course = get_course($event->courseid); - helper::set_up_item($course, $course->id, 'course', $course->{helper::COURSE_NAME}); + helper::set_up_item($course->id, helper::ITEM_TYPE_COURSE, $course->{helper::COURSE_NAME}, $course); $category = $DB->get_record('course_categories', ['id' => $course->category]); - helper::set_up_item($course, $category->id, 'category', $category->name); + helper::set_up_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name, $course); + } + + /** + * Process course_category_created event. + * + * @param course_category_created $event The event. + */ + public static function course_category_created(course_category_created $event): void { + global $DB; + + $category = $DB->get_record('course_categories', ['id' => $event->objectid]); + helper::set_up_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name); } } diff --git a/db/events.php b/db/events.php index 5744525..8d5dd1e 100644 --- a/db/events.php +++ b/db/events.php @@ -33,4 +33,8 @@ 'eventname' => '\core\event\course_created', 'callback' => '\tool_enrolprofile\observer::course_created', ], + [ + '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 93ae625..ee5e586 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -125,9 +125,8 @@ protected function create_cohort_custom_field(string $shortname, string $datatyp /** * Check logic when adding a tag. - * @return void */ - public function test_tag_added() { + public function test_tag_added(): void { global $DB; $course = $this->getDataGenerator()->create_course(); @@ -155,7 +154,7 @@ public function test_tag_added() { $this->assertSame($tag->id, $customfield->export_value()); } if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) { - $this->assertSame('tag', $customfield->export_value()); + $this->assertSame(helper::ITEM_TYPE_TAG, $customfield->export_value()); } } @@ -177,15 +176,13 @@ public function test_tag_added() { /** * Check logic when creating a course. - * @return void */ - public function test_course_created() { + public function test_course_created(): void { global $DB; $coursename = 'Course name'; $this->assertEmpty($DB->get_record('cohort', ['name' => $coursename])); $this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id])); - $this->assertEmpty($DB->get_record('tag', ['rawname' => $coursename])); $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $coursename])); $course = $this->getDataGenerator()->create_course(['fullname' => $coursename]); @@ -203,7 +200,7 @@ public function test_course_created() { $this->assertSame($course->id, $customfield->export_value()); } if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) { - $this->assertSame('course', $customfield->export_value()); + $this->assertSame(helper::ITEM_TYPE_COURSE, $customfield->export_value()); } } @@ -232,7 +229,7 @@ public function test_course_created() { $this->assertSame($category->id, $customfield->export_value()); } if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) { - $this->assertSame('category', $customfield->export_value()); + $this->assertSame(helper::ITEM_TYPE_CATEGORY, $customfield->export_value()); } } @@ -250,4 +247,43 @@ public function test_course_created() { $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); $this->assertNotEmpty($enrol); } + + /** + * Check logic when creating a course category. + */ + public function test_course_category_created(): 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]); + + // 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); + foreach ($cohort->customfields as $customfield) { + if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_ID) { + $this->assertSame($category->id, $customfield->export_value()); + } + if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) { + $this->assertSame(helper::ITEM_TYPE_CATEGORY, $customfield->export_value()); + } + } + + $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); + } }