StudentAnswerAnalysisController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\TaskManager;
  5. use App\Services\LocalAIAnalysisService;
  6. use App\Services\StudentAnswerAnalysisService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. class StudentAnswerAnalysisController extends Controller
  12. {
  13. public function __construct(
  14. private readonly TaskManager $taskManager,
  15. private readonly LocalAIAnalysisService $aiAnalysisService,
  16. private readonly StudentAnswerAnalysisService $answerAnalysisService
  17. ) {}
  18. /**
  19. * 接收学生作答结果并进行分析
  20. *
  21. * @param Request $request
  22. * @return JsonResponse
  23. */
  24. public function submitAnswers(Request $request): JsonResponse
  25. {
  26. // 优先从JSON body获取参数,支持向后兼容
  27. $payload = $request->json()->all();
  28. if (empty($payload)) {
  29. $payload = $request->all();
  30. }
  31. // student_id类型转换:支持数字和字符串输入
  32. if (isset($payload['student_id'])) {
  33. $payload['student_id'] = (string) $payload['student_id'];
  34. }
  35. // 验证参数
  36. $validator = validator($payload, [
  37. 'paper_id' => 'required|string',
  38. 'student_id' => 'required|string|min:1',
  39. 'answers' => 'required|array',
  40. 'answers.*.question_id' => 'required|string',
  41. 'answers.*.question_number' => 'nullable|string',
  42. 'answers.*.is_correct' => 'required|boolean',
  43. 'answers.*.student_answer' => 'nullable|string',
  44. 'answers.*.correct_answer' => 'nullable|string',
  45. 'answers.*.score' => 'nullable|numeric',
  46. 'answers.*.max_score' => 'nullable|numeric',
  47. 'answers.*.step_scores' => 'nullable|array', // 简答题步骤得分
  48. 'answers.*.knowledge_point' => 'nullable|string',
  49. 'answers.*.question_type' => 'nullable|string',
  50. 'answer_time' => 'nullable|timestamp',
  51. 'submit_time' => 'nullable|timestamp',
  52. 'source_system' => 'nullable|string',
  53. 'callback_url' => 'nullable|url',
  54. 'missing_questions' => 'nullable|array', // 缺题列表(可选)
  55. ]);
  56. if ($validator->fails()) {
  57. return response()->json([
  58. 'success' => false,
  59. 'message' => '参数错误',
  60. 'errors' => $validator->errors(),
  61. ], 422);
  62. }
  63. $data = $validator->validated();
  64. try {
  65. // 使用TaskManager创建异步任务
  66. $taskId = $this->taskManager->createTask(
  67. TaskManager::TASK_TYPE_ANALYSIS,
  68. array_merge($data, ['type' => 'answer_analysis'])
  69. );
  70. Log::info('StudentAnswerAnalysisController: 收到作答结果', [
  71. 'task_id' => $taskId,
  72. 'paper_id' => $data['paper_id'],
  73. 'student_id' => $data['student_id'],
  74. 'answer_count' => count($data['answers']),
  75. ]);
  76. // 触发后台分析处理
  77. $this->processAnswerAnalysis($taskId, $data);
  78. return response()->json([
  79. 'success' => true,
  80. 'message' => '作答结果已提交,正在分析中...',
  81. 'data' => [
  82. 'task_id' => $taskId,
  83. 'paper_id' => $data['paper_id'],
  84. 'student_id' => $data['student_id'],
  85. 'status' => 'processing',
  86. 'created_at' => now()->toISOString(),
  87. ],
  88. ]);
  89. } catch (\Exception $e) {
  90. Log::error('提交作答结果失败', [
  91. 'paper_id' => $data['paper_id'] ?? 'unknown',
  92. 'student_id' => $data['student_id'] ?? 'unknown',
  93. 'error' => $e->getMessage(),
  94. ]);
  95. return response()->json([
  96. 'success' => false,
  97. 'message' => '提交失败:' . $e->getMessage(),
  98. ], 500);
  99. }
  100. }
  101. /**
  102. * 查询分析任务状态
  103. */
  104. public function getAnalysisStatus(string $taskId): JsonResponse
  105. {
  106. try {
  107. $task = $this->taskManager->getTaskStatus($taskId);
  108. if (!$task) {
  109. return response()->json([
  110. 'success' => false,
  111. 'message' => '任务不存在',
  112. ], 404);
  113. }
  114. return response()->json([
  115. 'success' => true,
  116. 'data' => $task,
  117. ]);
  118. } catch (\Exception $e) {
  119. Log::error('查询分析状态失败', [
  120. 'task_id' => $taskId,
  121. 'error' => $e->getMessage(),
  122. ]);
  123. return response()->json([
  124. 'success' => false,
  125. 'message' => '查询失败:' . $e->getMessage(),
  126. ], 500);
  127. }
  128. }
  129. /**
  130. * 处理作答分析(后台任务)
  131. */
  132. private function processAnswerAnalysis(string $taskId, array $data): void
  133. {
  134. try {
  135. $this->taskManager->updateTaskProgress($taskId, 10, '正在处理缺题(默认正确)...');
  136. // 处理缺题:对于没有提交的题目,默认标记为正确
  137. $allAnswers = $this->processMissingQuestions($data);
  138. $this->taskManager->updateTaskProgress($taskId, 30, '正在保存作答记录...');
  139. // 保存作答记录到数据库(包含缺题)
  140. $answerRecord = $this->answerAnalysisService->saveAnswerRecord([
  141. ...$data,
  142. 'answers' => $allAnswers,
  143. ]);
  144. $this->taskManager->updateTaskProgress($taskId, 50, '正在分析每道题(包括缺题处理)...');
  145. // 简化分析:不调用AI,直接使用基础分析
  146. $questionAnalyses = [];
  147. foreach ($allAnswers as $answer) {
  148. $questionAnalyses[] = [
  149. 'question_id' => $answer['question_id'],
  150. 'question_number' => $answer['question_number'] ?? null,
  151. 'kp_code' => $answer['knowledge_point'] ?? null,
  152. 'student_answer' => $answer['student_answer'] ?? '',
  153. 'correct_answer' => $answer['correct_answer'] ?? '',
  154. 'is_correct' => $answer['is_correct'],
  155. 'score_obtained' => (float) ($answer['score'] ?? 0),
  156. 'max_score' => (float) ($answer['max_score'] ?? 10),
  157. 'difficulty' => 0.5,
  158. 'is_missing' => $answer['is_missing'] ?? false,
  159. 'model_used' => 'simple-rules',
  160. ];
  161. }
  162. $this->taskManager->updateTaskProgress($taskId, 60, '正在保存分析结果...');
  163. // 准备分析结果数据
  164. $analysisData = [
  165. 'question_results' => $questionAnalyses,
  166. 'total_questions' => count($questionAnalyses),
  167. 'correct_count' => count(array_filter($questionAnalyses, function($q) { return $q['correct'] ?? false; })),
  168. 'wrong_count' => count(array_filter($questionAnalyses, function($q) { return !($q['correct'] ?? true); })),
  169. 'model_used' => $questionAnalyses[0]['model_used'] ?? 'unknown',
  170. ];
  171. // 保存分析结果
  172. $this->answerAnalysisService->saveAnalysisResults($answerRecord, $analysisData, $questionAnalyses);
  173. $this->taskManager->updateTaskProgress($taskId, 80, '正在生成掌握度快照...');
  174. // 生成掌握度快照(记录每次分析的掌握度变化)
  175. $masterySnapshot = $this->answerAnalysisService->createMasterySnapshot(
  176. $data['student_id'],
  177. $data['paper_id'],
  178. $answerRecord['record_id']
  179. );
  180. $this->taskManager->updateTaskProgress($taskId, 90, '正在生成学情分析报告...');
  181. // 生成学情分析报告
  182. $reportUrl = $this->generateLearningReport($taskId, $data, $answerRecord, $questionAnalyses, $masterySnapshot);
  183. // 标记任务完成
  184. $this->taskManager->markTaskCompleted($taskId, [
  185. 'answer_record_id' => $answerRecord['record_id'],
  186. 'analysis_id' => 'analysis_' . uniqid(),
  187. 'mastery_snapshot_id' => $masterySnapshot['snapshot_id'] ?? null,
  188. 'correct_count' => $answerRecord['correct_count'],
  189. 'wrong_count' => $answerRecord['wrong_count'],
  190. 'overall_mastery' => $masterySnapshot['overall_mastery'] ?? null,
  191. 'report_url' => $reportUrl, // 学情分析报告URL
  192. ]);
  193. Log::info('作答分析完成', [
  194. 'task_id' => $taskId,
  195. 'paper_id' => $data['paper_id'],
  196. 'student_id' => $data['student_id'],
  197. 'answer_record_id' => $answerRecord['record_id'],
  198. ]);
  199. // 发送回调通知
  200. $this->taskManager->sendCallback($taskId);
  201. } catch (\Exception $e) {
  202. Log::error('作答分析失败', [
  203. 'task_id' => $taskId,
  204. 'paper_id' => $data['paper_id'],
  205. 'student_id' => $data['student_id'],
  206. 'error' => $e->getMessage(),
  207. ]);
  208. $this->taskManager->markTaskFailed($taskId, $e->getMessage());
  209. }
  210. }
  211. /**
  212. * 获取题目文本内容
  213. */
  214. private function getQuestionText(string $questionId): string
  215. {
  216. try {
  217. // 这里可以调用 QuestionBankService 获取题目内容
  218. // 目前返回空字符串,让AI分析基于学生答案进行分析
  219. return '';
  220. } catch (\Exception $e) {
  221. Log::warning('获取题目文本失败', [
  222. 'question_id' => $questionId,
  223. 'error' => $e->getMessage(),
  224. ]);
  225. return '';
  226. }
  227. }
  228. /**
  229. * 处理缺题逻辑:对于没有提交的题目,默认标记为正确
  230. * 通过paper_id查询paper_question表获取总题数
  231. */
  232. private function processMissingQuestions(array $data): array
  233. {
  234. $answers = $data['answers'] ?? [];
  235. $submittedQuestionIds = array_column($answers, 'question_id');
  236. // 获取缺题列表
  237. $missingQuestions = $data['missing_questions'] ?? [];
  238. // 如果没有提供missing_questions,通过paper_id查询paper_question表
  239. if (empty($missingQuestions)) {
  240. try {
  241. // 通过paper_id查询paper_question表获取题目总数
  242. $totalQuestions = DB::table('paper_questions')
  243. ->where('paper_id', $data['paper_id'])
  244. ->count();
  245. Log::info('从paper_question表获取题目总数', [
  246. 'paper_id' => $data['paper_id'],
  247. 'total_questions' => $totalQuestions,
  248. 'submitted_count' => count($submittedQuestionIds),
  249. ]);
  250. // 自动生成缺题列表(根据paper_question表的题目编号)
  251. $allQuestionIds = DB::table('paper_questions')
  252. ->where('paper_id', $data['paper_id'])
  253. ->pluck('question_id')
  254. ->toArray();
  255. foreach ($allQuestionIds as $questionId) {
  256. if (!in_array($questionId, $submittedQuestionIds)) {
  257. $missingQuestions[] = $questionId;
  258. }
  259. }
  260. } catch (\Exception $e) {
  261. Log::warning('查询paper_question表失败,跳过缺题处理', [
  262. 'paper_id' => $data['paper_id'],
  263. 'error' => $e->getMessage(),
  264. ]);
  265. }
  266. }
  267. // 为每个缺题创建默认正确的记录
  268. foreach ($missingQuestions as $missingQuestionId) {
  269. $answers[] = [
  270. 'question_id' => $missingQuestionId,
  271. 'question_number' => $missingQuestionId,
  272. 'is_correct' => true, // 缺题默认正确
  273. 'student_answer' => '[缺题]',
  274. 'correct_answer' => '[未作答]',
  275. 'score' => 0, // 缺题不计分
  276. 'max_score' => 0,
  277. 'knowledge_point' => null,
  278. 'question_type' => 'missing',
  279. 'is_missing' => true, // 标记为缺题
  280. 'answer_time' => $data['answer_time'] ?? now(),
  281. ];
  282. }
  283. Log::info('缺题处理完成', [
  284. 'paper_id' => $data['paper_id'],
  285. 'submitted_count' => count($data['answers'] ?? []),
  286. 'missing_count' => count($missingQuestions),
  287. 'total_count' => count($answers),
  288. ]);
  289. return $answers;
  290. }
  291. /**
  292. * 生成学情分析报告并异步生成PDF
  293. */
  294. private function generateLearningReport(
  295. string $taskId,
  296. array $data,
  297. array $answerRecord,
  298. array $questionAnalyses,
  299. ?array $masterySnapshot
  300. ): ?string {
  301. try {
  302. // 构建报告数据
  303. $reportData = [
  304. 'task_id' => $taskId,
  305. 'paper_id' => $data['paper_id'],
  306. 'student_id' => $data['student_id'],
  307. 'submit_time' => now()->toISOString(),
  308. 'answer_record' => $answerRecord,
  309. 'question_analyses' => $questionAnalyses,
  310. 'mastery_snapshot' => $masterySnapshot,
  311. 'report_type' => 'learning_analysis',
  312. ];
  313. // 创建异步任务生成PDF
  314. $pdfTaskId = $this->taskManager->createTask(
  315. TaskManager::TASK_TYPE_PDF,
  316. array_merge($reportData, ['type' => 'learning_report'])
  317. );
  318. Log::info('学情分析报告任务已创建', [
  319. 'pdf_task_id' => $pdfTaskId,
  320. 'paper_id' => $data['paper_id'],
  321. 'student_id' => $data['student_id'],
  322. ]);
  323. // 返回报告URL(异步生成)
  324. return route('api.reports.learning', [
  325. 'task_id' => $pdfTaskId,
  326. 'student_id' => $data['student_id'],
  327. ]);
  328. } catch (\Exception $e) {
  329. Log::error('生成学情分析报告失败', [
  330. 'task_id' => $taskId,
  331. 'error' => $e->getMessage(),
  332. ]);
  333. return null;
  334. }
  335. }
  336. /**
  337. * 获取学生学习历史
  338. */
  339. public function getStudentLearningHistory(string $studentId): JsonResponse
  340. {
  341. try {
  342. $history = $this->answerAnalysisService->getStudentLearningHistory($studentId);
  343. return response()->json([
  344. 'success' => true,
  345. 'data' => $history,
  346. ]);
  347. } catch (\Exception $e) {
  348. Log::error('获取学习历史失败', [
  349. 'student_id' => $studentId,
  350. 'error' => $e->getMessage(),
  351. ]);
  352. return response()->json([
  353. 'success' => false,
  354. 'message' => '获取失败:' . $e->getMessage(),
  355. ], 500);
  356. }
  357. }
  358. }