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()); }
/** * Count all the competencies in a learning plan template. * * @param template|int $templateorid The template or its ID. * @return int */ public static function count_competencies_in_template($templateorid) { static::require_enabled(); // First we do a permissions check. $template = $templateorid; if (!is_object($template)) { $template = new template($template); } if (!$template->can_read()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview', 'nopermissions', ''); } // OK - all set. return template_competency::count_competencies($template->get_id()); }