report_teacher_weekly_stats.php 35 KB

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