文档部署到 /ny-exam-platform 子路径 + 移除快照/趋势/异常 + 打印与 UI 全面完善

文档站(#88)
- base 默认改为 /ny-exam-platform/,可用 DOCS_BASE=/ 覆盖本地预览
- 新增 DOCS_OUT 环境变量,避免构建时清空输出目录失败导致产物半截
- 新增 tools/docs-preview.js:按子路径重写路由的本地预览服务
- 删除 docs/guide/snapshot.md,同步清理全部相关文档

移除存快照 / 趋势 / 异常(#89)
- 后端:删除 api.php 的 snapshot_save/list/delete/trend 四个 action 与
  $WRITE_ACTIONS 中的同名条目;删除 lib/Snapshot.php 与自动加载注册;
  DataService 移除 detectAnomalies() 及其调用
- 前端:移除三个按钮与三个弹窗、相关 JS 处理与 state 字段
- 测试:移除对应用例,补充 5 条名单导入列识别断言(17+9+18+32+29+11 全过)

打印完善(#90)
- 改用隐藏 iframe 输出,规避弹窗拦截;@page A4 支持纵向/横向
- 表头每页重复、行内不跨页断开、全表连续名次、可选「是否合格」列
- 分组小计(人数/平均/最高/最低/合格人数与合格率)、可附缺考名单另起一页
- 末尾签字栏;缺考名单打印改为同一套输出

UI / 交互 / Bug 修复(#91)
- dashboard.php:补「← 主系统 / 退出」入口(此前全屏后无法离开);
  修复成员下钻 onclick 里 JSON.stringify 双引号提前闭合属性导致点击报错,
  改为 data-sid 事件委托;补 Esc 关弹窗;后台标签页暂停轮询、切回自动补拉;
  刷新中断原因写入 title;数值字段统一兜底,避免 NaN / undefined%
- draw.php / draw.js:结果表新增「登陆账号」列(导出前可核对,缺失显示 学号 ⚠);
  弹窗只关最上层且支持 Esc;「全部」改为跟随候选池,手工改人数即解除;
  候选池不足时给出明确提示;自定义比例框隐藏时清空,避免旧权重被误读
- app.js / dashboard.php:成绩着色改为按「占本卷满分的百分比」判断,
  修复满分不是 100 时整列误标红
- style.css:数值列右对齐;新增 subbar、draw-stage-sub;
  看板顶栏窄屏下拉与按钮换行,消除横向滚动
This commit is contained in:
2026-09-11 16:23:08 +08:00
parent f271066266
commit 25a2e8a42c
30 changed files with 700 additions and 883 deletions
-87
View File
@@ -356,9 +356,6 @@ class DataService
'attempts_total'=> $attemptsTotal,
];
// 异常检测(防作弊线索,仅供教师人工判断,不做自动判定)
$anomalies = self::detectAnomalies($rows, $stats);
return [
'project' => [
'id' => ny_str($project['id'] ?? $projectId),
@@ -372,7 +369,6 @@ class DataService
'absent' => $absent,
'unmatched' => $unmatched,
'stats' => $stats,
'anomalies' => $anomalies,
'roster' => $students,
'roster_count' => count($students),
// 本卷独立配置(前端据此回显满分/合格率/免考);full_score 已按本卷解析
@@ -385,89 +381,6 @@ class DataService
];
}
/**
* 异常检测(防作弊线索):基于聚合后的学生行,输出三类线索供教师人工复核。
* 1) 短时长高得分:用时远低于平均(<平均 40% 且 <180s)却接近满分;
* 2) 高频重考:考试次数明显多于多数人(>1 且 >= 平均次数+3);
* 3) 雷同成绩:多人分数完全相同(疑似互抄,需满足该分数下人数 >=3)。
*
* 返回值:['fast_high'=>[], 'frequent'=>[], 'identical'=>[['score'=>x,'count'=>n,'names'=>[...]]]]
*/
public static function detectAnomalies(array $rows, array $stats): array
{
$avgDur = (int)($stats['avg_duration'] ?? 0);
$fullHint = max(1, (float)($stats['max_score'] ?? 100));
$fastHigh = [];
$frequent = [];
$attemptsAll = [];
foreach ($rows as $r) {
$dur = $r['duration'] ?? null;
$score = $r['score'] ?? null;
$attempts = (int)($r['attempts'] ?? 1);
$attemptsAll[] = $attempts;
// 短时长高得分:有时长记录、明显快于平均、分数 >= 满分 85%
if ($dur !== null && $avgDur > 0 && $dur < max(60, (int)($avgDur * 0.4))
&& $score !== null && $score >= $fullHint * 0.85) {
$fastHigh[] = [
'sid' => (string)($r['sid'] ?? ''),
'name' => (string)($r['name'] ?? ''),
'cls' => (string)($r['cls'] ?? ''),
'score' => $score,
'duration' => $dur,
'duration_txt' => (string)($r['duration_txt'] ?? ''),
];
}
// 高频重考:考试次数明显偏多
if ($attempts > 1) {
$frequent[] = [
'sid' => (string)($r['sid'] ?? ''),
'name' => (string)($r['name'] ?? ''),
'cls' => (string)($r['cls'] ?? ''),
'attempts' => $attempts,
'best_score' => $r['best_score'] ?? null,
];
}
}
// 高频重考阈值:平均次数 + 3(且至少 4 次才够"高频")
$avgAttempts = $attemptsAll !== [] ? array_sum($attemptsAll) / count($attemptsAll) : 0;
$freqThreshold = max(4, (int)ceil($avgAttempts + 3));
$frequent = array_values(array_filter($frequent, static fn($f) => $f['attempts'] >= $freqThreshold));
usort($frequent, static fn($a, $b) => $b['attempts'] <=> $a['attempts']);
// 雷同成绩:按分数分组,出现 >=3 人同分(且分数不为 null)视为疑似
$scoreGroups = [];
foreach ($rows as $r) {
$score = $r['score'] ?? null;
if ($score === null) {
continue;
}
$key = (string)$score;
$scoreGroups[$key][] = [
'sid' => (string)($r['sid'] ?? ''),
'name' => (string)($r['name'] ?? ''),
'cls' => (string)($r['cls'] ?? ''),
];
}
$identical = [];
foreach ($scoreGroups as $score => $members) {
if (count($members) >= 3) {
$identical[] = ['score' => (float)$score, 'count' => count($members), 'members' => $members];
}
}
usort($identical, static fn($a, $b) => $b['count'] <=> $a['count']);
return [
'fast_high' => $fastHigh,
'frequent' => $frequent,
'identical' => $identical,
];
}
/**
* 把聚合后的学生行展开为"每次考试一行",用于导出全部考试记录 / 明细内全部展示。
* 每行携带该次考试的成绩、提交时间、时长,以及该学生的总考试次数 attempts 与第几次 idx。
-164
View File
@@ -1,164 +0,0 @@
<?php
/**
* 成绩快照:把某次「拉取到的成绩」落盘成一份带标签的时间点存档,
* 用于跨时间 / 跨试卷的趋势对比(例如某学生几次考试的分数变化)。
*
* 存储位置:data/snapshots/<projectId>/<unix秒>_<md5(标签)>.json
* 每份快照内容为 { label, at, project:{id,name}, stats, students:[{sid,name,cls,grp,score,best_score,attempts,last_submit_at}] }
*/
declare(strict_types=1);
final class Snapshot
{
private static function dir(string $projectId): string
{
$d = NY_DATA . '/snapshots/' . preg_replace('/[^A-Za-z0-9_-]/', '_', $projectId);
if (!is_dir($d)) {
@mkdir($d, 0777, true);
}
return $d;
}
/** 从聚合结果里抽取轻量学生摘要(只保留趋势对比需要的字段) */
public static function summarize(array $result, string $label): array
{
$students = [];
foreach ((array)($result['rows'] ?? []) as $r) {
$students[] = [
'sid' => (string)($r['sid'] ?? ''),
'name' => (string)($r['name'] ?? ''),
'cls' => (string)($r['cls'] ?? ''),
'grp' => (string)($r['grp'] ?? ''),
'score' => ($r['score'] ?? null) === null ? null : (float)$r['score'],
'best_score' => ($r['best_score'] ?? null) === null ? null : (float)$r['best_score'],
'attempts' => (int)($r['attempts'] ?? 1),
'last_submit_at' => (string)($r['last_submit_at'] ?? ($r['submit_at'] ?? '-')),
];
}
// 按学号排序,保证趋势表行序稳定
usort($students, static function ($a, $b): int {
$sa = $a['sid']; $sb = $b['sid'];
if (is_numeric($sa) && is_numeric($sb)) {
return $sa <=> $sb;
}
return strnatcmp((string)$sa, (string)$sb);
});
return [
'label' => trim($label) === '' ? '快照 ' . date('m-d H:i') : trim($label),
'at' => time(),
'at_txt' => date('Y-m-d H:i:s'),
'project' => [
'id' => (string)($result['project']['id'] ?? ''),
'name' => (string)($result['project']['name'] ?? ''),
],
'stats' => $result['stats'] ?? [],
'students' => $students,
];
}
/** 保存一份快照,返回文件名 */
public static function save(string $projectId, array $snap): string
{
$dir = self::dir($projectId);
$name = time() . '_' . substr(md5($snap['label'] . $snap['at']), 0, 8) . '.json';
@file_put_contents($dir . '/' . $name, json_encode($snap, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), LOCK_EX);
return $name;
}
/** 列出某项目的全部快照(按时间倒序) */
public static function list(string $projectId): array
{
$dir = self::dir($projectId);
$out = [];
foreach (glob($dir . '/*.json') ?: [] as $f) {
$raw = @file_get_contents($f);
if ($raw === false) {
continue;
}
$j = json_decode($raw, true);
if (!is_array($j)) {
continue;
}
$out[] = [
'file' => basename($f),
'label' => (string)($j['label'] ?? basename($f)),
'at' => (int)($j['at'] ?? 0),
'at_txt' => (string)($j['at_txt'] ?? ''),
'project' => $j['project'] ?? [],
'count' => count((array)($j['students'] ?? [])),
];
}
usort($out, static fn($a, $b) => ($b['at'] <=> $a['at']));
return $out;
}
/** 读取单份快照完整内容 */
public static function get(string $projectId, string $file): ?array
{
$file = basename($file); // 防路径穿越
$path = self::dir($projectId) . '/' . $file;
if (!is_file($path)) {
return null;
}
$j = json_decode((string)@file_get_contents($path), true);
return is_array($j) ? $j : null;
}
/** 删除单份快照 */
public static function delete(string $projectId, string $file): bool
{
$file = basename($file);
$path = self::dir($projectId) . '/' . $file;
if (is_file($path)) {
return @unlink($path);
}
return false;
}
/**
* 跨快照趋势:以学号为行、各快照为列,输出每个学生在每份快照里的成绩。
* 用 best_score(真实最高分)作为趋势值,未出现记 null。
*/
public static function trend(array $snapshots): array
{
// 按时间升序排,最早的列在最前
$snaps = $snapshots;
usort($snaps, static fn($a, $b) => ($a['at'] <=> $b['at']));
$cols = [];
$bySid = [];
foreach ($snaps as $sn) {
$colKey = (string)$sn['at'] . ':' . (string)$sn['label'];
$cols[] = ['key' => $colKey, 'label' => (string)$sn['label'], 'at_txt' => (string)($sn['at_txt'] ?? '')];
foreach ((array)($sn['students'] ?? []) as $st) {
$sid = (string)($st['sid'] ?? '');
if ($sid === '') {
continue;
}
if (!isset($bySid[$sid])) {
$bySid[$sid] = [
'sid' => $sid,
'name' => (string)($st['name'] ?? ''),
'cls' => (string)($st['cls'] ?? ''),
'scores' => [],
];
}
$bySid[$sid]['scores'][$colKey] = ($st['best_score'] ?? null) === null
? null
: (float)$st['best_score'];
}
}
$rows = array_values($bySid);
// 按学号排序
usort($rows, static function ($a, $b): int {
if (is_numeric($a['sid']) && is_numeric($b['sid'])) {
return $a['sid'] <=> $b['sid'];
}
return strnatcmp((string)$a['sid'], (string)$b['sid']);
});
return ['columns' => $cols, 'rows' => $rows];
}
}