aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/questionnaires/VoteAnswer.vue
blob: 57ff98a38b522fa5d1b2e1335c16621d95c1f328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<template>

    <div :class="{ mandatory: question.questiondata.mandatory == 1}">
        <div class="description_container">
            <div class="icon_container">
                <StudipIcon shape="vote"
                            role="info"/>
            </div>
            <article class="description">
                <StudipIcon v-if="question.questiondata.mandatory == 1"
                            shape="star"
                            role="attention"/>
                <span v-if="question.questiondata.mandatory == 1">{{ $gettext('Pflichtantwort')}}</span>
                <div v-html="question.questiondata.description"></div>
            </article>
        </div>
        <!-- hidden invalidation notice -->

        <ul class="clean">

            <li v-for="option in shuffledOptions" :key="option.index">
                <!-- name="answers[<?= $vote->getId() ?>][answerdata][answers][<?= $index ?>]" -->
                    <input
                        :type="question.questiondata.multiplechoice == 1 ? 'checkbox' : 'radio'"
                        :name="'answers[' + question.id + '][answerdata][answers][' + option.index + ']'"
                        :value="option.index"
                        v-model="userAnswer"
                        :id="'question-' + question.id + '-' + option.index"
                    />
                <label :for="'question-' + question.id + '-' + option.index" class="question-label">{{ option.answer }}</label>
            </li>

            <template v-if="question.questiondata.freetextfield == 1">
                <li>
                    <input
                        :type="question.questiondata.multiplechoice == 1 ? 'checkbox' : 'radio'"
                        :name="'question-' + question.id"
                        v-model="freeTextEnabled"
                        id="free_answer"
                        :aria-label="$gettext('Geben Sie eine andere Antwort an')"
                        />
                    <label id="free_answer_label" for="free_answer" class="question-label">{{ $gettext('Sonstiges') + ':' }}</label>

                    <div>
                        <textarea
                            v-model="freeTextAnswer"
                            :disabled="!freeTextEnabled"
                            aria-labelledby="free_answer_label"
                        ></textarea>
                    </div>

                </li>
            </template>

        </ul>

    </div>

    <pre>{{ userAnswer }}</pre>
    <pre>{{ freeTextAnswer }}</pre>

</template>


<script setup>
import { ref, onMounted, watch } from 'vue';
import {$gettext} from "../../../assets/javascripts/lib/gettext";
import StudipIcon from "../StudipIcon.vue";

const props = defineProps({
    question: {
        type: Object,
        required: true
    }
})

const shuffledOptions = ref([])
const userAnswer = ref([])
const freeTextAnswer = ref('')
const freeTextEnabled = ref(false)

function prepareAnswers() {
    const options = props.question.questiondata.options

    // Antworten mit Originalindex kombinieren
    const mapped = options.map((answer, index) => ({
        answer,
        index
    }))

    // Shuffle falls aktiviert
    if (props.question.questiondata.randomize == 1) {
        for (let i = mapped.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1))
            ;[mapped[i], mapped[j]] = [mapped[j], mapped[i]]
        }
    }

    shuffledOptions.value = mapped
}


function initUserAnswer() {
    const answers = props.question.responseData?.answers ?? []

    if (props.question.questiondata.multiplechoice == 1) {
        userAnswer.value = answers
    } else {
        userAnswer.value = answers[0] ?? null
    }
}

onMounted(() => {
    prepareAnswers()
    initUserAnswer()
})

watch(
    () => props.question,
    () => {
        prepareAnswers()
        initUserAnswer()
    },
    { deep: true }
)

</script>

<style scoped lang="scss">


    .question-label {
        display: inline !important;
    }


</style>