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