StudentAnswerAnalysisController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 App\Services\MasteryCalculator;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. class StudentAnswerAnalysisController extends Controller
  13. {
  14. public function __construct(
  15. private readonly TaskManager $taskManager,
  16. private readonly LocalAIAnalysisService $aiAnalysisService,
  17. private readonly StudentAnswerAnalysisService $answerAnalysisService,
  18. private readonly MasteryCalculator $masteryCalculator
  19. ) {}
  20. /**
  21. * 将学案难度分类转换为1-4等级
  22. */
  23. private function mapDifficultyCategoryToLevel(string $difficultyCategory): int
  24. {
  25. // 移除空格并转为小写比较
  26. $category = trim($difficultyCategory);
  27. // 数字直接返回
  28. if (is_numeric($category)) {
  29. return max(1, min(4, intval($category)));
  30. }
  31. // 中文映射
  32. return match ($category) {
  33. '基础', '1' => 1, // 筑基
  34. '进阶', '中等', '2' => 2, // 提分
  35. '培优', '3' => 3, // 培优
  36. '竞赛', '4' => 4, // 竞赛
  37. default => 2, // 默认中级(提分)
  38. };
  39. }
  40. /**
  41. * 获取试卷的基准难度
  42. */
  43. private function getExamBaseDifficulty(string $paperId): int
  44. {
  45. try {
  46. // 从papers表获取difficulty_category
  47. $paper = DB::table('papers')
  48. ->where('paper_id', $paperId)
  49. ->first();
  50. if (!$paper || empty($paper->difficulty_category)) {
  51. Log::warning('未找到试卷或难度分类,使用默认难度', [
  52. 'paper_id' => $paperId,
  53. 'difficulty_category' => $paper->difficulty_category ?? null,
  54. ]);
  55. return 2; // 默认中级(提分)
  56. }
  57. $difficultyLevel = $this->mapDifficultyCategoryToLevel($paper->difficulty_category);
  58. Log::info('获取试卷基准难度', [
  59. 'paper_id' => $paperId,
  60. 'difficulty_category' => $paper->difficulty_category,
  61. 'difficulty_level' => $difficultyLevel,
  62. ]);
  63. return $difficultyLevel;
  64. } catch (\Exception $e) {
  65. Log::error('获取试卷基准难度失败,使用默认难度', [
  66. 'paper_id' => $paperId,
  67. 'error' => $e->getMessage(),
  68. ]);
  69. return 2; // 默认中级(提分)
  70. }
  71. }
  72. /**
  73. * 更新父节点掌握度(基于子节点平均值)
  74. */
  75. private function updateParentMastery(string $studentId, string $kpCode): void
  76. {
  77. try {
  78. // 获取知识点的父节点
  79. $knowledgePoint = DB::table('knowledge_points')
  80. ->where('kp_code', $kpCode)
  81. ->first();
  82. if (!$knowledgePoint || empty($knowledgePoint->parent_kp_code)) {
  83. return; // 没有父节点,无需更新
  84. }
  85. $parentKpCode = $knowledgePoint->parent_kp_code;
  86. // 使用MasteryCalculator计算父节点掌握度
  87. $parentMastery = $this->masteryCalculator->calculateParentMastery($studentId, $parentKpCode);
  88. // 保存到数据库(作为独立记录)
  89. DB::table('student_knowledge_mastery')
  90. ->updateOrInsert(
  91. ['student_id' => $studentId, 'kp_code' => $parentKpCode],
  92. [
  93. 'mastery_level' => $parentMastery,
  94. 'confidence_level' => 0.8, // 父节点置信度默认较高
  95. 'total_attempts' => DB::raw('COALESCE(total_attempts, 0)'),
  96. 'correct_attempts' => DB::raw('COALESCE(correct_attempts, 0)'),
  97. 'mastery_trend' => 'calculated', // 标记为计算得出,非直接测量
  98. 'last_mastery_update' => now(),
  99. 'updated_at' => now(),
  100. 'notes' => '父节点掌握度(基于子节点平均值计算)',
  101. ]
  102. );
  103. Log::info('父节点掌握度已更新', [
  104. 'student_id' => $studentId,
  105. 'child_kp_code' => $kpCode,
  106. 'parent_kp_code' => $parentKpCode,
  107. 'parent_mastery' => $parentMastery,
  108. ]);
  109. // 递归更新更上层的父节点
  110. $this->updateParentMastery($studentId, $parentKpCode);
  111. } catch (\Exception $e) {
  112. Log::error('更新父节点掌握度失败', [
  113. 'student_id' => $studentId,
  114. 'kp_code' => $kpCode,
  115. 'error' => $e->getMessage(),
  116. ]);
  117. }
  118. }
  119. /**
  120. * 接收学生作答结果并进行分析
  121. *
  122. * @param Request $request
  123. * @return JsonResponse
  124. */
  125. public function submitAnswers(Request $request): JsonResponse
  126. {
  127. // 优先从JSON body获取参数,支持向后兼容
  128. $payload = $request->json()->all();
  129. if (empty($payload)) {
  130. $payload = $request->all();
  131. }
  132. // student_id类型转换:支持数字和字符串输入
  133. if (isset($payload['student_id'])) {
  134. $payload['student_id'] = (string) $payload['student_id'];
  135. }
  136. // 【修复】验证参数 - 支持questions数组和is_correct数组格式
  137. $validator = validator($payload, [
  138. 'paper_id' => 'required|string',
  139. 'student_id' => 'required|string|min:1',
  140. 'questions' => 'required|array',
  141. 'questions.*.question_bank_id' => 'required|integer',
  142. 'questions.*.student_answer' => 'nullable|string',
  143. 'questions.*.is_correct' => 'required|array', // 支持数组格式(多小题/步骤)
  144. 'questions.*.teacher_comment' => 'nullable|string',
  145. ]);
  146. if ($validator->fails()) {
  147. return response()->json([
  148. 'success' => false,
  149. 'message' => '参数错误',
  150. 'errors' => $validator->errors(),
  151. ], 422);
  152. }
  153. $data = $validator->validated();
  154. try {
  155. // 使用TaskManager创建异步任务
  156. $taskId = $this->taskManager->createTask(
  157. TaskManager::TASK_TYPE_ANALYSIS,
  158. array_merge($data, ['type' => 'answer_analysis'])
  159. );
  160. Log::info('StudentAnswerAnalysisController: 收到作答结果', [
  161. 'task_id' => $taskId,
  162. 'paper_id' => $data['paper_id'],
  163. 'student_id' => $data['student_id'],
  164. 'question_count' => count($data['questions']),
  165. ]);
  166. // 触发后台分析处理
  167. $this->processAnswerAnalysis($taskId, $data);
  168. return response()->json([
  169. 'success' => true,
  170. 'message' => '作答结果已提交,正在分析中...',
  171. 'data' => [
  172. 'task_id' => $taskId,
  173. 'paper_id' => $data['paper_id'],
  174. 'student_id' => $data['student_id'],
  175. 'status' => 'processing',
  176. 'created_at' => now()->toISOString(),
  177. ],
  178. ]);
  179. } catch (\Exception $e) {
  180. Log::error('提交作答结果失败', [
  181. 'paper_id' => $data['paper_id'] ?? 'unknown',
  182. 'student_id' => $data['student_id'] ?? 'unknown',
  183. 'error' => $e->getMessage(),
  184. ]);
  185. return response()->json([
  186. 'success' => false,
  187. 'message' => '提交失败:' . $e->getMessage(),
  188. ], 500);
  189. }
  190. }
  191. /**
  192. * 查询分析任务状态
  193. */
  194. public function getAnalysisStatus(string $taskId): JsonResponse
  195. {
  196. try {
  197. $task = $this->taskManager->getTaskStatus($taskId);
  198. if (!$task) {
  199. return response()->json([
  200. 'success' => false,
  201. 'message' => '任务不存在',
  202. ], 404);
  203. }
  204. return response()->json([
  205. 'success' => true,
  206. 'data' => $task,
  207. ]);
  208. } catch (\Exception $e) {
  209. Log::error('查询分析状态失败', [
  210. 'task_id' => $taskId,
  211. 'error' => $e->getMessage(),
  212. ]);
  213. return response()->json([
  214. 'success' => false,
  215. 'message' => '查询失败:' . $e->getMessage(),
  216. ], 500);
  217. }
  218. }
  219. /**
  220. * 处理作答分析(后台任务)
  221. */
  222. private function processAnswerAnalysis(string $taskId, array $data): void
  223. {
  224. try {
  225. $this->taskManager->updateTaskProgress($taskId, 10, '正在处理缺题(默认正确)...');
  226. // 处理缺题:对于没有提交的题目,默认标记为正确
  227. $allAnswers = $this->processMissingQuestions($data);
  228. $this->taskManager->updateTaskProgress($taskId, 30, '正在保存作答记录...');
  229. // 保存作答记录到数据库(包含缺题)
  230. $answerRecord = $this->answerAnalysisService->saveAnswerRecord([
  231. ...$data,
  232. 'answers' => $allAnswers,
  233. ]);
  234. $this->taskManager->updateTaskProgress($taskId, 50, '正在分析每道题(包括缺题处理)...');
  235. // 简化分析:不调用AI,直接使用基础分析
  236. $questionAnalyses = [];
  237. foreach ($allAnswers as $answer) {
  238. $questionAnalyses[] = [
  239. 'question_id' => $answer['question_id'],
  240. 'question_number' => $answer['question_number'] ?? null,
  241. 'kp_code' => $answer['knowledge_point'] ?? null,
  242. 'student_answer' => $answer['student_answer'] ?? '',
  243. 'correct_answer' => $answer['correct_answer'] ?? '',
  244. 'is_correct' => $answer['is_correct'],
  245. 'score_obtained' => (float) ($answer['score'] ?? 0),
  246. 'max_score' => (float) ($answer['max_score'] ?? 10),
  247. 'difficulty' => 0.5,
  248. 'is_missing' => $answer['is_missing'] ?? false,
  249. 'model_used' => 'simple-rules',
  250. ];
  251. }
  252. $this->taskManager->updateTaskProgress($taskId, 60, '正在计算掌握度(包括子知识点动态加减和父节点平均)...');
  253. // 【新算法】使用MasteryCalculator计算掌握度
  254. // 1. 获取学案基准难度
  255. $examBaseDifficulty = $this->getExamBaseDifficulty($data['paper_id']);
  256. // 2. 为每个知识点计算掌握度
  257. $masteryResults = [];
  258. foreach ($questionAnalyses as $analysis) {
  259. if (empty($analysis['kp_code']) || $analysis['is_missing']) {
  260. continue; // 跳过没有知识点或缺题的题目
  261. }
  262. $kpCode = $analysis['kp_code'];
  263. if (!isset($masteryResults[$kpCode])) {
  264. $masteryResults[$kpCode] = [];
  265. }
  266. $masteryResults[$kpCode][] = [
  267. 'question_id' => $analysis['question_id'],
  268. 'is_correct' => $analysis['is_correct'],
  269. 'question_difficulty' => $analysis['difficulty'] ?? 0.5,
  270. ];
  271. }
  272. // 3. 调用MasteryCalculator计算每个知识点的掌握度
  273. foreach ($masteryResults as $kpCode => $attempts) {
  274. $this->masteryCalculator->calculateMasteryLevel(
  275. $data['student_id'],
  276. $kpCode,
  277. $attempts,
  278. $examBaseDifficulty
  279. );
  280. // 4. 计算父节点掌握度(子节点平均值)
  281. $this->updateParentMastery($data['student_id'], $kpCode);
  282. }
  283. $this->taskManager->updateTaskProgress($taskId, 65, '正在保存分析结果...');
  284. // 准备分析结果数据
  285. $analysisData = [
  286. 'question_results' => $questionAnalyses,
  287. 'total_questions' => count($questionAnalyses),
  288. 'correct_count' => count(array_filter($questionAnalyses, function($q) { return $q['is_correct'] ?? false; })),
  289. 'wrong_count' => count(array_filter($questionAnalyses, function($q) { return !($q['is_correct'] ?? true); })),
  290. 'model_used' => $questionAnalyses[0]['model_used'] ?? 'unknown',
  291. 'exam_base_difficulty' => $examBaseDifficulty,
  292. ];
  293. // 保存分析结果
  294. $this->answerAnalysisService->saveAnalysisResults($answerRecord, $analysisData, $questionAnalyses);
  295. $this->taskManager->updateTaskProgress($taskId, 80, '正在生成掌握度快照...');
  296. // 生成掌握度快照(记录每次分析的掌握度变化)
  297. $masterySnapshot = $this->answerAnalysisService->createMasterySnapshot(
  298. $data['student_id'],
  299. $data['paper_id'],
  300. $answerRecord['record_id']
  301. );
  302. $this->taskManager->updateTaskProgress($taskId, 90, '正在生成学情分析报告...');
  303. // 生成学情分析报告PDF
  304. $reportUrl = $this->generateLearningReport($taskId, $data, $answerRecord, $questionAnalyses, $masterySnapshot);
  305. // 标记任务完成
  306. $this->taskManager->markTaskCompleted($taskId, [
  307. 'answer_record_id' => $answerRecord['record_id'],
  308. 'analysis_id' => 'analysis_' . uniqid(),
  309. 'mastery_snapshot_id' => $masterySnapshot['snapshot_id'] ?? null,
  310. 'correct_count' => $answerRecord['correct_count'],
  311. 'wrong_count' => $answerRecord['wrong_count'],
  312. 'overall_mastery' => $masterySnapshot['overall_mastery'] ?? null,
  313. // 'report_url' => $reportUrl, // 临时禁用PDF报告
  314. ]);
  315. Log::info('作答分析完成', [
  316. 'task_id' => $taskId,
  317. 'paper_id' => $data['paper_id'],
  318. 'student_id' => $data['student_id'],
  319. 'answer_record_id' => $answerRecord['record_id'],
  320. ]);
  321. // 发送回调通知
  322. $this->taskManager->sendCallback($taskId);
  323. } catch (\Exception $e) {
  324. Log::error('作答分析失败', [
  325. 'task_id' => $taskId,
  326. 'paper_id' => $data['paper_id'],
  327. 'student_id' => $data['student_id'],
  328. 'error' => $e->getMessage(),
  329. ]);
  330. $this->taskManager->markTaskFailed($taskId, $e->getMessage());
  331. }
  332. }
  333. /**
  334. * 获取题目文本内容
  335. */
  336. private function getQuestionText(string $questionId): string
  337. {
  338. try {
  339. // 这里可以调用 QuestionBankService 获取题目内容
  340. // 目前返回空字符串,让AI分析基于学生答案进行分析
  341. return '';
  342. } catch (\Exception $e) {
  343. Log::warning('获取题目文本失败', [
  344. 'question_id' => $questionId,
  345. 'error' => $e->getMessage(),
  346. ]);
  347. return '';
  348. }
  349. }
  350. /**
  351. * 处理缺题逻辑:对于没有提交的题目,默认标记为正确
  352. * 通过paper_id查询paper_question表获取总题数
  353. */
  354. private function processMissingQuestions(array $data): array
  355. {
  356. // 【修复】处理questions数组格式
  357. $questions = $data['questions'] ?? [];
  358. $submittedQuestionIds = array_column($questions, 'question_bank_id');
  359. // 将questions转换为answers格式以便后续处理
  360. $answers = [];
  361. foreach ($questions as $q) {
  362. // 计算is_correct数组的平均值(判断整体是否正确)
  363. $isCorrectArray = $q['is_correct'] ?? [];
  364. $correctSteps = array_sum($isCorrectArray);
  365. $totalSteps = count($isCorrectArray);
  366. $isOverallCorrect = $totalSteps > 0 && ($correctSteps / $totalSteps) >= 0.6; // 60%以上步骤正确视为正确
  367. $answers[] = [
  368. 'question_id' => $q['question_bank_id'],
  369. 'question_number' => null,
  370. 'is_correct' => $isOverallCorrect,
  371. 'student_answer' => $q['student_answer'] ?? '',
  372. 'correct_answer' => '',
  373. 'score' => $correctSteps * 2, // 假设每个步骤2分
  374. 'max_score' => $totalSteps * 2,
  375. 'knowledge_point' => null,
  376. 'question_type' => 'mixed',
  377. 'is_missing' => false,
  378. 'is_correct_array' => $isCorrectArray, // 保留原始数组
  379. 'answer_time' => $data['answer_time'] ?? now(),
  380. ];
  381. }
  382. // 获取缺题列表
  383. $missingQuestions = $data['missing_questions'] ?? [];
  384. // 如果没有提供missing_questions,通过paper_id查询paper_question表
  385. if (empty($missingQuestions)) {
  386. try {
  387. // 通过paper_id查询paper_question表获取题目总数
  388. $totalQuestions = DB::table('paper_questions')
  389. ->where('paper_id', $data['paper_id'])
  390. ->count();
  391. Log::info('从paper_question表获取题目总数', [
  392. 'paper_id' => $data['paper_id'],
  393. 'total_questions' => $totalQuestions,
  394. 'submitted_count' => count($submittedQuestionIds),
  395. ]);
  396. // 自动生成缺题列表(根据paper_question表的题目编号)
  397. $allQuestionIds = DB::table('paper_questions')
  398. ->where('paper_id', $data['paper_id'])
  399. ->pluck('question_id')
  400. ->toArray();
  401. foreach ($allQuestionIds as $questionId) {
  402. if (!in_array($questionId, $submittedQuestionIds)) {
  403. $missingQuestions[] = $questionId;
  404. }
  405. }
  406. } catch (\Exception $e) {
  407. Log::warning('查询paper_question表失败,跳过缺题处理', [
  408. 'paper_id' => $data['paper_id'],
  409. 'error' => $e->getMessage(),
  410. ]);
  411. }
  412. }
  413. // 【修复】处理缺题和学生未作答的题目
  414. foreach ($missingQuestions as $missingQuestionId) {
  415. // 使用 Model 获取缺题的详细信息
  416. $questionDetails = \App\Models\PaperQuestion::where('paper_id', $data['paper_id'])
  417. ->where('question_id', $missingQuestionId)
  418. ->first();
  419. // 获取知识点和分数信息
  420. $kpCode = $questionDetails?->knowledge_point;
  421. if (empty($kpCode)) {
  422. // 【修复】不允许使用默认知识点,必须明确指定
  423. Log::warning('StudentAnswerAnalysisController: 缺题缺少知识点信息,跳过掌握度计算', [
  424. 'question_id' => $missingQuestionId,
  425. 'paper_id' => $data['paper_id'],
  426. ]);
  427. continue; // 跳过该缺题,不参与掌握度计算
  428. }
  429. $maxScore = floatval($questionDetails?->score ?? 10);
  430. $questionType = $questionDetails?->question_type ?? 'missing';
  431. // 【关键】区分缺题和学生未作答:
  432. // 1. 缺题(API请求中标记的):按正确计算,100%得分
  433. // 2. 学生没作答的题目:按30%得分率计算
  434. $isTrulyMissing = in_array($missingQuestionId, $data['missing_questions'] ?? []);
  435. $scoreRate = $isTrulyMissing ? 1.0 : 0.3; // 缺题100%,未作答30%
  436. $score = $maxScore * $scoreRate;
  437. $isCorrect = ($scoreRate >= 0.6); // 按60%及格线判断
  438. $answers[] = [
  439. 'question_id' => $missingQuestionId,
  440. 'question_number' => $questionDetails?->question_number ?? $missingQuestionId,
  441. 'is_correct' => $isCorrect,
  442. 'student_answer' => '[缺题]',
  443. 'correct_answer' => '[未作答]',
  444. 'score' => $score,
  445. 'max_score' => $maxScore,
  446. 'knowledge_point' => $kpCode, // 保留知识点信息,参与掌握度计算
  447. 'question_type' => $questionType,
  448. 'is_missing' => $isTrulyMissing, // 真正缺题标记
  449. 'missing_note' => $isTrulyMissing ? '缺题,按100%得分率计算' : '学生未作答,按30%得分率计算掌握度',
  450. 'answer_time' => $data['answer_time'] ?? now(),
  451. ];
  452. }
  453. // 【新增】处理已提交但学生未作答的题目(student_answer为空或null)
  454. foreach ($answers as &$answer) {
  455. if (!empty($answer['student_answer']) && $answer['student_answer'] !== '[缺题]') {
  456. continue; // 已作答的题目跳过
  457. }
  458. // 学生未作答的题目,按30%得分率计算
  459. if (empty($answer['student_answer']) || $answer['student_answer'] === '') {
  460. $maxScore = floatval($answer['max_score'] ?? 10);
  461. $scoreRate = 0.3;
  462. $score = $maxScore * $scoreRate;
  463. $answer['score'] = $score;
  464. $answer['is_correct'] = false; // 未作答视为错误
  465. $answer['missing_note'] = '学生未作答,按30%得分率计算掌握度';
  466. }
  467. }
  468. Log::info('缺题处理完成', [
  469. 'paper_id' => $data['paper_id'],
  470. 'submitted_count' => count($data['questions'] ?? []),
  471. 'missing_count' => count($missingQuestions),
  472. 'total_count' => count($answers),
  473. ]);
  474. return $answers;
  475. }
  476. /**
  477. * 生成学情分析报告并异步生成PDF
  478. */
  479. private function generateLearningReport(
  480. string $taskId,
  481. array $data,
  482. array $answerRecord,
  483. array $questionAnalyses,
  484. ?array $masterySnapshot
  485. ): ?string {
  486. try {
  487. // 构建报告数据
  488. $reportData = [
  489. 'task_id' => $taskId,
  490. 'paper_id' => $data['paper_id'],
  491. 'student_id' => $data['student_id'],
  492. 'submit_time' => now()->toISOString(),
  493. 'answer_record' => $answerRecord,
  494. 'question_analyses' => $questionAnalyses,
  495. 'mastery_snapshot' => $masterySnapshot,
  496. 'report_type' => 'learning_analysis',
  497. ];
  498. // 创建异步任务生成PDF
  499. $pdfTaskId = $this->taskManager->createTask(
  500. TaskManager::TASK_TYPE_PDF,
  501. array_merge($reportData, ['type' => 'learning_report'])
  502. );
  503. Log::info('学情分析报告任务已创建', [
  504. 'pdf_task_id' => $pdfTaskId,
  505. 'paper_id' => $data['paper_id'],
  506. 'student_id' => $data['student_id'],
  507. ]);
  508. // 返回报告URL(异步生成)
  509. return route('api.reports.learning', [
  510. 'task_id' => $pdfTaskId,
  511. 'student_id' => $data['student_id'],
  512. ]);
  513. } catch (\Exception $e) {
  514. Log::error('生成学情分析报告失败', [
  515. 'task_id' => $taskId,
  516. 'error' => $e->getMessage(),
  517. ]);
  518. return null;
  519. }
  520. }
  521. /**
  522. * 获取学生学习历史
  523. */
  524. public function getStudentLearningHistory(string $studentId): JsonResponse
  525. {
  526. try {
  527. $history = $this->answerAnalysisService->getStudentLearningHistory($studentId);
  528. return response()->json([
  529. 'success' => true,
  530. 'data' => $history,
  531. ]);
  532. } catch (\Exception $e) {
  533. Log::error('获取学习历史失败', [
  534. 'student_id' => $studentId,
  535. 'error' => $e->getMessage(),
  536. ]);
  537. return response()->json([
  538. 'success' => false,
  539. 'message' => '获取失败:' . $e->getMessage(),
  540. ], 500);
  541. }
  542. }
  543. }