public static function getFirstItem($test, $component, $sections)
    {
        $currentCmp = $component;
        $visitedNodes = [];

        while (true) {

            $visitedNodes[] = $currentCmp->getIdentifier();

            switch ($currentCmp->getQtiClassName()) {
                case "assessmentItemRef":
                    return $currentCmp;
                    break;

                case "assessmentSection":
                    $items = $currentCmp->getComponentsByClassName("assessmentItemRef")->getArrayCopy();

                    if (count($items) == 0) {

                        // Check for recursion

                        $sectparent = Utils::checkRecursion($currentCmp, $sections);

                        if ($sectparent != null)
                        {
                            $nextSectpart = null;
                            $currentFound = false;

                            foreach ($sectparent->getSectionParts() as $key => $scpt)
                            {
                                if ($currentFound) {
                                    $nextSectpart = $scpt;
                                    break;
                                }

                                if ($scpt == $currentCmp) {
                                    $currentFound = true;
                                }
                            }

                            if ($nextSectpart == null) {  // Check end of file or at a higher level
                                $currentCmp = $sectparent;
                            } else { // Recursive part
                                $currentCmp = $nextSectpart;
                            }
                        }
                        else { // No recursion

                            $nextSect = null;
                            $keyFound = null;

                            foreach ($sections as $sect) {

                                if (($keyFound) and (!in_array($sect->getIdentifier(), $visitedNodes))) {
                                    $nextSect = $sect;
                                    break;
                                }

                                if ($sect->getIdentifier() == $currentCmp->getIdentifier()) {
                                    $keyFound = true;
                                }
                            }

                            if ($nextSect == null) {
                                return null;
                            } else {
                                $currentCmp = $nextSect;
                            }
                        }
                    } else {
                        return $items[0];
                    }
                    break;

                case "testPart":
                    $items = $currentCmp->getComponentsByClassName("assessmentItemRef")->getArrayCopy();

                    if (count($items) == 0) {

                        // First item of the next testpart

                        $nextTest = null;
                        $keyFound = null;

                        foreach ($test->getComponentsByClassName($currentCmp->getQtiClassName()) as $test) {

                            if ($keyFound) {
                                $nextTest = $test;
                                break;
                            }

                            if ($test->getIdentifier() == $currentCmp->getIdentifier()) {
                                $keyFound = true;
                            }
                        }

                        if ($nextTest != null) {
                            $currentCmp = $nextTest;
                        } else {
                            return null;
                        }
                    } else {
                        return $items[0];
                    }
                    break;

                default:
                    return null;
            }

        }
    }