856 lines
34 KiB
JavaScript
856 lines
34 KiB
JavaScript
/* Редактор теста: рабочий минимум.
|
|
* Работает с эндпоинтами /api/tests/<id>/{draft, ai/generate-test, ai/generate-question}
|
|
* и /api/tests/<id> (PATCH chainActive).
|
|
*
|
|
* Полная мобильная отполировка UX (4 аккордеона, fixed footer, drag-n-drop)
|
|
* запланирована отдельным спринтом E1.7.
|
|
*/
|
|
(() => {
|
|
'use strict';
|
|
|
|
const root = document.getElementById('editor-root');
|
|
if (!root) return;
|
|
|
|
const TEST_ID = root.dataset.testId;
|
|
const initial = JSON.parse(root.dataset.initial);
|
|
|
|
const $ = (sel, parent = document) => parent.querySelector(sel);
|
|
const $$ = (sel, parent = document) => Array.from(parent.querySelectorAll(sel));
|
|
|
|
const titleEl = $('#test-title');
|
|
const descEl = $('#test-description');
|
|
const thresholdEl = $('#test-threshold');
|
|
const questionsEl = $('#questions');
|
|
const qCountEl = $('#q-count');
|
|
const saveStatusEl = $('#save-status');
|
|
const aiStatusEl = $('#ai-status');
|
|
const chainActiveEl = $('#chain-active');
|
|
const aiTopicEl = $('#ai-topic');
|
|
const aiQCountEl = $('#ai-q-count');
|
|
const aiOCountEl = $('#ai-o-count');
|
|
const introUpdatedEl = $('#intro-updated');
|
|
const introForkBannerEl = $('#intro-fork-banner');
|
|
const versionsListEl = $('#versions-list');
|
|
const attemptsListEl = $('#attempts-list');
|
|
const visibilityBtn = $('#btn-toggle-visibility');
|
|
const assignSearchEl = $('#assign-search');
|
|
const assignDeptEl = $('#assign-dept');
|
|
const assignClinicEl = $('#assign-clinic');
|
|
const assignListEl = $('#assign-list');
|
|
const assignSelectAllBtn = $('#assign-select-all');
|
|
const assignSubmitBtn = $('#assign-submit');
|
|
const assignStatusEl = $('#assign-status');
|
|
|
|
const tplQ = $('#tpl-question');
|
|
const tplO = $('#tpl-option');
|
|
|
|
let chainActive = true;
|
|
let assignPeople = [];
|
|
let assignSelected = new Set();
|
|
let hasAnyAttempts = false;
|
|
let hasForkRisk = Boolean(initial?.test?.hasForkRisk)
|
|
|| (introForkBannerEl && introForkBannerEl.dataset.forkRisk === '1');
|
|
let baselineDraftKey = '';
|
|
let dirtyCheckQueued = false;
|
|
|
|
function currentDraftKey() {
|
|
return JSON.stringify(collectPayload());
|
|
}
|
|
|
|
function isDirty() {
|
|
return baselineDraftKey !== '' && baselineDraftKey !== currentDraftKey();
|
|
}
|
|
|
|
function updateForkBanner() {
|
|
if (!introForkBannerEl) return;
|
|
introForkBannerEl.style.display = (hasForkRisk && isDirty()) ? '' : 'none';
|
|
}
|
|
|
|
function scheduleDirtyCheck() {
|
|
if (dirtyCheckQueued) return;
|
|
dirtyCheckQueued = true;
|
|
requestAnimationFrame(() => {
|
|
dirtyCheckQueued = false;
|
|
updateForkBanner();
|
|
});
|
|
}
|
|
|
|
function resetBaselineDraft() {
|
|
baselineDraftKey = currentDraftKey();
|
|
updateForkBanner();
|
|
}
|
|
|
|
// ─── render ─────────────────────────────────────────────────────────
|
|
|
|
function syncOptionInputTypes(qNode) {
|
|
const isMulti = $('.q-multi', qNode).checked;
|
|
const qName = `q-correct-${Math.random().toString(36).slice(2)}`;
|
|
$$('.opt-correct', qNode).forEach((input) => {
|
|
input.type = isMulti ? 'checkbox' : 'radio';
|
|
if (isMulti) input.removeAttribute('name');
|
|
else input.setAttribute('name', qName);
|
|
input.classList.add('question-option-row__mark');
|
|
});
|
|
}
|
|
|
|
function renderQuestion(q) {
|
|
const node = tplQ.content.firstElementChild.cloneNode(true);
|
|
node._q = { id: q.id || null };
|
|
$('.q-text', node).value = q.text || '';
|
|
$('.q-multi', node).checked = !!q.hasMultipleAnswers;
|
|
|
|
const optsEl = $('.q-options', node);
|
|
(q.options || []).forEach((o) => optsEl.appendChild(renderOption(o)));
|
|
|
|
bindQuestionEvents(node);
|
|
syncOptionInputTypes(node);
|
|
return node;
|
|
}
|
|
|
|
function renderOption(o) {
|
|
const node = tplO.content.firstElementChild.cloneNode(true);
|
|
$('.opt-text', node).value = o.text || '';
|
|
$('.opt-correct', node).checked = !!o.isCorrect;
|
|
$('.opt-delete', node).addEventListener('click', () => {
|
|
node.remove();
|
|
});
|
|
return node;
|
|
}
|
|
|
|
function bindQuestionEvents(node) {
|
|
$('.q-delete', node).addEventListener('click', () => {
|
|
if (!confirm('Удалить вопрос?')) return;
|
|
node.remove();
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
});
|
|
$('.q-up', node).addEventListener('click', () => {
|
|
if (node.previousElementSibling) {
|
|
node.parentNode.insertBefore(node, node.previousElementSibling);
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
}
|
|
});
|
|
$('.q-down', node).addEventListener('click', () => {
|
|
if (node.nextElementSibling) {
|
|
node.parentNode.insertBefore(node.nextElementSibling, node);
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
}
|
|
});
|
|
$('.q-add-option', node).addEventListener('click', () => {
|
|
$('.q-options', node).appendChild(renderOption({ text: '', isCorrect: false }));
|
|
syncOptionInputTypes(node);
|
|
scheduleDirtyCheck();
|
|
});
|
|
$('.q-ai', node).addEventListener('click', () => aiGenerateQuestion(node));
|
|
$('.q-multi', node).addEventListener('change', () => {
|
|
syncOptionInputTypes(node);
|
|
scheduleDirtyCheck();
|
|
});
|
|
}
|
|
|
|
function renumber() {
|
|
$$('#questions .q-item').forEach((li, i) => {
|
|
$('.q-num', li).textContent = `Вопрос #${i + 1}`;
|
|
});
|
|
const n = $$('#questions .q-item').length;
|
|
if (qCountEl) qCountEl.textContent = n;
|
|
const mirror = document.getElementById('q-count-mirror');
|
|
if (mirror) mirror.textContent = n;
|
|
}
|
|
|
|
function autoResize(el) {
|
|
if (!el) return;
|
|
el.style.height = 'auto';
|
|
el.style.height = el.scrollHeight + 'px';
|
|
}
|
|
|
|
function loadInitial() {
|
|
titleEl.value = initial.test.title || '';
|
|
descEl.value = initial.test.description || '';
|
|
autoResize(titleEl);
|
|
autoResize(descEl);
|
|
if (titleEl && titleEl.tagName === 'TEXTAREA') {
|
|
titleEl.addEventListener('input', () => autoResize(titleEl));
|
|
titleEl.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') e.preventDefault();
|
|
});
|
|
}
|
|
if (descEl) descEl.addEventListener('input', () => autoResize(descEl));
|
|
thresholdEl.value =
|
|
initial.test.passingThreshold == null ? '' : Number(initial.test.passingThreshold);
|
|
|
|
questionsEl.innerHTML = '';
|
|
(initial.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q)));
|
|
renumber();
|
|
if (aiTopicEl && !aiTopicEl.value.trim()) {
|
|
aiTopicEl.value = initial.test.title || '';
|
|
}
|
|
}
|
|
|
|
function fmtDt(iso) {
|
|
if (!iso) return '—';
|
|
try {
|
|
return new Date(iso).toLocaleString('ru-RU', { dateStyle: 'short', timeStyle: 'short' });
|
|
} catch {
|
|
return '—';
|
|
}
|
|
}
|
|
|
|
function escHtml(s) {
|
|
return String(s == null ? '' : s)
|
|
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
.replace(/"/g, '"').replace(/'/g, ''');
|
|
}
|
|
|
|
// ─── collect ───────────────────────────────────────────────────────
|
|
|
|
function collectPayload() {
|
|
const questions = $$('#questions .q-item').map((li, i) => ({
|
|
text: $('.q-text', li).value.trim(),
|
|
question_order: i + 1,
|
|
hasMultipleAnswers: $('.q-multi', li).checked,
|
|
options: $$('.opt-item', li).map((op, j) => ({
|
|
text: $('.opt-text', op).value.trim(),
|
|
isCorrect: $('.opt-correct', op).checked,
|
|
option_order: j + 1,
|
|
})),
|
|
}));
|
|
const payload = {
|
|
title: titleEl.value.trim() || null,
|
|
description: descEl.value.trim() || null,
|
|
questions,
|
|
};
|
|
const t = thresholdEl.value;
|
|
if (t !== '' && Number.isFinite(Number(t))) payload.passingThreshold = Number(t);
|
|
return payload;
|
|
}
|
|
|
|
function collectShape() {
|
|
return $$('#questions .q-item').map((li) => ({
|
|
optionsCount: Math.max(2, $$('.opt-item', li).length || 4),
|
|
hasMultipleAnswers: $('.q-multi', li).checked,
|
|
}));
|
|
}
|
|
|
|
// ─── actions ───────────────────────────────────────────────────────
|
|
|
|
$('#add-question').addEventListener('click', () => {
|
|
questionsEl.appendChild(
|
|
renderQuestion({
|
|
text: '',
|
|
hasMultipleAnswers: false,
|
|
options: [
|
|
{ text: '', isCorrect: true },
|
|
{ text: '', isCorrect: false },
|
|
{ text: '', isCorrect: false },
|
|
{ text: '', isCorrect: false },
|
|
],
|
|
}),
|
|
);
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
});
|
|
|
|
$('#save-draft').addEventListener('click', async () => {
|
|
saveStatusEl.textContent = 'Сохраняем…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/draft`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(collectPayload()),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'Не удалось сохранить.');
|
|
if (chainActiveEl.checked !== chainActive) {
|
|
const r2 = await fetch(`/api/tests/${TEST_ID}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ chainActive: chainActiveEl.checked }),
|
|
});
|
|
if (r2.ok) chainActive = chainActiveEl.checked;
|
|
}
|
|
saveStatusEl.textContent = data.forked
|
|
? 'Сохранено (создана новая версия — есть попытки прохождения).'
|
|
: 'Сохранено.';
|
|
resetBaselineDraft();
|
|
setTimeout(() => (saveStatusEl.textContent = ''), 4000);
|
|
} catch (e) {
|
|
saveStatusEl.textContent = '';
|
|
alert(e.message || 'Не удалось сохранить.');
|
|
}
|
|
});
|
|
|
|
$('#ai-generate-test').addEventListener('click', async () => {
|
|
const topic = (aiTopicEl?.value || titleEl.value || '').trim();
|
|
if (!topic) {
|
|
alert('Укажите тему.');
|
|
return;
|
|
}
|
|
const nQ = Math.min(30, Math.max(1, Number(aiQCountEl?.value || 7) || 7));
|
|
const nO = Math.min(8, Math.max(2, Number(aiOCountEl?.value || 3) || 3));
|
|
const shape = Array.from({ length: nQ }, () => ({
|
|
optionsCount: nO,
|
|
hasMultipleAnswers: false,
|
|
}));
|
|
aiStatusEl.textContent = 'Генерируем…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-test`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
testTitle: topic,
|
|
testDescription: descEl.value,
|
|
shape,
|
|
}),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'AI: ошибка.');
|
|
const draft = data.draft;
|
|
if (draft.title) {
|
|
titleEl.value = draft.title;
|
|
if (aiTopicEl) aiTopicEl.value = draft.title;
|
|
}
|
|
if (draft.description) descEl.value = draft.description;
|
|
questionsEl.innerHTML = '';
|
|
(draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q)));
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
aiStatusEl.textContent = `Готово: ${draft.questions?.length || 0} вопросов.`;
|
|
setTimeout(() => (aiStatusEl.textContent = ''), 4000);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
alert(e.message || 'AI: ошибка.');
|
|
}
|
|
});
|
|
|
|
// ─── импорт документа (E1.3) ───────────────────────────────────
|
|
|
|
$('#ai-import-file').addEventListener('change', async (ev) => {
|
|
const file = ev.target.files && ev.target.files[0];
|
|
ev.target.value = '';
|
|
if (!file) return;
|
|
aiStatusEl.textContent = `Загружаем «${file.name}»…`;
|
|
try {
|
|
const fd = new FormData();
|
|
fd.append('file', file);
|
|
const r = await fetch('/api/tests/import/document', { method: 'POST', body: fd });
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'Не удалось импортировать.');
|
|
const g = data.generation || {};
|
|
if (!g.available) {
|
|
aiStatusEl.textContent = '';
|
|
const msg = g.message || 'AI недоступен.';
|
|
const preview = (g.textPreview || data.extractedText || '').slice(0, 600);
|
|
alert(msg + (preview ? '\n\nПервые 600 символов:\n' + preview : ''));
|
|
return;
|
|
}
|
|
const ok = confirm(
|
|
`${g.message}\n\nПрименить как новый черновик?\n` +
|
|
`Текущие вопросы будут заменены.`,
|
|
);
|
|
if (!ok) {
|
|
aiStatusEl.textContent = '';
|
|
return;
|
|
}
|
|
const draft = g.draft;
|
|
if (draft.title) titleEl.value = draft.title;
|
|
if (draft.description) descEl.value = draft.description;
|
|
questionsEl.innerHTML = '';
|
|
(draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q)));
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
aiStatusEl.textContent = `Применено: ${draft.questions?.length || 0} вопросов.`;
|
|
setTimeout(() => (aiStatusEl.textContent = ''), 4000);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
alert(e.message || 'Не удалось импортировать.');
|
|
}
|
|
});
|
|
|
|
// ─── AI v2 (E1.8): generate-by-title / check / improve ─────────
|
|
|
|
function aiAlert(data, fallback) {
|
|
const msg = (data && data.error) || fallback || 'AI: ошибка.';
|
|
if (data && data.settingsUrl) {
|
|
if (confirm(msg + '\n\nОткрыть Настройки?')) {
|
|
window.location.href = data.settingsUrl;
|
|
}
|
|
return;
|
|
}
|
|
alert(msg);
|
|
}
|
|
|
|
const modal = $('#ai-modal');
|
|
const modalTitle = $('#ai-modal-title');
|
|
const modalBody = $('#ai-modal-body');
|
|
const modalActions = $('#ai-modal-actions');
|
|
$('#ai-modal-close').addEventListener('click', () => modal.close());
|
|
|
|
function openModal(title, bodyHtml, actions) {
|
|
modalTitle.textContent = title;
|
|
modalBody.innerHTML = bodyHtml;
|
|
modalActions.innerHTML = '';
|
|
(actions || []).forEach((a) => {
|
|
const b = document.createElement('button');
|
|
b.textContent = a.label;
|
|
b.className = a.className
|
|
|| 'px-3 py-2 rounded-lg bg-ink-100 hover:bg-ink-200 text-sm';
|
|
b.addEventListener('click', () => a.onClick(b));
|
|
modalActions.appendChild(b);
|
|
});
|
|
modal.showModal();
|
|
}
|
|
|
|
const aiGenerateByTitleBtn = $('#ai-generate-by-title');
|
|
if (aiGenerateByTitleBtn) aiGenerateByTitleBtn.addEventListener('click', async () => {
|
|
const title = titleEl.value.trim();
|
|
if (!title) {
|
|
alert('Сначала заполните название теста.');
|
|
titleEl.focus();
|
|
return;
|
|
}
|
|
const nQRaw = prompt('Сколько вопросов сгенерировать?', '10');
|
|
if (nQRaw == null) return;
|
|
const nQ = Math.max(3, Math.min(40, parseInt(nQRaw, 10) || 10));
|
|
const nORaw = prompt('Сколько вариантов в каждом вопросе?', '4');
|
|
if (nORaw == null) return;
|
|
const nO = Math.max(2, Math.min(12, parseInt(nORaw, 10) || 4));
|
|
aiStatusEl.textContent = 'Генерируем по названию…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-by-title`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
testTitle: title,
|
|
testDescription: descEl.value,
|
|
questionsCount: nQ,
|
|
optionsCount: nO,
|
|
}),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) {
|
|
aiStatusEl.textContent = '';
|
|
return aiAlert(data);
|
|
}
|
|
const draft = data.draft;
|
|
const ok = confirm(
|
|
`Готово: «${draft.title}», вопросов — ${draft.questions.length}.\n` +
|
|
'Применить как черновик? Текущие вопросы будут заменены.',
|
|
);
|
|
if (!ok) {
|
|
aiStatusEl.textContent = '';
|
|
return;
|
|
}
|
|
if (draft.title) titleEl.value = draft.title;
|
|
if (draft.description) descEl.value = draft.description;
|
|
questionsEl.innerHTML = '';
|
|
(draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q)));
|
|
renumber();
|
|
scheduleDirtyCheck();
|
|
aiStatusEl.textContent = `Применено: ${draft.questions.length} вопросов.`;
|
|
setTimeout(() => (aiStatusEl.textContent = ''), 4000);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
aiAlert(null, e.message);
|
|
}
|
|
});
|
|
|
|
const aiCheckBtn = $('#ai-check');
|
|
if (aiCheckBtn) aiCheckBtn.addEventListener('click', async () => {
|
|
const payload = collectPayload();
|
|
if (!payload.questions.length) {
|
|
alert('В тесте нет вопросов — нечего проверять.');
|
|
return;
|
|
}
|
|
aiStatusEl.textContent = 'Анализируем…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/ai/check`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
testTitle: titleEl.value,
|
|
testDescription: descEl.value,
|
|
questions: payload.questions,
|
|
}),
|
|
});
|
|
const data = await r.json();
|
|
aiStatusEl.textContent = '';
|
|
if (!r.ok) return aiAlert(data);
|
|
const rev = data.review || {};
|
|
const verdict = rev.verdict || 'warn';
|
|
const verdictMap = {
|
|
ok: ['Годен', 'bg-green-50 text-green-800 border-green-200'],
|
|
warn: ['Есть замечания', 'bg-yellow-50 text-yellow-800 border-yellow-200'],
|
|
bad: ['Серьёзные проблемы', 'bg-red-50 text-red-800 border-red-200'],
|
|
};
|
|
const [verdictText, verdictCls] = verdictMap[verdict] || verdictMap.warn;
|
|
let html = `<div class="rounded-lg border ${verdictCls} p-3 text-sm">
|
|
<div class="font-semibold">${verdictText}</div>
|
|
<div class="mt-1">${escHtml(rev.summary || '')}</div></div>`;
|
|
if (Array.isArray(rev.sections) && rev.sections.length) {
|
|
html += rev.sections.map((s) => `
|
|
<div class="mt-4">
|
|
<div class="font-semibold">${escHtml(s.title)}</div>
|
|
<ul class="mt-1 list-disc pl-5 text-sm space-y-1">
|
|
${s.items.map((it) => `<li>${escHtml(it)}</li>`).join('')}
|
|
</ul>
|
|
</div>`).join('');
|
|
} else {
|
|
html += '<p class="mt-4 text-sm text-ink-500">Замечаний нет.</p>';
|
|
}
|
|
openModal('Проверка теста', html, [
|
|
{ label: 'Закрыть', onClick: () => modal.close(),
|
|
className: 'px-3 py-2 rounded-lg bg-brand-600 hover:bg-brand-700 text-white text-sm' },
|
|
]);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
aiAlert(null, e.message);
|
|
}
|
|
});
|
|
|
|
const aiImproveBtn = $('#ai-improve');
|
|
if (aiImproveBtn) aiImproveBtn.addEventListener('click', async () => {
|
|
const payload = collectPayload();
|
|
if (!payload.questions.length) {
|
|
alert('В тесте нет вопросов — нечего улучшать.');
|
|
return;
|
|
}
|
|
aiStatusEl.textContent = 'Улучшаем…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/ai/improve`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
testTitle: titleEl.value,
|
|
testDescription: descEl.value,
|
|
questions: payload.questions,
|
|
}),
|
|
});
|
|
const data = await r.json();
|
|
aiStatusEl.textContent = '';
|
|
if (!r.ok) return aiAlert(data);
|
|
const items = data.items || [];
|
|
if (!items.length) {
|
|
openModal('Улучшение теста', '<p>Нечего улучшать.</p>', [
|
|
{ label: 'Закрыть', onClick: () => modal.close() },
|
|
]);
|
|
return;
|
|
}
|
|
const changed = items.filter((i) => i.changed);
|
|
if (!changed.length) {
|
|
openModal('Улучшение теста', '<p>AI не предложил изменений.</p>', [
|
|
{ label: 'Закрыть', onClick: () => modal.close() },
|
|
]);
|
|
return;
|
|
}
|
|
let html = `<p class="text-sm text-ink-500 mb-3">
|
|
Отметьте вопросы, к которым нужно применить улучшения. ${changed.length} из ${items.length}.</p>`;
|
|
html += changed.map((it) => `
|
|
<div class="rounded-xl border border-ink-300/60 p-3 mb-3" data-idx="${it.index}">
|
|
<label class="inline-flex items-center gap-2 text-sm font-medium">
|
|
<input type="checkbox" class="apply-q rounded border-ink-300 text-brand-600 focus:ring-brand-500" checked />
|
|
<span>Вопрос #${it.index + 1}</span>
|
|
</label>
|
|
<div class="mt-2 grid grid-cols-1 md:grid-cols-2 gap-3 text-sm">
|
|
<div>
|
|
<div class="text-xs uppercase text-ink-500">Было</div>
|
|
<div class="mt-1 ${it.textChanged ? 'line-through text-ink-500' : ''}">
|
|
${escHtml(it.original.text)}
|
|
</div>
|
|
<ul class="mt-1 list-disc pl-5">
|
|
${it.original.options.map((o) =>
|
|
`<li class="${it.optionsChanged ? 'text-ink-500' : ''}">
|
|
${o.isCorrect ? '✓ ' : ''}${escHtml(o.text)}</li>`).join('')}
|
|
</ul>
|
|
</div>
|
|
<div>
|
|
<div class="text-xs uppercase text-brand-700">Стало</div>
|
|
<div class="mt-1 ${it.textChanged ? 'font-medium' : ''}">
|
|
${escHtml(it.suggested.text)}
|
|
</div>
|
|
<ul class="mt-1 list-disc pl-5">
|
|
${it.suggested.options.map((o) =>
|
|
`<li>${o.isCorrect ? '✓ ' : ''}${escHtml(o.text)}</li>`).join('')}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>`).join('');
|
|
openModal('Улучшение теста', html, [
|
|
{ label: 'Отмена', onClick: () => modal.close() },
|
|
{
|
|
label: 'Применить выбранное',
|
|
className: 'px-3 py-2 rounded-lg bg-brand-600 hover:bg-brand-700 text-white text-sm',
|
|
onClick: () => {
|
|
const qs = $$('#questions .q-item');
|
|
modalBody.querySelectorAll('[data-idx]').forEach((row) => {
|
|
if (!$('.apply-q', row).checked) return;
|
|
const idx = parseInt(row.dataset.idx, 10);
|
|
const it = items.find((x) => x.index === idx);
|
|
if (!it || !qs[idx]) return;
|
|
const node = qs[idx];
|
|
$('.q-text', node).value = it.suggested.text;
|
|
$('.q-multi', node).checked = !!it.suggested.hasMultipleAnswers;
|
|
const optsEl = $('.q-options', node);
|
|
optsEl.innerHTML = '';
|
|
it.suggested.options.forEach((o) => optsEl.appendChild(renderOption(o)));
|
|
});
|
|
modal.close();
|
|
scheduleDirtyCheck();
|
|
aiStatusEl.textContent = 'Изменения применены. Не забудьте сохранить.';
|
|
setTimeout(() => (aiStatusEl.textContent = ''), 5000);
|
|
},
|
|
},
|
|
]);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
aiAlert(null, e.message);
|
|
}
|
|
});
|
|
|
|
async function aiGenerateQuestion(node) {
|
|
const qText = $('.q-text', node).value.trim();
|
|
const optsCount = Math.max(2, $$('.opt-item', node).length || 4);
|
|
const multi = $('.q-multi', node).checked;
|
|
aiStatusEl.textContent = 'AI: один вопрос…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-question`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
testTitle: titleEl.value,
|
|
testDescription: descEl.value,
|
|
questionText: qText,
|
|
optionsCount: optsCount,
|
|
hasMultipleAnswers: multi,
|
|
}),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'AI: ошибка.');
|
|
$('.q-text', node).value = data.text || '';
|
|
if (data.mode === 'full' && Array.isArray(data.options)) {
|
|
const optsEl = $('.q-options', node);
|
|
optsEl.innerHTML = '';
|
|
data.options.forEach((o) => optsEl.appendChild(renderOption(o)));
|
|
$('.q-multi', node).checked = !!data.hasMultipleAnswers;
|
|
}
|
|
scheduleDirtyCheck();
|
|
aiStatusEl.textContent = data.mode === 'full' ? 'AI: вопрос сгенерирован.' : 'AI: формулировка обновлена.';
|
|
setTimeout(() => (aiStatusEl.textContent = ''), 4000);
|
|
} catch (e) {
|
|
aiStatusEl.textContent = '';
|
|
alert(e.message || 'AI: ошибка.');
|
|
}
|
|
}
|
|
|
|
// ─── chain active state (грузим summary, чтобы знать стартовое значение) ───
|
|
|
|
fetch(`/api/tests/${TEST_ID}/summary`)
|
|
.then((r) => r.json())
|
|
.then((data) => {
|
|
if (data && data.test && typeof data.test.chainActive === 'boolean') {
|
|
chainActive = data.test.chainActive;
|
|
chainActiveEl.checked = chainActive;
|
|
} else {
|
|
chainActiveEl.checked = true;
|
|
chainActive = true;
|
|
}
|
|
})
|
|
.catch(() => {
|
|
chainActiveEl.checked = true;
|
|
});
|
|
|
|
function renderVersions(rows) {
|
|
if (!versionsListEl) return;
|
|
versionsListEl.innerHTML = '';
|
|
(rows || []).forEach((r) => {
|
|
const li = document.createElement('li');
|
|
li.className = 'surface-card version-card-list__item';
|
|
li.innerHTML = `
|
|
<div class="version-card-list__row">
|
|
<div class="version-card-list__main">
|
|
<div class="version-card-list__title-line">
|
|
<span class="font-headline" style="font-size:1rem;">v${r.version}</span>
|
|
${r.is_active ? '<span class="code-inline" style="font-size:0.7rem;">текущая</span>' : ''}
|
|
</div>
|
|
<p class="muted mono" style="margin:.4rem 0 0; font-size:.8rem;">${fmtDt(r.created_at)}</p>
|
|
<p class="muted" style="margin:.2rem 0 0; font-size:.8rem;">Активна: ${r.is_active ? 'да' : 'нет'}</p>
|
|
</div>
|
|
</div>`;
|
|
versionsListEl.appendChild(li);
|
|
});
|
|
}
|
|
|
|
function renderAttempts(rows) {
|
|
if (!attemptsListEl) return;
|
|
attemptsListEl.innerHTML = '';
|
|
(rows || []).forEach((a) => {
|
|
const when = a.completedAt ? fmtDt(a.completedAt) : (a.startedAt ? fmtDt(a.startedAt) : '—');
|
|
const result = a.status === 'completed' && a.totalQuestions != null
|
|
? `${a.correctCount}/${a.totalQuestions}${a.passed ? ' · зачёт' : ' · незачёт'}`
|
|
: a.status;
|
|
const li = document.createElement('li');
|
|
li.className = 'surface-card attempts-card-list__item';
|
|
li.innerHTML = `
|
|
<div class="attempts-card-list__row">
|
|
<div class="attempts-card-list__main">
|
|
<p class="muted mono" style="margin:0; font-size:.8rem;">${when}</p>
|
|
<p style="margin:.35rem 0 0; font-weight:600;">${escHtml(a.attempterName || '—')}
|
|
${a.attempterLogin ? `<span class="code-inline" style="font-size:11px; margin-left:6px;">${escHtml(a.attempterLogin)}</span>` : ''}
|
|
</p>
|
|
<p class="muted" style="margin:.25rem 0 0; font-size:.85rem;">v${a.testVersion} · ${escHtml(result)}</p>
|
|
</div>
|
|
${a.status === 'completed'
|
|
? `<a class="btn btn-ghost btn--sm attempts-card-list__action" href="/tests/${TEST_ID}/attempts/${a.id}/review">Разбор</a>`
|
|
: ''}
|
|
</div>`;
|
|
attemptsListEl.appendChild(li);
|
|
});
|
|
}
|
|
|
|
function renderAssignList() {
|
|
if (!assignListEl) return;
|
|
assignListEl.innerHTML = '';
|
|
assignPeople.forEach((p) => {
|
|
const row = document.createElement('label');
|
|
row.className = `assign-row${assignSelected.has(String(p.staffId)) ? ' assign-row--selected' : ''}`;
|
|
row.innerHTML = `
|
|
<input type="checkbox" ${assignSelected.has(String(p.staffId)) ? 'checked' : ''} />
|
|
<span class="assign-row__text">
|
|
<span class="assign-row__fio">${escHtml(p.fio || '—')}</span>
|
|
${p.webLogin ? `<span class="assign-row__login">${escHtml(p.webLogin)}</span>` : ''}
|
|
<span class="assign-row__meta">${escHtml(p.department || '—')}${p.clinicUserId ? ' · есть учётка' : ' · нет учётки (создадим при назначении)'}</span>
|
|
</span>`;
|
|
const cb = row.querySelector('input');
|
|
cb.addEventListener('change', () => {
|
|
const k = String(p.staffId);
|
|
if (cb.checked) assignSelected.add(k); else assignSelected.delete(k);
|
|
row.classList.toggle('assign-row--selected', cb.checked);
|
|
});
|
|
assignListEl.appendChild(row);
|
|
});
|
|
if (!assignPeople.length) assignListEl.innerHTML = '<p class="muted" style="padding:.75rem;">Никого не найдено.</p>';
|
|
}
|
|
|
|
async function loadDirectory() {
|
|
if (!assignListEl) return;
|
|
assignStatusEl.textContent = 'Загружаем…';
|
|
try {
|
|
const params = new URLSearchParams();
|
|
if (assignSearchEl.value.trim()) params.set('q', assignSearchEl.value.trim());
|
|
if (assignDeptEl.value && assignDeptEl.value !== '__all__') params.set('department', assignDeptEl.value);
|
|
params.set('clinic', assignClinicEl.value || 'all');
|
|
const r = await fetch(`/api/auth/dev/assignment-directory?${params.toString()}`);
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'Не удалось загрузить сотрудников');
|
|
assignPeople = data.people || [];
|
|
const depts = data.departments || [];
|
|
if (assignDeptEl.options.length <= 1) {
|
|
depts.forEach((d) => {
|
|
const o = document.createElement('option');
|
|
o.value = d;
|
|
o.textContent = d;
|
|
assignDeptEl.appendChild(o);
|
|
});
|
|
}
|
|
assignSelected = new Set();
|
|
renderAssignList();
|
|
assignStatusEl.textContent = '';
|
|
} catch (e) {
|
|
assignStatusEl.textContent = e.message || 'Ошибка загрузки';
|
|
}
|
|
}
|
|
|
|
if (assignSearchEl) {
|
|
let t = null;
|
|
assignSearchEl.addEventListener('input', () => {
|
|
clearTimeout(t);
|
|
t = setTimeout(loadDirectory, 350);
|
|
});
|
|
assignDeptEl.addEventListener('change', loadDirectory);
|
|
assignClinicEl.addEventListener('change', loadDirectory);
|
|
assignSelectAllBtn.addEventListener('click', () => {
|
|
assignPeople.forEach((p) => assignSelected.add(String(p.staffId)));
|
|
renderAssignList();
|
|
});
|
|
assignSubmitBtn.addEventListener('click', async () => {
|
|
const selectedRows = assignPeople.filter((p) => assignSelected.has(String(p.staffId)));
|
|
const userIds = selectedRows.filter((p) => p.clinicUserId).map((p) => p.clinicUserId);
|
|
const staffIds = selectedRows.filter((p) => !p.clinicUserId && p.staffId != null).map((p) => p.staffId);
|
|
if (!userIds.length && !staffIds.length) return;
|
|
assignStatusEl.textContent = 'Назначаем…';
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}/assign`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ userIds, staffIds }),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'Ошибка назначения');
|
|
assignStatusEl.textContent = `Назначено: ${data.count ?? selectedRows.length}`;
|
|
} catch (e) {
|
|
assignStatusEl.textContent = e.message || 'Ошибка назначения';
|
|
}
|
|
});
|
|
loadDirectory();
|
|
}
|
|
|
|
if (visibilityBtn) {
|
|
visibilityBtn.addEventListener('click', async () => {
|
|
const next = !chainActiveEl.checked;
|
|
try {
|
|
const r = await fetch(`/api/tests/${TEST_ID}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ chainActive: next }),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) throw new Error(data.error || 'Ошибка изменения видимости');
|
|
chainActiveEl.checked = !!next;
|
|
chainActive = !!next;
|
|
visibilityBtn.textContent = chainActive ? 'Скрыть из списка' : 'Снова показать в списке';
|
|
} catch (e) {
|
|
alert(e.message || 'Ошибка изменения видимости');
|
|
}
|
|
});
|
|
}
|
|
|
|
Promise.all([
|
|
fetch(`/api/tests/${TEST_ID}/versions`).then((r) => r.json()).catch(() => null),
|
|
fetch(`/api/tests/${TEST_ID}/attempts`).then((r) => r.json()).catch(() => null),
|
|
fetch(`/api/tests/${TEST_ID}/summary`).then((r) => r.json()).catch(() => null),
|
|
]).then(([v, a, s]) => {
|
|
if (v && Array.isArray(v.versions)) {
|
|
renderVersions(v.versions);
|
|
hasForkRisk = hasForkRisk || (v.versions.length > 1);
|
|
if (typeof v.hasAttempts === 'boolean') {
|
|
hasAnyAttempts = hasAnyAttempts || v.hasAttempts;
|
|
hasForkRisk = hasForkRisk || v.hasAttempts;
|
|
}
|
|
}
|
|
if (a && Array.isArray(a.attempts)) {
|
|
renderAttempts(a.attempts);
|
|
hasAnyAttempts = a.attempts.length > 0;
|
|
}
|
|
if (s && s.test) {
|
|
if (introUpdatedEl) introUpdatedEl.textContent = fmtDt(s.test.updated_at || s.test.updatedAt);
|
|
const versionEl = document.getElementById('intro-version');
|
|
if (versionEl && s.test.version != null) versionEl.textContent = s.test.version;
|
|
if (typeof s.test.hasAttempts === 'boolean') {
|
|
hasAnyAttempts = hasAnyAttempts || s.test.hasAttempts;
|
|
hasForkRisk = hasForkRisk || s.test.hasAttempts;
|
|
}
|
|
if (typeof s.test.versionCount === 'number') {
|
|
hasForkRisk = hasForkRisk || s.test.versionCount > 1;
|
|
}
|
|
}
|
|
updateForkBanner();
|
|
});
|
|
|
|
loadInitial();
|
|
resetBaselineDraft();
|
|
root.addEventListener('input', scheduleDirtyCheck);
|
|
root.addEventListener('change', scheduleDirtyCheck);
|
|
})();
|