Skip to content

Commit

Permalink
issue #5: process course name updating
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 10, 2024
1 parent 3defe4a commit a1b72b3
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
15 changes: 14 additions & 1 deletion classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
116 changes: 114 additions & 2 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,8 @@ public function test_tag_updated(): void {
'Another tag',
], $user2->$profilefield
);

}


/**
* Check logic when removing a tag.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit a1b72b3

Please sign in to comment.