MDL-52676 tool_lp: When deleting a template delete template_cohorts

This commit is contained in:
Issam Taboubi
2016-04-18 10:58:50 +08:00
committed by Frederic Massart
parent 3c230247ce
commit 5a4ee001cb
3 changed files with 83 additions and 5 deletions
+26 -1
View File
@@ -1117,6 +1117,7 @@ class api {
/**
* Delete a learning plan template by id.
* If the learning plan template has associated cohorts they will be deleted.
*
* Requires tool/lp:templatemanage capability.
*
@@ -1124,6 +1125,7 @@ class api {
* @return boolean
*/
public static function delete_template($id) {
global $DB;
$template = new template($id);
// First we do a permissions check.
@@ -1131,8 +1133,31 @@ class api {
throw new required_capability_exception($template->get_context(), 'tool/lp:templatemanage', 'nopermissions', '');
}
$transaction = $DB->start_delegated_transaction();
$success = true;
// Check if there are cohorts associated.
$templatecohorts = template_cohort::get_relations_by_templateid($template->get_id());
foreach ($templatecohorts as $templatecohort) {
$success = $templatecohort->delete();
if (!$success) {
break;
}
}
// OK - all set.
return $template->delete();
if ($success) {
$success = $template->delete();
}
if ($success) {
// Commit the transaction.
$transaction->allow_commit();
} else {
$transaction->rollback(new moodle_exception('Error while deleting the template.'));
}
return $success;
}
/**
+25
View File
@@ -149,6 +149,31 @@ class template_cohort extends persistent {
return $relation;
}
/**
* Get a relations by templateid.
*
* This does not perform any validation on the data passed. If the relation exists in the database
* then it is loaded in a the model, if not then it is up to the developer to save the model.
*
* @param int $templateid
* @return template_cohort[] array of template cohort
*/
public static function get_relations_by_templateid($templateid) {
global $DB;
$params = array(
'templateid' => $templateid
);
$relations = array();
$records = $DB->get_records(self::TABLE, $params);
foreach ($records as $record) {
$relations[] = new template_cohort(0, $record);
}
return $relations;
}
/**
* Return an array of templates persistent with their missing userids.
*
+32 -4
View File
@@ -324,7 +324,7 @@ class tool_lp_api_testcase extends advanced_testcase {
$relatedcompetencies = $compduplicated1->get_related_competencies();
$comprelated = current($relatedcompetencies);
$this->assertEquals($comprelated->get_idnumber(), $competency2->get_idnumber());
// Check if config rule have been ported correctly.
$competency4duplicated = competency::get_record(array(
'idnumber' => $competency4->get_idnumber(),
@@ -342,8 +342,7 @@ class tool_lp_api_testcase extends advanced_testcase {
$competenciesidsrules[] = $value['id'];
}
$this->assertTrue($competency4duplicated->is_parent_of($competenciesidsrules));
}
/**
@@ -628,7 +627,6 @@ class tool_lp_api_testcase extends advanced_testcase {
$tpl1 = $lpg->create_template();
$tpl2 = $lpg->create_template();
// Create plans with data not matching templates.
$time = time();
$plan1 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u1->id,
@@ -1660,6 +1658,36 @@ class tool_lp_api_testcase extends advanced_testcase {
$this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
}
public function test_delete_template() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$c1 = $dg->create_cohort();
$c2 = $dg->create_cohort();
$template = $lpg->create_template();
$id = $template->get_id();
// Create 2 template cohorts.
$tc1 = $lpg->create_template_cohort(array('templateid' => $template->get_id(), 'cohortid' => $c1->id));
$tc1 = $lpg->create_template_cohort(array('templateid' => $template->get_id(), 'cohortid' => $c2->id));
// Check pre-test.
$this->assertTrue(tool_lp\template::record_exists($id));
$this->assertEquals(2, \tool_lp\template_cohort::count_records(array('templateid' => $id)));
$result = api::delete_template($template->get_id());
$this->assertTrue($result);
// Check that the template deos not exist anymore.
$this->assertFalse(tool_lp\template::record_exists($id));
// Test if associated cohorts are also deleted.
$this->assertEquals(0, \tool_lp\template_cohort::count_records(array('templateid' => $id)));
}
public function test_delete_template_cohort() {
$this->resetAfterTest(true);
$this->setAdminUser();