public function test_create_related_competency() { $this->resetAfterTest(true); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $this->assertEquals(0, related_competency::count_records()); $rc = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id())); $rc = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c3->get_id())); $this->assertEquals(2, related_competency::count_records()); $this->assertInstanceOf('\\core_competency\\related_competency', $rc); }
public function test_delete_framework_competency_used_in_course() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $this->setAdminUser(); $cat1 = $dg->create_category(); $u1 = $dg->create_user(); $course = $dg->create_course(array('category' => $cat1->id)); $f1 = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c2id = $c2->get_id(); $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id())); $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id())); $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id())); $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id())); // Create related competencies. $rc = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c11b->get_id())); $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid()); // Creating a standard evidence with minimal information. $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id())); $usercompetencyid = $uc1->get_id(); $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid)); $this->assertEquals($uc1->get_id(), $evidence->get_usercompetencyid()); $uc1->delete(); // Add competency to course. $cc = $lpg->create_course_competency(array('courseid' => $course->id, 'competencyid' => $c11b->get_id())); // We can not delete a framework if the competency or competencies children is linked to a course. $this->assertFalse(api::delete_framework($f1->get_id())); // Check that none of associated data are deleted. $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid()); $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid()); // We can delete the framework if we remove the competency from course. $cc->delete(); $this->assertTrue(api::delete_framework($f1->get_id())); $this->assertFalse(competency::record_exists($c1->get_id())); $this->assertFalse(competency::record_exists($c2->get_id())); $this->assertFalse(competency::record_exists($c1a->get_id())); $this->assertFalse(competency::record_exists($c1b->get_id())); $this->assertFalse(competency::record_exists($c11b->get_id())); $this->assertFalse(competency::record_exists($c12b->get_id())); // Check if evidence are also deleted. $this->assertEquals(0, \core_competency\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id()))); // Check if related conpetency relation is deleted. $this->assertEquals(0, count(\core_competency\related_competency::get_multiple_relations(array($c2id)))); }
/** * Remove a related competency. * * @param int $competencyid The id of the competency. * @param int $relatedcompetencyid The id of the related competency. * @return bool True when it was deleted, false when it wasn't or the relation doesn't exist. */ public static function remove_related_competency($competencyid, $relatedcompetencyid) { static::require_enabled(); $competency = new competency($competencyid); // This only check if we have the permission in either competency because both competencies // should belong to the same framework. require_capability('moodle/competency:competencymanage', $competency->get_context()); $relatedcompetency = related_competency::get_relation($competencyid, $relatedcompetencyid); if ($relatedcompetency->get_id()) { return $relatedcompetency->delete(); } return false; }
/** * Create a related competency. * * @param array|stdClass $record * @return related_competency */ public function create_related_competency($record = null) { $record = (object) $record; if (!isset($record->competencyid)) { throw new coding_exception('Property competencyid is required.'); } if (!isset($record->relatedcompetencyid)) { throw new coding_exception('Property relatedcompetencyid is required.'); } $relation = related_competency::get_relation($record->competencyid, $record->relatedcompetencyid); if ($relation->get_id()) { throw new coding_exception('Relation already exists'); } $relation->create(); return $relation; }
/** * Return the related competencies. * * @return competency[] */ public function get_related_competencies() { return related_competency::get_related_competencies($this->get_id()); }
/** * Test that we can remove related competencies. * * @return void */ public function test_remove_related_competency() { $this->setUser($this->creator); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $rc1 = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id())); $rc2 = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c3->get_id())); $this->assertEquals(2, related_competency::count_records()); // Returns false when the relation does not exist. $result = external::remove_related_competency($c1->get_id(), $c3->get_id()); $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result); $this->assertFalse($result); // Returns true on success. $result = external::remove_related_competency($c2->get_id(), $c3->get_id()); $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result); $this->assertTrue($result); $this->assertEquals(1, related_competency::count_records()); // We don't need to specify competencyid and relatedcompetencyid in the right order. $result = external::remove_related_competency($c2->get_id(), $c1->get_id()); $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result); $this->assertTrue($result); $this->assertEquals(0, related_competency::count_records()); }