/** * Recursively duplicate competencies from a tree, we start duplicating from parents to children to have a correct path. * This method does not copy the related competencies. * * @param int $frameworkid - framework id * @param competency[] $tree - array of competencies object * @param int $oldparent - old parent id * @param int $newparent - new parent id * @return competency[] $matchids - List of old competencies ids matched with new competencies object. */ protected static function duplicate_competency_tree($frameworkid, $tree, $oldparent = 0, $newparent = 0) { $matchids = array(); foreach ($tree as $node) { if ($node->competency->get_parentid() == $oldparent) { $parentid = $node->competency->get_id(); // Create the competency. $competency = new competency(0, $node->competency->to_record()); $competency->set_competencyframeworkid($frameworkid); $competency->set_parentid($newparent); $competency->set_path(''); $competency->set_id(0); $competency->reset_rule(); $competency->create(); // Trigger the created event competency. \core\event\competency_created::create_from_competency($competency)->trigger(); // Match the old id with the new one. $matchids[$parentid] = $competency; if (!empty($node->children)) { // Duplicate children competency. $childrenids = self::duplicate_competency_tree($frameworkid, $node->children, $parentid, $competency->get_id()); // Array_merge does not keep keys when merging so we use the + operator. $matchids = $matchids + $childrenids; } } } return $matchids; }