Skip to content

Commit

Permalink
MDL-52676 tool_lp: When deleting a template delete template_cohorts
Browse files Browse the repository at this point in the history
  • Loading branch information
taboubi authored and Frederic Massart committed Apr 18, 2016
1 parent 3c23024 commit 5a4ee00
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
27 changes: 26 additions & 1 deletion admin/tool/lp/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,22 +1117,47 @@ public static function duplicate_template($id) {

/**
* Delete a learning plan template by id.
* If the learning plan template has associated cohorts they will be deleted.
*
* Requires tool/lp:templatemanage capability.
*
* @param int $id The record to delete.
* @return boolean
*/
public static function delete_template($id) {
global $DB;
$template = new template($id);

// First we do a permissions check.
if (!$template->can_manage()) {
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;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions admin/tool/lp/classes/template_cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ public static function get_relation($templateid, $cohortid) {
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.
*
Expand Down
36 changes: 32 additions & 4 deletions admin/tool/lp/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function test_duplicate_framework() {
$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(),
Expand All @@ -342,8 +342,7 @@ public function test_duplicate_framework() {
$competenciesidsrules[] = $value['id'];
}
$this->assertTrue($competency4duplicated->is_parent_of($competenciesidsrules));



}

/**
Expand Down Expand Up @@ -628,7 +627,6 @@ public function test_update_template_updates_plans() {
$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,
Expand Down Expand Up @@ -1660,6 +1658,36 @@ public function test_create_template_cohort() {
$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();
Expand Down

0 comments on commit 5a4ee00

Please sign in to comment.