MDL-53920 cohort: notify competencies on cohort deletion

This commit is contained in:
Mark Nelson 2016-04-29 18:22:11 +08:00
parent 3219a4535f
commit 02113beb10
3 changed files with 44 additions and 0 deletions

View File

@ -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,

View File

@ -4706,6 +4706,19 @@ class api {
$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.
*

View File

@ -179,4 +179,32 @@ class core_competency_hooks_testcase extends advanced_testcase {
$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())));
}
}