/** * Test duplicate a framework. */ public function test_duplicate_framework() { $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $this->resetAfterTest(true); $this->setAdminUser(); $syscontext = context_system::instance(); $params = array('shortname' => 'shortname_a', 'idnumber' => 'idnumber_c', 'description' => 'description', 'descriptionformat' => FORMAT_HTML, 'visible' => true, 'contextid' => $syscontext->id); $framework = $lpg->create_framework($params); $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $competency41 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $competency4->get_id())); $competency42 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $competency4->get_id())); $competencyidnumbers = array($competency1->get_idnumber(), $competency2->get_idnumber(), $competency3->get_idnumber(), $competency4->get_idnumber(), $competency41->get_idnumber(), $competency42->get_idnumber()); $config = json_encode(array('base' => array('points' => 4), 'competencies' => array(array('id' => $competency41->get_id(), 'points' => 3, 'required' => 0), array('id' => $competency42->get_id(), 'points' => 2, 'required' => 1)))); $competency4->set_ruletype('core_competency\\competency_rule_points'); $competency4->set_ruleoutcome(\core_competency\competency::OUTCOME_EVIDENCE); $competency4->set_ruleconfig($config); $competency4->update(); api::add_related_competency($competency1->get_id(), $competency2->get_id()); api::add_related_competency($competency3->get_id(), $competency4->get_id()); $frameworkduplicated1 = api::duplicate_framework($framework->get_id()); $frameworkduplicated2 = api::duplicate_framework($framework->get_id()); $this->assertEquals($framework->get_idnumber() . '_1', $frameworkduplicated1->get_idnumber()); $this->assertEquals($framework->get_idnumber() . '_2', $frameworkduplicated2->get_idnumber()); $competenciesfr1 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated1->get_id())); $competenciesfr2 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated2->get_id())); $competencyidsfr1 = array(); $competencyidsfr2 = array(); foreach ($competenciesfr1 as $cmp) { $competencyidsfr1[] = $cmp->get_idnumber(); } foreach ($competenciesfr2 as $cmp) { $competencyidsfr2[] = $cmp->get_idnumber(); } $this->assertEmpty(array_diff($competencyidsfr1, $competencyidnumbers)); $this->assertEmpty(array_diff($competencyidsfr2, $competencyidnumbers)); $this->assertCount(6, $competenciesfr1); $this->assertCount(6, $competenciesfr2); // Test the related competencies. reset($competenciesfr1); $compduplicated1 = current($competenciesfr1); $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(), 'competencyframeworkid' => $frameworkduplicated2->get_id())); $configduplicated = json_decode($competency4duplicated->get_ruleconfig(), true); $configorigin = json_decode($config, true); // Check that the 2 config have the same base. $this->assertEquals($configorigin['base'], $configduplicated['base']); $this->assertEquals(count($configorigin['competencies']), count($configduplicated['competencies'])); $competencyidsrules = array(); foreach ($configduplicated['competencies'] as $key => $value) { // Check that the only difference between the 2 config is id competency. $this->assertEquals(1, count(array_diff($value, $configorigin['competencies'][$key]))); $competencyidsrules[] = $value['id']; } $this->assertTrue($competency4duplicated->is_parent_of($competencyidsrules)); // Test duplicate an empty framework. $emptyfrm = $lpg->create_framework(); $emptyfrmduplicated = api::duplicate_framework($emptyfrm->get_id()); $this->assertEquals($emptyfrm->get_idnumber() . '_1', $emptyfrmduplicated->get_idnumber()); $nbcomp = api::count_competencies(array('competencyframeworkid' => $emptyfrmduplicated->get_id())); $this->assertEquals(0, $nbcomp); }
/** * Adds a related competency. * * @param int $competencyid * @param int $relatedcompetencyid * @return bool */ public static function add_related_competency($competencyid, $relatedcompetencyid) { $params = self::validate_parameters(self::add_related_competency_parameters(), array('competencyid' => $competencyid, 'relatedcompetencyid' => $relatedcompetencyid)); $competency = api::read_competency($params['competencyid']); self::validate_context($competency->get_context()); return api::add_related_competency($params['competencyid'], $params['relatedcompetencyid']); }
public function set_related_competencies($record) { if (!empty($record->related)) { foreach ($record->related as $related) { api::add_related_competency($record->id, $related->id); } } foreach ($record->children as $child) { $this->set_related_competencies($child); } }
private function set_related($record) { $comp = $record->createdcomp; if ($record->relatedidnumbers) { $allidnumbers = explode(',', $record->relatedidnumbers); foreach ($allidnumbers as $rawidnumber) { $idnumber = str_replace('%2C', ',', $rawidnumber); if (isset($this->flat[$idnumber])) { $relatedcomp = $this->flat[$idnumber]->createdcomp; api::add_related_competency($comp->get_id(), $relatedcomp->get_id()); } } } foreach ($record->children as $child) { $this->set_related($child); } }