feat(feedback): add prev-date endpoint, resident last_date, works display, mark stripe update
This commit is contained in:
+168
-19
@@ -359,6 +359,7 @@
|
||||
|
||||
let residentNamesLoaded = false;
|
||||
const residentGroups = new Map();
|
||||
const residentLastDates = new Map();
|
||||
const normName = s => String(s || '').trim().replace(/\s+/g, ' ').toLowerCase();
|
||||
async function loadResidentNames() {
|
||||
if (residentNamesLoaded) return;
|
||||
@@ -367,9 +368,11 @@
|
||||
if (!r.ok) return;
|
||||
const rows = await r.json();
|
||||
residentGroups.clear();
|
||||
residentLastDates.clear();
|
||||
(rows||[]).forEach(x => {
|
||||
const k = normName(x.name);
|
||||
if (k && !residentGroups.has(k)) residentGroups.set(k, x.group_id);
|
||||
if (k && x.last_date && !residentLastDates.has(k)) residentLastDates.set(k, x.last_date);
|
||||
});
|
||||
const dl = document.getElementById('fbResidentList');
|
||||
if (dl) dl.innerHTML = (rows||[]).map(x => `<option value="${escapeHtml(x.name)}">`).join('');
|
||||
@@ -387,6 +390,140 @@
|
||||
sel.value = String(gid);
|
||||
}
|
||||
|
||||
function weekAgoIso() {
|
||||
try { if (typeof isoAddDays === 'function') return isoAddDays(todayIso(), -7); } catch (e) {}
|
||||
const d = new Date(); d.setDate(d.getDate() - 7);
|
||||
const y=d.getFullYear(), mo=String(d.getMonth()+1).padStart(2,'0'), da=String(d.getDate()).padStart(2,'0');
|
||||
return `${y}-${mo}-${da}`;
|
||||
}
|
||||
|
||||
function fmtDateLong(iso) {
|
||||
try { if (typeof fmtDateOnlyLongIso === 'function') return fmtDateOnlyLongIso(iso); } catch (e) {}
|
||||
return iso || '';
|
||||
}
|
||||
|
||||
function journalLink(name, iso) {
|
||||
return `journal.html?student=${encodeURIComponent(name)}&date=${encodeURIComponent(iso)}`;
|
||||
}
|
||||
|
||||
function applyResidentDateInfo() {
|
||||
const inp = document.getElementById('fbResident');
|
||||
const info = document.getElementById('fbResidentAtt');
|
||||
if (!inp || !info) return;
|
||||
const name = inp.value.trim();
|
||||
const k = normName(name);
|
||||
const last = k ? residentLastDates.get(k) : '';
|
||||
if (!last) { info.innerHTML = ''; info.hidden = true; return; }
|
||||
info.hidden = false;
|
||||
info.innerHTML = `<b>${escapeHtml(fmtDateLong(last))}</b> — <a href="${escapeHtml(journalLink(name, last))}" target="_blank" rel="noopener">посмотреть в журнале</a>`;
|
||||
}
|
||||
|
||||
function applyResidentAutoDate() {
|
||||
if (document.getElementById('fbId').value) return;
|
||||
const k = normName(document.getElementById('fbResident').value);
|
||||
if (!k || !residentGroups.has(k)) return;
|
||||
const last = residentLastDates.get(k) || '';
|
||||
const dateEl = document.getElementById('fbDate');
|
||||
dateEl.value = (last && last >= weekAgoIso()) ? last : todayIso();
|
||||
}
|
||||
|
||||
let fbWorksTimer = null, fbWorksSeq = 0;
|
||||
const FB_WORKS_FALLBACK_DAYS = 30;
|
||||
|
||||
function fbIsImageFile(name) {
|
||||
return /\.(jpe?g|jfif|png|gif|webp|bmp|avif|svg|ico)$/i.test(String(name || ''));
|
||||
}
|
||||
|
||||
function fbIsPlayableVideo(name) {
|
||||
return /\.(mp4|m4v|webm|ogv)$/i.test(String(name || ''));
|
||||
}
|
||||
|
||||
function fbEntryWhen(iso) {
|
||||
try { if (typeof fmtDateTime === 'function') return fmtDateTime(iso); } catch (e) {}
|
||||
return iso ? String(iso).slice(0, 10) : '';
|
||||
}
|
||||
|
||||
async function fetchResidentWorks() {
|
||||
const wrap = document.getElementById('fbWorksWrap');
|
||||
const list = document.getElementById('fbWorksList');
|
||||
const dateEl = document.getElementById('fbDate');
|
||||
if (!wrap || !list) return;
|
||||
const name = document.getElementById('fbResident').value.trim();
|
||||
const k = normName(name);
|
||||
const date = dateEl ? dateEl.value : '';
|
||||
if (!k || !residentGroups.has(k) || !date) {
|
||||
wrap.style.display = 'none';
|
||||
list.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
const seq = ++fbWorksSeq;
|
||||
try {
|
||||
const fbId = document.getElementById('fbId').value;
|
||||
const qp = new URLSearchParams({ resident: name, before: date });
|
||||
if (fbId) qp.set('exclude', fbId);
|
||||
let prev = '';
|
||||
try {
|
||||
const pr = await fetch(`${API}/api/feedbacks/prev-date?${qp}`, { headers: hdr() });
|
||||
if (pr.ok) { const pd = await pr.json(); prev = pd.date || ''; }
|
||||
} catch (e) {}
|
||||
if (seq !== fbWorksSeq) return;
|
||||
const from = prev ? isoAddDays(prev, 1) : isoAddDays(todayIso(), -FB_WORKS_FALLBACK_DAYS);
|
||||
const p = new URLSearchParams({ student_name: name, date_from: from, date_to: date, limit: '200' });
|
||||
const r = await fetch(`${API}/api/entries?${p}`, { headers: hdr() });
|
||||
if (seq !== fbWorksSeq) return;
|
||||
if (!r.ok) { wrap.style.display = 'none'; list.innerHTML = ''; return; }
|
||||
const d = await r.json();
|
||||
if (seq !== fbWorksSeq) return;
|
||||
renderResidentWorks(d.entries || [], name, from, date, !!prev);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function renderResidentWorks(entries, name, from, to, hasPrev) {
|
||||
const wrap = document.getElementById('fbWorksWrap');
|
||||
const list = document.getElementById('fbWorksList');
|
||||
const dateEl = document.getElementById('fbWorksDate');
|
||||
const link = document.getElementById('fbWorksJournal');
|
||||
if (!wrap || !list) return;
|
||||
if (dateEl) dateEl.textContent = hasPrev ? `с ${fmtDateLong(from)} по ${fmtDateLong(to)}` : `за последние ${FB_WORKS_FALLBACK_DAYS} дней`;
|
||||
if (link) { link.href = journalLink(name, to); link.hidden = false; }
|
||||
const FB_WORKS_MAX_ENTRIES = 4;
|
||||
const blocks = [];
|
||||
let hidden = 0;
|
||||
entries.forEach(e => {
|
||||
const files = e.files || [];
|
||||
if (!files.length) return;
|
||||
if (blocks.length >= FB_WORKS_MAX_ENTRIES) { hidden++; return; }
|
||||
const filesHtml = files.map(f => {
|
||||
const url = `${API}/api/files/${f.token}`;
|
||||
if (fbIsImageFile(f.name)) {
|
||||
return `<a href="#" class="fb-work-thumb" data-img="${escapeHtml(url)}" title="${escapeHtml(f.name)}"><img src="${escapeHtml(thumbSrc(url))}" alt="${escapeHtml(f.name)}" loading="lazy"></a>`;
|
||||
}
|
||||
const href = fbIsPlayableVideo(f.name) ? `${url}?play=1` : url;
|
||||
return `<a class="fb-work-file" href="${escapeHtml(href)}" title="Скачать">${escapeHtml(f.name)}</a>`;
|
||||
}).join('');
|
||||
const when = fbEntryWhen(e.created_at);
|
||||
const txt = escapeHtml((e.description || '').slice(0, 160));
|
||||
blocks.push(`<div class="fb-work">${when ? `<div class="fb-work-head">${escapeHtml(when)}</div>` : ''}${txt ? `<div class="fb-work-text">${txt}</div>` : ''}<div class="fb-work-files">${filesHtml}</div></div>`);
|
||||
});
|
||||
if (!blocks.length) {
|
||||
list.innerHTML = '<div class="fb-works-empty">За выбранный период прикреплённых файлов нет</div>';
|
||||
} else {
|
||||
const more = hidden ? `<div class="fb-works-empty">…и ещё записей: ${hidden}</div>` : '';
|
||||
list.innerHTML = blocks.join('') + more;
|
||||
}
|
||||
wrap.style.display = '';
|
||||
}
|
||||
|
||||
function applyResidentContext() {
|
||||
applyResidentGroup();
|
||||
applyResidentDateInfo();
|
||||
applyResidentAutoDate();
|
||||
applyResidentPhoto();
|
||||
applyMarkStripe();
|
||||
clearTimeout(fbWorksTimer);
|
||||
fbWorksTimer = setTimeout(fetchResidentWorks, 300);
|
||||
}
|
||||
|
||||
let studentPhotosLoaded = false;
|
||||
const studentPhotos = new Map();
|
||||
async function loadStudentPhotos() {
|
||||
@@ -422,6 +559,10 @@
|
||||
return `<button type="button" class="student-avatar fb-card-photo" data-img="${escapeHtml(fbPhotoFull(p))}" title="Увеличить фото"><img src="${escapeHtml(fbStudentThumb(p))}" alt="" loading="lazy"></button>`;
|
||||
}
|
||||
|
||||
function fbPhotoStripeHtml() {
|
||||
return '<span class="fb-mark-stripe" id="fbMarkStripe" hidden></span>';
|
||||
}
|
||||
|
||||
function applyResidentPhoto() {
|
||||
const el = document.getElementById('fbResidentPhoto');
|
||||
if (!el) return;
|
||||
@@ -431,16 +572,16 @@
|
||||
el.hidden = true;
|
||||
el.removeAttribute('data-img');
|
||||
delete el.dataset.photoPath;
|
||||
el.innerHTML = '';
|
||||
el.innerHTML = fbPhotoStripeHtml();
|
||||
return;
|
||||
}
|
||||
if (el.dataset.photoPath !== p) {
|
||||
el.dataset.photoPath = p;
|
||||
el.dataset.img = fbPhotoFull(p);
|
||||
el.title = 'Увеличить фото';
|
||||
el.innerHTML = `<img src="${escapeHtml(fbStudentThumb(p))}" alt="" loading="lazy">`;
|
||||
el.innerHTML = `${fbPhotoStripeHtml()}<img src="${escapeHtml(fbStudentThumb(p))}" alt="" loading="lazy">`;
|
||||
const img = el.querySelector('img');
|
||||
if (img) img.addEventListener('error', () => { el.hidden = true; el.removeAttribute('data-img'); delete el.dataset.photoPath; el.innerHTML = ''; }, { once: true });
|
||||
if (img) img.addEventListener('error', () => { el.hidden = true; el.removeAttribute('data-img'); delete el.dataset.photoPath; el.innerHTML = fbPhotoStripeHtml(); }, { once: true });
|
||||
}
|
||||
el.hidden = false;
|
||||
}
|
||||
@@ -467,19 +608,20 @@
|
||||
if (!el) return;
|
||||
const k = normName(document.getElementById('fbResident').value);
|
||||
const m = k ? workMarks.get(k) : null;
|
||||
const photo = document.getElementById('fbResidentPhoto');
|
||||
if (!m) {
|
||||
el.hidden = true;
|
||||
el.style.background = '';
|
||||
el.style.border = '';
|
||||
el.style.boxShadow = '';
|
||||
el.removeAttribute('title');
|
||||
if (photo) photo.title = 'Увеличить фото';
|
||||
return;
|
||||
}
|
||||
const t = Math.max(-4, Math.min(4, m.total));
|
||||
const hue = Math.round(120 * (t + 4) / 8);
|
||||
el.hidden = false;
|
||||
el.style.background = `hsl(${hue}, 72%, 45%)`;
|
||||
el.style.boxShadow = `0 0 8px hsla(${hue}, 72%, 45%, .35)`;
|
||||
el.title = `Отметки работы: ${m.total > 0 ? '+' : ''}${m.total} (отметок: ${m.marks})`;
|
||||
el.style.border = `3px solid hsl(${hue}, 72%, 45%)`;
|
||||
el.style.boxShadow = `inset 0 0 10px hsla(${hue}, 72%, 45%, .4)`;
|
||||
if (photo) photo.title = `Отметки работы: ${m.total > 0 ? '+' : ''}${m.total} (отметок: ${m.marks})`;
|
||||
}
|
||||
|
||||
let topicsLoaded = false;
|
||||
@@ -585,8 +727,8 @@
|
||||
if (!row) { el.textContent = 'Прошлый отзыв'; return; }
|
||||
const meta = [`Дата: ${row.feedback_date || '—'}`, `Тема: ${row.topic || '—'}`].join(' | ');
|
||||
el.innerHTML = '';
|
||||
el.append(`Прошлый отзыв по резиденту: ${row.resident || ''}`);
|
||||
el.append(document.createElement('br'));
|
||||
/*el.append(`Прошлый отзыв по резиденту: ${row.resident || ''}`);
|
||||
el.append(document.createElement('br'));*/
|
||||
el.append(meta);
|
||||
}
|
||||
|
||||
@@ -622,8 +764,16 @@
|
||||
document.getElementById('fbDate').value = todayIso();
|
||||
document.getElementById('fbGroup').value = '';
|
||||
document.getElementById('fbResident').value = '';
|
||||
applyMarkStripe();
|
||||
applyResidentPhoto();
|
||||
applyMarkStripe();
|
||||
const attInfo = document.getElementById('fbResidentAtt');
|
||||
if (attInfo) { attInfo.innerHTML = ''; attInfo.hidden = true; }
|
||||
const worksWrap = document.getElementById('fbWorksWrap');
|
||||
if (worksWrap) { worksWrap.style.display = 'none'; }
|
||||
const worksList = document.getElementById('fbWorksList');
|
||||
if (worksList) worksList.innerHTML = '';
|
||||
clearTimeout(fbWorksTimer);
|
||||
fbWorksSeq++;
|
||||
document.getElementById('fbTopic').value = '';
|
||||
document.getElementById('fbPastCheck').checked = false;
|
||||
document.getElementById('fbPastReviewWrap').style.display = 'none';
|
||||
@@ -653,8 +803,7 @@
|
||||
document.getElementById('fbDate').value = f.feedback_date || todayIso();
|
||||
document.getElementById('fbGroup').value = f.group_id || '';
|
||||
document.getElementById('fbResident').value = f.resident || '';
|
||||
applyMarkStripe();
|
||||
applyResidentPhoto();
|
||||
applyResidentContext();
|
||||
document.getElementById('fbTopic').value = f.topic || '';
|
||||
if (f.past_review) {
|
||||
document.getElementById('fbPastCheck').checked = true;
|
||||
@@ -833,15 +982,15 @@
|
||||
document.getElementById('fbResident').addEventListener('input', () => {
|
||||
clearTimeout(shortsFetchTimer);
|
||||
const v = document.getElementById('fbResident').value.trim();
|
||||
applyResidentGroup();
|
||||
applyMarkStripe();
|
||||
applyResidentPhoto();
|
||||
applyResidentContext();
|
||||
shortsFetchTimer = setTimeout(() => fetchShorts(v), 400);
|
||||
});
|
||||
document.getElementById('fbResident').addEventListener('change', () => {
|
||||
applyResidentGroup();
|
||||
applyMarkStripe();
|
||||
applyResidentPhoto();
|
||||
applyResidentContext();
|
||||
});
|
||||
document.getElementById('fbDate').addEventListener('change', () => {
|
||||
clearTimeout(fbWorksTimer);
|
||||
fbWorksTimer = setTimeout(fetchResidentWorks, 300);
|
||||
});
|
||||
document.getElementById('fbPastCheck').addEventListener('change', e => {
|
||||
document.getElementById('fbPastReviewWrap').style.display = e.target.checked ? '' : 'none';
|
||||
|
||||
+10
-1
@@ -2304,7 +2304,16 @@ document.getElementById('linkCopyBtn').addEventListener('click', copyLink);
|
||||
document.querySelectorAll('#viewToggle button').forEach(b => {
|
||||
b.classList.toggle('active', b.dataset.view === view);
|
||||
});
|
||||
setDefaultDates();
|
||||
const qs = new URLSearchParams(location.search);
|
||||
const qsStudent = (qs.get('student') || '').trim();
|
||||
const qsDate = /^\d{4}-\d{2}-\d{2}$/.test(qs.get('date') || '') ? qs.get('date') : '';
|
||||
if (qsDate) {
|
||||
document.getElementById('dateFrom').value = qsDate;
|
||||
document.getElementById('dateTo').value = qsDate;
|
||||
} else {
|
||||
setDefaultDates();
|
||||
}
|
||||
if (qsStudent) document.getElementById('studentFilter').value = qsStudent;
|
||||
loadGroupOptions();
|
||||
loadModuleOptions();
|
||||
loadStudentOptions();
|
||||
|
||||
Reference in New Issue
Block a user