public static function getLastItem($test, $component, $sections)
    {
        $currentCmp = $component;
        // $sections = null;

        while (true)
        {
            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) {
                            $prevSectPart = null;

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

                                $prevSectPart = $scpt;
                            }

                            if ($prevSectPart == null)
                            {
                                $currentCmp = $sectparent;
                            } else {
                                $currentCmp = $prevSectPart;
                            }
                        }
                        else {

                            // No recursion
                            $prevSect = null;
                            $keyFound = null;

                            foreach ($sections as $sect) {

                                if ($sect->getIdentifier() == $currentCmp->getIdentifier()) {
                                    break;
                                } else {
                                    $prevSect = $sect;
                                }
                            }

                            if ($prevSect == null) {
                                return null;
                            } else {
                                $currentCmp = $prevSect;
                            }
                        }
                    } else { // Case with sub items
                        return $items[count($items) - 1];
                    }
                    break;

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

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

                        // First item of the next testpart

                        $prevTest = null;
                        $keyFound = null;

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

                            if ($test->getIdentifier() == $currentCmp->getIdentifier()) {
                                break;
                            } else {
                                $prevTest = $test;
                            }
                        }

                        if ($prevTest != null) {
                            $currentCmp = $prevTest;
                        } else {
                            return null;
                        }
                    } else {
                        return $items[count($items) - 1];
                    }
                    break;

                default:
                    return null;
            }
        }
    }