From a1b72b30a6bf904c3e9e510e820cbc930f852f3f Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Tue, 10 Sep 2024 20:52:14 +1000 Subject: [PATCH] issue #5: process course name updating --- classes/observer.php | 15 +++++- db/events.php | 4 ++ tests/observer_test.php | 116 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/classes/observer.php b/classes/observer.php index b6a10e4..2578db9 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -20,6 +20,7 @@ use core\event\course_category_deleted; use core\event\course_created; use core\event\course_deleted; +use core\event\course_updated; use core\event\tag_added; use core\event\tag_removed; use core\event\tag_deleted; @@ -83,7 +84,7 @@ public static function tag_deleted(tag_deleted $event): void { } /** - * Process tag_deleted event. + * Process tag_updated event. * * @param tag_updated $event The event. */ @@ -108,6 +109,18 @@ public static function course_created(course_created $event): void { helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name, $course); } + /** + * Process course_updated event. + * + * @param course_updated $event The event. + */ + public static function course_updated(course_updated $event): void { + if (key_exists(helper::COURSE_NAME, $event->other['updatedfields'])) { + $newcoursename = $event->other['updatedfields'][helper::COURSE_NAME]; + helper::rename_item($event->courseid, helper::ITEM_TYPE_COURSE, $newcoursename); + } + } + /** * Process course_deleted event. * diff --git a/db/events.php b/db/events.php index 9e6f3b7..c4a20e2 100644 --- a/db/events.php +++ b/db/events.php @@ -45,6 +45,10 @@ 'eventname' => '\core\event\course_created', 'callback' => '\tool_enrolprofile\observer::course_created', ], + [ + 'eventname' => '\core\event\course_updated', + 'callback' => '\tool_enrolprofile\observer::course_updated', + ], [ 'eventname' => '\core\event\course_deleted', 'callback' => '\tool_enrolprofile\observer::course_deleted', diff --git a/tests/observer_test.php b/tests/observer_test.php index d9011a7..74c5432 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -299,10 +299,8 @@ public function test_tag_updated(): void { 'Another tag', ], $user2->$profilefield ); - } - /** * Check logic when removing a tag. */ @@ -442,6 +440,120 @@ public function test_course_created(): void { $this->assertNotEmpty($enrol); } + /** + * Check logic when updating a name of the course. + */ + public function test_course_name_updated(): void { + global $DB; + + $coursename = 'Course name'; + $profilefield = 'profile_field_' . helper::ITEM_TYPE_COURSE; + + $course = $this->getDataGenerator()->create_course(['fullname' => $coursename]); + $this->getDataGenerator()->create_course(['fullname' => 'Not course']); + + // 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(helper::ITEM_TYPE_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); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertNotEmpty($enrol); + + $user1 = $this->getDataGenerator()->create_user(); + profile_save_data((object)[ + 'id' => $user1->id, + $profilefield => [ + 'Not course', + $coursename, + ] + ]); + + $user2 = $this->getDataGenerator()->create_user(); + profile_save_data((object)[ + 'id' => $user2->id, + $profilefield => [ + $coursename, + 'Not course', + ] + ]); + + profile_load_data($user1); + $this->assertSame([ + 'Not course', + $coursename, + ], $user1->$profilefield + ); + profile_load_data($user2); + $this->assertSame([ + $coursename, + 'Not course', + ], $user2->$profilefield + ); + + // Update course full name. + $newcoursename = 'New course name'; + $course->fullname = $newcoursename; + update_course($course); + + $coursecohort = $DB->get_record('cohort', ['name' => $newcoursename]); + $this->assertNotEmpty($coursecohort); + $this->assertEmpty($DB->get_record('cohort', ['name' => $coursename])); + + $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(helper::ITEM_TYPE_COURSE, $customfield->export_value()); + } + } + + $profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id]); + $this->assertNotEmpty($profilefielddata); + $this->assertTrue(in_array($newcoursename, explode("\n", $profilefielddata))); + $this->assertFalse(in_array($coursename, explode("\n", $profilefielddata))); + + $rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $newcoursename]); + $this->assertNotEmpty($rule); + $this->assertEquals($cohort->id, $rule->cohortid); + $this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $coursename])); + + $enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]); + $this->assertNotEmpty($enrol); + + profile_load_data($user1); + $this->assertSame([ + 'Not course', + $newcoursename, + ], $user1->$profilefield + ); + profile_load_data($user2); + $this->assertSame([ + $newcoursename, + 'Not course', + ], $user2->$profilefield + ); + } + /** * Check logic when deleting a course. */