protected static function migrate_competency_tree_rules($tree, $matchids) {

        foreach ($tree as $node) {
            $oldcompid = $node->competency->get('id');
            if ($node->competency->get('ruletype') && array_key_exists($oldcompid, $matchids)) {
                try {
                    // Get the new competency.
                    $competency = $matchids[$oldcompid];
                    $class = $node->competency->get('ruletype');
                    $newruleconfig = $class::migrate_config($node->competency->get('ruleconfig'), $matchids);
                    $competency->set('ruleconfig', $newruleconfig);
                    $competency->set('ruletype', $class);
                    $competency->set('ruleoutcome', $node->competency->get('ruleoutcome'));
                    $competency->update();
                } catch (\Exception $e) {
                    debugging('Could not migrate competency rule from: ' . $oldcompid . ' to: ' . $competency->get('id') . '.' .
                        ' Exception: ' . $e->getMessage(), DEBUG_DEVELOPER);
                    $competency->reset_rule();
                }
            }

            if (!empty($node->children)) {
                self::migrate_competency_tree_rules($node->children, $matchids);
            }
        }
    }