From 02113beb100099406fb036c3dd5af31a34c26e73 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 29 Apr 2016 18:22:11 +0800 Subject: [PATCH] MDL-53920 cohort: notify competencies on cohort deletion --- cohort/lib.php | 3 +++ competency/classes/api.php | 13 +++++++++++++ competency/tests/hooks_test.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/cohort/lib.php b/cohort/lib.php index de015a41c6665..467c592121dec 100644 --- a/cohort/lib.php +++ b/cohort/lib.php @@ -113,6 +113,9 @@ function cohort_delete_cohort($cohort) { $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id)); $DB->delete_records('cohort', array('id'=>$cohort->id)); + // Notify the competency subsystem. + \core_competency\api::hook_cohort_deleted($cohort); + $event = \core\event\cohort_deleted::create(array( 'context' => context::instance_by_id($cohort->contextid), 'objectid' => $cohort->id, diff --git a/competency/classes/api.php b/competency/classes/api.php index 2a76ecd06b7fe..a70ac33de01cf 100644 --- a/competency/classes/api.php +++ b/competency/classes/api.php @@ -4706,6 +4706,19 @@ public static function hook_course_reset_competency_ratings($courseid) { $DB->delete_records(user_competency_course::TABLE, array('courseid' => $courseid)); } + /** + * Action to perform when a cohort is deleted. + * + * Do not call this directly, this is reserved for core use. + * + * @param \stdClass $cohort The cohort object. + * @return void + */ + public static function hook_cohort_deleted(\stdClass $cohort) { + global $DB; + $DB->delete_records(template_cohort::TABLE, array('cohortid' => $cohort->id)); + } + /** * Manually grade a user competency. * diff --git a/competency/tests/hooks_test.php b/competency/tests/hooks_test.php index 568b1897e98fd..c6657c7aa8b78 100644 --- a/competency/tests/hooks_test.php +++ b/competency/tests/hooks_test.php @@ -179,4 +179,32 @@ public function test_hook_course_reset_competency_ratings() { $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id])); } + public function test_hook_cohort_deleted() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $datagen = $this->getDataGenerator(); + $corecompgen = $datagen->get_plugin_generator('core_competency'); + + $c1 = $datagen->create_cohort(); + $c2 = $datagen->create_cohort(); + $t1 = $corecompgen->create_template(); + $t2 = $corecompgen->create_template(); + + // Create the template cohorts. + core_competency\api::create_template_cohort($t1->get_id(), $c1->id); + core_competency\api::create_template_cohort($t1->get_id(), $c2->id); + core_competency\api::create_template_cohort($t2->get_id(), $c1->id); + + // Check that the association was made. + $this->assertEquals(2, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id()))); + $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id()))); + + // Delete the first cohort. + cohort_delete_cohort($c1); + + // Check that the association was removed. + $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id()))); + $this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id()))); + } }