public function test_delete_template_unlink_plans() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $u1 = $dg->create_user(); $f = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $tpl = $lpg->create_template(); $tplc1 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1->get_id(), 'sortorder' => 1)); $tplc2 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c2->get_id(), 'sortorder' => 2)); $p1 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u1->id)); // Check pre-test. $this->assertTrue(\core_competency\template::record_exists($tpl->get_id())); $this->assertEquals(2, \core_competency\template_competency::count_competencies($tpl->get_id())); $this->assertEquals(1, count(\core_competency\plan::get_records(array('templateid' => $tpl->get_id())))); $result = api::delete_template($tpl->get_id(), false); $this->assertTrue($result); // Check that the template does not exist anymore. $this->assertFalse(\core_competency\template::record_exists($tpl->get_id())); // Check that associated competencies are also deleted. $this->assertEquals(0, \core_competency\template_competency::count_competencies($tpl->get_id())); // Check that associated plan still exist but unlink from template. $plans = \core_competency\plan::get_records(array('id' => $p1->get_id())); $this->assertEquals(1, count($plans)); $this->assertEquals($plans[0]->get_origtemplateid(), $tpl->get_id()); $this->assertNull($plans[0]->get_templateid()); }
/** * Delete a learning plan template by id. * If the learning plan template has associated cohorts they will be deleted. * * Requires moodle/competency:templatemanage capability. * * @param int $id The record to delete. * @param boolean $deleteplans True to delete plans associaated to template, false to unlink them. * @return boolean */ public static function delete_template($id, $deleteplans = true) { global $DB; static::require_enabled(); $template = new template($id); // First we do a permissions check. if (!$template->can_manage()) { throw new required_capability_exception($template->get_context(), 'moodle/competency: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; } } // Still OK, delete or unlink the plans from the template. if ($success) { $plans = plan::get_records(array('templateid' => $template->get_id())); foreach ($plans as $plan) { $success = $deleteplans ? self::delete_plan($plan->get_id()) : self::unlink_plan_from_template($plan); if (!$success) { break; } } } // Still OK, delete the template comptencies. if ($success) { $success = template_competency::delete_by_templateid($template->get_id()); } // OK - all set. if ($success) { // Create a template deleted event. $event = \core\event\competency_template_deleted::create_from_template($template); $success = $template->delete(); } if ($success) { // Trigger a template deleted event. $event->trigger(); // Commit the transaction. $transaction->allow_commit(); } else { $transaction->rollback(new moodle_exception('Error while deleting the template.')); } return $success; }