Skip to content

Commit

Permalink
MDL-53920 cohort: notify competencies on cohort deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed May 2, 2016
1 parent 3219a45 commit 02113be
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions competency/tests/hooks_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
}

0 comments on commit 02113be

Please sign in to comment.