public function getCopyFields($requests)
    {
        $prefix     = 'CP';
        $token      = $this->token;
        $surveyCode = $token->getSurvey()->getCode();
        $surveyId   = $token->getSurveyId();

        $flipRequests = array_flip($requests);

        // Check from the last token back, we need to find the last answered token.
        // To be improved with a custom token loader that selects tokens answered before this one to prevent looping the complete track.
        $prev = $token->getRespondentTrack()->getLastToken();
        do {
            if ($prev->getReceptionCode()->isSuccess() && $prev->isCompleted()) {
                // Check first on survey id and when that does not work by code (if not empty).
                if ($prev->getSurveyId() === $surveyId || (!empty($surveyCode) && $prev->getSurvey()->getCode() === $surveyCode)) {
                    // @@TODO: Make case insensitive from here on, also change $check array to $requests
                    $answers   = $prev->getRawAnswers();
                    $answersUc = array_change_key_case($answers, CASE_UPPER);
                    $values    = array_intersect_key($answersUc, $flipRequests);
                    // Values now has the CP prefix requested answers that have a no-prefix match
                    // If this is not the complete set, we look for CP prefix matches to allow copy from start
                    if (count($values) !== count($requests)) {
                        $missing = array_diff_key($flipRequests, $values);
                        foreach ($missing as $key => $value) {
                            $prefixKey = $prefix . $key;
                            if (array_key_exists($prefixKey, $answersUc)) {
                                $values[$key] = $answersUc[$prefixKey];
                            }
                        }
                    }
                    $results = [];
                    foreach ($values as $key => $value) {
                        $newKey           = $flipRequests[$key];
                        $results[$newKey] = $value;
                    }
                    return $results;
                }
            }
        } while ($prev = $prev->getPreviousToken());

        return [];
    }
public function getCopyFields($requests)
    {
        $prefix     = 'CP';
        $token      = $this->token;
        $surveyCode = $token->getSurvey()->getCode();
        $surveyId   = $token->getSurveyId();

        $flipRequests = array_flip($requests);

        // Check from the last token back, we need to find the last answered token.
        // To be improved with a custom token loader that selects tokens answered before this one to prevent looping the complete track.
        $prev = $token->getRespondentTrack()->getLastToken();
        do {
            if ($prev->getReceptionCode()->isSuccess() && $prev->isCompleted()) {
                // Check first on survey id and when that does not work by code (if not empty).
                if ($prev->getSurveyId() === $surveyId || (!empty($surveyCode) && $prev->getSurvey()->getCode() === $surveyCode)) {
                    // @@TODO: Make case insensitive from here on, also change $check array to $requests
                    $answers   = $prev->getRawAnswers();
                    $answersUc = array_change_key_case($answers, CASE_UPPER);
                    $values    = array_intersect_key($answersUc, $flipRequests);
                    // Values now has the CP prefix requested answers that have a no-prefix match
                    // If this is not the complete set, we look for CP prefix matches to allow copy from start
                    if (count($values) !== count($requests)) {
                        $missing = array_diff_key($flipRequests, $values);
                        foreach ($missing as $key => $value) {
                            $prefixKey = $prefix . $key;
                            if (array_key_exists($prefixKey, $answersUc)) {
                                $values[$key] = $answersUc[$prefixKey];
                            }
                        }
                    }
                    $results = [];
                    foreach ($values as $key => $value) {
                        $newKey           = $flipRequests[$key];
                        $results[$newKey] = $value;
                    }
                    return $results;
                }
            }
        } while ($prev = $prev->getPreviousToken());

        return [];
    }
public function getCopyFields ( $ requests ) { $ prefix = 'CP' ; $ token = $ this -> token ; $ surveyCode = $ token -> getSurvey ( ) -> getCode ( ) ; $ surveyId = $ token -> getSurveyId ( ) ; $ flipRequests = array_flip ( $ requests ) ; // Check from the last token back, we need to find the last answered token. // To be improved with a custom token loader that selects tokens answered before this one to prevent looping the complete track. $ prev = $ token -> getRespondentTrack ( ) -> getLastToken ( ) ; do { if ( $ prev -> getReceptionCode ( ) -> isSuccess ( ) && $ prev -> isCompleted ( ) ) { // Check first on survey id and when that does not work by code (if not empty). if ( $ prev -> getSurveyId ( ) === $ surveyId || ( ! empty ( $ surveyCode ) && $ prev -> getSurvey ( ) -> getCode ( ) === $ surveyCode ) ) { // @@TODO: Make case insensitive from here on, also change $check array to $requests $ answers = $ prev -> getRawAnswers ( ) ; $ answersUc = array_change_key_case ( $ answers , CASE_UPPER ) ; $ values = array_intersect_key ( $ answersUc , $ flipRequests ) ; // Values now has the CP prefix requested answers that have a no-prefix match // If this is not the complete set, we look for CP prefix matches to allow copy from start if ( count ( $ values ) !== count ( $ requests ) ) { $ missing = array_diff_key ( $ flipRequests , $ values ) ; foreach ( $ missing as $ key => $ value ) { $ prefixKey = $ prefix . $ key ; if ( array_key_exists ( $ prefixKey , $ answersUc ) ) { $ values [ $ key ] = $ answersUc [ $ prefixKey ] ; } } } $ results = [ ] ; foreach ( $ values as $ key => $ value ) { $ newKey = $ flipRequests [ $ key ] ; $ results [ $ newKey ] = $ value ; } return $ results ; } } } while ( $ prev = $ prev -> getPreviousToken ( ) ) ; return [ ] ; }
Tries to fulfill request to copy fields

Copy fields will be fulfilled by searching the track in reverse
- first to see if there is a field without the CP prefix
- then if there are fields with the CP prefix
And only return the match when (in the end) ALL fields are found. If not
it will start again for the next answered token to the same survey, or
survey with the same code as the requesting token.

@param array $requests
@return array
Tries to fulfill request to copy fields