report_teacher_weekly_stats.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. // ============================================================
  445. // 1 月 6 日起每日学案量点状图
  446. // ============================================================
  447. $jan6 = $todayStart->copy()->setDate(2026, 1, 6);
  448. $totalCumDays = (int) $jan6->diffInDays($todayStart) + 1;
  449. $dotLabels = [];
  450. $dotValues = [];
  451. for ($i = 0; $i < $totalCumDays; $i++) {
  452. $dayStart = $jan6->copy()->addDays($i);
  453. $dayEnd = $dayStart->copy()->addDay();
  454. $dotLabels[] = $dayStart->format('m/d');
  455. $dotValues[] = (int) $db::table('papers')
  456. ->whereNotNull('teacher_id')
  457. ->where('teacher_id', '!=', '')
  458. ->where('created_at', '>=', $dayStart)
  459. ->where('created_at', '<', $dayEnd)
  460. ->count();
  461. }
  462. $totalDays = count($dotValues);
  463. $cumulativeTotal = array_sum($dotValues);
  464. $cumulativeAvg = $totalDays > 0 ? round($cumulativeTotal / $totalDays, 1) : 0;
  465. $cumulativeMax = $totalDays > 0 ? max($dotValues) : 0;
  466. $cumulativeMaxDate = '';
  467. foreach ($dotValues as $di => $dv) {
  468. if ($dv === $cumulativeMax && $cumulativeMax === $cumulativeMax) {
  469. $cumulativeMaxDate = $dotLabels[$di];
  470. break;
  471. }
  472. }
  473. $buildDotChartHtml = static function (array $labels, array $values, int $totalDays, float $avg) use ($tz, $generatedAt): string {
  474. $maxY = max(1, ...$values);
  475. $W = 800;
  476. $H = 360;
  477. $padL = 48;
  478. $padR = 50;
  479. $padT = 32;
  480. $padB = 68;
  481. $gw = $W - $padL - $padR;
  482. $gh = $H - $padT - $padB;
  483. $n = max(1, count($labels));
  484. $xAt = static function (int $i) use ($padL, $gw, $n): float {
  485. return $padL + ($n <= 1 ? $gw / 2 : $gw * $i / ($n - 1));
  486. };
  487. $yAtF = static function (float $v) use ($padT, $gh, $maxY): float {
  488. return $padT + $gh - ($v / $maxY) * $gh;
  489. };
  490. $yAt = static function (int $v) use ($yAtF): float {
  491. return $yAtF((float) $v);
  492. };
  493. $svg = sprintf(
  494. '<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d">',
  495. $W, $H, $W, $H
  496. );
  497. $svg .= sprintf('<rect x="0" y="0" width="%d" height="%d" fill="#fafafa"/>', $W, $H);
  498. // Y grid
  499. $yTickSteps = 5;
  500. for ($k = 0; $k <= $yTickSteps; $k++) {
  501. $v = (int) round($maxY * $k / $yTickSteps);
  502. $y = $yAt($v);
  503. $svg .= sprintf(
  504. '<line x1="%d" y1="%.1f" x2="%d" y2="%.1f" stroke="#e5e7eb" stroke-width="1"/>',
  505. $padL, $y, $padL + $gw, $y
  506. );
  507. $svg .= sprintf(
  508. '<text x="%d" y="%.1f" font-size="8" fill="#6b7280">%d</text>',
  509. 4, $y + 3, $v
  510. );
  511. }
  512. // X axis
  513. $xAxisY = $padT + $gh;
  514. $svg .= sprintf(
  515. '<line x1="%d" y1="%.1f" x2="%d" y2="%.1f" stroke="#9ca3af" stroke-width="1"/>',
  516. $padL, $xAxisY, $padL + $gw, $xAxisY
  517. );
  518. // Avg line — use float for exact horizontal position
  519. $avgY = $yAtF($avg);
  520. $svg .= sprintf(
  521. '<line x1="%d" y1="%.2f" x2="%d" y2="%.2f" stroke="#f59e0b" stroke-width="1.2" stroke-dasharray="5,3"/>',
  522. $padL, $avgY, $padL + $gw, $avgY
  523. );
  524. $svg .= sprintf(
  525. '<text x="%d" y="%.1f" font-size="8" fill="#d97706">avg %.0f</text>',
  526. $padL + $gw + 2, $avgY + 3, $avg
  527. );
  528. // Dots + polyline
  529. $pts = [];
  530. $dots = '';
  531. $xLabelStep = max(1, (int) ceil($n / 12));
  532. $xLabels = '';
  533. for ($i = 0; $i < $n; $i++) {
  534. $x = $xAt($i);
  535. $y = $yAt((int) $values[$i]);
  536. $pts[] = round($x, 1) . ',' . round($y, 1);
  537. $color = $values[$i] >= $avg ? '#2563eb' : '#93c5fd';
  538. $radius = $values[$i] >= $avg ? 3.5 : 2.8;
  539. $dots .= sprintf(
  540. '<circle cx="%.1f" cy="%.1f" r="%.1f" fill="%s"/>',
  541. $x, $y, $radius, $color
  542. );
  543. if ($i % $xLabelStep === 0 || $i === $n - 1) {
  544. $xLabels .= sprintf(
  545. '<text x="%.1f" y="%.1f" text-anchor="middle" font-size="7" fill="#6b7280">%s</text>',
  546. $x, $xAxisY + 12,
  547. htmlspecialchars($labels[$i], ENT_QUOTES, 'UTF-8')
  548. );
  549. }
  550. }
  551. $svg .= sprintf(
  552. '<polyline fill="none" stroke="#93c5fd" stroke-width="1" points="%s"/>',
  553. implode(' ', $pts)
  554. );
  555. $svg .= $dots;
  556. $svg .= $xLabels;
  557. // Legend
  558. $svg .= sprintf('<circle cx="%d" cy="%d" r="3.5" fill="#2563eb"/>', $padL + 4, $H - 16);
  559. $svg .= sprintf('<text x="%d" y="%d" font-size="9" fill="#111">≥ avg</text>', $padL + 12, $H - 12);
  560. $svg .= sprintf('<circle cx="%d" cy="%d" r="2.8" fill="#93c5fd"/>', $padL + 54, $H - 16);
  561. $svg .= sprintf('<text x="%d" y="%d" font-size="9" fill="#111">< avg</text>', $padL + 62, $H - 12);
  562. $svg .= sprintf('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="#f59e0b" stroke-width="1.2" stroke-dasharray="5,3"/>', $padL + 104, $H - 16, $padL + 120, $H - 16);
  563. $svg .= sprintf('<text x="%d" y="%d" font-size="9" fill="#d97706">daily avg</text>', $padL + 124, $H - 12);
  564. $svg .= '</svg>';
  565. return $svg;
  566. };
  567. $dotChartSvg = $totalDays > 0 ? $buildDotChartHtml($dotLabels, $dotValues, $totalDays, $cumulativeAvg) : '';
  568. echo sprintf("## 老师组卷与学情分析(近%d个自然日)\n\n", $reportPeriodDays);
  569. echo "> 生成 {$generatedAt} · {$tz} · 本 {$windowCur} · 上 {$windowPrev}\n";
  570. echo sprintf("> 口径:自然日边界(config timezone);今日段为当日 0:00—生成时刻;上周期为紧邻的前 **%d** 个完整自然日。\n\n", $reportPeriodDays);
  571. echo "### 总量\n\n";
  572. echo "| 指标 | 本周期 | 上周期 | 环比 |\n";
  573. echo "| :---: | :---: | :---: | :---: |\n";
  574. echo sprintf("| 组卷总套数 | %d | %d | %s |\n", $totalPapersCur, $totalPapersPrev, $wowLineHtml($totalPapersCur, $totalPapersPrev, $colorPapers));
  575. echo sprintf("| 学情分析套数(卷去重) | %d | %d | %s |\n", $totalAnalysisCur, $totalAnalysisPrev, $wowLineHtml($totalAnalysisCur, $totalAnalysisPrev, $colorAnalysis));
  576. echo sprintf("| 有组卷老师数 | %d | %d | %s |\n", $teachersCur, $teachersPrev, $wowLineHtml($teachersCur, $teachersPrev, $colorStudents));
  577. echo "### 逐日对比(左:学案 · 右:学情)\n\n";
  578. echo '<div class="weekly-chart">';
  579. echo $chartSvg;
  580. echo "</div>\n\n";
  581. if ($dotChartSvg !== '') {
  582. echo sprintf("### 1月6日至今每日学案量(共 %d 天,累计 %d 套)\n\n", $totalDays, $cumulativeTotal);
  583. echo sprintf("> 日均 **%.1f** 套 · 峰值 **%d** 套(%s)\n\n", $cumulativeAvg, $cumulativeMax, $cumulativeMaxDate);
  584. echo '<div class="weekly-chart">';
  585. echo $dotChartSvg;
  586. echo "</div>\n\n";
  587. }
  588. // 老师列表汇总:先给出增/降总体倾向,再给出异常清单
  589. $calcPct = static function (int $delta, int $prev): ?float {
  590. if ($prev <= 0) {
  591. return null;
  592. }
  593. return round(($delta / $prev) * 100, 1);
  594. };
  595. $fmtDeltaWithPct = static function (int $delta, int $prev): string {
  596. $sign = $delta > 0 ? '+' : '';
  597. $pct = $prev > 0 ? sprintf('(%s%.1f%%)', $sign, ($delta / $prev) * 100) : '(上0)';
  598. return $sign.$delta.$pct;
  599. };
  600. $paperUp = 0; $paperDown = 0; $paperFlat = 0;
  601. $analysisUp = 0; $analysisDown = 0; $analysisFlat = 0;
  602. $studentUp = 0; $studentDown = 0; $studentFlat = 0;
  603. $sumPaperDelta = 0; $sumAnalysisDelta = 0; $sumStudentDelta = 0;
  604. $anomalies = [];
  605. foreach ($rows as $r) {
  606. $tidKey = (string) $r['teacher_id'];
  607. $name = (string) $r['name'];
  608. $papersCur = (int) $r['papers'];
  609. $papersPrev = (int) $r['papers_prev'];
  610. $analysisCur = (int) $r['analysis_sets'];
  611. $analysisPrev = (int) $r['analysis_sets_prev'];
  612. $studentCur = (int) ($studentUnionCurMap[$tidKey] ?? 0);
  613. $studentPrev = (int) ($studentUnionPrevMap[$tidKey] ?? 0);
  614. $paperDelta = $papersCur - $papersPrev;
  615. $analysisDelta = $analysisCur - $analysisPrev;
  616. $studentDelta = $studentCur - $studentPrev;
  617. $sumPaperDelta += $paperDelta;
  618. $sumAnalysisDelta += $analysisDelta;
  619. $sumStudentDelta += $studentDelta;
  620. if ($paperDelta > 0) { $paperUp++; } elseif ($paperDelta < 0) { $paperDown++; } else { $paperFlat++; }
  621. if ($analysisDelta > 0) { $analysisUp++; } elseif ($analysisDelta < 0) { $analysisDown++; } else { $analysisFlat++; }
  622. if ($studentDelta > 0) { $studentUp++; } elseif ($studentDelta < 0) { $studentDown++; } else { $studentFlat++; }
  623. $paperPct = $calcPct($paperDelta, $papersPrev);
  624. $analysisPct = $calcPct($analysisDelta, $analysisPrev);
  625. $studentPct = $calcPct($studentDelta, $studentPrev);
  626. // 异常口径(用于快速巡检):
  627. // 1) 学案/学情明显下滑;2) 学案有量但学情为 0;3) 学情明显高于学案;
  628. // 4) 学案与学情方向背离;5) 覆盖学生数异常波动。
  629. if (
  630. ($papersPrev >= 20 && ($paperDelta <= -20 || ($paperPct !== null && $paperPct <= -50.0))) ||
  631. ($analysisPrev >= 15 && ($analysisDelta <= -15 || ($analysisPct !== null && $analysisPct <= -50.0))) ||
  632. ($papersCur >= 10 && $analysisCur === 0) ||
  633. ($analysisCur >= 10 && $analysisCur > $papersCur + 5) ||
  634. (($paperDelta > 0 && $analysisDelta < 0 && abs($analysisDelta) >= 5) ||
  635. ($paperDelta < 0 && $analysisDelta > 0 && abs($paperDelta) >= 5)) ||
  636. (abs($studentDelta) >= 10 || ($studentPct !== null && abs($studentPct) >= 100.0 && $studentPrev >= 5))
  637. ) {
  638. $severity = abs($paperDelta) + abs($analysisDelta) + abs($studentDelta);
  639. if ($papersCur >= 10 && $analysisCur === 0) { $severity += 20; }
  640. if ($analysisCur >= 10 && $analysisCur > $papersCur + 5) { $severity += 12; }
  641. $reason = [];
  642. if ($papersPrev >= 20 && ($paperDelta <= -20 || ($paperPct !== null && $paperPct <= -50.0))) { $reason[] = '学案下滑'; }
  643. if ($analysisPrev >= 15 && ($analysisDelta <= -15 || ($analysisPct !== null && $analysisPct <= -50.0))) { $reason[] = '学情下滑'; }
  644. if ($papersCur >= 10 && $analysisCur === 0) { $reason[] = '学案有量但学情为0'; }
  645. if ($analysisCur >= 10 && $analysisCur > $papersCur + 5) { $reason[] = '学情高于学案'; }
  646. if (($paperDelta > 0 && $analysisDelta < 0) || ($paperDelta < 0 && $analysisDelta > 0)) { $reason[] = '学案学情背离'; }
  647. if (abs($studentDelta) >= 10 || ($studentPct !== null && abs($studentPct) >= 100.0 && $studentPrev >= 5)) { $reason[] = '学生覆盖波动'; }
  648. $anomalies[] = [
  649. 'severity' => $severity,
  650. 'teacher_id' => $tidKey,
  651. 'name' => $name,
  652. 'papers_cur' => $papersCur,
  653. 'papers_prev' => $papersPrev,
  654. 'analysis_cur' => $analysisCur,
  655. 'analysis_prev' => $analysisPrev,
  656. 'student_cur' => $studentCur,
  657. 'student_prev' => $studentPrev,
  658. 'paper_delta_text' => $fmtDeltaWithPct($paperDelta, $papersPrev),
  659. 'analysis_delta_text' => $fmtDeltaWithPct($analysisDelta, $analysisPrev),
  660. 'student_delta_text' => $fmtDeltaWithPct($studentDelta, $studentPrev),
  661. 'reason' => implode(' / ', array_values(array_unique($reason))),
  662. ];
  663. }
  664. }
  665. usort($anomalies, static fn ($a, $b) => $b['severity'] <=> $a['severity']);
  666. $paperTrend = $paperUp > $paperDown ? '学案整体偏增长' : ($paperUp < $paperDown ? '学案整体偏下降' : '学案整体增降持平');
  667. $analysisTrend = $analysisUp > $analysisDown ? '学情整体偏增长' : ($analysisUp < $analysisDown ? '学情整体偏下降' : '学情整体增降持平');
  668. $studentTrend = $studentUp > $studentDown ? '学生覆盖整体偏增长' : ($studentUp < $studentDown ? '学生覆盖整体偏下降' : '学生覆盖整体增降持平');
  669. echo "### 老师列表汇总(增降对比)\n\n";
  670. echo "| 维度 | 增长老师数 | 下降老师数 | 持平老师数 | 判断 |\n";
  671. echo "| :--- | ---: | ---: | ---: | :--- |\n";
  672. echo sprintf("| 学案(组卷) | %d | %d | %d | %s |\n", $paperUp, $paperDown, $paperFlat, $paperTrend);
  673. echo sprintf("| 学情(分析) | %d | %d | %d | %s |\n", $analysisUp, $analysisDown, $analysisFlat, $analysisTrend);
  674. echo sprintf("| 学生覆盖(去重) | %d | %d | %d | %s |\n\n", $studentUp, $studentDown, $studentFlat, $studentTrend);
  675. echo "### 异常数据(学案本/上超过100,文字清单)\n\n";
  676. $highVolumeAnomalies = array_values(array_filter(
  677. $anomalies,
  678. static fn (array $a): bool => ((int) ($a['papers_cur'] ?? 0) > 100) || ((int) ($a['papers_prev'] ?? 0) > 100)
  679. ));
  680. if ($highVolumeAnomalies === []) {
  681. echo "本周期未命中“学案本/上超过100”的异常老师。\n\n";
  682. } else {
  683. $display = array_slice($highVolumeAnomalies, 0, 30);
  684. $idx = 1;
  685. foreach ($display as $a) {
  686. $nameEsc = htmlspecialchars((string) $a['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  687. $tidEsc = htmlspecialchars((string) $a['teacher_id'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  688. $reasonEsc = htmlspecialchars((string) $a['reason'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  689. $line = sprintf(
  690. '%d) %s(%s):学案 %d/%d(%s);学情 %d/%d(%s);学生 %d/%d(%s);异常:%s',
  691. $idx++,
  692. $nameEsc,
  693. $tidEsc,
  694. (int) $a['papers_cur'],
  695. (int) $a['papers_prev'],
  696. (string) $a['paper_delta_text'],
  697. (int) $a['analysis_cur'],
  698. (int) $a['analysis_prev'],
  699. (string) $a['analysis_delta_text'],
  700. (int) $a['student_cur'],
  701. (int) $a['student_prev'],
  702. (string) $a['student_delta_text'],
  703. $reasonEsc
  704. );
  705. echo '- '.$line."\n";
  706. }
  707. if (count($highVolumeAnomalies) > 30) {
  708. echo sprintf("\n> 仅展示前 30 条,共 %d 条。\n", count($highVolumeAnomalies));
  709. }
  710. echo "\n";
  711. }
  712. echo sprintf("### 按老师 本周期有组卷 **%d** 人。\n\n", count($rows));
  713. echo '<table class="weekly-teacher-table">';
  714. echo '<colgroup>';
  715. echo '<col style="width:3%" /><col class="col-name" style="width:18mm;max-width:18mm;" />';
  716. echo '<col class="col-slash" style="width:11%" /><col style="width:14%" />';
  717. echo '<col class="col-slash" style="width:11%" /><col style="width:14%" />';
  718. echo '<col class="col-slash" style="width:11%" />';
  719. echo '</colgroup>';
  720. echo '<thead><tr>';
  721. echo '<th>排名</th><th>老师</th><th>学案数量</th><th>学案·环比</th><th>分析数量</th><th>学情·环比</th><th>学生数</th>';
  722. echo "</tr></thead>\n<tbody>\n";
  723. $i = 1;
  724. foreach ($rows as $r) {
  725. $tidKey = (string) $r['teacher_id'];
  726. $nmRaw = (string) $r['name'];
  727. $nmEsc = htmlspecialchars($nmRaw, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  728. $tidEsc = htmlspecialchars($tidKey, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  729. $nameWithId = $nmEsc.' <span class="teacher-id">('.$tidEsc.')</span>';
  730. $pc = $r['papers'];
  731. $pp = $r['papers_prev'];
  732. $ac = $r['analysis_sets'];
  733. $ap = $r['analysis_sets_prev'];
  734. $stuC = (int) ($studentUnionCurMap[$tidKey] ?? 0);
  735. $stuP = (int) ($studentUnionPrevMap[$tidKey] ?? 0);
  736. echo '<tr>';
  737. echo '<td>'.((string) $i++).'</td>';
  738. echo '<td class="td-name">'.$nameWithId.'</td>';
  739. echo '<td class="td-slash td-slash-papers" title="本周期 / 上周期:组卷套数">'.$slashPairAccentHtml($pc, $pp, $colorPapers).'</td>';
  740. echo '<td class="td-wow-papers">'.$compareCellHtml($pc, $pp, $colorPapers).'</td>';
  741. echo '<td class="td-slash td-slash-analysis" title="本周期 / 上周期:学情分析套数(卷去重)">'.$slashPairAccentHtml($ac, $ap, $colorAnalysis).'</td>';
  742. echo '<td class="td-wow-analysis">'.$compareCellHtml($ac, $ap, $colorAnalysis).'</td>';
  743. echo '<td class="td-slash td-slash-stu td-stu" title="本周期 / 上周期:组卷∪学情学生合并去重">'.$slashPairAccentHtml($stuC, $stuP, $colorStudents).'</td>';
  744. echo "</tr>\n";
  745. }
  746. echo "</tbody></table>\n";