refactor [P-36 УМТ]: страница конкретного теста

This commit is contained in:
Denzel committed 2025-02-17 16:32:30 +03:00
1 parent 18bff58f66
commit 52625d4992
6 files changed
+98 -49

No files matched your search

@@ -5,16 +5,18 @@
<v-card elevation="2">
<v-card-title class="text-h6 font-weight-bold">Информация</v-card-title>
<v-card-text class="d-flex flex-lg-column ga-6 border-b-thin">
<p>Текст описания теста</p>
<p>
Текст описания технических возможностей тестирования (можно пропускать вопросы,
возвращаться, менять ответы, завершить досрочно тест...)
{{ data?.description }}
</p>
<p>
<strong>Время на прохождение — {{ data?.time }} минут</strong>
</p>
<p>
<strong>Срок окончания — {{ new Date(data?.dateend).toLocaleDateString() }}</strong>
</p>
<p><strong>Время на прохождение — 15 минут</strong></p>
<p><strong>Срок прохождения — 22.06.2024</strong></p>
</v-card-text>
<v-card-actions class="mx-2 my-4">
<p>Попытка 1 из 2</p>
<p>Попытка 1 из {{ data?.attemps_number }}</p>
<v-btn color="white" class="ml-auto bg-teal-darken-1 px-9" @click="startTest">
Начать
</v-btn>
@@ -29,6 +31,7 @@
export default {
props: {
startTest: Function,
data: Object,
},
};
</script>
@@ -6,19 +6,20 @@
:formattedTime="formattedTime"
:finishTest="openFinishDialog"
:currentQuestion="currentQuestion"
:totalQuestions="questions.length"
:totalQuestions="data?.question_count"
@change-question="changeQuestion"
:data="data"
/>
<!-- Блок информации о тесте -->
<BodyDetailTesting v-if="!testStarted && !testFinished" :startTest="startTest" />
<BodyDetailTesting v-if="!testStarted && !testFinished" :data="data" :startTest="startTest" />
<!-- Вопросы теста -->
<QuestionBlock
v-if="testStarted"
:question="questions[currentQuestion]"
:question="data.questions[currentQuestion]"
:currentQuestion="currentQuestion"
:totalQuestions="questions.length"
:totalQuestions="data.question_count"
:answers="answers"
@change-question="changeQuestion"
/>
@@ -30,7 +31,7 @@
:testPassed="testPassed"
:correctAnswers="correctAnswers"
:answeredQuestions="answeredQuestions"
:totalQuestions="questions.length"
:totalQuestions="data.question_count"
:formattedTime="formattedTime"
/>
@@ -49,21 +50,28 @@
</template>
<script>
import { computed, ref, onUnmounted } from 'vue';
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 timeLeft = ref(900);
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 questions = ref([
{
@@ -85,48 +93,36 @@ export default {
{ question: 'Сколько ног у паука?', options: ['4', '6', '8', '10'], correct: '8' },
]);
const answers = ref(new Array(questions.value.length).fill(null));
const formattedTime = computed(() => {
const minutes = Math.floor(timeLeft.value / 60);
const seconds = timeLeft.value % 60;
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 correctAnswers = computed(
() =>
answers.value.filter((answer, index) => answer === questions.value[index].correct).length,
);
const answeredQuestions = computed(
() => answers.value.filter((answer) => answer !== null).length,
);
const testPassed = computed(() => correctAnswers.value >= questions.value.length / 2);
const startTest = () => {
testStarted.value = true;
testFinished.value = false;
// Переводим минуты в секунды перед запуском таймера
data.value = { ...data.value, time: data.value.time * 60 };
timer = setInterval(() => {
if (timeLeft.value > 0) {
timeLeft.value--;
if (data.value?.time > 0) {
data.value = { ...data.value, time: data.value.time - 1 };
} else {
clearInterval(timer);
openFinishDialog();
finishTest(); // Завершаем тест, если время закончилось
}
}, 1000);
};
const changeQuestion = (newIndex) => {
if (newIndex >= 0 && newIndex < questions.value.length) {
currentQuestion.value = newIndex;
}
};
const openFinishDialog = () => {
showFinishDialog.value = true;
};
const finishTest = () => {
clearInterval(timer);
testStarted.value = false;
@@ -134,10 +130,45 @@ export default {
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,
@@ -153,6 +184,7 @@ export default {
correctAnswers,
answeredQuestions,
testPassed,
data,
};
},
};
@@ -2,7 +2,7 @@
<v-container class="pa-4">
<v-row align="center" class="border-b-thin">
<v-col class="text-h6 font-weight-regular">
Комплекс тестовых вопросов на знание в области информационно-коммуникационных технологий
{{ data?.name }}
</v-col>
<span class="text-h6 text-grey-darken-1 font-weight-bold mr-4">{{ formattedTime }}</span>
</v-row>
@@ -51,7 +51,6 @@
<v-dialog v-model="showExitDialog" width="400">
<v-card>
<v-card-title class="text-h6">Вы действительно хотите выйти?</v-card-title>
<v-card-text>Прогресс будет сброшен.</v-card-text>
<v-card-actions>
<v-btn color="red" @click="exitTest">Да</v-btn>
<v-btn color="grey" @click="showExitDialog = false">Отмена</v-btn>
@@ -71,6 +70,12 @@ export default {
finishTest: Function,
currentQuestion: Number,
totalQuestions: Number,
data: {
name: {
type: String,
default: 'Тест',
},
},
},
emits: ['change-question'],
setup(props, { emit }) {
@@ -4,10 +4,10 @@
Вопрос {{ currentQuestion + 1 }} из {{ totalQuestions }}
</v-card-title>
<v-card-text>
<p>{{ question.question }}</p>
<p>{{ question.name }}</p>
<v-radio-group v-model="answers[currentQuestion]">
<v-radio
v-for="(option, index) in question.options"
v-for="(option, index) in question.settings.options"
:key="index"
:label="option"
:value="option"
@@ -1,5 +1,5 @@
<template>
<v-card v-if="testFinished" class="mt-4 pa-4">
<v-card v-if="testFinished" class="mt-4 pa-4" flat>
<v-card-title class="text-h6 font-weight-bold">Результаты тестирования</v-card-title>
<v-card-text>
<v-alert v-if="testPassed" type="success" class="mt-4 d-flex align-center">
+11 -2
View File
@@ -31,7 +31,12 @@
<v-tabs-window-item class="w-75 mx-auto h-100">
<CatalogTest :activeTab="activeTab" :testingCard="getAllTests" title="Каталог">
<template #buttons>
<v-btn class="mr-2 text-teal-darken-1" color="#00897B1F" elevation="0">
<v-btn
class="mr-2 text-teal-darken-1"
color="#00897B1F"
elevation="0"
@click="goToTest"
>
Тренироваться
</v-btn>
</template>
@@ -46,12 +51,14 @@ import CatalogTest from '@/components/testing/CatalogTest.vue';
import { axiosApiInstance } from '@/utils/axios-api';
import { computed, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
export default {
components: {
CatalogTest,
},
setup() {
const router = useRouter();
const activeTab = ref(0);
const currentTest = ref('');
@@ -76,7 +83,6 @@ export default {
() => activeTab.value,
() => {
getTestById();
console.log(getAllTests);
},
);
@@ -86,10 +92,13 @@ export default {
{ value: 2, title: 'Каталог' },
]);
const goToTest = () => router.push('/test/testdetail/:id');
return {
activeTab,
tabs,
getAllTests,
goToTest,
};
},
};