report_teacher_weekly_stats.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. /**
  3. * 按「自然日」统计:近 N 个自然日(今日为 0 点—生成时刻)vs 紧邻的前 N 个完整自然日;exam_analysis_results 按 paper_id 去重计套。
  4. * 按老师:学案/分析/学生数为「本 / 上」并列;保留学案·环比、学情·环比。
  5. * 用法:
  6. * php scripts/report_teacher_weekly_stats.php
  7. * TEACHER_WEEKLY_REPORT_DAYS=14 php scripts/report_teacher_weekly_stats.php
  8. * php scripts/report_teacher_weekly_stats.php > storage/app/reports/teacher-weekly-stats-$(date +%Y-%m-%d)_$(date +%H%M%S).md
  9. *
  10. * 环境变量 / artisan:TEACHER_WEEKLY_REPORT_DAYS(默认 7)= 本/上周期各包含几个自然日。时区:config app.timezone。
  11. */
  12. require __DIR__ . '/../vendor/autoload.php';
  13. $app = require_once __DIR__ . '/../bootstrap/app.php';
  14. $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
  15. $tz = (string) config('app.timezone', 'UTC');
  16. $reportPeriodDays = 7;
  17. $envDays = getenv('TEACHER_WEEKLY_REPORT_DAYS');
  18. if ($envDays !== false && $envDays !== '') {
  19. $reportPeriodDays = max(1, min(366, (int) $envDays));
  20. }
  21. /** 本周期:自「今日 0 点」往回数第 N 天 0 点起,至当前时刻。上周期:紧邻的前 N 个完整自然日。 */
  22. $endCurrent = now()->timezone($tz);
  23. $todayStart = $endCurrent->copy()->startOfDay();
  24. $startCurrent = $todayStart->copy()->subDays($reportPeriodDays - 1);
  25. $startPrev = $todayStart->copy()->subDays(2 * $reportPeriodDays - 1);
  26. $endPrev = $todayStart->copy()->subDays($reportPeriodDays - 1);
  27. $db = \Illuminate\Support\Facades\DB::class;
  28. $sumPapers = static function ($from, $toExclusive) use ($db) {
  29. $q = $db::table('papers')
  30. ->whereNotNull('teacher_id')
  31. ->where('teacher_id', '!=', '')
  32. ->where('created_at', '>=', $from);
  33. if ($toExclusive !== null) {
  34. $q->where('created_at', '<', $toExclusive);
  35. }
  36. return (int) $q->count();
  37. };
  38. /** 学情分析:以卷子为单位,同一 paper_id 在区间内多条记录只计 1 */
  39. $countDistinctAnalysisPapers = static function ($from, $toExclusive) use ($db) {
  40. $q = $db::table('exam_analysis_results as ear')
  41. ->join('papers as p', 'p.paper_id', '=', 'ear.paper_id')
  42. ->whereNotNull('p.teacher_id')
  43. ->where('p.teacher_id', '!=', '')
  44. ->where('ear.created_at', '>=', $from);
  45. if ($toExclusive !== null) {
  46. $q->where('ear.created_at', '<', $toExclusive);
  47. }
  48. return (int) $q->distinct()->count('ear.paper_id');
  49. };
  50. $countActiveTeachers = static function ($from, $toExclusive) use ($db) {
  51. $q = $db::table('papers')
  52. ->whereNotNull('teacher_id')
  53. ->where('teacher_id', '!=', '')
  54. ->where('created_at', '>=', $from);
  55. if ($toExclusive !== null) {
  56. $q->where('created_at', '<', $toExclusive);
  57. }
  58. return (int) $q->distinct()->count('teacher_id');
  59. };
  60. $totalPapersCur = $sumPapers($startCurrent, $endCurrent);
  61. $totalPapersPrev = $sumPapers($startPrev, $endPrev);
  62. $totalAnalysisCur = $countDistinctAnalysisPapers($startCurrent, $endCurrent);
  63. $totalAnalysisPrev = $countDistinctAnalysisPapers($startPrev, $endPrev);
  64. $teachersCur = $countActiveTeachers($startCurrent, $endCurrent);
  65. $teachersPrev = $countActiveTeachers($startPrev, $endPrev);
  66. $wowLine = static function (int $cur, int $prev): string {
  67. $delta = $cur - $prev;
  68. if ($prev === 0) {
  69. if ($cur === 0) {
  70. return '0';
  71. }
  72. return sprintf('+%d(上周期0)', $delta);
  73. }
  74. $pct = round(($delta / $prev) * 100, 2);
  75. $sign = $delta >= 0 ? '+' : '';
  76. $dir = match (true) {
  77. $delta > 0 => '↑',
  78. $delta < 0 => '↓',
  79. default => '→',
  80. };
  81. return sprintf('%s%d(%s%.2f%%)%s', $sign, $delta, $sign, $pct, $dir);
  82. };
  83. /** 总量表环比列:正增长着色(学案蓝 / 学情橙 / 其余绿) */
  84. $wowLineHtml = static function (int $cur, int $prev, string $posColor = '#16a34a') use ($wowLine): string {
  85. $plain = $wowLine($cur, $prev);
  86. if ($cur > $prev) {
  87. return '<span style="color:'.$posColor.';font-weight:600;">'.$plain.'</span>';
  88. }
  89. return $plain;
  90. };
  91. /** 环比列:正增长用 $posColor(学案蓝 / 学情橙 / 默认绿) */
  92. $compareCellHtml = static function (int $cur, int $prev, string $posColor = '#16a34a'): string {
  93. $d = $cur - $prev;
  94. if ($d === 0) {
  95. return '0';
  96. }
  97. if ($prev === 0) {
  98. if ($cur === 0) {
  99. return '0';
  100. }
  101. return '<span style="color:'.$posColor.';font-weight:600;">+'.$d.'(上0)</span>';
  102. }
  103. $pct = round(($d / $prev) * 100, 1);
  104. $sign = $d > 0 ? '+' : '';
  105. $text = sprintf('%s%d(%s%.1f%%)', $sign, $d, $sign, $pct);
  106. if ($d > 0) {
  107. return '<span style="color:'.$posColor.';font-weight:600;">'.$text.'</span>';
  108. }
  109. return $text;
  110. };
  111. /** 「本 / 上」并列:本>上时本侧用强调色(与上方左/右图折线一致);「 / 」与上周期数字固定深灰黑 */
  112. $slashPairAccentHtml = static function (int $cur, int $prev, string $accent): string {
  113. $rest = '<span style="color:#111827;font-weight:normal;"> / '.$prev.'</span>';
  114. if ($cur > $prev) {
  115. return '<span style="color:'.$accent.';font-weight:700;">'.$cur.'</span>'.$rest;
  116. }
  117. return '<span style="color:#111827;font-weight:normal;">'.$cur.'</span>'.$rest;
  118. };
  119. /** 与每日对比图一致:学案蓝、学情橙、学生绿 */
  120. $colorPapers = '#2563eb';
  121. $colorAnalysis = '#ea580c';
  122. $colorStudents = '#16a34a';
  123. $byTeacher = \Illuminate\Support\Facades\DB::table('papers')
  124. ->whereNotNull('teacher_id')
  125. ->where('teacher_id', '!=', '')
  126. ->where('created_at', '>=', $startCurrent)
  127. ->where('created_at', '<', $endCurrent)
  128. ->selectRaw('teacher_id, COUNT(*) as paper_count')
  129. ->groupBy('teacher_id')
  130. ->get();
  131. // 本时间窗内学情:按 ear.paper_id 去重后归到 papers.teacher_id
  132. $analysisByTeacher = \Illuminate\Support\Facades\DB::table('exam_analysis_results as ear')
  133. ->join('papers as p', 'p.paper_id', '=', 'ear.paper_id')
  134. ->whereNotNull('p.teacher_id')
  135. ->where('p.teacher_id', '!=', '')
  136. ->where('ear.created_at', '>=', $startCurrent)
  137. ->where('ear.created_at', '<', $endCurrent)
  138. ->selectRaw('p.teacher_id, COUNT(DISTINCT ear.paper_id) AS paper_set_count')
  139. ->groupBy('p.teacher_id')
  140. ->get();
  141. $analysisMap = [];
  142. foreach ($analysisByTeacher as $r) {
  143. $analysisMap[(string) $r->teacher_id] = (int) $r->paper_set_count;
  144. }
  145. $paperMap = [];
  146. foreach ($byTeacher as $r) {
  147. $paperMap[(string) $r->teacher_id] = (int) $r->paper_count;
  148. }
  149. $byTeacherPrev = $db::table('papers')
  150. ->whereNotNull('teacher_id')
  151. ->where('teacher_id', '!=', '')
  152. ->where('created_at', '>=', $startPrev)
  153. ->where('created_at', '<', $endPrev)
  154. ->selectRaw('teacher_id, COUNT(*) as paper_count')
  155. ->groupBy('teacher_id')
  156. ->get();
  157. $analysisByTeacherPrev = $db::table('exam_analysis_results as ear')
  158. ->join('papers as p', 'p.paper_id', '=', 'ear.paper_id')
  159. ->whereNotNull('p.teacher_id')
  160. ->where('p.teacher_id', '!=', '')
  161. ->where('ear.created_at', '>=', $startPrev)
  162. ->where('ear.created_at', '<', $endPrev)
  163. ->selectRaw('p.teacher_id, COUNT(DISTINCT ear.paper_id) AS paper_set_count')
  164. ->groupBy('p.teacher_id')
  165. ->get();
  166. $paperMapPrev = [];
  167. foreach ($byTeacherPrev as $r) {
  168. $paperMapPrev[(string) $r->teacher_id] = (int) $r->paper_count;
  169. }
  170. $analysisMapPrev = [];
  171. foreach ($analysisByTeacherPrev as $r) {
  172. $analysisMapPrev[(string) $r->teacher_id] = (int) $r->paper_set_count;
  173. }
  174. /** 学生数:组卷 ∪ 学情,student_id 合并去重(按老师、时间窗) */
  175. $studentUnionSql = <<<'SQL'
  176. SELECT u.teacher_id, COUNT(DISTINCT u.student_id) AS c
  177. FROM (
  178. SELECT teacher_id, student_id FROM papers
  179. WHERE teacher_id IS NOT NULL AND teacher_id != ''
  180. AND student_id IS NOT NULL AND student_id != ''
  181. AND created_at >= ? AND created_at < ?
  182. UNION
  183. SELECT p.teacher_id, ear.student_id
  184. FROM exam_analysis_results ear
  185. INNER JOIN papers p ON p.paper_id = ear.paper_id
  186. WHERE p.teacher_id IS NOT NULL AND p.teacher_id != ''
  187. AND ear.student_id IS NOT NULL AND ear.student_id != ''
  188. AND ear.created_at >= ? AND ear.created_at < ?
  189. ) u
  190. GROUP BY u.teacher_id
  191. SQL;
  192. $studentUnionCurRows = $db::select($studentUnionSql, [$startCurrent, $endCurrent, $startCurrent, $endCurrent]);
  193. $studentUnionPrevRows = $db::select($studentUnionSql, [$startPrev, $endPrev, $startPrev, $endPrev]);
  194. $studentUnionCurMap = [];
  195. foreach ($studentUnionCurRows as $row) {
  196. $studentUnionCurMap[(string) $row->teacher_id] = (int) $row->c;
  197. }
  198. $studentUnionPrevMap = [];
  199. foreach ($studentUnionPrevRows as $row) {
  200. $studentUnionPrevMap[(string) $row->teacher_id] = (int) $row->c;
  201. }
  202. $names = \Illuminate\Support\Facades\DB::table('teachers')->pluck('name', 'teacher_id');
  203. $nameStrMap = [];
  204. foreach ($names as $tid => $nm) {
  205. $nameStrMap[(string) $tid] = $nm;
  206. }
  207. $rows = [];
  208. foreach ($paperMap as $tid => $paperCount) {
  209. $rows[] = [
  210. 'teacher_id' => $tid,
  211. 'name' => (string) ($nameStrMap[$tid] ?? $tid),
  212. 'papers' => $paperCount,
  213. 'analysis_sets' => (int) ($analysisMap[$tid] ?? 0),
  214. 'papers_prev' => (int) ($paperMapPrev[$tid] ?? 0),
  215. 'analysis_sets_prev' => (int) ($analysisMapPrev[$tid] ?? 0),
  216. ];
  217. }
  218. usort($rows, static fn ($a, $b) => $b['papers'] <=> $a['papers']);
  219. $windowCur = sprintf(
  220. '%s ~ %s',
  221. $startCurrent->format('Y-m-d H:i:s'),
  222. $endCurrent->format('Y-m-d H:i:s')
  223. );
  224. $windowPrev = sprintf(
  225. '%s ~ %s',
  226. $startPrev->format('Y-m-d H:i:s'),
  227. $endPrev->format('Y-m-d H:i:s')
  228. );
  229. $generatedAt = $endCurrent->format('Y-m-d H:i:s');
  230. /**
  231. * 按自然日切段(app.timezone):每天一格;本周期末段为「今日 0 点—当前时刻」。
  232. * 学案:当日创建的组卷数,各日之和 = 窗口总量。
  233. * 学情:该自然日内有 analysis 记录的卷去重(同日多条只计 1);跨日同一卷可在多日重复出现,故各日之和可不等于上方「窗口内卷仅计一次」的总量。
  234. *
  235. * @return array{labels: list<string>, papers: list<int>, analysis: list<int>}
  236. */
  237. $dailyNaturalSlices = static function (string $which) use ($db, $reportPeriodDays, $todayStart, $endCurrent): array {
  238. $n = $reportPeriodDays;
  239. $labels = [];
  240. $papers = [];
  241. $analysis = [];
  242. $countAnalysisSetsInRange = static function ($from, $toExclusive) use ($db): int {
  243. return (int) $db::table('exam_analysis_results as ear')
  244. ->join('papers as p', 'p.paper_id', '=', 'ear.paper_id')
  245. ->whereNotNull('p.teacher_id')
  246. ->where('p.teacher_id', '!=', '')
  247. ->where('ear.created_at', '>=', $from)
  248. ->where('ear.created_at', '<', $toExclusive)
  249. ->distinct()
  250. ->count('ear.paper_id');
  251. };
  252. if ($which === 'current') {
  253. for ($i = 0; $i < $n; $i++) {
  254. $dayStart = $todayStart->copy()->subDays($n - 1 - $i);
  255. $dayEnd = ($i < $n - 1) ? $dayStart->copy()->addDay() : $endCurrent;
  256. $labels[] = $dayStart->format('j');
  257. $papers[] = (int) $db::table('papers')
  258. ->whereNotNull('teacher_id')
  259. ->where('teacher_id', '!=', '')
  260. ->where('created_at', '>=', $dayStart)
  261. ->where('created_at', '<', $dayEnd)
  262. ->count();
  263. $analysis[] = $countAnalysisSetsInRange($dayStart, $dayEnd);
  264. }
  265. } else {
  266. for ($i = 0; $i < $n; $i++) {
  267. $dayStart = $todayStart->copy()->subDays(2 * $n - 1 - $i);
  268. $dayEnd = $dayStart->copy()->addDay();
  269. $labels[] = $dayStart->format('j');
  270. $papers[] = (int) $db::table('papers')
  271. ->whereNotNull('teacher_id')
  272. ->where('teacher_id', '!=', '')
  273. ->where('created_at', '>=', $dayStart)
  274. ->where('created_at', '<', $dayEnd)
  275. ->count();
  276. $analysis[] = $countAnalysisSetsInRange($dayStart, $dayEnd);
  277. }
  278. }
  279. return ['labels' => $labels, 'papers' => $papers, 'analysis' => $analysis];
  280. };
  281. $curDaily = $dailyNaturalSlices('current');
  282. $prevDaily = $dailyNaturalSlices('prev');
  283. /** 左侧:学案(组卷)套数;右侧:学情分析套数(卷去重)。横轴:本周期日 / 上周期同序号日。 */
  284. $buildDualChartsHtml = static function (array $curDaily, array $prevDaily): string {
  285. $axisCur = $curDaily['labels'];
  286. $axisPrev = $prevDaily['labels'];
  287. $pc = $curDaily['papers'];
  288. $ac = $curDaily['analysis'];
  289. $pp = $prevDaily['papers'];
  290. $ap = $prevDaily['analysis'];
  291. $halfSvg = static function (
  292. array $labelsCur,
  293. array $labelsPrev,
  294. array $cur,
  295. array $prev,
  296. string $strokeCur,
  297. string $strokePrev,
  298. string $legCur,
  299. string $legPrev
  300. ): string {
  301. $maxY = max(1, ...$cur, ...$prev);
  302. $W = 296;
  303. $H = 256;
  304. $padL = 42;
  305. /** 右侧、顶部留白:数值标签 text-anchor=middle 时不依赖末列特殊锚点 */
  306. $padR = 62;
  307. $padT = 72;
  308. $padB = 56;
  309. $gw = $W - $padL - $padR;
  310. $gh = $H - $padT - $padB;
  311. $n = max(1, count($labelsCur));
  312. $xAt = static function (int $i) use ($padL, $gw, $n): float {
  313. return $padL + ($n <= 1 ? $gw / 2 : $gw * $i / ($n - 1));
  314. };
  315. $yAt = static function (int $v) use ($padT, $gh, $maxY): float {
  316. return $padT + $gh - ($v / $maxY) * $gh;
  317. };
  318. $lineWithDots = static function (array $vals, string $stroke, string $dash, float $sw = 1.6) use ($xAt, $yAt, $n): string {
  319. $pts = [];
  320. $circles = '';
  321. for ($i = 0; $i < $n; $i++) {
  322. $x = $xAt($i);
  323. $y = $yAt((int) $vals[$i]);
  324. $pts[] = round($x, 1).','.round($y, 1);
  325. $circles .= sprintf(
  326. '<circle cx="%.1f" cy="%.1f" r="3.2" fill="%s" stroke="#fff" stroke-width="1"/>',
  327. $x,
  328. $y,
  329. $stroke
  330. );
  331. }
  332. $poly = implode(' ', $pts);
  333. $dashAttr = $dash !== '' ? ' stroke-dasharray="'.$dash.'"' : '';
  334. return '<polyline fill="none" stroke="'.$stroke.'" stroke-width="'.$sw.'"'.$dashAttr.' points="'.$poly.'" />'.$circles;
  335. };
  336. /** 标签:每列只放一条「本/上」,y = 最高点y - 固定偏移(以 dominant-baseline=middle 作为中心y) */
  337. $labelFillCur = $strokeCur === '#2563eb' ? '#1d4ed8' : '#c2410c';
  338. $labelFillPrev = $strokePrev === '#93c5fd' ? '#475569' : '#78716c';
  339. $valueLabels = '<g font-family="sun-exta,sans-serif">';
  340. $labelOffset = 22; // 标签中心到点的固定上移像素
  341. for ($vi = 0; $vi < $n; $vi++) {
  342. $xv = $xAt($vi);
  343. $yCv = $yAt((int) $cur[$vi]);
  344. $yPv = $yAt((int) $prev[$vi]);
  345. $vC = (int) $cur[$vi];
  346. $vP = (int) $prev[$vi];
  347. $yUpper = min($yCv, $yPv);
  348. // 允许跑到绘图区上方的留白里;只在极端情况下防止贴到 SVG 顶边
  349. $baseY = max(14, $yUpper - $labelOffset);
  350. $valueLabels .= sprintf(
  351. '<text x="%.1f" y="%.1f" text-anchor="middle" dominant-baseline="middle" font-size="9" font-family="sun-exta,sans-serif"><tspan fill="%s">%d</tspan><tspan fill="#94a3b8">/</tspan><tspan fill="%s">%d</tspan></text>',
  352. $xv,
  353. $baseY,
  354. $labelFillCur,
  355. $vC,
  356. $labelFillPrev,
  357. $vP
  358. );
  359. }
  360. $valueLabels .= '</g>';
  361. $xAxisY = $padT + $gh;
  362. $tickTxt = '';
  363. $axisRowY = $H - 48;
  364. for ($i = 0; $i < $n; $i++) {
  365. $x = $xAt($i);
  366. $lc = htmlspecialchars((string) ($labelsCur[$i] ?? ''), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  367. $lp = htmlspecialchars((string) ($labelsPrev[$i] ?? ''), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  368. $tickTxt .= sprintf(
  369. '<text x="%.1f" y="%d" text-anchor="middle" font-size="8" font-family="sun-exta,sans-serif"><tspan fill="#111827">%s</tspan><tspan fill="#94a3b8">/</tspan><tspan fill="#475569">%s</tspan></text>',
  370. $x,
  371. $axisRowY,
  372. $lc,
  373. $lp
  374. );
  375. }
  376. $yTick = '';
  377. for ($k = 0; $k <= 4; $k++) {
  378. $v = (int) round($maxY * $k / 4);
  379. $y = $yAt($v);
  380. $yTick .= sprintf(
  381. '<line x1="%d" y1="%.1f" x2="%d" y2="%.1f" stroke="#e5e7eb" stroke-width="1"/>',
  382. $padL,
  383. $y,
  384. $padL + $gw,
  385. $y
  386. );
  387. $yTick .= sprintf(
  388. '<text x="%d" y="%.1f" font-size="8" fill="#6b7280" font-family="sun-exta,sans-serif">%d</text>',
  389. 2,
  390. $y + 3,
  391. $v
  392. );
  393. }
  394. $legend = '<g font-family="sun-exta,sans-serif" font-size="9">';
  395. $lx = $padL + 4;
  396. $ly = $H - 26;
  397. $items = [
  398. [$strokeCur, $legCur, ''],
  399. [$strokePrev, $legPrev, '6,4'],
  400. ];
  401. foreach ($items as $idx => $it) {
  402. $yy = $ly + $idx * 12;
  403. $legend .= sprintf(
  404. '<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="2" %s/>',
  405. $lx,
  406. $yy,
  407. $lx + 16,
  408. $yy,
  409. $it[0],
  410. $it[2] !== '' ? 'stroke-dasharray="'.$it[2].'"' : ''
  411. );
  412. $legend .= sprintf(
  413. '<text x="%d" y="%d" fill="#111">%s</text>',
  414. $lx + 20,
  415. $yy + 4,
  416. htmlspecialchars($it[1], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
  417. );
  418. }
  419. $legend .= '</g>';
  420. $svg = sprintf(
  421. '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 %d %d" width="100%%" height="auto" style="max-width:100%%;">',
  422. $W,
  423. $H
  424. );
  425. $svg .= sprintf('<rect x="0" y="0" width="%d" height="%d" fill="#fafafa"/>', $W, $H);
  426. $svg .= $yTick;
  427. $svg .= sprintf('<line x1="%d" y1="%.1f" x2="%.1f" y2="%.1f" stroke="#9ca3af" stroke-width="1"/>', $padL, $xAxisY, $padL + $gw, $xAxisY);
  428. $svg .= $lineWithDots($prev, $strokePrev, '6,4', 1.4);
  429. $svg .= $lineWithDots($cur, $strokeCur, '', 1.8);
  430. $svg .= $valueLabels;
  431. $svg .= $tickTxt;
  432. $svg .= $legend;
  433. $svg .= '</svg>';
  434. return $svg;
  435. };
  436. $left = $halfSvg($axisCur, $axisPrev, $pc, $pp, '#2563eb', '#93c5fd', '学案·本周期', '学案·上周期');
  437. $right = $halfSvg($axisCur, $axisPrev, $ac, $ap, '#ea580c', '#fdba74', '学情·本周期', '学情·上周期');
  438. return '<table class="weekly-chart-pair" style="width:100%;border-collapse:collapse;margin:0;"><tr>'
  439. .'<td style="width:50%;vertical-align:top;padding:2px 5px 2px 0;">'.$left.'</td>'
  440. .'<td style="width:50%;vertical-align:top;padding:2px 0 2px 5px;">'.$right.'</td>'
  441. .'</tr></table>';
  442. };
  443. $chartSvg = $buildDualChartsHtml($curDaily, $prevDaily);
  444. echo sprintf("## 老师组卷与学情分析(近%d个自然日)\n\n", $reportPeriodDays);
  445. echo "> 生成 {$generatedAt} · {$tz} · 本 {$windowCur} · 上 {$windowPrev}\n";
  446. echo sprintf("> 口径:自然日边界(config timezone);今日段为当日 0:00—生成时刻;上周期为紧邻的前 **%d** 个完整自然日。\n\n", $reportPeriodDays);
  447. echo "### 总量\n\n";
  448. echo "| 指标 | 本周期 | 上周期 | 环比 |\n";
  449. echo "| :---: | :---: | :---: | :---: |\n";
  450. echo sprintf("| 组卷总套数 | %d | %d | %s |\n", $totalPapersCur, $totalPapersPrev, $wowLineHtml($totalPapersCur, $totalPapersPrev, $colorPapers));
  451. echo sprintf("| 学情分析套数(卷去重) | %d | %d | %s |\n", $totalAnalysisCur, $totalAnalysisPrev, $wowLineHtml($totalAnalysisCur, $totalAnalysisPrev, $colorAnalysis));
  452. echo sprintf("| 有组卷老师数 | %d | %d | %s |\n", $teachersCur, $teachersPrev, $wowLineHtml($teachersCur, $teachersPrev, $colorStudents));
  453. echo "### 逐日对比(左:学案 · 右:学情)\n\n";
  454. echo '<div class="weekly-chart">';
  455. echo $chartSvg;
  456. echo "</div>\n\n";
  457. // 老师列表汇总:先给出增/降总体倾向,再给出异常清单
  458. $calcPct = static function (int $delta, int $prev): ?float {
  459. if ($prev <= 0) {
  460. return null;
  461. }
  462. return round(($delta / $prev) * 100, 1);
  463. };
  464. $fmtDeltaWithPct = static function (int $delta, int $prev): string {
  465. $sign = $delta > 0 ? '+' : '';
  466. $pct = $prev > 0 ? sprintf('(%s%.1f%%)', $sign, ($delta / $prev) * 100) : '(上0)';
  467. return $sign.$delta.$pct;
  468. };
  469. $paperUp = 0; $paperDown = 0; $paperFlat = 0;
  470. $analysisUp = 0; $analysisDown = 0; $analysisFlat = 0;
  471. $studentUp = 0; $studentDown = 0; $studentFlat = 0;
  472. $sumPaperDelta = 0; $sumAnalysisDelta = 0; $sumStudentDelta = 0;
  473. $anomalies = [];
  474. foreach ($rows as $r) {
  475. $tidKey = (string) $r['teacher_id'];
  476. $name = (string) $r['name'];
  477. $papersCur = (int) $r['papers'];
  478. $papersPrev = (int) $r['papers_prev'];
  479. $analysisCur = (int) $r['analysis_sets'];
  480. $analysisPrev = (int) $r['analysis_sets_prev'];
  481. $studentCur = (int) ($studentUnionCurMap[$tidKey] ?? 0);
  482. $studentPrev = (int) ($studentUnionPrevMap[$tidKey] ?? 0);
  483. $paperDelta = $papersCur - $papersPrev;
  484. $analysisDelta = $analysisCur - $analysisPrev;
  485. $studentDelta = $studentCur - $studentPrev;
  486. $sumPaperDelta += $paperDelta;
  487. $sumAnalysisDelta += $analysisDelta;
  488. $sumStudentDelta += $studentDelta;
  489. if ($paperDelta > 0) { $paperUp++; } elseif ($paperDelta < 0) { $paperDown++; } else { $paperFlat++; }
  490. if ($analysisDelta > 0) { $analysisUp++; } elseif ($analysisDelta < 0) { $analysisDown++; } else { $analysisFlat++; }
  491. if ($studentDelta > 0) { $studentUp++; } elseif ($studentDelta < 0) { $studentDown++; } else { $studentFlat++; }
  492. $paperPct = $calcPct($paperDelta, $papersPrev);
  493. $analysisPct = $calcPct($analysisDelta, $analysisPrev);
  494. $studentPct = $calcPct($studentDelta, $studentPrev);
  495. // 异常口径(用于快速巡检):
  496. // 1) 学案/学情明显下滑;2) 学案有量但学情为 0;3) 学情明显高于学案;
  497. // 4) 学案与学情方向背离;5) 覆盖学生数异常波动。
  498. if (
  499. ($papersPrev >= 20 && ($paperDelta <= -20 || ($paperPct !== null && $paperPct <= -50.0))) ||
  500. ($analysisPrev >= 15 && ($analysisDelta <= -15 || ($analysisPct !== null && $analysisPct <= -50.0))) ||
  501. ($papersCur >= 10 && $analysisCur === 0) ||
  502. ($analysisCur >= 10 && $analysisCur > $papersCur + 5) ||
  503. (($paperDelta > 0 && $analysisDelta < 0 && abs($analysisDelta) >= 5) ||
  504. ($paperDelta < 0 && $analysisDelta > 0 && abs($paperDelta) >= 5)) ||
  505. (abs($studentDelta) >= 10 || ($studentPct !== null && abs($studentPct) >= 100.0 && $studentPrev >= 5))
  506. ) {
  507. $severity = abs($paperDelta) + abs($analysisDelta) + abs($studentDelta);
  508. if ($papersCur >= 10 && $analysisCur === 0) { $severity += 20; }
  509. if ($analysisCur >= 10 && $analysisCur > $papersCur + 5) { $severity += 12; }
  510. $reason = [];
  511. if ($papersPrev >= 20 && ($paperDelta <= -20 || ($paperPct !== null && $paperPct <= -50.0))) { $reason[] = '学案下滑'; }
  512. if ($analysisPrev >= 15 && ($analysisDelta <= -15 || ($analysisPct !== null && $analysisPct <= -50.0))) { $reason[] = '学情下滑'; }
  513. if ($papersCur >= 10 && $analysisCur === 0) { $reason[] = '学案有量但学情为0'; }
  514. if ($analysisCur >= 10 && $analysisCur > $papersCur + 5) { $reason[] = '学情高于学案'; }
  515. if (($paperDelta > 0 && $analysisDelta < 0) || ($paperDelta < 0 && $analysisDelta > 0)) { $reason[] = '学案学情背离'; }
  516. if (abs($studentDelta) >= 10 || ($studentPct !== null && abs($studentPct) >= 100.0 && $studentPrev >= 5)) { $reason[] = '学生覆盖波动'; }
  517. $anomalies[] = [
  518. 'severity' => $severity,
  519. 'teacher_id' => $tidKey,
  520. 'name' => $name,
  521. 'papers_cur' => $papersCur,
  522. 'papers_prev' => $papersPrev,
  523. 'analysis_cur' => $analysisCur,
  524. 'analysis_prev' => $analysisPrev,
  525. 'student_cur' => $studentCur,
  526. 'student_prev' => $studentPrev,
  527. 'paper_delta_text' => $fmtDeltaWithPct($paperDelta, $papersPrev),
  528. 'analysis_delta_text' => $fmtDeltaWithPct($analysisDelta, $analysisPrev),
  529. 'student_delta_text' => $fmtDeltaWithPct($studentDelta, $studentPrev),
  530. 'reason' => implode(' / ', array_values(array_unique($reason))),
  531. ];
  532. }
  533. }
  534. usort($anomalies, static fn ($a, $b) => $b['severity'] <=> $a['severity']);
  535. $paperTrend = $paperUp > $paperDown ? '学案整体偏增长' : ($paperUp < $paperDown ? '学案整体偏下降' : '学案整体增降持平');
  536. $analysisTrend = $analysisUp > $analysisDown ? '学情整体偏增长' : ($analysisUp < $analysisDown ? '学情整体偏下降' : '学情整体增降持平');
  537. $studentTrend = $studentUp > $studentDown ? '学生覆盖整体偏增长' : ($studentUp < $studentDown ? '学生覆盖整体偏下降' : '学生覆盖整体增降持平');
  538. echo "### 老师列表汇总(增降对比)\n\n";
  539. echo "| 维度 | 增长老师数 | 下降老师数 | 持平老师数 | 判断 |\n";
  540. echo "| :--- | ---: | ---: | ---: | :--- |\n";
  541. echo sprintf("| 学案(组卷) | %d | %d | %d | %s |\n", $paperUp, $paperDown, $paperFlat, $paperTrend);
  542. echo sprintf("| 学情(分析) | %d | %d | %d | %s |\n", $analysisUp, $analysisDown, $analysisFlat, $analysisTrend);
  543. echo sprintf("| 学生覆盖(去重) | %d | %d | %d | %s |\n\n", $studentUp, $studentDown, $studentFlat, $studentTrend);
  544. echo "### 异常数据(学案本/上超过100,文字清单)\n\n";
  545. $highVolumeAnomalies = array_values(array_filter(
  546. $anomalies,
  547. static fn (array $a): bool => ((int) ($a['papers_cur'] ?? 0) > 100) || ((int) ($a['papers_prev'] ?? 0) > 100)
  548. ));
  549. if ($highVolumeAnomalies === []) {
  550. echo "本周期未命中“学案本/上超过100”的异常老师。\n\n";
  551. } else {
  552. $display = array_slice($highVolumeAnomalies, 0, 30);
  553. $idx = 1;
  554. foreach ($display as $a) {
  555. $nameEsc = htmlspecialchars((string) $a['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  556. $tidEsc = htmlspecialchars((string) $a['teacher_id'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  557. $reasonEsc = htmlspecialchars((string) $a['reason'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  558. $line = sprintf(
  559. '%d) %s(%s):学案 %d/%d(%s);学情 %d/%d(%s);学生 %d/%d(%s);异常:%s',
  560. $idx++,
  561. $nameEsc,
  562. $tidEsc,
  563. (int) $a['papers_cur'],
  564. (int) $a['papers_prev'],
  565. (string) $a['paper_delta_text'],
  566. (int) $a['analysis_cur'],
  567. (int) $a['analysis_prev'],
  568. (string) $a['analysis_delta_text'],
  569. (int) $a['student_cur'],
  570. (int) $a['student_prev'],
  571. (string) $a['student_delta_text'],
  572. $reasonEsc
  573. );
  574. echo '- '.$line."\n";
  575. }
  576. if (count($highVolumeAnomalies) > 30) {
  577. echo sprintf("\n> 仅展示前 30 条,共 %d 条。\n", count($highVolumeAnomalies));
  578. }
  579. echo "\n";
  580. }
  581. echo sprintf("### 按老师 本周期有组卷 **%d** 人。\n\n", count($rows));
  582. echo '<table class="weekly-teacher-table">';
  583. echo '<colgroup>';
  584. echo '<col style="width:3%" /><col class="col-name" style="width:18mm;max-width:18mm;" />';
  585. echo '<col class="col-slash" style="width:11%" /><col style="width:14%" />';
  586. echo '<col class="col-slash" style="width:11%" /><col style="width:14%" />';
  587. echo '<col class="col-slash" style="width:11%" />';
  588. echo '</colgroup>';
  589. echo '<thead><tr>';
  590. echo '<th>排名</th><th>老师</th><th>学案数量</th><th>学案·环比</th><th>分析数量</th><th>学情·环比</th><th>学生数</th>';
  591. echo "</tr></thead>\n<tbody>\n";
  592. $i = 1;
  593. foreach ($rows as $r) {
  594. $tidKey = (string) $r['teacher_id'];
  595. $nmRaw = (string) $r['name'];
  596. $nmEsc = htmlspecialchars($nmRaw, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  597. $tidEsc = htmlspecialchars($tidKey, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  598. $nameWithId = $nmEsc.' <span class="teacher-id">('.$tidEsc.')</span>';
  599. $pc = $r['papers'];
  600. $pp = $r['papers_prev'];
  601. $ac = $r['analysis_sets'];
  602. $ap = $r['analysis_sets_prev'];
  603. $stuC = (int) ($studentUnionCurMap[$tidKey] ?? 0);
  604. $stuP = (int) ($studentUnionPrevMap[$tidKey] ?? 0);
  605. echo '<tr>';
  606. echo '<td>'.((string) $i++).'</td>';
  607. echo '<td class="td-name">'.$nameWithId.'</td>';
  608. echo '<td class="td-slash td-slash-papers" title="本周期 / 上周期:组卷套数">'.$slashPairAccentHtml($pc, $pp, $colorPapers).'</td>';
  609. echo '<td class="td-wow-papers">'.$compareCellHtml($pc, $pp, $colorPapers).'</td>';
  610. echo '<td class="td-slash td-slash-analysis" title="本周期 / 上周期:学情分析套数(卷去重)">'.$slashPairAccentHtml($ac, $ap, $colorAnalysis).'</td>';
  611. echo '<td class="td-wow-analysis">'.$compareCellHtml($ac, $ap, $colorAnalysis).'</td>';
  612. echo '<td class="td-slash td-slash-stu td-stu" title="本周期 / 上周期:组卷∪学情学生合并去重">'.$slashPairAccentHtml($stuC, $stuP, $colorStudents).'</td>';
  613. echo "</tr>\n";
  614. }
  615. echo "</tbody></table>\n";