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;
    }