feat(quizzes): клиентское перемешивание вопросов и вариантов на публичной странице

- shuffleList()/prepareQuestions() в public/js/quiz-public.js
- флаги shuffle_questions/shuffle_options применяются при загрузке и старте
- отправка по question_id/option_id, порядок не хранится на сервере
This commit is contained in:
dev
2026-12-02 22:29:00 +03:00
parent f634d88cae
commit a92fa830a2
3 changed files with 24 additions and 3 deletions
+22 -2
View File
@@ -58,6 +58,25 @@
return API + '/api/public-quizzes/files/' + encodeURIComponent(name) + '?x=1' + play;
}
function shuffleList(list) {
const arr = Array.isArray(list) ? list.slice() : [];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function prepareQuestions(list) {
const quiz = state.quiz || {};
let items = Array.isArray(list) ? list.slice() : [];
if (quiz.shuffle_questions) items = shuffleList(items);
if (quiz.shuffle_options) {
items = items.map(q => ({ ...q, options: shuffleList(q.options || []) }));
}
return items;
}
function toast(message, isError) {
const el = document.getElementById('toast');
if (!el) return;
@@ -344,7 +363,8 @@
};
if (data.quiz) {
state.quiz = data.quiz.quiz || state.quiz;
state.questions = data.quiz.questions || state.questions;
const fresh = data.quiz.questions;
state.questions = Array.isArray(fresh) && fresh.length ? prepareQuestions(fresh) : state.questions;
state.quizAssets = data.quiz.quiz_assets || state.quizAssets;
}
render();
@@ -540,7 +560,7 @@
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.error || 'Тест не найден');
state.quiz = data.quiz;
state.questions = data.questions || [];
state.questions = prepareQuestions(data.questions || []);
state.quizAssets = data.quiz_assets || [];
const kindLabel = state.quiz.kind === 'survey' ? 'Опрос' : 'Тест';
document.title = (state.quiz.title || kindLabel) + ' — ' + kindLabel.toLowerCase();