feat: student achievements (admin-managed types + per-student records, UI & backup)
This commit is contained in:
@@ -6,9 +6,13 @@
|
||||
let activeTab = 'entries';
|
||||
let facesLoaded = false;
|
||||
let facesData = null;
|
||||
let achTypes = [];
|
||||
let achTypesLoaded = false;
|
||||
let achEditId = null;
|
||||
|
||||
const TABS = [
|
||||
{ key: 'entries', label: 'Журнал', icon: 'book-open' },
|
||||
{ key: 'ach', label: 'Достижения', icon: 'trophy' },
|
||||
{ key: 'shorts', label: 'Короткие сообщения', icon: 'zap' },
|
||||
{ key: 'feedbacks', label: 'Обратная связь', icon: 'message-square-heart' },
|
||||
{ key: 'photos', label: 'Фото', icon: 'image' },
|
||||
@@ -82,8 +86,15 @@
|
||||
</button>`;
|
||||
}
|
||||
|
||||
function achCount() {
|
||||
if (!data.achievements) return 0;
|
||||
if (typeof data.achievements.total === 'number') return data.achievements.total;
|
||||
return (data.achievements.items || []).length;
|
||||
}
|
||||
|
||||
function tabCount(key) {
|
||||
if (key === 'entries') return data.entries.total;
|
||||
if (key === 'ach') return achCount();
|
||||
if (key === 'shorts') return data.shorts.total;
|
||||
if (key === 'feedbacks') return data.feedbacks.total;
|
||||
if (key === 'photos') return data.photos.items.length + (data.photos.photo_path ? 1 : 0);
|
||||
@@ -235,6 +246,10 @@
|
||||
pane.innerHTML = facesHtml();
|
||||
} else if (activeTab === 'entries') {
|
||||
pane.innerHTML = entriesHtml();
|
||||
} else if (activeTab === 'ach') {
|
||||
ensureAchTypes();
|
||||
pane.innerHTML = achievementsHtml();
|
||||
bindAch();
|
||||
} else if (activeTab === 'shorts') {
|
||||
pane.innerHTML = shortsHtml();
|
||||
} else if (activeTab === 'feedbacks') {
|
||||
@@ -256,6 +271,166 @@
|
||||
if (activeTab === 'faces') renderPane();
|
||||
}
|
||||
|
||||
function achItemHtml(a) {
|
||||
return `<div class="sp-ach-item" data-ach-id="${a.id}">
|
||||
<div class="sp-ach-top">
|
||||
<span class="sp-ach-type"><i data-lucide="trophy"></i> ${esc(a.type_name)}</span>
|
||||
${a.value ? `<span class="sp-ach-value">${esc(a.value)}</span>` : ''}
|
||||
<span class="sp-ach-date">
|
||||
${a.achieved_at ? esc(fmtDateIso(a.achieved_at)) : ''}
|
||||
<button type="button" class="icon-btn" data-ach-edit="${a.id}" title="Редактировать"><i data-lucide="pencil"></i></button>
|
||||
<button type="button" class="icon-btn danger" data-ach-del="${a.id}" title="Удалить"><i data-lucide="trash-2"></i></button>
|
||||
</span>
|
||||
</div>
|
||||
${a.note ? `<div class="sp-ach-note">${esc(a.note)}</div>` : ''}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function achFormHtml() {
|
||||
return `<form class="sp-ach-form" id="spAchForm" hidden>
|
||||
<select id="spAchType"></select>
|
||||
<input id="spAchValue" maxlength="200" placeholder="Результат: 271 знак/мин, 1 место">
|
||||
<input type="date" id="spAchDate">
|
||||
<textarea id="spAchNote" maxlength="800" rows="2" placeholder="Комментарий (необязательно)"></textarea>
|
||||
<div class="sp-ach-acts">
|
||||
<button type="button" class="btn-primary ghost" id="spAchCancelBtn">Отмена</button>
|
||||
<button type="submit" class="btn-primary" id="spAchSaveBtn">Сохранить</button>
|
||||
</div>
|
||||
</form>`;
|
||||
}
|
||||
|
||||
function achievementsHtml() {
|
||||
const list = (data.achievements && data.achievements.items) || [];
|
||||
return `<div class="sp-ach-head">
|
||||
<button type="button" class="btn-link" id="spAchAddBtn"><i data-lucide="plus"></i> Добавить достижение</button>
|
||||
${achTypesLoaded ? (achTypes.length ? '<span class="sp-ach-hint">Типы достижений настраивает администратор (Настройки → Достижения)</span>' : '<span class="sp-ach-hint">Администратор ещё не добавил типы — он делает это в Настройки → Достижения</span>') : ''}
|
||||
</div>${achFormHtml()}${list.length ? `<div class="sp-ach-list" id="spAchList">${list.map(achItemHtml).join('')}</div>` : emptyHtml('Достижений пока нет')}`;
|
||||
}
|
||||
|
||||
function achTypeForm(selected) {
|
||||
const act = achTypes.filter(t => t.is_active);
|
||||
const a = v => esc(String(v)).replace(/"/g, '"');
|
||||
const opts = ['<option value="">Выберите тип…</option>']
|
||||
.concat(act.map(t => `<option value="${a(t.name)}"${selected === t.name ? ' selected' : ''}>${esc(t.name)}</option>`));
|
||||
if (selected && !act.some(t => t.name === selected)) {
|
||||
opts.push(`<option value="${a(selected)}" selected>${esc(selected)} (недоступен)</option>`);
|
||||
}
|
||||
return opts.join('');
|
||||
}
|
||||
|
||||
function achFormOpen(rec) {
|
||||
const form = document.getElementById('spAchForm');
|
||||
if (!form) return;
|
||||
achEditId = rec ? rec.id : null;
|
||||
document.getElementById('spAchType').innerHTML = achTypeForm(rec ? rec.type_name : '');
|
||||
document.getElementById('spAchValue').value = rec ? (rec.value || '') : '';
|
||||
document.getElementById('spAchDate').value = rec ? (rec.achieved_at || '') : todayIso();
|
||||
document.getElementById('spAchNote').value = rec ? (rec.note || '') : '';
|
||||
form.hidden = false;
|
||||
document.getElementById('spAchType').focus();
|
||||
}
|
||||
|
||||
function achFormClose() {
|
||||
const form = document.getElementById('spAchForm');
|
||||
if (form) form.hidden = true;
|
||||
achEditId = null;
|
||||
}
|
||||
|
||||
async function ensureAchTypes() {
|
||||
if (achTypesLoaded) return;
|
||||
try {
|
||||
const res = await fetch(`${API}/api/achievement-types`, { headers: hdr() });
|
||||
const d = await res.json();
|
||||
achTypes = res.ok && Array.isArray(d.items) ? d.items : [];
|
||||
achTypesLoaded = true;
|
||||
} catch {
|
||||
achTypes = [];
|
||||
}
|
||||
if (activeTab === 'ach') renderPane();
|
||||
}
|
||||
|
||||
function bindAch() {
|
||||
const addBtn = document.getElementById('spAchAddBtn');
|
||||
if (addBtn) addBtn.addEventListener('click', () => achFormOpen(null));
|
||||
const form = document.getElementById('spAchForm');
|
||||
if (!form) return;
|
||||
form.addEventListener('submit', onAchSubmit);
|
||||
document.getElementById('spAchCancelBtn').addEventListener('click', achFormClose);
|
||||
const list = document.getElementById('spAchList');
|
||||
if (list) {
|
||||
list.addEventListener('click', async e => {
|
||||
const items = (data.achievements && data.achievements.items) || [];
|
||||
const edit = e.target.closest('[data-ach-edit]');
|
||||
if (edit) {
|
||||
const rec = items.find(x => String(x.id) === edit.dataset.achEdit);
|
||||
if (rec) achFormOpen(rec);
|
||||
return;
|
||||
}
|
||||
const del = e.target.closest('[data-ach-del]');
|
||||
if (del) {
|
||||
const rec = items.find(x => String(x.id) === del.dataset.achDel);
|
||||
if (!rec) return;
|
||||
if (!confirm(`Удалить «${rec.type_name}${rec.value ? ' — ' + rec.value : ''}»?`)) return;
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${studentId}/achievements/${rec.id}`, { method: 'DELETE', headers: hdr() });
|
||||
if (!res.ok) throw new Error('delete failed');
|
||||
showToast('Достижение удалено');
|
||||
await refreshAch();
|
||||
} catch {
|
||||
showToast('Не удалось удалить достижение', true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function onAchSubmit(e) {
|
||||
e.preventDefault();
|
||||
const typeName = document.getElementById('spAchType').value.trim();
|
||||
if (!typeName) { showToast('Выберите тип достижения', true); return; }
|
||||
const body = {
|
||||
type_name: typeName,
|
||||
value: document.getElementById('spAchValue').value.trim(),
|
||||
achieved_at: document.getElementById('spAchDate').value || null,
|
||||
note: document.getElementById('spAchNote').value.trim(),
|
||||
};
|
||||
const btn = document.getElementById('spAchSaveBtn');
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const url = achEditId
|
||||
? `${API}/api/students/${studentId}/achievements/${achEditId}`
|
||||
: `${API}/api/students/${studentId}/achievements`;
|
||||
const res = await fetch(url, {
|
||||
method: achEditId ? 'PUT' : 'POST',
|
||||
headers: { ...hdr(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err.error || 'Не удалось сохранить достижение');
|
||||
}
|
||||
showToast(achEditId ? 'Достижение обновлено' : 'Достижение добавлено');
|
||||
achFormClose();
|
||||
await refreshAch();
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Не удалось сохранить достижение', true);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAch() {
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/${studentId}/achievements`, { headers: hdr() });
|
||||
if (res.ok) {
|
||||
const d = await res.json();
|
||||
data.achievements = { items: d.items || [], total: typeof d.total === 'number' ? d.total : (d.items || []).length };
|
||||
}
|
||||
} catch {}
|
||||
renderTabs();
|
||||
if (activeTab === 'ach') renderPane();
|
||||
}
|
||||
|
||||
async function downloadProfile() {
|
||||
const btn = document.getElementById('spDownloadBtn');
|
||||
if (btn) btn.disabled = true;
|
||||
|
||||
Reference in New Issue
Block a user