R20:客观题分值口径 + 大题一位小数评分 + 页面刷新修复 + 大题导出 Word

A1/A2 客观题:
- 清理题库占位题(答案恒 A/true、分值恒 5),客观题分值硬约束为 1 分
- 新增 scripts/check_question_bank_health.py(带变异注入自检,已验证判别力)
- ci-gates.sh 由 9 步扩为 10 步,纳入题库健康门禁

B 大题评分一位小数:
- 新增 app/lib/decimal_score.js:DECIMAL(6,1) + getter 兜底,单点收口
  「DB 列 / model 声明 / dialectOptions.decimalNumbers」三处必须同时成立
- 10 个 model 的 score 字段改走该工厂
- database/migrations/025:17 列 INT → DECIMAL(6,1)(6 列题目分值保持 int)
- service/bigQuestion.js:setSubScores 用 Math.round(n*10)/10 收敛,不再抹成整数

C 页面状态刷新:
- 修复 onQuestionTypeChange 的 `bigQuestions.length === 0` 守卫(切组后旧数组仍非空
  ⇒ 永不重新拉取)
- 修复评分后只刷一半(saveBigScores / saveExamScore 各只刷一侧列表)
- 收敛为唯一刷新入口 refreshQuestionData();ensureBigData 改按「数据属于哪个组」
  判定;fetchBigQuestions/fetchBigAnswers 增加组别竞态守卫

D 大题导出 Word(单题 / 批量 / 全部):
- 新增 app/lib/docx.js:零新依赖手写 OOXML(复用已有 archiver@8),支持 p/strong/
  span/a/br/table/ul-ol 子集,具名捕获组词法解析,pPr 严格排在 runs 之前
- service.bigQuestion.exportQuestions + controller.exportBigQuestions +
  GET /api/admin/big-questions/:group/export
- 前端导出选中/全部两按钮 + 表格多选(reserve-selection)

计划外发现并修复(已在报告中披露):
- 管理端跨组写分越权:setSubScores 定位作答行时缺 group 约束,可用任意组的路径
  改写他组考生成绩(读写不对称:读取侧有 where:{group},写入侧没有)
  ⇒ 已补 group 约束,并区分「不属于本组」与「尚未抽题」两种文案
- 导出路由注册顺序:export 原注册在 :group/:id 之后(当时因后者仅 PUT/DELETE 而
  未暴露问题),已前移以消除「将来新增 GET /:group/:id 会吞掉 export」的隐患

验证(全部实测,非仅看退出码):
- ci-gates.sh 10/10 全绿;四连门禁 52/0 · 22/0 · 20/0 · 46/0
- 端到端探针 probe_d_03(导出+小数)ALL PASS;probe_e_01(切组刷新)22/22 ALL PASS
- 前端构建 VITE_EXIT=0

未 push:须先上线验证通过再单独授权。
This commit is contained in:
2026-09-23 18:29:50 +08:00
parent a019df510d
commit 079c1b31c9
18 changed files with 906 additions and 135 deletions

View File

@@ -5,6 +5,18 @@ const path = require('path');
const fs = require('fs');
const XLSX = require('xlsx');
/**
* 客观题(选择题 / 判断题)每小题固定分值。
*
* 赛制口径(2026-09-23 统一):中职 / 高职组的选择题与判断题**每小题 1 分**,
* 客观题满分 = 抽题数(50 + 20 = 70)。
* 此前分值可由管理端任意设置、且 Excel 导入缺省 5 分,一旦「5 分题的答案
* 恰好全是 A / true」,考生全选 A 即可白拿高分(实测高职判断题得分率 95.3%)。
* 综合性任务(word)不在本约束范围内。
*/
const OBJECTIVE_SCORE_PER_QUESTION = 1;
const OBJECTIVE_TYPES = ['choice', 'judge'];
/**
* 管理后台服务 - AdminService
*
@@ -960,6 +972,27 @@ class AdminService extends Service {
}
}
/**
* 计算题目的落库分值。
*
* 客观题(choice / judge)一律强制为 `OBJECTIVE_SCORE_PER_QUESTION`(=1),
* 忽略调用方传入的任何分值;综合性任务(word)保留传入分值(须为正数)。
*
* @param {string} questionType - choice / judge / word
* @param {*} rawScore - 调用方传入的分值(客观题会忽略)
* @returns {number} 实际落库分值
*/
_resolveQuestionScore(questionType, rawScore) {
if (OBJECTIVE_TYPES.includes(questionType)) {
return OBJECTIVE_SCORE_PER_QUESTION;
}
const n = Number(rawScore);
if (!isFinite(n) || n <= 0) {
throw new Error('综合性任务分值(score)必须为大于 0 的合法数值');
}
return n;
}
/**
* 管理考试题目(替换操作:先清空再创建)
* @param {Array} questions - 题目数组
@@ -982,11 +1015,8 @@ class AdminService extends Service {
// 批量插入新题目
const questionData = questions.map((q, index) => {
// 分值必须为大于 0 的合法数值;0 分题属非法业务数据,整批替换时直接拒绝
const scoreNum = Number(q.score);
if (!isFinite(scoreNum) || scoreNum <= 0) {
throw new Error(`题目分值(score)必须为大于 0 的合法数值(第 ${index + 1} 题)`);
}
// 客观题(choice/judge)分值由服务层强制为 1(赛制口径),忽略传入值
const scoreNum = this._resolveQuestionScore(questionType, q.score);
const base = {
group,
score: scoreNum,
@@ -1037,12 +1067,8 @@ class AdminService extends Service {
async addQuestion(questionData, group, questionType) {
const QuestionModel = this._getQuestionModel(questionType);
// 分值必须为大于 0 的合法数值;0 分题属非法业务数据
// (抽中该题的考生答对也得 0 分,且不同人满分不一致)。缺失/非法分值直接拒绝。
const scoreNum = Number(questionData.score);
if (!isFinite(scoreNum) || scoreNum <= 0) {
throw new Error('题目分值(score)必须为大于 0 的合法数值');
}
// 客观题(choice/judge)一律 1 分(赛制口径);word 题型保留传入分值
const scoreNum = this._resolveQuestionScore(questionType, questionData.score);
const data = {
group,
@@ -1081,7 +1107,10 @@ class AdminService extends Service {
}
const updateData = {
score: questionData.score !== undefined ? questionData.score : question.score,
// 客观题分值恒为 1(忽略传入值,防止管理端改回 5 分);word 题型沿用原逻辑
score: OBJECTIVE_TYPES.includes(questionType)
? OBJECTIVE_SCORE_PER_QUESTION
: (questionData.score !== undefined ? Number(questionData.score) : question.score),
sort_order: questionData.sort_order !== undefined ? questionData.sort_order : question.sort_order,
};
@@ -1373,8 +1402,10 @@ class AdminService extends Service {
async setTypeScore(group, questionType, score) {
const { ctx } = this;
const QuestionModel = this._getQuestionModel(questionType);
// 客观题只能被设为 1 分(赛制口径);word 题型按传入值设置
const resolved = this._resolveQuestionScore(questionType, score);
const [updated] = await QuestionModel.update(
{ score },
{ score: resolved },
{ where: { group } }
);
return updated;
@@ -1477,9 +1508,9 @@ class AdminService extends Service {
}
}
const scoreRaw = row['分值'];
let score = parseInt(scoreRaw, 10);
if (isNaN(score) || score < 1) score = 5;
// 客观题分值恒为 1(赛制口径)。Excel 的「分值」列一律忽略 ——
// 旧逻辑在缺列/非法时缺省 5 分,正是「高分题」污染题库的来源之一。
const score = OBJECTIVE_SCORE_PER_QUESTION;
const data = { group, question, answer, score, sort_order: 0 };
if (qType === 'choice') data.options = options;
@@ -1503,11 +1534,12 @@ class AdminService extends Service {
* @returns {Promise<Buffer>} xlsx 文件 Buffer
*/
async buildQuestionTemplate() {
// 「分值」列已废弃(客观题恒为 1 分),保留列名仅为兼容旧模板与旧脚本
const header = ['题型', '题目', '选项A', '选项B', '选项C', '选项D', '正确答案', '分值'];
const example = ['选择题', '北斗系统由多少颗卫星组成?', '24', '35', '55', '12', 'C', 5];
const example = ['选择题', '北斗系统由多少颗卫星组成?', '24', '35', '55', '12', 'C', 1];
const ws = XLSX.utils.aoa_to_sheet([header, example]);
// 判断题示例行(另起一行,便于参考)
XLSX.utils.sheet_add_aoa(ws, [['判断题', '北斗系统由我国自主研发。', '', '', '', '', '对', 5]], { origin: -1 });
XLSX.utils.sheet_add_aoa(ws, [['判断题', '北斗系统由我国自主研发。', '', '', '', '', '对', 1]], { origin: -1 });
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, '客观题');
return XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });

View File

@@ -37,6 +37,18 @@ const SUB_QUESTION_COUNT = 3;
const ANSWER_FIELDS = ['answer_1', 'answer_2', 'answer_3'];
const SCORE_FIELDS = ['score_1', 'score_2', 'score_3'];
/** 组别中文名(导出文档抬头 / 文件名用) */
const GROUP_LABELS = { zhongzhi: '中职组', gaozhi: '高职组', benke: '本科组', jiaoshi: '教师组' };
/** 导出文件名里的非法字符(Windows 禁止 \ / : * ? " < > |,另剔除控制字符) */
function safeFilePart(str) {
return String(str == null ? '' : str)
// eslint-disable-next-line no-control-regex
.replace(/[\\/:*?"<>|\u0000-\u001f]/g, '_')
.replace(/\s+/g, '_')
.slice(0, 80) || 'untitled';
}
class BigQuestionService extends Service {
/** 该组别是否启用综合大题 */
isEnabledGroup(group) {
@@ -315,14 +327,30 @@ class BigQuestionService extends Service {
return { ok: false, code: 'GROUP_NOT_ENABLED', message: '该组别未启用综合大题' };
}
// ⚠️ 必须带 group(与读取路径 listAnswersForAdmin 同源):只按 aggregation_uid 定位时,
// 用任意组别的路径即可改写他组考生成绩,且下方 updateScores(..., group) 会用错组
// 去同步总分 ⇒ 组别隔离在写入侧被绕过(2026-09-23 实测:
// PUT /big-answers/gaozhi/<中职考生uid>/scores 返回 code=0 并真实改写了中职行)。
const record = await ctx.model.BigQuestionAnswer.findOne({
where: { aggregation_uid: aggregationUid },
where: { group, aggregation_uid: aggregationUid },
});
if (!record) {
return { ok: false, code: 'NOT_FOUND', message: '该考生尚未抽取大题,无法评分' };
// 区分「不属于本组」与「尚未抽题」:前者通常意味着前端残留了上一组的 uid,
// 文案要能直接暴露这类误操作,而不是把它伪装成「考生没抽题」。
const elsewhere = await ctx.model.BigQuestionAnswer.findOne({
where: { aggregation_uid: aggregationUid },
attributes: ['id'],
});
return {
ok: false,
code: 'NOT_FOUND',
message: elsewhere
? '该考生不属于当前组别,无法在本组评分'
: '该考生尚未抽取大题,无法评分',
};
}
// 逐问校验 0–10 整数(分值口径来自题库 score_per_sub,默认 10)
// 逐问校验 0–per 的数值(可含一位小数;per 来自题库 score_per_sub,默认 10)
const per = await this.resolveScorePerSub(group, record);
const parsed = [];
for (let i = 0; i < SUB_QUESTION_COUNT; i++) {
@@ -332,11 +360,13 @@ class BigQuestionService extends Service {
}
const n = Number(raw);
if (!Number.isFinite(n) || n < 0 || n > per) {
return { ok: false, code: 'INVALID_SCORE', message: `第 ${i + 1} 小问得分须为 0-${per} 的数值` };
return { ok: false, code: 'INVALID_SCORE', message: `第 ${i + 1} 小问得分须为 0-${per} 的数值(可含一位小数)` };
}
parsed.push(Math.round(n));
// 2026-09-23:人工评分允许小数点后一位 ⇒ 收敛到 1 位小数,⛔ 不可用 Math.round 抹成整数
parsed.push(Math.round(n * 10) / 10);
}
const total = parsed.reduce((a, b) => a + b, 0);
// 浮点求和再收敛一次,避免 0.1+0.2=0.30000000000000004 之类脏值落库/回显
const total = Math.round(parsed.reduce((a, b) => a + b, 0) * 10) / 10;
await record.update({
score_1: parsed[0],
@@ -398,6 +428,107 @@ class BigQuestionService extends Service {
}));
}
/**
* 导出大题为 Word(.docx)(2026-09-23 需求:单独 / 批量 / 全部导出中职·高职大题)
*
* 行为:
* · 指定 ids(单个或多个)→ 只导出这些题;
* · 不传 ids → 导出该组**全部**;
* · 命中 1 道 → 直接返回 .docx;
* · 命中多道 → 返回「多个 .docx 打成的一个 .zip」(每道题一个独立 Word 文件,
* 便于分别打印/分发;而不是把多道题塞进一个长文档)。
*
* 只读操作:不写库、不改任何状态。
*
* @param {string} group - 组别(zhongzhi / gaozhi)
* @param {Array<number>|string} [ids] - 大题 ID 列表(数组或逗号分隔字符串)
* @returns {Promise<Object>} { ok:true, fileName, buffer, count, isZip? } 或 { ok:false, code, message }
*/
async exportQuestions(group, ids) {
const { ctx } = this;
if (!this.isEnabledGroup(group)) {
return { ok: false, code: 'GROUP_NOT_ENABLED', message: '该组别未启用综合大题' };
}
const where = { group };
if (ids !== undefined && ids !== null && ids !== '') {
const raw = Array.isArray(ids) ? ids : String(ids).split(',');
const idList = raw
.map(v => Number(String(v).trim()))
.filter(v => Number.isInteger(v) && v > 0);
if (!idList.length) {
return { ok: false, code: 'INVALID_IDS', message: '导出的大题 ID 列表非法' };
}
// Sequelize:数组值自动生成 IN (...) 条件,无需 Op
where.id = idList;
}
const rows = await ctx.model.BigQuestion.findAll({
where,
order: [['sort_order', 'ASC'], ['id', 'ASC']],
});
if (!rows.length) {
return { ok: false, code: 'NOT_FOUND', message: '该组别下没有可导出的大题' };
}
const docx = require('../lib/docx');
const groupLabel = GROUP_LABELS[group] || group;
const files = [];
for (const r of rows) {
const blocks = [];
// 抬头
blocks.push({
type: 'p',
align: 'center',
runs: [{ text: r.title || '(无标题)', bold: true, size: 32 }],
});
blocks.push({
type: 'p',
runs: [{
text: `组别:${groupLabel}  分值:每小题 ${r.score_per_sub} 分 × ${SUB_QUESTION_COUNT} 小题 = ${r.total_score} 分`,
bold: false,
}],
});
// 题干(富文本 HTML → 段落 + 真实 Word 表格)
blocks.push({ type: 'p', runs: [] });
blocks.push({ type: 'p', runs: [{ text: '【题目】', bold: true, size: 26 }] });
blocks.push(...docx.htmlToBlocks(r.description || ''));
// 三小问
for (let i = 1; i <= SUB_QUESTION_COUNT; i++) {
blocks.push({ type: 'p', runs: [] });
blocks.push({
type: 'p',
runs: [{ text: `【第 ${i} 小问】(${r.score_per_sub} 分)`, bold: true, size: 26 }],
});
blocks.push(...docx.textToBlocks(r[`sub_prompt_${i}`] || ''));
}
const buffer = await docx.buildDocx(blocks);
// 文件名用 question_uid(稳定标识)而非标题:标题可能含空格/标点,且允许重名
files.push({
name: `${groupLabel}_${safeFilePart(r.question_uid || `big_${r.id}`)}.docx`,
buffer,
});
}
if (files.length === 1) {
return { ok: true, fileName: files[0].name, buffer: files[0].buffer, count: 1 };
}
const buffer = await docx.zipDocxFiles(files);
return {
ok: true,
isZip: true,
fileName: `${groupLabel}_综合大题_${files.length}道.zip`,
buffer,
count: files.length,
};
}
/**
* 新建大题
* @param {string} group
@@ -533,6 +664,8 @@ class BigQuestionService extends Service {
title,
description,
subPrompts,
// 每小题分值 = 赛制口径(每问 10 分、大题 30 分),刻意保持整数;
// 人工评分的小数精度只作用于「考生得分」,见 setSubScores。
scorePerSub: Math.round(scorePerSub),
sortOrder,
status,