diff --git a/classes/helper.php b/classes/helper.php index 470b8ae..b64c6bf 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -36,24 +36,19 @@ class helper { /** - * Profile field category. + * Tag item type. */ - public const PROFILE_CATEGORY = 'Profile field'; + public const ITEM_TYPE_TAG = 'tag'; /** - * Field shortname - */ - public const FIELD_TAG = 'tag'; - - /** - * Field shortname + * Category item type. */ - public const FIELD_CATEGORY = 'category'; + public const ITEM_TYPE_CATEGORY = 'category'; /** - * Field shortname + * Course item type. */ - public const FIELD_COURSE = 'course'; + public const ITEM_TYPE_COURSE = 'course'; /** * Field shortname @@ -61,7 +56,7 @@ class helper { public const FIELD_ENROLLED_UNTIL = 'enrolleduntil'; /** - * Course fiel name. + * Course field name. */ public const COURSE_NAME = 'fullname'; @@ -80,6 +75,39 @@ class helper { */ public const STUDENT_ROLE = 'student'; + /** + * 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 $itemname Item name. + */ + public static function set_up_item(stdClass $course, int $itemid, string $itemtype, string $itemname): void { + $cohort = helper::get_cohort_by_item($itemid, $itemtype); + + if (empty($cohort)) { + $cohort = new stdClass(); + $cohort->contextid = context_system::instance()->id; + $cohort->name = $itemname; + $cohort->idnumber = $itemname; + $cohort->description = ucfirst($itemtype) . ' related'; + $typefieled = 'customfield_' . helper::COHORT_FIELD_TYPE; + $cohort->$typefieled = $itemtype; + $idfieled = 'customfield_' . helper::COHORT_FIELD_ID; + $cohort->$idfieled = $itemid; + + // Create a new cohort. + $cohort->id = helper::add_cohort($cohort); + } + + // Create a dynamic cohort rule associated with this cohort. + helper::add_rule($cohort, $itemtype); + // Add a tag to a custom profile field. + helper::update_profile_field($itemtype, $itemname); + // If yes, create enrolment method for the cohort for a given course. + helper::add_enrolment_method($course, $cohort); + } /** * Get cohort by provided item type and item id. diff --git a/classes/observer.php b/classes/observer.php index c2ba8be..e3ad72f 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -16,9 +16,8 @@ namespace tool_enrolprofile; +use core\event\course_created; use core\event\tag_added; -use stdClass; -use context_system; /** * Event observer class. @@ -43,36 +42,23 @@ public static function tag_added(tag_added $event): void { $tagid = $event->other['tagid']; $tagname = $event->other['tagrawname']; + $course = get_course($event->other['itemid']); - $courseid = $event->other['itemid']; - $course = get_course($courseid); + helper::set_up_item($course, $tagid, 'tag', $tagname); + } - $cohort = new stdClass(); - $cohort->contextid = context_system::instance()->id; - $cohort->name = $tagname; - $cohort->idnumber = $tagname; - $cohort->description = 'Tag related'; - $typefieled = 'customfield_' . helper::COHORT_FIELD_TYPE; - $cohort->$typefieled = 'tag'; - $idfieled = 'customfield_' . helper::COHORT_FIELD_ID; - $cohort->$idfieled = $tagid; + /** + * Process course_created event. + * + * @param course_created $event The event. + */ + public static function course_created(course_created $event): void { + global $DB; - // Check if cohort already exists. - $existingcohort = helper::get_cohort_by_item($tagid, 'tag'); + $course = get_course($event->courseid); + helper::set_up_item($course, $course->id, 'course', $course->{helper::COURSE_NAME}); - // If not. - if (empty($existingcohort)) { - // Create a new cohort. - $cohort->id = helper::add_cohort($cohort); - // Create a dynamic cohort rule associated with this cohort. - helper::add_rule($cohort, helper::FIELD_TAG); - // Add a tag to a custom profile field. - helper::update_profile_field(helper::FIELD_TAG, $tagname); - // Create enrolment method for the cohort for a given course. - helper::add_enrolment_method($course, $cohort); - } else { - // If yes, create enrolment method for the cohort for a given course. - helper::add_enrolment_method($course, $existingcohort); - } + $category = $DB->get_record('course_categories', ['id' => $course->category]); + helper::set_up_item($course, $category->id, 'category', $category->name); } } diff --git a/db/events.php b/db/events.php index 0b0db27..5744525 100644 --- a/db/events.php +++ b/db/events.php @@ -29,4 +29,8 @@ 'eventname' => '\core\event\tag_added', 'callback' => '\tool_enrolprofile\observer::tag_added', ], + [ + 'eventname' => '\core\event\course_created', + 'callback' => '\tool_enrolprofile\observer::course_created', + ], ]; diff --git a/tests/observer_test.php b/tests/observer_test.php index bb3e577..93ae625 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -32,6 +32,41 @@ */ class observer_test extends advanced_testcase { + /** + * Tag profile field for testing. + * @var \stdClass + */ + protected $tagprofilefield; + + /** + * Course profile field for testing. + * @var \stdClass + */ + protected $courseprofilefield; + + /** + * Category profile field for testing. + * @var \stdClass + */ + protected $categoryprofilefield; + + /** + * Set up before every test. + * + * @return void + */ + public function setUp(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + $this->tagprofilefield = $this->add_user_profile_field(helper::ITEM_TYPE_TAG, 'autocomplete'); + $this->courseprofilefield = $this->add_user_profile_field(helper::ITEM_TYPE_COURSE, 'autocomplete'); + $this->categoryprofilefield = $this->add_user_profile_field(helper::ITEM_TYPE_CATEGORY, 'autocomplete'); + + $this->create_cohort_custom_field(helper::COHORT_FIELD_ID); + $this->create_cohort_custom_field(helper::COHORT_FIELD_TYPE); + } + /** * A helper function to create a custom profile field. * @@ -95,24 +130,18 @@ protected function create_cohort_custom_field(string $shortname, string $datatyp public function test_tag_added() { global $DB; - $this->resetAfterTest(); - $this->setAdminUser(); - - $prodilefield = $this->add_user_profile_field(helper::FIELD_TAG, 'autocomplete'); - $this->create_cohort_custom_field(helper::COHORT_FIELD_ID); - $this->create_cohort_custom_field(helper::COHORT_FIELD_TYPE); - $course1 = $this->getDataGenerator()->create_course(); - $course2 = $this->getDataGenerator()->create_course(); + $course = $this->getDataGenerator()->create_course(); $tagname = 'A tag'; $this->assertEmpty($DB->get_record('cohort', ['name' => $tagname])); - $this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $prodilefield->id])); + $this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $this->tagprofilefield->id])); $this->assertEmpty($DB->get_record('tag', ['rawname' => $tagname])); $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $tagname])); - $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $tagname])); - $this->assertCount(0, $DB->get_records('enrol', ['courseid' => $course1->id, 'enrol' => 'cohort'])); - core_tag_tag::set_item_tags('core', 'course', $course1->id, context_course::instance($course1->id), [$tagname]); + // Should be already course and category cohorts. + $this->assertCount(2, $DB->get_records('enrol', ['courseid' => $course->id, 'enrol' => 'cohort'])); + + core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), [$tagname]); $tag = $DB->get_record('tag', ['rawname' => $tagname]); $this->assertNotEmpty($tag); @@ -120,7 +149,7 @@ public function test_tag_added() { $cohort = $DB->get_record('cohort', ['name' => $tagname]); $this->assertNotEmpty($cohort); - $cohort = cohort_get_cohort($cohort->id, context_course::instance($course1->id), true); + $cohort = cohort_get_cohort($cohort->id, context_course::instance($course->id), true); foreach ($cohort->customfields as $customfield) { if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_ID) { $this->assertSame($tag->id, $customfield->export_value()); @@ -130,7 +159,7 @@ public function test_tag_added() { } } - $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $prodilefield->id]); + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->tagprofilefield->id]); $this->assertNotEmpty($profilefielddata); $this->assertTrue(in_array($tagname, explode("\n", $profilefielddata))); @@ -141,11 +170,84 @@ public function test_tag_added() { $conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]); $this->assertCount(2, $conditions); - $this->assertCount(1, $DB->get_records('enrol', ['courseid' => $course1->id, 'enrol' => 'cohort'])); - $enrol = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'cohort']); + $this->assertCount(3, $DB->get_records('enrol', ['courseid' => $course->id, 'enrol' => 'cohort'])); + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertNotEmpty($enrol); + } + + /** + * Check logic when creating a course. + * @return void + */ + public function test_course_created() { + 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]); + + // Should be course and category cohorts. + $this->assertCount(2, $DB->get_records('enrol', ['courseid' => $course->id, 'enrol' => 'cohort'])); + + // Check everything about course cohort. + $coursecohort = $DB->get_record('cohort', ['name' => $coursename]); + $this->assertNotEmpty($coursecohort); + + $cohort = cohort_get_cohort($coursecohort->id, context_course::instance($course->id), true); + foreach ($cohort->customfields as $customfield) { + if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_ID) { + $this->assertSame($course->id, $customfield->export_value()); + } + if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) { + $this->assertSame('course', $customfield->export_value()); + } + } + + $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); - $this->assertEquals($cohort->id, $enrol->customint1); - core_tag_tag::set_item_tags('core', 'course', $course2->id, \context_course::instance($course1->id), [$tagname]); + // Check everything about category cohort. + $category = $DB->get_record('course_categories', ['id' => $course->category]); + $categorycohort = $DB->get_record('cohort', ['name' => $category->name]); + $this->assertNotEmpty($categorycohort); + + $cohort = cohort_get_cohort($categorycohort->id, context_course::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('category', $customfield->export_value()); + } + } + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]); + $this->assertNotEmpty($profilefielddata); + $this->assertTrue(in_array($category->name, explode("\n", $profilefielddata))); + + $rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $category->name]); + $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); } }