Skip to content

Commit

Permalink
issue #5: process course_deleted event
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 6, 2024
1 parent 95483ea commit 4b2bf29
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
12 changes: 12 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_created;
use core\event\course_deleted;
use core\event\tag_added;
use core\event\tag_removed;
use core\event\tag_deleted;
Expand Down Expand Up @@ -95,6 +96,17 @@ public static function course_created(course_created $event): void {
helper::add_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name, $course);
}

/**
* Process course_deleted event.
*
* @param course_deleted $event The event.
*/
public static function course_deleted(course_deleted $event): void {
$courseid = $event->courseid;
$coursename = $event->other['fullname'];
helper::remove_item($courseid, helper::ITEM_TYPE_COURSE, $coursename);
}

/**
* Process course_category_created event.
*
Expand Down
4 changes: 4 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
'eventname' => '\core\event\course_created',
'callback' => '\tool_enrolprofile\observer::course_created',
],
[
'eventname' => '\core\event\course_deleted',
'callback' => '\tool_enrolprofile\observer::course_deleted',
],
[
'eventname' => '\core\event\course_category_created',
'callback' => '\tool_enrolprofile\observer::course_category_created',
Expand Down
47 changes: 47 additions & 0 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,53 @@ public function test_course_created(): void {
$this->assertNotEmpty($enrol);
}

/**
* Check logic when deleting a course.
*/
public function test_course_deleted(): void {
global $DB;

$coursename = 'Course name';
$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']));

$coursecohort = $DB->get_record('cohort', ['name' => $coursename]);
$this->assertNotEmpty($coursecohort);

$cohort = cohort_get_cohort($coursecohort->id, context_course::instance($course->id), true);

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

delete_course($course->id, false);

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

$profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id]);
$this->assertFalse(in_array($coursename, explode("\n", $profilefielddata)));

$this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $coursename]));
$conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]);
$this->assertCount(0, $conditions);

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

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

0 comments on commit 4b2bf29

Please sign in to comment.