Тесты и опросы: модуль тестов, анонимные опросы, страницы и бэкап
- lib/quizzes.js: таблицы quizzes*, CRUD, привязка медиа, подсчёт баллов, попытки, статистика, purge и уборка ассетов - server.js: роуты CRUD/прохождения/статистики, отдача медиа, публичные роуты анонимного опроса, sweep и restore - db/init.sql + db/migration.sql: схема шести таблиц, индексы и идемпотентные ALTER/CHECK - backup-restore.js: нормализация и списки таблиц, покрытие в backup.selftest.js - страницы quizzes/quiz-edit/quiz/quiz-public и публичный опрос без входа, пункт сайдбара «Тесты и опросы» - quiz.selftest.js: юнит-проверки подсчёта баллов и нормализации ответов - AGENTS.md: раздел 3t и File Map
This commit is contained in:
+115
-1
@@ -24,6 +24,7 @@ const BACKUP_TABLES = [
|
||||
'notification_reads', 'banned_ips', 'chat_threads', 'chat_messages', 'chat_attachments',
|
||||
'short_messages', 'feedbacks', 'feedback_versions', 'bookmarks', 'student_face_refs', 'photo_faces',
|
||||
'student_work_marks', 'achievement_types', 'student_achievements', 'doc_sections', 'doc_articles', 'doc_images', 'doc_files', 'sidebar_items', 'digests', 'digest_images', 'farewell_messages', 'calendar_events', 'calendar_action_log',
|
||||
'quizzes', 'quiz_questions', 'quiz_options', 'quiz_assets', 'quiz_attempts', 'quiz_attempt_answers',
|
||||
];
|
||||
const BACKUP_SEQUENCE_TABLES = [
|
||||
'groups', 'students', 'entries', 'project_files', 'branches', 'users', 'user_notes', 'group_photos',
|
||||
@@ -31,6 +32,7 @@ const BACKUP_SEQUENCE_TABLES = [
|
||||
'lesson_report_versions', 'feedback_versions', 'audit_log', 'notifications', 'chat_threads', 'chat_messages',
|
||||
'chat_attachments', 'short_messages', 'feedbacks', 'bookmarks', 'student_face_refs', 'photo_faces',
|
||||
'student_work_marks', 'achievement_types', 'student_achievements', 'doc_sections', 'doc_articles', 'doc_images', 'doc_files', 'sidebar_items', 'digests', 'digest_images', 'farewell_messages', 'calendar_events', 'calendar_action_log',
|
||||
'quizzes', 'quiz_questions', 'quiz_options', 'quiz_assets', 'quiz_attempts', 'quiz_attempt_answers',
|
||||
];
|
||||
|
||||
function isSupportedBackupVersion(data) {
|
||||
@@ -848,7 +850,119 @@ function normalizeRestoreData(data) {
|
||||
created_at: optTs(x.created_at),
|
||||
updated_at: optTs(x.updated_at),
|
||||
})).filter(x => x.source_id && x.photo_path);
|
||||
return { groups, students, entries, project_files, settings, branches, users, user_branches, user_settings, user_notes, entry_photos, student_photos, group_photos, share_links, modules, photo_jobs, lesson_reports, lesson_report_versions, audit_log, notifications, notification_reads, banned_ips, chat_threads, chat_messages, chat_attachments, short_messages, feedbacks, farewell_messages, bookmarks, calendar_events: calendarEvents, calendar_action_log: calendarActionLog, achievement_types: achievementTypes, student_achievements: studentAchievements, student_face_refs: faceRefs, photo_faces: faceRows, student_work_marks, doc_sections: docSections, doc_articles: docArticles, doc_images: docImages, doc_files: docFiles, sidebar_items: sidebarItems, digests, digest_images: digestImages };
|
||||
const quizKinds = ['test', 'survey'];
|
||||
const quizQuestionKinds = ['single', 'multiple', 'select', 'line', 'text', 'number', 'date'];
|
||||
const quizWeight = v => Math.min(100, Math.max(0, Math.round((Number(v) || 0) * 100) / 100));
|
||||
const quizAssetKinds = ['image', 'audio', 'video', 'file'];
|
||||
const quizInt = (v, lo, hi, def = null) => {
|
||||
if (v === null || v === undefined || v === '') return def;
|
||||
const n = Number(v);
|
||||
if (!Number.isFinite(n)) return def;
|
||||
return Math.min(hi, Math.max(lo, Math.trunc(n)));
|
||||
};
|
||||
const quizzes = (data.quizzes || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
title: optText(x.title, 200) || '',
|
||||
description: optText(x.description, 20000) || null,
|
||||
kind: quizKinds.includes(x.kind) ? x.kind : 'test',
|
||||
is_published: x.is_published === true,
|
||||
shuffle_questions: x.shuffle_questions === true,
|
||||
shuffle_options: x.shuffle_options === true,
|
||||
show_results: x.show_results !== false,
|
||||
max_attempts: quizInt(x.max_attempts, 1, 1000),
|
||||
time_limit_min: quizInt(x.time_limit_min, 0, 1440, 0),
|
||||
pass_percent: quizInt(x.pass_percent, 0, 100, 60),
|
||||
cover_path: optUploadPath(x.cover_path, 300) || null,
|
||||
allow_anonymous: x.allow_anonymous === true && (quizKinds.includes(x.kind) ? x.kind : 'test') === 'survey',
|
||||
author_id: x.author_id == null ? null : reqInt(x.author_id),
|
||||
created_at: optTs(x.created_at),
|
||||
updated_at: optTs(x.updated_at),
|
||||
deleted_at: optTs(x.deleted_at),
|
||||
purge_at: optTs(x.purge_at),
|
||||
})).filter(x => x.id > 0);
|
||||
const quizQuestions = (data.quiz_questions || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
quiz_id: reqInt(x.quiz_id),
|
||||
sort_order: optInt(x.sort_order, -10000000, 10000000) ?? 0,
|
||||
kind: quizQuestionKinds.includes(x.kind) ? x.kind : 'single',
|
||||
text: optText(x.text, 5000) || '',
|
||||
description: optText(x.description, 5000) || null,
|
||||
weight: quizWeight(x.weight),
|
||||
partial_credit: x.partial_credit === true,
|
||||
required: x.required !== false,
|
||||
min_length: quizInt(x.min_length, 0, 2000, 0),
|
||||
options_inline: x.options_inline === true,
|
||||
text_match: x.text_match === 'contains' ? 'contains' : 'exact',
|
||||
accepted_answers: (() => {
|
||||
if (!Array.isArray(x.accepted_answers)) return [];
|
||||
const seen = new Set();
|
||||
const out = [];
|
||||
for (const raw of x.accepted_answers) {
|
||||
if (typeof raw !== 'string') continue;
|
||||
const value = raw.trim().slice(0, 200);
|
||||
const key = value.toLowerCase().replace(/ё/g, 'е').replace(/\s+/g, ' ');
|
||||
if (!key || seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
out.push(value);
|
||||
if (out.length >= 50) break;
|
||||
}
|
||||
return out;
|
||||
})(),
|
||||
created_at: optTs(x.created_at),
|
||||
updated_at: optTs(x.updated_at),
|
||||
})).filter(x => x.quiz_id);
|
||||
const quizOptions = (data.quiz_options || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
question_id: reqInt(x.question_id),
|
||||
sort_order: optInt(x.sort_order, -10000000, 10000000) ?? 0,
|
||||
text: optText(x.text, 5000) || '',
|
||||
description: optText(x.description, 5000) || null,
|
||||
is_correct: x.is_correct === true,
|
||||
weight: quizWeight(x.weight),
|
||||
created_at: optTs(x.created_at),
|
||||
updated_at: optTs(x.updated_at),
|
||||
})).filter(x => x.question_id);
|
||||
const quizAssets = (data.quiz_assets || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
quiz_id: x.quiz_id == null ? null : reqInt(x.quiz_id),
|
||||
question_id: x.question_id == null ? null : reqInt(x.question_id),
|
||||
option_id: x.option_id == null ? null : reqInt(x.option_id),
|
||||
kind: quizAssetKinds.includes(x.kind) ? x.kind : 'file',
|
||||
path: optUploadPath(x.path, 300) || '',
|
||||
original_name: optText(x.original_name, 300) || null,
|
||||
mime: optText(x.mime, 160) || null,
|
||||
size_bytes: x.size_bytes == null ? null : quizInt(x.size_bytes, 0, Number.MAX_SAFE_INTEGER),
|
||||
caption: optText(x.caption, 300) || null,
|
||||
sort_order: optInt(x.sort_order, -10000000, 10000000) ?? 0,
|
||||
created_by: x.created_by == null ? null : reqInt(x.created_by),
|
||||
created_at: optTs(x.created_at),
|
||||
})).filter(x => x.path && [x.quiz_id, x.question_id, x.option_id].filter(v => v != null).length <= 1);
|
||||
const quizAttempts = (data.quiz_attempts || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
quiz_id: reqInt(x.quiz_id),
|
||||
user_id: x.user_id == null ? null : reqInt(x.user_id),
|
||||
status: x.status === 'done' ? 'done' : 'in_progress',
|
||||
score: quizWeight(x.score),
|
||||
max_score: quizWeight(x.max_score),
|
||||
percent: Math.min(100, quizWeight(x.percent)),
|
||||
passed: x.passed == null ? null : x.passed === true,
|
||||
anon_token: null,
|
||||
started_at: optTs(x.started_at),
|
||||
finished_at: optTs(x.finished_at),
|
||||
})).filter(x => x.quiz_id);
|
||||
const quizAttemptAnswers = (data.quiz_attempt_answers || []).map(x => ({
|
||||
id: reqInt(x.id),
|
||||
attempt_id: reqInt(x.attempt_id),
|
||||
question_id: x.question_id == null ? null : reqInt(x.question_id),
|
||||
question_text: optText(x.question_text, 5000) || '',
|
||||
option_ids: Array.isArray(x.option_ids) ? x.option_ids.map(Number).filter(n => Number.isInteger(n) && n > 0).slice(0, 25) : [],
|
||||
answer_snapshot: x.answer_snapshot && typeof x.answer_snapshot === 'object' && !Array.isArray(x.answer_snapshot) ? x.answer_snapshot : {},
|
||||
text_answer: optText(x.text_answer, 2000) || null,
|
||||
is_correct: x.is_correct == null ? null : x.is_correct === true,
|
||||
score: quizWeight(x.score),
|
||||
max_score: quizWeight(x.max_score),
|
||||
})).filter(x => x.attempt_id);
|
||||
return { groups, students, entries, project_files, settings, branches, users, user_branches, user_settings, user_notes, entry_photos, student_photos, group_photos, share_links, modules, photo_jobs, lesson_reports, lesson_report_versions, audit_log, notifications, notification_reads, banned_ips, chat_threads, chat_messages, chat_attachments, short_messages, feedbacks, farewell_messages, bookmarks, calendar_events: calendarEvents, calendar_action_log: calendarActionLog, achievement_types: achievementTypes, student_achievements: studentAchievements, student_face_refs: faceRefs, photo_faces: faceRows, student_work_marks, doc_sections: docSections, doc_articles: docArticles, doc_images: docImages, doc_files: docFiles, sidebar_items: sidebarItems, digests, digest_images: digestImages, quizzes, quiz_questions: quizQuestions, quiz_options: quizOptions, quiz_assets: quizAssets, quiz_attempts: quizAttempts, quiz_attempt_answers: quizAttemptAnswers };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user