feat(quizzes): клиентское перемешивание вопросов и вариантов на публичной странице
- shuffleList()/prepareQuestions() в public/js/quiz-public.js - флаги shuffle_questions/shuffle_options применяются при загрузке и старте - отправка по question_id/option_id, порядок не хранится на сервере
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user