<?php
    function setArrayTitles($groups, $questions) {

        $returnGroups = [];

        foreach($groups as $id => $group) {
            $returnGroups[$id] = $group;
            $returnGroups[$id]['GROUP_ID'] = $id;
            $returnGroups[$id]['QUESTIONS'] = [];

            foreach($questions as $question) {
                if($question['C_SORT'][0] - 1 == $id) {
                    $returnGroups[$id]['QUESTIONS'][] = $question;
                }
            }
        }

        return $returnGroups;
    }

    function getField($rootDir, $arQuestion, $arAnswer, $getValue = null) {
        /*
         * 0 - radio
         * 1 - checkbox
         * 2 - select
         * 3 - multiselect
         * 4 - text
         * 5 - textarea
         */

        $value = $getValue;

        switch($arAnswer["FIELD_TYPE"]) {
            case 0:
                $id = $arAnswer["QUESTION_ID"];
                $fieldName = "vote_radio_" . $id;

                $value = (isset($_REQUEST[$fieldName]) && $_REQUEST[$fieldName] == $arAnswer["ID"])
                    ? 'checked="checked"'
                    : '';

                include $rootDir . '/fields/input.radio.php';
                break;
            case 1:
                $id = $arAnswer["QUESTION_ID"];
                $fieldName = "vote_checkbox_" . $id;

                $value = (isset($_REQUEST[$fieldName]) &&
                    array_search($id, $_REQUEST[$fieldName]) !== false)
                    ? 'checked="checked"'
                    : '';

                include $rootDir . '/fields/input.checkbox.php';
                break;
            case 2:
                $id = $arAnswer["QUESTION_ID"];
                $fieldName = "vote_dropdown_" . $id;

                $value = (isset($_REQUEST[$fieldName]))
                    ? $_REQUEST[$fieldName]
                    : false;

                include $rootDir . '/fields/input.dropdown.php';
                break;
            case 3:
                $id = $arAnswer["QUESTION_ID"];
                $fieldName = "vote_multiselect_" . $id;

                $value = (isset($_REQUEST[$fieldName]))
                    ? $_REQUEST[$fieldName]
                    : array();

                include $rootDir . '/fields/input.multiselect.php';
                break;
            case 4:
                $id = $arAnswer["ID"];
                $fieldName = "vote_field_" . $id;

                $value = isset($_REQUEST[$fieldName])
                    ? htmlspecialcharsbx($_REQUEST[$fieldName])
                    : '';

                include $rootDir . '/fields/input.text.php';
                break;
            case 5:
                $id = $arAnswer["ID"];
                $fieldName = "vote_memo_" . $id;

                $value = isset($_REQUEST[$fieldName])
                    ? htmlspecialcharsbx($_REQUEST[$fieldName])
                    : '';

                include $rootDir . '/fields/input.textarea.php';
                break;
        }
    }