QuestionQualityCheckService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Schema;
  7. /**
  8. * 题目质检服务
  9. *
  10. * 校验规则:题干、答案、解析、选项、公式、PDF 呈现、AI 答案校验
  11. * 结果由命令输出,不落库(避免本地库覆盖)
  12. */
  13. class QuestionQualityCheckService
  14. {
  15. private ?AiClientService $aiClient = null;
  16. public function __construct(?AiClientService $aiClient = null)
  17. {
  18. $this->aiClient = $aiClient ?? (app()->bound(AiClientService::class) ? app(AiClientService::class) : null);
  19. }
  20. public const RULES = [
  21. 'STEM_EMPTY' => ['name' => '题干为空', 'severity' => 'error'],
  22. 'ANSWER_EMPTY' => ['name' => '答案为空', 'severity' => 'error'],
  23. 'SOLUTION_EMPTY' => ['name' => '解析为空', 'severity' => 'warning'],
  24. 'CHOICE_OPTIONS_MISSING' => ['name' => '选择题缺选项', 'severity' => 'error'],
  25. 'CHOICE_OPTIONS_JSON_INVALID' => ['name' => '选择题选项JSON无效', 'severity' => 'error'],
  26. 'CHOICE_OPTION_TEXT_EMPTY' => ['name' => '选择题存在空白选项', 'severity' => 'warning'],
  27. 'ANSWER_OPTION_MISMATCH' => ['name' => '选择题答案不在选项中', 'severity' => 'error'],
  28. 'FORMULA_INVALID' => ['name' => '公式异常', 'severity' => 'error'],
  29. 'CONTENT_TOO_SHORT' => ['name' => '题干过短', 'severity' => 'warning'],
  30. 'AI_ANSWER_INVALID' => ['name' => 'AI 判定答案错误', 'severity' => 'error'],
  31. 'AI_ANSWER_MISMATCH' => ['name' => 'AI 判定答案与题目不匹配', 'severity' => 'error'],
  32. ];
  33. /**
  34. * 对单道题目执行自动质检
  35. *
  36. * @param array $question 题目数据,需包含 stem, answer, solution, question_type, options;
  37. * 可选 options_json_invalid=true(options 字段为字符串但 JSON 解析失败)
  38. * @param int|null $questionTemId questions_tem 表 ID
  39. * @param int|null $questionId questions 表 ID
  40. * @param array $options ['ai_check' => bool] 是否启用 AI 校验(答案正确性、与题目匹配)
  41. * @return array ['passed' => bool, 'results' => array, 'errors' => array]
  42. */
  43. public function runAutoCheck(array $question, ?int $questionTemId = null, ?int $questionId = null, array $options = []): array
  44. {
  45. $stem = $question['stem'] ?? $question['content'] ?? '';
  46. $answer = $question['answer'] ?? '';
  47. $solution = $question['solution'] ?? '';
  48. $questionType = $this->normalizeQuestionType(
  49. (string) ($question['question_type'] ?? $question['tags'] ?? '')
  50. );
  51. $options = $question['options'] ?? null;
  52. $optionsJsonInvalid = ! empty($question['options_json_invalid']);
  53. $results = [];
  54. $errors = [];
  55. // STEM_EMPTY / CONTENT_TOO_SHORT
  56. $stemLen = mb_strlen(trim((string) $stem));
  57. if ($stemLen === 0) {
  58. $results[] = $this->recordCheck('STEM_EMPTY', false, '题干为空');
  59. $errors[] = 'STEM_EMPTY';
  60. } elseif ($stemLen < 5) {
  61. $results[] = $this->recordCheck('CONTENT_TOO_SHORT', false, "题干过短({$stemLen}字)");
  62. $errors[] = 'CONTENT_TOO_SHORT';
  63. } else {
  64. $results[] = $this->recordCheck('STEM_EMPTY', true);
  65. }
  66. // ANSWER_EMPTY
  67. if (trim((string) $answer) === '') {
  68. $results[] = $this->recordCheck('ANSWER_EMPTY', false, '答案为空');
  69. $errors[] = 'ANSWER_EMPTY';
  70. } else {
  71. $results[] = $this->recordCheck('ANSWER_EMPTY', true);
  72. }
  73. // SOLUTION_EMPTY(解答题强校验)
  74. $isAnswerType = $questionType === 'answer';
  75. if ($isAnswerType && trim((string) $solution) === '') {
  76. $results[] = $this->recordCheck('SOLUTION_EMPTY', false, '解答题解析为空');
  77. $errors[] = 'SOLUTION_EMPTY';
  78. } else {
  79. $results[] = $this->recordCheck('SOLUTION_EMPTY', true);
  80. }
  81. // CHOICE_OPTIONS_*(与 PDF 导出口径:选项 JSON、非空文案数量)
  82. $isChoice = $questionType === 'choice';
  83. if ($isChoice) {
  84. if ($optionsJsonInvalid) {
  85. $results[] = $this->recordCheck('CHOICE_OPTIONS_JSON_INVALID', false, 'options 字段不是合法 JSON');
  86. $errors[] = 'CHOICE_OPTIONS_JSON_INVALID';
  87. $results[] = $this->recordCheck('CHOICE_OPTIONS_MISSING', true, null, 'skip');
  88. $results[] = $this->recordCheck('CHOICE_OPTION_TEXT_EMPTY', true, null, 'skip');
  89. } else {
  90. $optionTexts = $this->extractOptionTexts(is_array($options) ? $options : null);
  91. $nonEmpty = array_values(array_filter($optionTexts, fn ($t) => mb_strlen(trim((string) $t)) > 0));
  92. if (count($nonEmpty) < 2) {
  93. $results[] = $this->recordCheck('CHOICE_OPTIONS_MISSING', false, '选择题有效选项不足2个');
  94. $errors[] = 'CHOICE_OPTIONS_MISSING';
  95. } else {
  96. $results[] = $this->recordCheck('CHOICE_OPTIONS_MISSING', true);
  97. }
  98. $hasEmptySlot = count($optionTexts) > 0
  99. && count($nonEmpty) < count($optionTexts);
  100. if ($hasEmptySlot) {
  101. $results[] = $this->recordCheck('CHOICE_OPTION_TEXT_EMPTY', false, '存在空白选项项');
  102. $errors[] = 'CHOICE_OPTION_TEXT_EMPTY';
  103. } else {
  104. $results[] = $this->recordCheck('CHOICE_OPTION_TEXT_EMPTY', true);
  105. }
  106. }
  107. } else {
  108. $results[] = $this->recordCheck('CHOICE_OPTIONS_MISSING', true, null, 'skip');
  109. $results[] = $this->recordCheck('CHOICE_OPTIONS_JSON_INVALID', true, null, 'skip');
  110. $results[] = $this->recordCheck('CHOICE_OPTION_TEXT_EMPTY', true, null, 'skip');
  111. }
  112. // ANSWER_OPTION_MISMATCH:选择题答案必须在选项中
  113. if ($isChoice && ! $optionsJsonInvalid && is_array($options)) {
  114. $answerLetter = $this->extractChoiceAnswerLetter((string) $answer);
  115. $optionKeysRaw = array_keys($options);
  116. $optionLetters = array_map(fn ($k) => strtoupper(substr((string) $k, 0, 1)), $optionKeysRaw);
  117. if ($answerLetter !== null && ! in_array($answerLetter, $optionLetters, true)) {
  118. $results[] = $this->recordCheck('ANSWER_OPTION_MISMATCH', false, "答案 {$answerLetter} 不在选项 " . implode(',', $optionLetters) . " 中");
  119. $errors[] = 'ANSWER_OPTION_MISMATCH';
  120. } elseif ($answerLetter === null && trim((string) $answer) !== '') {
  121. $results[] = $this->recordCheck('ANSWER_OPTION_MISMATCH', false, '答案格式无法识别为选项(应为 A/B/C/D)');
  122. $errors[] = 'ANSWER_OPTION_MISMATCH';
  123. } else {
  124. $results[] = $this->recordCheck('ANSWER_OPTION_MISMATCH', true);
  125. }
  126. } else {
  127. $results[] = $this->recordCheck('ANSWER_OPTION_MISMATCH', true, null, 'skip');
  128. }
  129. // FORMULA_INVALID(题干、答案、解析、选择题各选项;捕获异常)
  130. try {
  131. $processed = MathFormulaProcessor::processFormulas($stem);
  132. $processedAnswer = MathFormulaProcessor::processFormulas($answer);
  133. $processedSolution = MathFormulaProcessor::processFormulas($solution);
  134. $hasError = $this->detectFormulaError($processed)
  135. || $this->detectFormulaError($processedAnswer)
  136. || $this->detectFormulaError($processedSolution);
  137. if ($isChoice && ! $optionsJsonInvalid && is_array($options)) {
  138. foreach ($this->extractOptionTexts($options) as $optText) {
  139. if (trim((string) $optText) === '') {
  140. continue;
  141. }
  142. $po = MathFormulaProcessor::processFormulas((string) $optText);
  143. if ($this->detectFormulaError($po)) {
  144. $hasError = true;
  145. break;
  146. }
  147. }
  148. }
  149. if ($hasError) {
  150. $results[] = $this->recordCheck('FORMULA_INVALID', false, '公式定界符不匹配或存在异常');
  151. $errors[] = 'FORMULA_INVALID';
  152. } else {
  153. $results[] = $this->recordCheck('FORMULA_INVALID', true);
  154. }
  155. } catch (\Throwable $e) {
  156. $results[] = $this->recordCheck('FORMULA_INVALID', false, $e->getMessage());
  157. $errors[] = 'FORMULA_INVALID';
  158. }
  159. // AI 校验:答案正确性、答案与题目匹配(需开启 ai_check,且基础校验通过时再调 AI)
  160. $aiCheck = $options['ai_check'] ?? false;
  161. if ($aiCheck && $this->aiClient && empty($errors) && trim((string) $answer) !== '') {
  162. try {
  163. $aiResult = $this->runAiAnswerCheck($question);
  164. if (is_array($aiResult)) {
  165. if (! ($aiResult['answer_correct'] ?? true)) {
  166. $results[] = $this->recordCheck('AI_ANSWER_INVALID', false, $aiResult['reason'] ?? 'AI 判定答案错误');
  167. $errors[] = 'AI_ANSWER_INVALID';
  168. } else {
  169. $results[] = $this->recordCheck('AI_ANSWER_INVALID', true);
  170. }
  171. if (! ($aiResult['answer_matches_question'] ?? true)) {
  172. $results[] = $this->recordCheck('AI_ANSWER_MISMATCH', false, $aiResult['reason'] ?? 'AI 判定答案与题目不匹配');
  173. $errors[] = 'AI_ANSWER_MISMATCH';
  174. } else {
  175. $results[] = $this->recordCheck('AI_ANSWER_MISMATCH', true);
  176. }
  177. } else {
  178. $results[] = $this->recordCheck('AI_ANSWER_INVALID', true, null, 'skip');
  179. $results[] = $this->recordCheck('AI_ANSWER_MISMATCH', true, null, 'skip');
  180. }
  181. } catch (\Throwable $e) {
  182. Log::warning('QuestionQualityCheckService: AI 校验异常', ['error' => $e->getMessage()]);
  183. $results[] = $this->recordCheck('AI_ANSWER_INVALID', true, null, 'skip');
  184. $results[] = $this->recordCheck('AI_ANSWER_MISMATCH', true, null, 'skip');
  185. }
  186. } else {
  187. $results[] = $this->recordCheck('AI_ANSWER_INVALID', true, null, 'skip');
  188. $results[] = $this->recordCheck('AI_ANSWER_MISMATCH', true, null, 'skip');
  189. }
  190. $passed = empty($errors);
  191. return [
  192. 'passed' => $passed,
  193. 'results' => $results,
  194. 'errors' => $errors,
  195. ];
  196. }
  197. /**
  198. * 与组卷/PDF 口径一致:归一化为 choice | fill | answer
  199. */
  200. private function normalizeQuestionType(string $raw): string
  201. {
  202. $t = strtolower(trim($raw));
  203. if ($t === '') {
  204. return 'answer';
  205. }
  206. return match (true) {
  207. str_contains($t, 'choice') || str_contains($t, '选择') => 'choice',
  208. str_contains($t, 'fill') || str_contains($t, 'blank') || str_contains($t, '填空') => 'fill',
  209. in_array($t, ['single_choice', 'multiple_choice', 'select'], true) => 'choice',
  210. in_array($t, ['fill_in_the_blank'], true) => 'fill',
  211. in_array($t, ['answer', 'calculation', 'word_problem', 'proof', '解答', '简答'], true) => 'answer',
  212. default => 'answer',
  213. };
  214. }
  215. /**
  216. * 将 options 转为选项文案列表(与 ExamPdfController::normalizeOptions 语义对齐)
  217. *
  218. * @param array|null $options
  219. * @return list<string>
  220. */
  221. private function extractOptionTexts(?array $options): array
  222. {
  223. if ($options === null || $options === []) {
  224. return [];
  225. }
  226. if (! isset($options[0]) && $options !== []) {
  227. return array_values(array_map(static fn ($v) => (string) $v, $options));
  228. }
  229. if (isset($options[0]) && is_array($options[0])) {
  230. $normalized = [];
  231. foreach ($options as $opt) {
  232. if (! is_array($opt)) {
  233. $normalized[] = (string) $opt;
  234. continue;
  235. }
  236. if (isset($opt['text'])) {
  237. $normalized[] = (string) $opt['text'];
  238. } elseif (isset($opt['value'])) {
  239. $normalized[] = (string) $opt['value'];
  240. } else {
  241. $normalized[] = (string) reset($opt);
  242. }
  243. }
  244. return $normalized;
  245. }
  246. return array_values(array_map(static fn ($v) => (string) $v, $options));
  247. }
  248. /**
  249. * 检测公式处理后的内容是否仍有明显错误(如未闭合的 $)
  250. */
  251. private function detectFormulaError(string $content): bool
  252. {
  253. $len = strlen($content);
  254. $dollarCount = 0;
  255. $inEscape = false;
  256. for ($i = 0; $i < $len; $i++) {
  257. $c = $content[$i];
  258. if ($c === '\\' && !$inEscape) {
  259. $inEscape = true;
  260. continue;
  261. }
  262. if ($c === '$') {
  263. $dollarCount++;
  264. }
  265. $inEscape = false;
  266. }
  267. return ($dollarCount % 2) !== 0;
  268. }
  269. /**
  270. * 提取选择题答案字母(A/B/C/D)
  271. */
  272. private function extractChoiceAnswerLetter(string $answer): ?string
  273. {
  274. $answer = trim($answer);
  275. if (preg_match('/^([A-D])$/i', $answer, $m)) {
  276. return strtoupper($m[1]);
  277. }
  278. if (preg_match('/答案[::]\s*([A-D])/iu', $answer, $m)) {
  279. return strtoupper($m[1]);
  280. }
  281. if (preg_match('/([A-D])[\.、.:]/u', $answer, $m)) {
  282. return strtoupper($m[1]);
  283. }
  284. if (preg_match('/([A-D])/i', $answer, $m)) {
  285. return strtoupper($m[1]);
  286. }
  287. return null;
  288. }
  289. /**
  290. * AI 校验:答案是否正确、是否与题目匹配
  291. */
  292. private function runAiAnswerCheck(array $question): ?array
  293. {
  294. $stem = $question['stem'] ?? $question['content'] ?? '';
  295. $answer = $question['answer'] ?? '';
  296. $solution = $question['solution'] ?? '';
  297. $questionType = $question['question_type'] ?? ($question['tags'] ?? 'answer');
  298. $options = $question['options'] ?? null;
  299. $optionsStr = '无';
  300. if (is_array($options)) {
  301. $parts = [];
  302. foreach ($options as $k => $v) {
  303. $v = is_string($v) ? $v : json_encode($v);
  304. $parts[] = "{$k}: " . mb_substr($v, 0, 200);
  305. }
  306. $optionsStr = implode("\n", $parts);
  307. } elseif (is_string($options)) {
  308. $optionsStr = mb_substr($options, 0, 500);
  309. }
  310. $prompt = str_replace(
  311. ['{question_type}', '{stem}', '{options}', '{answer}', '{solution}'],
  312. [$questionType, mb_substr((string) $stem, 0, 1500), $optionsStr, (string) $answer, mb_substr((string) $solution, 0, 800)],
  313. config('ai.answer_validation_prompt', '')
  314. );
  315. if ($prompt === '') {
  316. return null;
  317. }
  318. $data = $this->aiClient->callJson($prompt);
  319. return is_array($data) && isset($data['answer_correct']) ? $data : null;
  320. }
  321. private function recordCheck(string $ruleCode, bool $passed, ?string $detail = null, string $result = 'pass'): array
  322. {
  323. $info = self::RULES[$ruleCode] ?? ['name' => $ruleCode, 'severity' => 'error'];
  324. return [
  325. 'rule_code' => $ruleCode,
  326. 'rule_name' => $info['name'],
  327. 'passed' => $passed,
  328. 'auto_result' => $passed ? 'pass' : ($result === 'skip' ? 'skip' : 'fail'),
  329. 'detail' => $detail,
  330. ];
  331. }
  332. /**
  333. * 获取下学期章节关联的、题少的 KP 列表(用于筛选 questions_tem)
  334. *
  335. * @param int|null $textbookId 教材 ID,null 则取默认教材
  336. * @param int $semesterCode 学期 1=上 2=下
  337. * @param int $limit 返回前 N 个题少的 KP
  338. * @return array [['kp_code' => string, 'question_count' => int], ...]
  339. */
  340. public function getKpsWithFewQuestions(?int $textbookId = null, int $semesterCode = 2, int $limit = 50): array
  341. {
  342. $textbooksQuery = DB::table('textbooks');
  343. if (Schema::hasColumn('textbooks', 'is_deleted')) {
  344. $textbooksQuery->where('is_deleted', 0);
  345. }
  346. if ($textbookId) {
  347. $textbooksQuery->where('id', $textbookId);
  348. }
  349. if (Schema::hasColumn('textbooks', 'semester_code')) {
  350. $textbooksQuery->where('semester_code', $semesterCode);
  351. }
  352. $textbookIds = $textbooksQuery->pluck('id')->toArray();
  353. if (empty($textbookIds)) {
  354. Log::warning('QuestionQualityCheckService: 未找到下学期教材');
  355. return [];
  356. }
  357. $kpCodes = DB::table('textbook_chapter_knowledge_relation as tckr')
  358. ->join('textbook_catalog_nodes as tcn', 'tckr.catalog_chapter_id', '=', 'tcn.id')
  359. ->whereIn('tcn.textbook_id', $textbookIds)
  360. ->where(function ($q) {
  361. $q->where('tckr.is_deleted', 0)->orWhereNull('tckr.is_deleted');
  362. })
  363. ->distinct()
  364. ->pluck('tckr.kp_code')
  365. ->toArray();
  366. if (empty($kpCodes)) {
  367. return [];
  368. }
  369. $counts = DB::table('questions')
  370. ->whereIn('kp_code', $kpCodes)
  371. ->where('audit_status', 0)
  372. ->selectRaw('kp_code, count(*) as cnt')
  373. ->groupBy('kp_code')
  374. ->pluck('cnt', 'kp_code')
  375. ->toArray();
  376. $result = [];
  377. foreach ($kpCodes as $kp) {
  378. $result[] = [
  379. 'kp_code' => $kp,
  380. 'question_count' => $counts[$kp] ?? 0,
  381. ];
  382. }
  383. usort($result, fn ($a, $b) => $a['question_count'] <=> $b['question_count']);
  384. return array_slice($result, 0, $limit);
  385. }
  386. /**
  387. * 获取待质检题目(与 questions 不重复),按知识点分组
  388. * 用于按 KP 生成 PDF
  389. *
  390. * @param string $table 待质检表名,默认 questions_tem
  391. * @param int|null $textbookId 教材 ID
  392. * @param int $semesterCode 学期 1=上 2=下
  393. * @param int $kpLimit 取前 N 个题少的 KP(当 $singleKp 为 null 时生效)
  394. * @param int|null $perKpLimit 每个 KP 最多取题数,null 不限制
  395. * @param string|null $singleKp 指定单个 KP,则只返回该 KP
  396. * @return array<string, Collection> [kp_code => Collection of question rows]
  397. */
  398. public function getQcQuestionsGroupedByKp(
  399. string $table = 'questions_tem',
  400. ?int $textbookId = null,
  401. int $semesterCode = 2,
  402. int $kpLimit = 10,
  403. ?int $perKpLimit = null,
  404. ?string $singleKp = null
  405. ): array {
  406. if (! Schema::hasTable($table)) {
  407. return [];
  408. }
  409. $kpCodes = $singleKp
  410. ? [$singleKp]
  411. : array_column($this->getKpsWithFewQuestions($textbookId, $semesterCode, $kpLimit), 'kp_code');
  412. if (empty($kpCodes)) {
  413. return [];
  414. }
  415. $query = DB::table($table)->whereIn('kp_code', $kpCodes);
  416. if ($table === 'questions_tem'
  417. && Schema::hasTable('questions')
  418. && Schema::hasColumn($table, 'question_code')
  419. && Schema::hasColumn('questions', 'question_code')
  420. ) {
  421. $query->whereNotIn('question_code', DB::table('questions')->select('question_code'));
  422. }
  423. $all = $query->orderBy('kp_code')->orderBy('id')->get();
  424. $grouped = [];
  425. foreach ($all as $row) {
  426. $kp = $row->kp_code ?? '';
  427. if ($kp === '') {
  428. continue;
  429. }
  430. if ($perKpLimit !== null && isset($grouped[$kp]) && $grouped[$kp]->count() >= $perKpLimit) {
  431. continue;
  432. }
  433. $grouped[$kp] ??= new Collection;
  434. $grouped[$kp]->push($row);
  435. }
  436. return $grouped;
  437. }
  438. }