Files
ClassManager/frontend/assets/js/homework-manage.js
T
canglan d0cdb5b928 历史记录/操作日志与作业按次登记改进
1. 历史记录:批次合并视图新增"批量反撤销"(需反撤销权限);单条/批量撤销与反撤销提示更明确(说明记录去向,不会删除);操作列权限门控补充 restore_records
2. 操作日志:批量撤销/反撤销写入日志;单条撤销/反撤销/加减分日志详情补充学生姓名(学号)、分值变动、原因;新增"详情关键词"筛选(学生姓名/原因等)
3. 节假日接口:证书校验失败自动回退重试,提高服务器兼容性
4. 作业按次登记:默认列出全部学生、缺交次数默认0(0=已交),支持全员已交登记;后端允许空缺交名单
2026-10-15 20:04:41 +08:00

393 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 班级操行分管理系统 - 作业扣分页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const hwNotSubmit = window.DEDUCTION_HOMEWORK_NOT_SUBMIT || 2;
const hwLate = window.DEDUCTION_HOMEWORK_LATE || 1;
let studentsData = [];
// 更新页面中的配置值显示(加减分上限来自权限组动态配置)
document.querySelectorAll('.hw-not-submit').forEach(el => el.textContent = hwNotSubmit);
document.querySelectorAll('.hw-late').forEach(el => el.textContent = hwLate);
document.querySelectorAll('.hw-max').forEach(el => el.textContent = describeScoreLimits());
// 更新输入框的 min/max(按权限组动态上限)
applyScoreLimits(document.getElementById('pointsChange'));
// 按次登记默认单次缺交扣分
const regPerInput = document.getElementById('hwRegPerDeduction');
if (regPerInput) regPerInput.value = hwNotSubmit;
// 加载科目列表(学习委员)
async function loadSubjectsForHomework() {
const subjectSelect = document.getElementById('hwSubjectSelect');
if (!subjectSelect) return;
// 作业下拉只显示已启用的科目
const res = await apiGet('/api/subject/list', { is_active: true });
if (res && res.success && res.data && res.data.subjects) {
let html = '<option value="">不选择科目</option>';
res.data.subjects.forEach(s => {
html += `<option value="${escapeHtml(s.subject_name)}">${escapeHtml(s.subject_name)}</option>`;
});
subjectSelect.innerHTML = html;
}
}
async function loadStudents() {
const res = await apiGet('/api/admin/students', {page_size: 1000});
if (res && res.success) {
studentsData = res.data.students || [];
let html = '';
res.data.students.forEach(student => {
html += `<tr>
<td><input type="checkbox" class="student-checkbox" data-id="${student.student_id}"></td>
<td>${escapeHtml(student.student_no)}</td>
<td>${escapeHtml(student.name)}</td>
<td>${student.total_points}</td>
<td><button class="btn btn-sm btn-outline" onclick="showSinglePointsModal(${student.student_id}, '${escapeHtml(student.name)}')">加减分</button></td>
</tr>`;
});
if (res.data.students.length === 0) {
html = '<tr><td colspan="5" style="text-align:center;">暂无学生数据</td></tr>';
}
document.getElementById('studentList').innerHTML = html;
}
}
function handleSubmitPoints() {
const pointsChange = parseInt(document.getElementById('pointsChange').value);
const limitError = validateScoreChange(pointsChange);
if (limitError) {
showToast(limitError, 'warning');
return;
}
// 附加科目前缀、具体作业和缴交时间
const subjectSelect = document.getElementById('hwSubjectSelect');
const subjectName = subjectSelect ? subjectSelect.value : '';
const hwTitle = document.getElementById('hwTitle').value.trim();
const hwDeadline = document.getElementById('hwDeadline').value;
const reasonEl = document.getElementById('pointsReason');
let prefix = '';
if (subjectName) {
prefix = `[${subjectName}]`;
}
if (hwTitle) {
prefix += `[${hwTitle}]`;
}
if (hwDeadline) {
prefix += ` 缴交:${hwDeadline}`;
}
if (prefix) {
reasonEl.value = prefix + ' ' + reasonEl.value;
}
submitBatchPoints({ related_type: 'homework' });
}
// ========== 科目管理功能 ==========
function toggleSubjectPanel() {
const content = document.getElementById('subjectPanelContent');
const toggle = document.getElementById('subjectPanelToggle');
if (!content || !toggle) return;
const isExpanded = content.classList.contains('expanded');
if (isExpanded) {
content.classList.remove('expanded');
toggle.classList.remove('expanded');
toggle.textContent = '▶ 展开';
} else {
content.classList.add('expanded');
toggle.classList.add('expanded');
toggle.textContent = '▼ 收起';
loadSubjectList();
}
}
let subjectsCache = [];
async function loadSubjectList() {
const res = await apiGet('/api/subject/list');
if (res && res.success && res.data) {
subjectsCache = res.data.subjects || [];
let html = '';
subjectsCache.forEach((sub, index) => {
const safeName = escapeHtml(sub.subject_name || '');
const safeCode = escapeHtml(sub.subject_code || '');
const sortOrder = sub.sort_order || 0;
const codeHtml = safeCode ? `<span class="subject-code">${safeCode}</span>` : '';
const upBtn = index > 0
? `<button class="btn btn-sm btn-ghost" title="上移" onclick="moveSubject(${sub.subject_id}, -1)">↑</button>`
: '';
const downBtn = index < subjectsCache.length - 1
? `<button class="btn btn-sm btn-ghost" title="下移" onclick="moveSubject(${sub.subject_id}, 1)">↓</button>`
: '';
html += `
<div class="subject-item">
<span class="subject-name">${safeName}</span>
${codeHtml}
<span class="subject-status ${sub.is_active ? 'subject-status-active' : 'subject-status-inactive'}">
${sub.is_active ? '启用' : '禁用'}
</span>
<span class="subject-order">排序 ${sortOrder}</span>
<span class="subject-actions">
${upBtn}
${downBtn}
<button class="btn btn-sm btn-outline" onclick="showEditSubjectModal(${sub.subject_id}, '${safeName.replace(/'/g, "\\'")}', '${safeCode.replace(/'/g, "\\'")}', ${sortOrder})">编辑</button>
<button class="btn btn-sm btn-ghost" onclick="toggleSubjectStatus(${sub.subject_id}, ${sub.is_active ? 'false' : 'true'})">
${sub.is_active ? '禁用' : '启用'}
</button>
<button class="btn btn-sm btn-outline-danger" onclick="deleteSubject(${sub.subject_id})">删除</button>
</span>
</div>
`;
});
if (subjectsCache.length === 0) {
html = '<p style="text-align:center;padding:40px;">暂无科目,请点击"添加科目"</p>';
}
document.getElementById('subjectList').innerHTML = html;
}
}
async function moveSubject(subjectId, direction) {
const index = subjectsCache.findIndex(s => s.subject_id === subjectId);
if (index < 0) return;
const targetIndex = index + direction;
if (targetIndex < 0 || targetIndex >= subjectsCache.length) return;
const ids = subjectsCache.map(s => s.subject_id);
ids[index] = subjectsCache[targetIndex].subject_id;
ids[targetIndex] = subjectId;
const res = await apiPut('/api/subject/reorder', { subject_ids: ids });
if (res && res.success) {
loadSubjectList();
loadSubjectsForHomework();
} else {
showToast(res?.message || '排序失败', 'error');
}
}
function showAddSubjectModal() {
const form = document.getElementById('addSubjectFormInHw');
if (form) form.reset();
document.getElementById('addSubjectModal').style.display = 'flex';
}
async function submitAddSubject() {
const subjectName = document.getElementById('subjectName').value.trim();
const subjectCode = document.getElementById('subjectCode').value.trim();
if (!subjectName) {
showToast('请填写科目名称', 'warning');
return;
}
const res = await apiPost('/api/subject/create', {
subject_name: subjectName,
subject_code: subjectCode
});
if (res && res.success) {
showToast('科目添加成功');
closeModal('addSubjectModal');
loadSubjectList();
loadSubjectsForHomework();
} else {
showToast(res?.message || '添加失败', 'error');
}
}
async function toggleSubjectStatus(subjectId, enable) {
const res = await apiPut(`/api/subject/update/${subjectId}`, { is_active: enable });
if (res && res.success) {
showToast(enable ? '科目已启用' : '科目已禁用');
loadSubjectList();
loadSubjectsForHomework();
} else {
showToast(res?.message || '操作失败', 'error');
}
}
async function deleteSubject(subjectId) {
if (!confirm('确定要删除该科目吗?如果科目下有作业数据将无法删除。')) return;
const res = await apiDelete('/api/subject/delete/' + subjectId);
if (res && res.success) {
showToast('科目删除成功');
loadSubjectList();
loadSubjectsForHomework();
} else {
showToast(res?.message || '删除失败', 'error');
}
}
function showEditSubjectModal(subjectId, name, code, sortOrder) {
document.getElementById('editSubjectId').value = subjectId;
document.getElementById('editSubjectName').value = name;
document.getElementById('editSubjectCode').value = code;
document.getElementById('editSubjectSortOrder').value = sortOrder;
document.getElementById('editSubjectModal').style.display = 'flex';
}
async function submitEditSubject() {
const subjectId = document.getElementById('editSubjectId').value;
const subjectName = document.getElementById('editSubjectName').value.trim();
const subjectCode = document.getElementById('editSubjectCode').value.trim();
const sortOrder = document.getElementById('editSubjectSortOrder').value;
if (!subjectName) {
showToast('请填写科目名称', 'warning');
return;
}
const data = {
subject_name: subjectName,
subject_code: subjectCode
};
if (sortOrder !== '') data.sort_order = parseInt(sortOrder);
const res = await apiPut(`/api/subject/update/${subjectId}`, data);
if (res && res.success) {
showToast('科目更新成功');
closeModal('editSubjectModal');
loadSubjectList();
loadSubjectsForHomework();
} else {
showToast(res?.message || '更新失败', 'error');
}
}
// ========== 作业按次登记 ==========
async function showHomeworkRegisterModal() {
if (!studentsData || studentsData.length === 0) {
showToast('学生列表尚未加载,请稍后重试', 'warning');
return;
}
// 科目下拉(值为科目ID)
const subjectSelect = document.getElementById('hwRegSubject');
const subRes = await apiGet('/api/subject/list', { is_active: true });
let options = '<option value="">请选择科目</option>';
if (subRes && subRes.success && subRes.data && subRes.data.subjects) {
subRes.data.subjects.forEach(s => {
options += `<option value="${s.subject_id}">${escapeHtml(s.subject_name)}</option>`;
});
}
subjectSelect.innerHTML = options;
// 默认列出全部学生,缺交次数默认 0(0 = 已交)
let html = '';
studentsData.forEach(student => {
html += `<div class="hw-reg-item">
<span class="hw-reg-name">${escapeHtml(student.name)} <small>${escapeHtml(student.student_no)}</small></span>
<span class="hw-reg-miss-label" style="margin-left:auto;">缺交</span>
<input type="number" class="hw-reg-miss" data-id="${student.student_id}" min="0" value="0" style="width:64px;">
<span class="hw-reg-miss-label">次</span>
</div>`;
});
document.getElementById('hwRegStudentList').innerHTML = html;
const deadlineInput = document.getElementById('hwRegDeadline');
if (!deadlineInput.value) {
const now = new Date();
deadlineInput.value = now.getFullYear() + '-' +
String(now.getMonth() + 1).padStart(2, '0') + '-' +
String(now.getDate()).padStart(2, '0');
}
document.getElementById('homeworkRegisterModal').style.display = 'flex';
}
async function submitHomeworkRegister() {
const subjectId = parseInt(document.getElementById('hwRegSubject').value);
const title = document.getElementById('hwRegTitle').value.trim();
const deadline = document.getElementById('hwRegDeadline').value;
const totalTimes = parseInt(document.getElementById('hwRegTotal').value);
const perDeductionRaw = document.getElementById('hwRegPerDeduction').value;
const note = document.getElementById('hwRegNote').value.trim();
if (!subjectId) { showToast('请选择科目', 'warning'); return; }
if (!deadline) { showToast('请选择缴交/截止日期', 'warning'); return; }
if (!totalTimes || totalTimes < 1 || totalTimes > 99) {
showToast('总提交次数需在1-99之间', 'warning');
return;
}
const inputs = Array.from(document.querySelectorAll('.hw-reg-miss'));
const records = [];
for (const input of inputs) {
const miss = parseInt(input.value) || 0;
if (miss < 0 || miss > totalTimes) {
showToast('缺交次数需在0~总提交次数之间', 'warning');
return;
}
if (miss > 0) {
records.push({ student_id: parseInt(input.dataset.id), miss_count: miss });
}
}
const payload = {
subject_id: subjectId,
deadline: deadline,
total_times: totalTimes,
records: records
};
if (title) payload.title = title;
if (perDeductionRaw) payload.per_deduction = parseInt(perDeductionRaw);
if (note) payload.note = note;
const btn = document.getElementById('hwRegisterSubmitBtn');
btn.disabled = true;
btn.textContent = '提交中...';
try {
const res = await apiPost('/api/admin/homework/register', payload);
if (res && res.success) {
showToast(res.message || '登记完成');
closeModal('homeworkRegisterModal');
document.querySelectorAll('.student-checkbox').forEach(cb => cb.checked = false);
const selectAll = document.getElementById('selectAll');
if (selectAll) selectAll.checked = false;
loadStudents();
} else {
showToast((res && res.message) || '登记失败', 'error');
}
} finally {
btn.disabled = false;
btn.textContent = '确认登记';
}
}
// 绑定科目管理折叠面板
var subjectHeader = document.getElementById('subjectPanelHeader');
if (subjectHeader) {
subjectHeader.addEventListener('click', toggleSubjectPanel);
}
loadStudents();
loadSubjectsForHomework();
window.loadStudents = loadStudents;
window.handleSubmitPoints = handleSubmitPoints;
window.toggleSubjectPanel = toggleSubjectPanel;
window.showAddSubjectModal = showAddSubjectModal;
window.submitAddSubject = submitAddSubject;
window.toggleSubjectStatus = toggleSubjectStatus;
window.deleteSubject = deleteSubject;
window.showEditSubjectModal = showEditSubjectModal;
window.submitEditSubject = submitEditSubject;
window.moveSubject = moveSubject;
window.showHomeworkRegisterModal = showHomeworkRegisterModal;
window.submitHomeworkRegister = submitHomeworkRegister;
})();