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

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

        }
    }
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 ; } } }
Returns the first AssessmentItem that will be prompted if a branch targets the
QtiComponent set as parameter.

This method depends heavily of the QtiClass of the QtiComponent. Some require multiples loops where we need to
find the firstItem from another QtiComponent : this is then done iteratively.

@param $test \qtism\data\AssessmentTest The AssessmentTest where we are searching the item from where the branch
comes.
@param $component \qtism\data\QtiComponent The QtiComponent targeted by a branch.
@param $sections AssessmentSectionCollection The collection of all sections in this AssessmentTest.
@return \qtism\data\AssessmentItem|null The first AssessmentItem that will be prompted if a branch targets the
QtiComponent set as parameter. Returns null, if there are no more AssessmentItem because the end of the test
has been reached.
Returns the first AssessmentItem that will be prompted if a branch targets the QtiComponent set as parameter .