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)))); }
/** * Duplicate a competency framework by id. * * Requires moodle/competency:competencymanage capability at the system context. * * @param int $id The record to duplicate. All competencies associated and related will be duplicated. * @return competency_framework the framework duplicated */ public static function duplicate_framework($id) { global $DB; static::require_enabled(); $framework = new competency_framework($id); require_capability('moodle/competency:competencymanage', $framework->get_context()); // Starting transaction. $transaction = $DB->start_delegated_transaction(); try { // Get a uniq idnumber based on the origin framework. $idnumber = competency_framework::get_unused_idnumber($framework->get_idnumber()); $framework->set_idnumber($idnumber); // Adding the suffix copy to the shortname. $framework->set_shortname(get_string('duplicateditemname', 'core_competency', $framework->get_shortname())); $framework->set_id(0); $framework = $framework->create(); // Array that match the old competencies ids with the new one to use when copying related competencies. $frameworkcompetency = competency::get_framework_tree($id); $matchids = self::duplicate_competency_tree($framework->get_id(), $frameworkcompetency, 0, 0); // Copy the related competencies. $relcomps = related_competency::get_multiple_relations(array_keys($matchids)); foreach ($relcomps as $relcomp) { $compid = $relcomp->get_competencyid(); $relcompid = $relcomp->get_relatedcompetencyid(); if (isset($matchids[$compid]) && isset($matchids[$relcompid])) { $newcompid = $matchids[$compid]->get_id(); $newrelcompid = $matchids[$relcompid]->get_id(); if ($newcompid < $newrelcompid) { $relcomp->set_competencyid($newcompid); $relcomp->set_relatedcompetencyid($newrelcompid); } else { $relcomp->set_competencyid($newrelcompid); $relcomp->set_relatedcompetencyid($newcompid); } $relcomp->set_id(0); $relcomp->create(); } else { // Debugging message when there is no match found. debugging('related competency id not found'); } } // Setting rules on duplicated competencies. self::migrate_competency_tree_rules($frameworkcompetency, $matchids); $transaction->allow_commit(); } catch (\Exception $e) { $transaction->rollback($e); } // Trigger a competency framework created event. \core\event\competency_framework_created::create_from_framework($framework)->trigger(); return $framework; }