Skip to content

Commit

Permalink
issue #6: process category deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 6, 2024
1 parent fe07309 commit 9688cba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
14 changes: 14 additions & 0 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace tool_enrolprofile;

use core\event\course_category_created;
use core\event\course_category_deleted;
use core\event\course_created;
use core\event\course_deleted;
use core\event\tag_added;
Expand Down Expand Up @@ -118,4 +119,17 @@ public static function course_category_created(course_category_created $event):
$category = $DB->get_record('course_categories', ['id' => $event->objectid]);
helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name);
}

/**
* Process course_category_deleted event.
*
* @param course_category_deleted $event The event.
*/
public static function course_category_deleted(course_category_deleted $event): void {

$categoryid = $event->objectid;
$categoryname = $event->other['name'];

helper::remove_item($categoryid, helper::ITEM_TYPE_CATEGORY, $categoryname);
}
}
5 changes: 4 additions & 1 deletion db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
'eventname' => '\core\event\course_category_created',
'callback' => '\tool_enrolprofile\observer::course_category_created',
],

[
'eventname' => '\core\event\course_category_deleted',
'callback' => '\tool_enrolprofile\observer::course_category_deleted',
],
];
48 changes: 48 additions & 0 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,52 @@ public function test_course_category_created(): void {
$conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]);
$this->assertCount(2, $conditions);
}

/**
* Check logic when deleting a course category.
*/
public function test_course_category_deleted(): 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]);
$course = $this->getDataGenerator()->create_course(['category' => $category->id]);

// 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);

$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);

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

$category->delete_full(false);

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

$profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]);
$this->assertFalse(in_array($categoryname, explode("\n", $profilefielddata)));
$this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname]));
$this->assertEmpty($DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]));

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

0 comments on commit 9688cba

Please sign in to comment.