172 lines
5.4 KiB
Vue
172 lines
5.4 KiB
Vue
<template>
|
||
<v-container class="pa-4">
|
||
<!-- Верхняя панель -->
|
||
<HeaderDetailTesting
|
||
:testStarted="testStarted"
|
||
:formattedTime="formattedTime"
|
||
:finishTest="openFinishDialog"
|
||
:currentQuestion="currentQuestion"
|
||
:totalQuestions="data?.question_count"
|
||
@change-question="changeQuestion"
|
||
:data="data"
|
||
/>
|
||
|
||
<!-- Блок информации о тесте -->
|
||
<BodyDetailTesting v-if="!testStarted && !testFinished" :data="data" :startTest="startTest" />
|
||
|
||
<!-- Вопросы теста -->
|
||
<QuestionBlock
|
||
v-if="testStarted"
|
||
:question="data.questions[currentQuestion]"
|
||
:currentQuestion="currentQuestion"
|
||
:totalQuestions="data.question_count"
|
||
:answers="answers"
|
||
:finishTest="openFinishDialog"
|
||
@change-question="changeQuestion"
|
||
/>
|
||
|
||
<!-- Результаты теста -->
|
||
<TestResults
|
||
v-if="testFinished"
|
||
:testFinished="testFinished"
|
||
:testPassed="testPassed"
|
||
:correctAnswers="correctAnswers"
|
||
:answeredQuestions="answeredQuestions"
|
||
:totalQuestions="data.question_count"
|
||
:formattedTime="formattedTime"
|
||
/>
|
||
|
||
<!-- Диалог подтверждения завершения теста -->
|
||
<v-dialog v-model="showFinishDialog" persistent max-width="400px">
|
||
<v-card>
|
||
<v-card-title class="text-h6">Завершить тест?</v-card-title>
|
||
<v-card-text>Вы уверены, что хотите завершить тестирование?</v-card-text>
|
||
<v-card-actions class="d-flex justify-end">
|
||
<v-btn color="teal-darken-4" @click="finishTest">Да, завершить</v-btn>
|
||
<v-btn color="grey" @click="showFinishDialog = false">Отмена</v-btn>
|
||
</v-card-actions>
|
||
</v-card>
|
||
</v-dialog>
|
||
</v-container>
|
||
</template>
|
||
|
||
<script>
|
||
import { computed, ref, onUnmounted, onMounted, watch } from 'vue';
|
||
import HeaderDetailTesting from '@/components/testing/HeaderDetailTesting.vue';
|
||
import BodyDetailTesting from '@/components/testing/BodyDetailTesting.vue';
|
||
import QuestionBlock from '@/components/testing/QuestionBlock.vue';
|
||
import TestResults from '@/components/testing/TestResults.vue';
|
||
import { axiosApiInstance } from '@/utils/axios-api';
|
||
|
||
export default {
|
||
components: { BodyDetailTesting, HeaderDetailTesting, QuestionBlock, TestResults },
|
||
setup() {
|
||
const testStarted = ref(false);
|
||
const testFinished = ref(false);
|
||
const showFinishDialog = ref(false);
|
||
const currentQuestion = ref(0);
|
||
let timer = null;
|
||
const data = ref(null);
|
||
|
||
const getTestById = async (id) => {
|
||
const resp = await axiosApiInstance.get(`/api/tests/testdetail/${id}`);
|
||
data.value = resp.data;
|
||
console.log(data.value);
|
||
};
|
||
|
||
const formattedTime = computed(() => {
|
||
if (!data.value?.time) return '00:00';
|
||
|
||
// Если тест не запущен и не завершён — считаем, что время в data.value.time в минутах
|
||
if (!testStarted.value && !testFinished.value) {
|
||
return `${String(data.value.time).padStart(2, '0')}:00`;
|
||
}
|
||
|
||
// Если тест запущен или завершён — data.value.time уже в секундах
|
||
const minutes = Math.floor(data.value.time / 60);
|
||
const seconds = data.value.time % 60;
|
||
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
|
||
});
|
||
|
||
const startTest = () => {
|
||
testStarted.value = true;
|
||
testFinished.value = false;
|
||
|
||
// Переводим минуты в секунды перед запуском таймера
|
||
data.value = { ...data.value, time: data.value.time * 60 };
|
||
|
||
timer = setInterval(() => {
|
||
if (data.value?.time > 0) {
|
||
data.value = { ...data.value, time: data.value.time - 1 };
|
||
} else {
|
||
finishTest(); // Завершаем тест, если время закончилось
|
||
}
|
||
}, 1000);
|
||
};
|
||
|
||
const finishTest = () => {
|
||
clearInterval(timer);
|
||
testStarted.value = false;
|
||
testFinished.value = true;
|
||
showFinishDialog.value = false;
|
||
};
|
||
|
||
const answers = ref([]);
|
||
|
||
watch(data?.questions, (newData) => {
|
||
if (newData && Array.isArray(newData.questions)) {
|
||
answers.value = newData.questions.map(() => null);
|
||
}
|
||
});
|
||
|
||
const correctAnswers = computed(
|
||
() =>
|
||
answers.value.filter(
|
||
(answer, index) => answer === data?.value.questions[index].settings.correct,
|
||
).length,
|
||
);
|
||
|
||
const answeredQuestions = computed(
|
||
() => answers.value.filter((answer) => answer !== null).length,
|
||
);
|
||
|
||
const testPassed = computed(() => correctAnswers.value >= data?.value.questions.length / 2);
|
||
|
||
const changeQuestion = (newIndex) => {
|
||
if (newIndex >= 0 && newIndex < data?.value.questions.length) {
|
||
currentQuestion.value = newIndex;
|
||
}
|
||
};
|
||
|
||
const openFinishDialog = () => {
|
||
showFinishDialog.value = true;
|
||
};
|
||
|
||
onUnmounted(() => {
|
||
if (timer) clearInterval(timer);
|
||
});
|
||
|
||
onMounted(() => {
|
||
getTestById(1);
|
||
});
|
||
|
||
return {
|
||
testStarted,
|
||
testFinished,
|
||
formattedTime,
|
||
startTest,
|
||
finishTest,
|
||
answers,
|
||
currentQuestion,
|
||
changeQuestion,
|
||
showFinishDialog,
|
||
openFinishDialog,
|
||
correctAnswers,
|
||
answeredQuestions,
|
||
testPassed,
|
||
data,
|
||
};
|
||
},
|
||
};
|
||
</script>
|