Skip to content

Commit

Permalink
issue #6: process category renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 10, 2024
1 parent a1b72b3 commit da46b20
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
13 changes: 13 additions & 0 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use core\event\course_category_created;
use core\event\course_category_deleted;
use core\event\course_category_updated;
use core\event\course_created;
use core\event\course_deleted;
use core\event\course_updated;
Expand Down Expand Up @@ -144,6 +145,18 @@ public static function course_category_created(course_category_created $event):
helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name);
}

/**
* Process course_category_updated event.
*
* @param course_category_updated $event The event.
*/
public static function course_category_updated(course_category_updated $event): void {
global $DB;

$category = $DB->get_record('course_categories', ['id' => $event->objectid]);
helper::rename_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name);
}

/**
* Process course_category_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 @@ -57,6 +57,10 @@
'eventname' => '\core\event\course_category_created',
'callback' => '\tool_enrolprofile\observer::course_category_created',
],
[
'eventname' => '\core\event\course_category_updated',
'callback' => '\tool_enrolprofile\observer::course_category_updated',
],
[
'eventname' => '\core\event\course_category_deleted',
'callback' => '\tool_enrolprofile\observer::course_category_deleted',
Expand Down
111 changes: 110 additions & 1 deletion tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public function test_course_name_updated(): void {

$enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]);
$this->assertNotEmpty($enrol);

profile_load_data($user1);
$this->assertSame([
'Not course',
Expand Down Expand Up @@ -640,6 +640,115 @@ public function test_course_category_created(): void {
$this->assertCount(2, $conditions);
}

/**
* Check logic when updating a name of the course category.
*/
public function test_course_category_name_updated(): void {
global $DB;

$categoryname = 'Category name';
$profilefield = 'profile_field_' . helper::ITEM_TYPE_CATEGORY;

$category = $this->getDataGenerator()->create_category(['name' => $categoryname]);
$this->getDataGenerator()->create_category(['name' => 'Not category']);

// Check everything about category cohort.
$categorycohort = $DB->get_record('cohort', ['name' => $categoryname]);
$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);

$user1 = $this->getDataGenerator()->create_user();
profile_save_data((object)[
'id' => $user1->id,
$profilefield => [
'Not category',
$categoryname,
]
]);

$user2 = $this->getDataGenerator()->create_user();
profile_save_data((object)[
'id' => $user2->id,
$profilefield => [
$categoryname,
'Not category',
]
]);

profile_load_data($user1);
$this->assertSame([
'Not category',
$categoryname,
], $user1->$profilefield
);
profile_load_data($user2);
$this->assertSame([
$categoryname,
'Not category',
], $user2->$profilefield
);

// Update category name.
$newcategoryname = 'New category name';
$categoryrecord = $DB->get_record('course_categories', ['id' => $category->id]);
$categoryrecord->name = $newcategoryname;
$category->update($categoryrecord);

$categorycohort = $DB->get_record('cohort', ['name' => $newcategoryname]);
$this->assertNotEmpty($categorycohort);
$this->assertEmpty($DB->get_record('cohort', ['name' => $categoryname]));

$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($newcategoryname, explode("\n", $profilefielddata)));
$this->assertFalse(in_array($categoryname, explode("\n", $profilefielddata)));

$rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $newcategoryname]);
$this->assertNotEmpty($rule);
$this->assertEquals($cohort->id, $rule->cohortid);
$this->assertEmpty($DB->get_record('cohort', ['name' => $categoryname]));

profile_load_data($user1);
$this->assertSame([
'Not category',
$newcategoryname,
], $user1->$profilefield
);
profile_load_data($user2);
$this->assertSame([
$newcategoryname,
'Not category',
], $user2->$profilefield
);
}

/**
* Check logic when deleting a course category.
*/
Expand Down

0 comments on commit da46b20

Please sign in to comment.