ExamAnswerAnalysisService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Collection;
  6. /**
  7. * 考试答题分析服务(步骤级分析)
  8. * 基于卷子分析思考文档的思路实现
  9. *
  10. * 核心流程:
  11. * 1. 接收卷子ID和每道题的对错、简答题的分步骤对错
  12. * 2. 将原子信息映射到知识点/技能
  13. * 3. 计算知识点掌握度向量
  14. * 4. 生成详细分析报告
  15. * 5. 提供智能出卷推荐依据
  16. */
  17. class ExamAnswerAnalysisService
  18. {
  19. public function __construct(
  20. private readonly MasteryCalculator $masteryCalculator,
  21. private readonly KnowledgeMasteryService $knowledgeMasteryService,
  22. private readonly LocalAIAnalysisService $aiAnalysisService,
  23. private readonly QuestionBankService $questionBankService
  24. ) {}
  25. /**
  26. * 分析考试答题数据
  27. *
  28. * @param array $examData 考试数据
  29. * [
  30. * 'exam_id' => 'exam_001',
  31. * 'student_id' => 'student_001',
  32. * 'questions' => [
  33. * [
  34. * 'question_id' => 'Q1',
  35. * 'score' => 5,
  36. * 'score_obtained' => 5,
  37. * 'steps' => [
  38. * ['step_index' => 1, 'is_correct' => true, 'kp_id' => 'K-SQRT-SIMPLE'],
  39. * ['step_index' => 2, 'is_correct' => true, 'kp_id' => 'K-NUM-ADD-SUB']
  40. * ]
  41. * ]
  42. * ]
  43. * ]
  44. *
  45. * @return array 分析结果
  46. */
  47. public function analyzeExamAnswers(array $examData): array
  48. {
  49. Log::info('开始分析考试答题', [
  50. 'exam_id' => $examData['exam_id'] ?? 'unknown',
  51. 'student_id' => $examData['student_id'] ?? 'unknown',
  52. 'question_count' => count($examData['questions'] ?? [])
  53. ]);
  54. $studentId = $examData['student_id'];
  55. $questions = $examData['questions'] ?? [];
  56. // 1. 保存答题记录到数据库
  57. $this->saveExamAnswerRecords($examData);
  58. // 2. 获取题目知识点映射
  59. $questionMappings = $this->getQuestionKnowledgeMappings($questions);
  60. // 3. 计算每个知识点的加权掌握度
  61. $knowledgeMasteryVector = $this->calculateKnowledgeMasteryVector($questions, $questionMappings);
  62. // 4. 更新学生掌握度
  63. $updatedMastery = $this->updateStudentMastery($studentId, $knowledgeMasteryVector);
  64. // 5. 生成题目维度分析
  65. $questionAnalysis = $this->analyzeQuestions($questions, $questionMappings);
  66. // 6. 生成知识点维度分析
  67. $knowledgePointAnalysis = $this->analyzeKnowledgePoints($knowledgeMasteryVector, $questionMappings);
  68. // 7. 生成整体掌握度总结
  69. $overallSummary = $this->generateOverallSummary($updatedMastery);
  70. // 8. 生成智能出卷推荐依据
  71. $smartQuizRecommendation = $this->generateSmartQuizRecommendation($updatedMastery);
  72. // 9. 保存分析结果
  73. $analysisResult = [
  74. 'exam_id' => $examData['exam_id'],
  75. 'student_id' => $studentId,
  76. 'timestamp' => now()->toISOString(),
  77. 'question_analysis' => $questionAnalysis,
  78. 'knowledge_point_analysis' => $knowledgePointAnalysis,
  79. 'overall_summary' => $overallSummary,
  80. 'smart_quiz_recommendation' => $smartQuizRecommendation,
  81. 'mastery_vector' => $updatedMastery,
  82. ];
  83. $this->saveAnalysisResult($studentId, $examData['exam_id'], $analysisResult);
  84. Log::info('考试答题分析完成', [
  85. 'student_id' => $studentId,
  86. 'exam_id' => $examData['exam_id'],
  87. 'analyzed_knowledge_points' => count($knowledgeMasteryVector)
  88. ]);
  89. return $analysisResult;
  90. }
  91. /**
  92. * 获取题目知识点映射
  93. */
  94. private function getQuestionKnowledgeMappings(array $questions): array
  95. {
  96. $mappings = [];
  97. $questionIds = array_column($questions, 'question_id');
  98. // 从题库获取题目知识点映射
  99. try {
  100. $response = $this->questionBankService->getQuestionsKnowledgeMapping($questionIds);
  101. foreach ($response as $mapping) {
  102. $mappings[$mapping['question_id']] = $mapping;
  103. }
  104. } catch (\Exception $e) {
  105. Log::warning('获取题目知识点映射失败,使用默认映射', [
  106. 'error' => $e->getMessage(),
  107. 'question_ids' => $questionIds
  108. ]);
  109. // 使用默认映射:每道题至少映射到一个知识点
  110. foreach ($questions as $question) {
  111. $mappings[$question['question_id']] = [
  112. 'question_id' => $question['question_id'],
  113. 'kp_mapping' => [
  114. ['kp_id' => 'K-GENERAL', 'kp_name' => '综合', 'weight' => 1.0]
  115. ]
  116. ];
  117. }
  118. }
  119. return $mappings;
  120. }
  121. /**
  122. * 计算知识点掌握度向量
  123. * 基于文档中的简单实用更新公式
  124. */
  125. private function calculateKnowledgeMasteryVector(array $questions, array $questionMappings): array
  126. {
  127. $knowledgeScores = [];
  128. foreach ($questions as $question) {
  129. $questionId = $question['question_id'];
  130. $score = floatval($question['score_obtained'] ?? 0);
  131. $maxScore = floatval($question['score'] ?? $score);
  132. $steps = $question['steps'] ?? [];
  133. $mapping = $questionMappings[$questionId] ?? null;
  134. if (!$mapping || !isset($mapping['kp_mapping'])) {
  135. continue;
  136. }
  137. // 如果有步骤级分析,使用步骤分析
  138. if (!empty($steps)) {
  139. foreach ($steps as $step) {
  140. $kpId = $step['kp_id'] ?? 'K-GENERAL';
  141. $stepScore = floatval($step['score'] ?? ($maxScore / count($steps)));
  142. $stepWeight = floatval($step['weight'] ?? 1.0);
  143. if (!isset($knowledgeScores[$kpId])) {
  144. $knowledgeScores[$kpId] = [
  145. 'total_weight' => 0,
  146. 'correct_weight' => 0,
  147. 'step_details' => []
  148. ];
  149. }
  150. $knowledgeScores[$kpId]['total_weight'] += $stepScore * $stepWeight;
  151. if ($step['is_correct']) {
  152. $knowledgeScores[$kpId]['correct_weight'] += $stepScore * $stepWeight;
  153. }
  154. $knowledgeScores[$kpId]['step_details'][] = [
  155. 'question_id' => $questionId,
  156. 'step_index' => $step['step_index'],
  157. 'score' => $stepScore,
  158. 'is_correct' => $step['is_correct']
  159. ];
  160. }
  161. } else {
  162. // 没有步骤级分析,使用题目整体分析
  163. foreach ($mapping['kp_mapping'] as $kpMapping) {
  164. $kpId = $kpMapping['kp_id'];
  165. $weight = floatval($kpMapping['weight'] ?? 1.0);
  166. $kpMaxScore = $maxScore * $weight;
  167. if (!isset($knowledgeScores[$kpId])) {
  168. $knowledgeScores[$kpId] = [
  169. 'total_weight' => 0,
  170. 'correct_weight' => 0,
  171. 'step_details' => []
  172. ];
  173. }
  174. $knowledgeScores[$kpId]['total_weight'] += $kpMaxScore;
  175. if ($score > 0) {
  176. $knowledgeScores[$kpId]['correct_weight'] += $score * $weight;
  177. }
  178. }
  179. }
  180. }
  181. // 计算掌握度
  182. $masteryVector = [];
  183. foreach ($knowledgeScores as $kpId => $data) {
  184. $mastery = $data['total_weight'] > 0
  185. ? $data['correct_weight'] / $data['total_weight']
  186. : 0;
  187. // 置信度校正:考得越多,评价越稳定
  188. $confidence = 1 - exp(-$data['total_weight'] / 5);
  189. $masteryVector[$kpId] = [
  190. 'kp_id' => $kpId,
  191. 'mastery' => $mastery,
  192. 'confidence' => $confidence,
  193. 'total_weight' => $data['total_weight'],
  194. 'correct_weight' => $data['correct_weight'],
  195. 'step_details' => $data['step_details'],
  196. ];
  197. }
  198. return $masteryVector;
  199. }
  200. /**
  201. * 更新学生掌握度(与历史数据合并)
  202. */
  203. private function updateStudentMastery(string $studentId, array $knowledgeMasteryVector): array
  204. {
  205. $updatedMastery = [];
  206. foreach ($knowledgeMasteryVector as $kpId => $data) {
  207. // 获取历史掌握度
  208. $historyMastery = DB::connection('pgsql')
  209. ->table('student_knowledge_mastery')
  210. ->where('student_id', $studentId)
  211. ->where('kp_code', $kpId)
  212. ->first();
  213. $historyMasteryLevel = $historyMastery->mastery_level ?? 0.5;
  214. $historyWeight = $historyMastery->total_attempts ?? 0;
  215. $currentWeight = $data['total_weight'];
  216. // 合并计算:历史权重 + 当前权重
  217. $newMastery = $historyWeight > 0
  218. ? ($historyWeight * $historyMasteryLevel + $currentWeight * $data['mastery'])
  219. / ($historyWeight + $currentWeight)
  220. : $data['mastery'];
  221. $newConfidence = $data['confidence'];
  222. // 保存到数据库
  223. DB::connection('pgsql')
  224. ->table('student_knowledge_mastery')
  225. ->updateOrInsert(
  226. ['student_id' => $studentId, 'kp_code' => $kpId],
  227. [
  228. 'mastery_level' => $newMastery,
  229. 'confidence_level' => $newConfidence,
  230. 'total_attempts' => ($historyMastery->total_attempts ?? 0) + 1,
  231. 'correct_attempts' => ($historyMastery->correct_attempts ?? 0) + intval($data['correct_weight'] > 0),
  232. 'mastery_trend' => $this->determineMasteryTrend($historyMasteryLevel, $newMastery),
  233. 'last_mastery_update' => now(),
  234. 'updated_at' => now(),
  235. ]
  236. );
  237. $updatedMastery[$kpId] = [
  238. 'kp_id' => $kpId,
  239. 'current_mastery' => $newMastery,
  240. 'previous_mastery' => $historyMasteryLevel,
  241. 'confidence' => $newConfidence,
  242. 'change' => $newMastery - $historyMasteryLevel,
  243. 'weight' => $currentWeight
  244. ];
  245. }
  246. return $updatedMastery;
  247. }
  248. /**
  249. * 生成题目维度分析
  250. */
  251. private function analyzeQuestions(array $questions, array $questionMappings): array
  252. {
  253. $analysis = [];
  254. foreach ($questions as $question) {
  255. $questionId = $question['question_id'];
  256. $score = floatval($question['score_obtained'] ?? 0);
  257. $maxScore = floatval($question['score'] ?? $score);
  258. $steps = $question['steps'] ?? [];
  259. $mapping = $questionMappings[$questionId] ?? ['kp_mapping' => []];
  260. // 步骤分析
  261. $stepAnalysis = [];
  262. if (!empty($steps)) {
  263. foreach ($steps as $step) {
  264. $kpId = $step['kp_id'] ?? 'K-GENERAL';
  265. $stepAnalysis[] = [
  266. 'step_index' => $step['step_index'],
  267. 'is_correct' => $step['is_correct'],
  268. 'kp_id' => $kpId,
  269. 'description' => $step['description'] ?? ''
  270. ];
  271. }
  272. }
  273. // 知识点关联
  274. $knowledgePoints = array_map(function($kp) {
  275. return [
  276. 'kp_id' => $kp['kp_id'],
  277. 'kp_name' => $kp['kp_name'] ?? $kp['kp_id'],
  278. 'weight' => $kp['weight'] ?? 1.0
  279. ];
  280. }, $mapping['kp_mapping']);
  281. $analysis[] = [
  282. 'question_id' => $questionId,
  283. 'score_obtained' => $score,
  284. 'max_score' => $maxScore,
  285. 'accuracy_rate' => $maxScore > 0 ? $score / $maxScore : 0,
  286. 'step_analysis' => $stepAnalysis,
  287. 'knowledge_points' => $knowledgePoints,
  288. 'performance_summary' => $this->generateQuestionPerformanceSummary($question, $stepAnalysis)
  289. ];
  290. }
  291. return $analysis;
  292. }
  293. /**
  294. * 生成知识点维度分析
  295. */
  296. private function analyzeKnowledgePoints(array $knowledgeMasteryVector, array $questionMappings): array
  297. {
  298. $analysis = [];
  299. foreach ($knowledgeMasteryVector as $kpId => $data) {
  300. $analysis[] = [
  301. 'kp_id' => $kpId,
  302. 'mastery_level' => $data['mastery'],
  303. 'confidence_level' => $data['confidence'],
  304. 'performance_in_exam' => $this->evaluatePerformanceLevel($data['mastery']),
  305. 'evidence_count' => count($data['step_details']),
  306. 'step_evidence' => $data['step_details'],
  307. 'recommendation' => $this->generateKnowledgePointRecommendation($data)
  308. ];
  309. }
  310. return $analysis;
  311. }
  312. /**
  313. * 生成整体掌握度总结
  314. */
  315. private function generateOverallSummary(array $updatedMastery): array
  316. {
  317. $knowledgePoints = array_values($updatedMastery);
  318. if (empty($knowledgePoints)) {
  319. return [
  320. 'total_knowledge_points' => 0,
  321. 'average_mastery' => 0,
  322. 'mastery_distribution' => [
  323. 'mastered' => 0,
  324. 'good' => 0,
  325. 'weak' => 0
  326. ],
  327. 'top_strengths' => [],
  328. 'top_weaknesses' => []
  329. ];
  330. }
  331. // 计算平均掌握度
  332. $averageMastery = array_sum(array_column($knowledgePoints, 'current_mastery')) / count($knowledgePoints);
  333. // 掌握度分布
  334. $mastered = array_filter($knowledgePoints, fn($kp) => $kp['current_mastery'] >= 0.85);
  335. $good = array_filter($knowledgePoints, fn($kp) => $kp['current_mastery'] >= 0.70 && $kp['current_mastery'] < 0.85);
  336. $weak = array_filter($knowledgePoints, fn($kp) => $kp['current_mastery'] < 0.70);
  337. // 排序找出优势和薄弱点
  338. usort($knowledgePoints, fn($a, $b) => $b['current_mastery'] <=> $a['current_mastery']);
  339. $topStrengths = array_slice($knowledgePoints, 0, 3);
  340. $topWeaknesses = array_slice(array_reverse($knowledgePoints), 0, 3);
  341. return [
  342. 'total_knowledge_points' => count($knowledgePoints),
  343. 'average_mastery' => round($averageMastery, 4),
  344. 'mastery_distribution' => [
  345. 'mastered' => count($mastered),
  346. 'good' => count($good),
  347. 'weak' => count($weak)
  348. ],
  349. 'top_strengths' => $topStrengths,
  350. 'top_weaknesses' => $topWeaknesses,
  351. 'overall_performance' => $this->evaluateOverallPerformance($averageMastery)
  352. ];
  353. }
  354. /**
  355. * 生成智能出卷推荐依据
  356. * 基于文档中的推荐优先级算法
  357. */
  358. private function generateSmartQuizRecommendation(array $updatedMastery): array
  359. {
  360. $recommendations = [];
  361. foreach ($updatedMastery as $kpId => $data) {
  362. $mastery = $data['current_mastery'];
  363. $confidence = $data['confidence'];
  364. $weight = $data['weight'];
  365. // 推荐优先级 = (1 - 掌握度) * 重要性 * 覆盖需求
  366. // 重要性可以根据知识点在中考/阶段考试中的权重,这里简化为1.0
  367. $importance = 1.0;
  368. // 覆盖需求:最近没考过或考得少,值大
  369. $coverageNeed = max(1.0, 1.5 - ($weight / 10));
  370. $priority = (1 - $mastery) * $importance * $coverageNeed;
  371. $recommendations[] = [
  372. 'kp_id' => $kpId,
  373. 'current_mastery' => $mastery,
  374. 'priority' => $priority,
  375. 'recommended_questions' => $this->calculateRecommendedQuestions($mastery),
  376. 'focus_type' => $this->determineFocusType($mastery)
  377. ];
  378. }
  379. // 按优先级排序
  380. usort($recommendations, fn($a, $b) => $b['priority'] <=> $a['priority']);
  381. // 控制难度节奏:40%巩固型 + 40%修补型 + 20%挑战型
  382. $totalRecommendations = count($recommendations);
  383. $consolidation = array_slice($recommendations, 0, intval($totalRecommendations * 0.4));
  384. $remediation = array_slice($recommendations, intval($totalRecommendations * 0.4), intval($totalRecommendations * 0.4));
  385. $challenge = array_slice($recommendations, intval($totalRecommendations * 0.8));
  386. return [
  387. 'priority_list' => $recommendations,
  388. 'quiz_structure' => [
  389. 'consolidation_type' => $consolidation,
  390. 'remediation_type' => $remediation,
  391. 'challenge_type' => $challenge
  392. ],
  393. 'total_recommended_questions' => array_sum(array_column($recommendations, 'recommended_questions'))
  394. ];
  395. }
  396. /**
  397. * 保存考试答题记录
  398. */
  399. private function saveExamAnswerRecords(array $examData): void
  400. {
  401. $studentId = $examData['student_id'];
  402. $examId = $examData['exam_id'];
  403. foreach ($examData['questions'] as $question) {
  404. $questionId = $question['question_id'];
  405. $steps = $question['steps'] ?? [];
  406. // 保存步骤级记录
  407. if (!empty($steps)) {
  408. foreach ($steps as $step) {
  409. DB::connection('pgsql')->table('student_answer_steps')->insert([
  410. 'student_id' => $studentId,
  411. 'exam_id' => $examId,
  412. 'question_id' => $questionId,
  413. 'step_index' => $step['step_index'],
  414. 'kp_id' => $step['kp_id'] ?? 'K-GENERAL',
  415. 'is_correct' => $step['is_correct'],
  416. 'step_score' => $step['score'] ?? 0,
  417. 'created_at' => now(),
  418. 'updated_at' => now(),
  419. ]);
  420. }
  421. } else {
  422. // 保存题目级记录
  423. DB::connection('pgsql')->table('student_answer_questions')->insert([
  424. 'student_id' => $studentId,
  425. 'exam_id' => $examId,
  426. 'question_id' => $questionId,
  427. 'score_obtained' => $question['score_obtained'] ?? 0,
  428. 'max_score' => $question['score'] ?? 0,
  429. 'created_at' => now(),
  430. 'updated_at' => now(),
  431. ]);
  432. }
  433. }
  434. }
  435. /**
  436. * 保存分析结果
  437. */
  438. private function saveAnalysisResult(string $studentId, string $examId, array $result): void
  439. {
  440. DB::connection('pgsql')->table('exam_analysis_results')->insert([
  441. 'student_id' => $studentId,
  442. 'exam_id' => $examId,
  443. 'analysis_data' => json_encode($result),
  444. 'created_at' => now(),
  445. 'updated_at' => now(),
  446. ]);
  447. }
  448. /**
  449. * 判断掌握度趋势
  450. */
  451. private function determineMasteryTrend(float $previous, float $current): string
  452. {
  453. $change = $current - $previous;
  454. if ($change > 0.1) {
  455. return 'improving';
  456. } elseif ($change < -0.1) {
  457. return 'declining';
  458. } else {
  459. return 'stable';
  460. }
  461. }
  462. /**
  463. * 评估表现水平
  464. */
  465. private function evaluatePerformanceLevel(float $mastery): string
  466. {
  467. if ($mastery >= 0.85) {
  468. return 'excellent';
  469. } elseif ($mastery >= 0.70) {
  470. return 'good';
  471. } elseif ($mastery >= 0.50) {
  472. return 'fair';
  473. } else {
  474. return 'poor';
  475. }
  476. }
  477. /**
  478. * 生成题目表现总结
  479. */
  480. private function generateQuestionPerformanceSummary(array $question, array $stepAnalysis): string
  481. {
  482. if (empty($stepAnalysis)) {
  483. return '整题作答';
  484. }
  485. $correctSteps = count(array_filter($stepAnalysis, fn($s) => $s['is_correct']));
  486. $totalSteps = count($stepAnalysis);
  487. if ($correctSteps === $totalSteps) {
  488. return '所有步骤正确';
  489. } elseif ($correctSteps > 0) {
  490. return "部分正确 ({$correctSteps}/{$totalSteps} 步骤正确)";
  491. } else {
  492. return '所有步骤错误';
  493. }
  494. }
  495. /**
  496. * 生成知识点建议
  497. */
  498. private function generateKnowledgePointRecommendation(array $data): string
  499. {
  500. $mastery = $data['mastery'];
  501. if ($mastery >= 0.85) {
  502. return '掌握良好,可安排综合练习';
  503. } elseif ($mastery >= 0.70) {
  504. return '基本掌握,建议加强练习';
  505. } elseif ($mastery >= 0.50) {
  506. return '需要重点练习,建议安排专项训练';
  507. } else {
  508. return '薄弱知识点,建议系统学习和大量练习';
  509. }
  510. }
  511. /**
  512. * 评估整体表现
  513. */
  514. private function evaluateOverallPerformance(float $averageMastery): string
  515. {
  516. if ($averageMastery >= 0.85) {
  517. return '优秀';
  518. } elseif ($averageMastery >= 0.70) {
  519. return '良好';
  520. } elseif ($averageMastery >= 0.50) {
  521. return '一般';
  522. } else {
  523. return '需加强';
  524. }
  525. }
  526. /**
  527. * 计算推荐题目数量
  528. */
  529. private function calculateRecommendedQuestions(float $mastery): int
  530. {
  531. if ($mastery >= 0.85) {
  532. return 1; // 巩固型:1题
  533. } elseif ($mastery >= 0.50) {
  534. return 2; // 修补型:2题
  535. } else {
  536. return 3; // 挑战型:3题
  537. }
  538. }
  539. /**
  540. * 确定重点类型
  541. */
  542. private function determineFocusType(float $mastery): string
  543. {
  544. if ($mastery >= 0.70 && $mastery < 0.85) {
  545. return 'consolidation'; // 巩固型
  546. } elseif ($mastery < 0.70) {
  547. return 'remediation'; // 修补型
  548. } else {
  549. return 'challenge'; // 挑战型
  550. }
  551. }
  552. }