ExamPdfController.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Paper;
  4. use App\Services\QuestionBankService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class ExamPdfController extends Controller
  10. {
  11. /**
  12. * 标准化选项格式为数组值列表
  13. * 支持格式:
  14. * 1. {"A": "0", "B": "5", "C": "-3", "D": "12"} -> ["0", "5", "-3", "12"]
  15. * 2. [["label": "A", "text": "选项A"], ...] -> ["选项A", "选项B", ...]
  16. * 3. ["A", "B", "C", "D"] -> ["A", "B", "C", "D"]
  17. */
  18. private function normalizeOptions($options): array
  19. {
  20. if (empty($options)) {
  21. return [];
  22. }
  23. // 如果是对象格式 {"A": "值1", "B": "值2", ...}
  24. if (is_array($options) && ! isset($options[0])) {
  25. return array_values($options);
  26. }
  27. // 如果是AI生成格式 [{"label": "A", "text": "选项A"}, ...]
  28. if (is_array($options) && isset($options[0]) && is_array($options[0])) {
  29. // 提取text字段,如果不存在则使用整个数组元素
  30. $normalized = [];
  31. foreach ($options as $opt) {
  32. if (isset($opt['text'])) {
  33. $normalized[] = $opt['text'];
  34. } elseif (isset($opt['value'])) {
  35. $normalized[] = $opt['value'];
  36. } else {
  37. // 如果既没有text也没有value,取数组的第一个值
  38. $normalized[] = is_array($opt) ? (string) reset($opt) : (string) $opt;
  39. }
  40. }
  41. return $normalized;
  42. }
  43. // 如果已经是简单数组格式 ["A", "B", "C", "D"]
  44. if (is_array($options)) {
  45. return array_values($options);
  46. }
  47. // 其他情况返回空数组
  48. return [];
  49. }
  50. /**
  51. * 根据题目内容或类型字段判断题型
  52. */
  53. private function determineQuestionType(array $question): string
  54. {
  55. // 优先根据题目内容判断(而不是数据库字段)
  56. $stem = $question['stem'] ?? $question['content'] ?? '';
  57. $tags = $question['tags'] ?? '';
  58. $skills = $question['skills'] ?? [];
  59. // 1. 根据题干内容判断 - 选择题特征:必须包含 A. B. C. D. 选项(至少2个)
  60. if (is_string($stem)) {
  61. // 选择题特征:必须包含 A. B. C. D. 四个选项(至少2个)
  62. $hasOptionA = preg_match('/\bA\s*[\.\、\:]/', $stem) || preg_match('/\(A\)/', $stem) || preg_match('/^A[\.\s]/', $stem);
  63. $hasOptionB = preg_match('/\bB\s*[\.\、\:]/', $stem) || preg_match('/\(B\)/', $stem) || preg_match('/^B[\.\s]/', $stem);
  64. $hasOptionC = preg_match('/\bC\s*[\.\、\:]/', $stem) || preg_match('/\(C\)/', $stem) || preg_match('/^C[\.\s]/', $stem);
  65. $hasOptionD = preg_match('/\bD\s*[\.\、\:]/', $stem) || preg_match('/\(D\)/', $stem) || preg_match('/^D[\.\s]/', $stem);
  66. $hasOptionE = preg_match('/\bE\s*[\.\、\:]/', $stem) || preg_match('/\(E\)/', $stem) || preg_match('/^E[\.\s]/', $stem);
  67. // 至少有2个选项就认为是选择题(降低阈值)
  68. $optionCount = ($hasOptionA ? 1 : 0) + ($hasOptionB ? 1 : 0) + ($hasOptionC ? 1 : 0) + ($hasOptionD ? 1 : 0) + ($hasOptionE ? 1 : 0);
  69. if ($optionCount >= 2) {
  70. return 'choice';
  71. }
  72. // 检查是否有"( )"或"( )"括号,这通常是选择题的标志
  73. if (preg_match('/(\s*)|\(\s*\)/', $stem) && (strpos($stem, 'A.') !== false || strpos($stem, 'B.') !== false || strpos($stem, 'C.') !== false || strpos($stem, 'D.') !== false)) {
  74. return 'choice';
  75. }
  76. }
  77. // 2. 根据技能点判断
  78. if (is_array($skills)) {
  79. $skillsStr = implode(',', $skills);
  80. if (strpos($skillsStr, '选择题') !== false) {
  81. return 'choice';
  82. }
  83. if (strpos($skillsStr, '填空题') !== false) {
  84. return 'fill';
  85. }
  86. if (strpos($skillsStr, '解答题') !== false) {
  87. return 'answer';
  88. }
  89. }
  90. // 3. 根据题目已有类型字段判断(作为后备)
  91. if (! empty($question['question_type'])) {
  92. $type = strtolower(trim($question['question_type']));
  93. if (in_array($type, ['choice', '选择题', 'choice question'])) {
  94. return 'choice';
  95. }
  96. if (in_array($type, ['fill', '填空题', 'fill in the blank'])) {
  97. return 'fill';
  98. }
  99. if (in_array($type, ['answer', '解答题', 'calculation', '简答题'])) {
  100. return 'answer';
  101. }
  102. }
  103. if (! empty($question['type'])) {
  104. $type = strtolower(trim($question['type']));
  105. if (in_array($type, ['choice', '选择题', 'choice question'])) {
  106. return 'choice';
  107. }
  108. if (in_array($type, ['fill', '填空题', 'fill in the blank'])) {
  109. return 'fill';
  110. }
  111. if (in_array($type, ['answer', '解答题', 'calculation', '简答题'])) {
  112. return 'answer';
  113. }
  114. }
  115. // 4. 根据标签判断
  116. if (is_string($tags)) {
  117. if (strpos($tags, '选择') !== false || strpos($tags, '选择题') !== false) {
  118. return 'choice';
  119. }
  120. if (strpos($tags, '填空') !== false || strpos($tags, '填空题') !== false) {
  121. return 'fill';
  122. }
  123. if (strpos($tags, '解答') !== false || strpos($tags, '简答') !== false || strpos($tags, '证明') !== false) {
  124. return 'answer';
  125. }
  126. }
  127. // 5. 填空题特征:连续下划线或明显的填空括号
  128. if (is_string($stem)) {
  129. // 检查填空题特征:连续下划线
  130. if (preg_match('/_{3,}/', $stem) || strpos($stem, '____') !== false) {
  131. return 'fill';
  132. }
  133. // 空括号填空
  134. if (preg_match('/(\s*)/', $stem) || preg_match('/\(\s*\)/', $stem)) {
  135. return 'fill';
  136. }
  137. }
  138. // 6. 根据题干内容关键词判断
  139. if (is_string($stem)) {
  140. // 有证明、解答、计算、求证等关键词的是解答题
  141. if (preg_match('/(证明|求证|解方程|计算:|求解|推导|说明理由)/', $stem)) {
  142. return 'answer';
  143. }
  144. }
  145. // 默认是解答题(更安全的默认值)
  146. return 'answer';
  147. }
  148. private function normalizeQuestionTypeValue(string $type): string
  149. {
  150. $type = trim($type);
  151. $lower = strtolower($type);
  152. if (in_array($lower, ['choice', 'single_choice', 'multiple_choice'], true)) {
  153. return 'choice';
  154. }
  155. if (in_array($lower, ['fill', 'blank', 'fill_in_the_blank'], true)) {
  156. return 'fill';
  157. }
  158. if (in_array($lower, ['answer', 'calculation', 'word_problem', 'proof'], true)) {
  159. return 'answer';
  160. }
  161. if (in_array($type, ['CHOICE', 'SINGLE_CHOICE', 'MULTIPLE_CHOICE'], true)) {
  162. return 'choice';
  163. }
  164. if (in_array($type, ['FILL', 'FILL_IN_THE_BLANK'], true)) {
  165. return 'fill';
  166. }
  167. if (in_array($type, ['CALCULATION', 'WORD_PROBLEM', 'PROOF'], true)) {
  168. return 'answer';
  169. }
  170. if (in_array($type, ['选择题'], true)) {
  171. return 'choice';
  172. }
  173. if (in_array($type, ['填空题'], true)) {
  174. return 'fill';
  175. }
  176. if (in_array($type, ['解答题', '计算题'], true)) {
  177. return 'answer';
  178. }
  179. return $lower ?: 'answer';
  180. }
  181. /**
  182. * 从题目内容中提取选项
  183. */
  184. private function extractOptions(string $content): array
  185. {
  186. $options = [];
  187. // 【修复】先移除SVG内容,避免误匹配SVG注释中的 BD:DC、A:B 等内容
  188. $contentWithoutSvg = preg_replace('/<svg[^>]*>.*?<\/svg>/is', '[SVG_PLACEHOLDER]', $content);
  189. // 1. 尝试匹配多种格式的选项:A. / A、/ A: / A.(中文句点)/ A.(无空格)
  190. // 【修复】选项标记必须在行首或空白后,避免误匹配 SVG 注释中的 BD:DC 等内容
  191. $pattern = '/(?:^|\s)([A-D])[\.、:.:]\s*(.+?)(?=(?:^|\s)[A-D][\.、:.:]|$)/su';
  192. if (preg_match_all($pattern, $contentWithoutSvg, $matches, PREG_SET_ORDER)) {
  193. foreach ($matches as $match) {
  194. $optionText = trim($match[2]);
  195. // 移除末尾的换行和空白
  196. $optionText = preg_replace('/\s+$/', '', $optionText);
  197. // 清理 LaTeX 格式但保留内容
  198. $optionText = preg_replace('/^\$\$\s*/', '', $optionText);
  199. $optionText = preg_replace('/\s*\$\$$/', '', $optionText);
  200. if (! empty($optionText)) {
  201. $options[] = $optionText;
  202. }
  203. }
  204. }
  205. // 2. 如果上面没提取到,尝试按换行分割
  206. if (empty($options)) {
  207. $lines = preg_split('/[\r\n]+/', $contentWithoutSvg);
  208. foreach ($lines as $line) {
  209. $line = trim($line);
  210. // 【修复】行首匹配选项标记
  211. if (preg_match('/^([A-D])[\.、:.:]\s*(.+)$/u', $line, $match)) {
  212. $optionText = trim($match[2]);
  213. if (! empty($optionText)) {
  214. $options[] = $optionText;
  215. }
  216. }
  217. }
  218. }
  219. Log::debug('选项提取结果', [
  220. 'content_preview' => mb_substr($content, 0, 150),
  221. 'options_count' => count($options),
  222. 'options' => $options,
  223. ]);
  224. return $options;
  225. }
  226. /**
  227. * 分离题干内容和选项
  228. */
  229. private function separateStemAndOptions(string $content): array
  230. {
  231. // 【修复】先移除SVG内容,避免误匹配SVG注释中的 BD:DC、A:B 等内容
  232. $contentWithoutSvg = preg_replace('/<svg[^>]*>.*?<\/svg>/is', '[SVG_PLACEHOLDER]', $content);
  233. // 【修复】检测是否有选项时,要求选项标记在行首或空白后
  234. $hasOptions = preg_match('/(?:^|\s)[A-D][\.、:.:]/u', $contentWithoutSvg);
  235. if (! $hasOptions) {
  236. return [$content, []];
  237. }
  238. // 提取选项
  239. $options = $this->extractOptions($content);
  240. // 如果提取到选项,分离题干
  241. if (! empty($options)) {
  242. // 【修复】找到第一个选项的位置,要求选项标记在行首或空白后
  243. if (preg_match('/^(.+?)(?=(?:^|\s)[A-D][\.、:.:])/su', $contentWithoutSvg, $match)) {
  244. $stem = trim($match[1]);
  245. // 如果题干中有SVG占位符,从原始内容中提取对应部分
  246. if (strpos($stem, '[SVG_PLACEHOLDER]') !== false) {
  247. // 找到原始内容中对应位置的题干
  248. $stemLength = mb_strlen(str_replace('[SVG_PLACEHOLDER]', '', $stem));
  249. // 使用更精确的方法:找到第一个有效选项标记的位置
  250. foreach (['A.', 'A、', 'A:', 'A.', 'A:'] as $marker) {
  251. // 只匹配在空白后的选项标记
  252. if (preg_match('/\s'.preg_quote($marker, '/').'/', $content, $m, PREG_OFFSET_CAPTURE)) {
  253. $pos = $m[0][1];
  254. if ($pos > 0) {
  255. $stem = trim(mb_substr($content, 0, $pos));
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. } else {
  262. // 如果正则失败,尝试按位置分割
  263. $stem = $content;
  264. foreach (['A.', 'A、', 'A:', 'A.', 'A:'] as $marker) {
  265. // 【修复】只匹配在空白后的选项标记
  266. if (preg_match('/\s'.preg_quote($marker, '/').'/', $content, $m, PREG_OFFSET_CAPTURE)) {
  267. $pos = $m[0][1];
  268. if ($pos > 0) {
  269. $stem = trim(mb_substr($content, 0, $pos));
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. // 移除末尾的括号或空白
  276. $stem = preg_replace('/()\s*$/', '', $stem);
  277. $stem = trim($stem);
  278. return [$stem, $options];
  279. }
  280. return [$content, []];
  281. }
  282. /**
  283. * 根据题型获取默认分数
  284. */
  285. private function getQuestionScore(string $type): int
  286. {
  287. switch ($type) {
  288. case 'choice':
  289. return 5; // 选择题5分
  290. case 'fill':
  291. return 5; // 填空题5分
  292. case 'answer':
  293. return 10; // 解答题10分
  294. default:
  295. return 5;
  296. }
  297. }
  298. /**
  299. * 获取学生信息
  300. */
  301. private function getStudentInfo(?string $studentId): array
  302. {
  303. if (! $studentId) {
  304. return [
  305. 'name' => '未知学生',
  306. 'grade' => '未知年级',
  307. 'class' => '未知班级',
  308. ];
  309. }
  310. try {
  311. $student = DB::table('students')
  312. ->where('student_id', $studentId)
  313. ->first();
  314. if ($student) {
  315. return [
  316. 'name' => $student->name ?? $studentId,
  317. 'grade' => $student->grade ?? '未知',
  318. 'class' => $student->class ?? '未知',
  319. ];
  320. }
  321. } catch (\Exception $e) {
  322. Log::warning('获取学生信息失败', [
  323. 'student_id' => $studentId,
  324. 'error' => $e->getMessage(),
  325. ]);
  326. }
  327. return [
  328. 'name' => $studentId,
  329. 'grade' => '未知',
  330. 'class' => '未知',
  331. ];
  332. }
  333. /**
  334. * 为 PDF 预览筛选题目(简化版)
  335. */
  336. private function selectBestQuestionsForPdf(array $questions, int $targetCount, string $difficultyCategory): array
  337. {
  338. if (count($questions) <= $targetCount) {
  339. return $questions;
  340. }
  341. // 1. 按题型分类题目
  342. $categorizedQuestions = [
  343. 'choice' => [],
  344. 'fill' => [],
  345. 'answer' => [],
  346. ];
  347. foreach ($questions as $question) {
  348. $type = $this->determineQuestionType($question);
  349. if (! isset($categorizedQuestions[$type])) {
  350. $type = 'answer';
  351. }
  352. $categorizedQuestions[$type][] = $question;
  353. }
  354. // 2. 默认题型配比
  355. $typeRatio = [
  356. '选择题' => 50, // 50%
  357. '填空题' => 30, // 30%
  358. '解答题' => 20, // 20%
  359. ];
  360. // 3. 根据配比选择题目
  361. $selectedQuestions = [];
  362. foreach ($typeRatio as $type => $ratio) {
  363. $typeKey = $type === '选择题' ? 'choice' : ($type === '填空题' ? 'fill' : 'answer');
  364. $countForType = floor($targetCount * $ratio / 100);
  365. if ($countForType > 0 && ! empty($categorizedQuestions[$typeKey])) {
  366. $availableCount = count($categorizedQuestions[$typeKey]);
  367. $takeCount = min($countForType, $availableCount, $targetCount - count($selectedQuestions));
  368. // 随机选择题目
  369. $keys = array_keys($categorizedQuestions[$typeKey]);
  370. shuffle($keys);
  371. $selectedKeys = array_slice($keys, 0, $takeCount);
  372. foreach ($selectedKeys as $key) {
  373. $selectedQuestions[] = $categorizedQuestions[$typeKey][$key];
  374. }
  375. }
  376. }
  377. // 4. 如果数量不足,随机补充
  378. while (count($selectedQuestions) < $targetCount) {
  379. $randomQuestion = $questions[array_rand($questions)];
  380. if (! in_array($randomQuestion, $selectedQuestions)) {
  381. $selectedQuestions[] = $randomQuestion;
  382. }
  383. }
  384. // 5. 限制数量并打乱
  385. shuffle($selectedQuestions);
  386. return array_slice($selectedQuestions, 0, $targetCount);
  387. }
  388. /**
  389. * 获取教师信息
  390. */
  391. private function getTeacherInfo(?string $teacherId): array
  392. {
  393. if (! $teacherId) {
  394. return [
  395. 'name' => '未知教师',
  396. ];
  397. }
  398. try {
  399. $teacher = DB::table('teachers')
  400. ->where('teacher_id', $teacherId)
  401. ->first();
  402. if ($teacher) {
  403. return [
  404. 'name' => $teacher->name ?? $teacherId,
  405. ];
  406. }
  407. } catch (\Exception $e) {
  408. Log::warning('获取教师信息失败', [
  409. 'teacher_id' => $teacherId,
  410. 'error' => $e->getMessage(),
  411. ]);
  412. }
  413. return [
  414. 'name' => $teacherId,
  415. ];
  416. }
  417. public function show(Request $request, $paper_id)
  418. {
  419. // 获取是否显示答案的参数,默认为true
  420. $includeAnswer = $request->query('answer', 'true') !== 'false';
  421. // 使用 Eloquent 模型获取试卷数据
  422. $paper = \App\Models\Paper::where('paper_id', $paper_id)->first();
  423. if (! $paper) {
  424. // 尝试从缓存中获取生成的试卷数据(用于 demo 试卷)
  425. $cached = Cache::get('generated_exam_'.$paper_id);
  426. if ($cached) {
  427. Log::info('从缓存获取试卷数据', [
  428. 'paper_id' => $paper_id,
  429. 'cached_count' => count($cached['questions'] ?? []),
  430. 'cached_question_types' => array_column($cached['questions'] ?? [], 'question_type'),
  431. ]);
  432. // 构造临时 Paper 对象
  433. $paper = (object) [
  434. 'paper_id' => $paper_id,
  435. 'paper_name' => $cached['paper_name'] ?? 'Demo Paper',
  436. 'student_id' => $cached['student_id'] ?? null,
  437. 'teacher_id' => $cached['teacher_id'] ?? null,
  438. ];
  439. // 对于 demo 试卷,需要检查题目数量并限制为用户要求的数量
  440. $questionsData = $cached['questions'] ?? [];
  441. $totalQuestions = $cached['total_questions'] ?? count($questionsData);
  442. $difficultyCategory = $cached['difficulty_category'] ?? '中等';
  443. // 为 demo 试卷获取完整的题目详情(包括选项)
  444. if (! empty($questionsData)) {
  445. $questionBankService = app(QuestionBankService::class);
  446. $questionIds = array_column($questionsData, 'id');
  447. $questionsResponse = $questionBankService->getQuestionsByIds($questionIds);
  448. $responseData = $questionsResponse['data'] ?? [];
  449. if (! empty($responseData)) {
  450. $responseDataMap = [];
  451. foreach ($responseData as $respQ) {
  452. $responseDataMap[$respQ['id']] = $respQ;
  453. }
  454. // 合并题库数据
  455. $questionsData = array_map(function ($q) use ($responseDataMap) {
  456. if (isset($responseDataMap[$q['id']])) {
  457. $apiData = $responseDataMap[$q['id']];
  458. $rawContent = $apiData['stem'] ?? $q['stem'] ?? $q['content'] ?? '';
  459. // 分离题干和选项
  460. [$stem, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  461. $q['stem'] = $stem;
  462. $q['content'] = $stem; // 同时设置content字段
  463. $q['answer'] = $apiData['answer'] ?? $q['answer'] ?? '';
  464. $q['solution'] = $apiData['solution'] ?? $q['solution'] ?? '';
  465. $q['tags'] = $apiData['tags'] ?? $q['tags'] ?? '';
  466. // 优先使用API选项,支持多种数据格式
  467. $apiOptions = $apiData['options'] ?? null;
  468. if (! empty($apiOptions)) {
  469. // 标准化options格式为数组值列表
  470. $q['options'] = $this->normalizeOptions($apiOptions);
  471. Log::debug('使用标准化API options', [
  472. 'question_id' => $q['id'],
  473. 'raw_options' => $apiOptions,
  474. 'normalized_options' => $q['options'],
  475. ]);
  476. } else {
  477. // 备选:从题干中提取的选项
  478. $q['options'] = $extractedOptions;
  479. Log::debug('使用提取的options', [
  480. 'question_id' => $q['id'],
  481. 'extracted_options' => $extractedOptions,
  482. ]);
  483. }
  484. }
  485. return $q;
  486. }, $questionsData);
  487. }
  488. }
  489. if (count($questionsData) > $totalQuestions) {
  490. Log::info('PDF预览时发现题目过多,进行筛选', [
  491. 'paper_id' => $paper_id,
  492. 'cached_count' => count($questionsData),
  493. 'required_count' => $totalQuestions,
  494. ]);
  495. $questionsData = $this->selectBestQuestionsForPdf($questionsData, $totalQuestions, $difficultyCategory);
  496. Log::info('筛选后题目数据', [
  497. 'paper_id' => $paper_id,
  498. 'filtered_count' => count($questionsData),
  499. 'filtered_types' => array_column($questionsData, 'question_type'),
  500. ]);
  501. }
  502. } else {
  503. abort(404, '试卷未找到');
  504. }
  505. } else {
  506. // 获取试卷题目
  507. $paperQuestions = \App\Models\PaperQuestion::where('paper_id', $paper_id)
  508. ->orderBy('question_number')
  509. ->get();
  510. Log::info('从数据库获取题目', [
  511. 'paper_id' => $paper_id,
  512. 'question_count' => $paperQuestions->count(),
  513. ]);
  514. // 将 paper_questions 表的数据转换为题库格式
  515. $questionsData = [];
  516. foreach ($paperQuestions as $pq) {
  517. $questionsData[] = [
  518. 'id' => $pq->question_bank_id,
  519. 'question_number' => $pq->question_number, // 【关键】保留题目序号,用于排序
  520. 'kp_code' => $pq->knowledge_point,
  521. 'question_type' => $pq->question_type ?? 'answer', // 包含题目类型
  522. 'stem' => $pq->question_text ?? '题目内容缺失', // 如果有存储题目文本
  523. 'solution' => $pq->solution ?? '', // 保存解题思路!
  524. 'answer' => $pq->correct_answer ?? '', // 保存正确答案
  525. 'difficulty' => $pq->difficulty ?? 0.5,
  526. 'score' => $pq->score ?? 5, // 包含已计算的分值
  527. 'tags' => '',
  528. 'content' => $pq->question_text ?? '',
  529. ];
  530. }
  531. Log::info('paper_questions表原始数据', [
  532. 'paper_id' => $paper_id,
  533. 'sample_questions' => array_slice($questionsData, 0, 3),
  534. 'all_types' => array_column($questionsData, 'question_type'),
  535. ]);
  536. // 如果需要完整题目详情(stem等),可以从题库获取
  537. // 但要严格限制只获取这8道题
  538. if (! empty($questionsData)) {
  539. $questionBankService = app(QuestionBankService::class);
  540. $questionIds = array_column($questionsData, 'id');
  541. $questionsResponse = $questionBankService->getQuestionsByIds($questionIds);
  542. $responseData = $questionsResponse['data'] ?? [];
  543. // 确保只返回请求的ID对应的题目,并保留数据库中的 question_type
  544. if (! empty($responseData)) {
  545. // 创建题库返回数据的映射
  546. $responseDataMap = [];
  547. foreach ($responseData as $respQ) {
  548. $responseDataMap[$respQ['id']] = $respQ;
  549. }
  550. // 遍历所有数据库中的题目,合并题库返回的数据
  551. $questionsData = array_map(function ($q) use ($responseDataMap, $paperQuestions) {
  552. // 从题库API获取的详细数据(如果有)
  553. if (isset($responseDataMap[$q['id']])) {
  554. $apiData = $responseDataMap[$q['id']];
  555. $rawContent = $apiData['stem'] ?? $q['stem'] ?? '题目内容缺失';
  556. // 分离题干和选项
  557. [$stem, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  558. // 合并数据,优先使用题库API的 stem、answer、solution、options
  559. $q['stem'] = $stem;
  560. $q['content'] = $stem; // 同时设置content字段
  561. $q['answer'] = $apiData['answer'] ?? $q['answer'] ?? '';
  562. $q['solution'] = $apiData['solution'] ?? $q['solution'] ?? '';
  563. $q['tags'] = $apiData['tags'] ?? $q['tags'] ?? '';
  564. // 优先使用API选项,支持多种数据格式
  565. $apiOptions = $apiData['options'] ?? null;
  566. if (! empty($apiOptions)) {
  567. // 标准化options格式为数组值列表
  568. $q['options'] = $this->normalizeOptions($apiOptions);
  569. Log::debug('使用标准化API options', [
  570. 'question_id' => $q['id'],
  571. 'raw_options' => $apiOptions,
  572. 'normalized_options' => $q['options'],
  573. ]);
  574. } else {
  575. // 备选:从题干中提取的选项
  576. $q['options'] = $extractedOptions;
  577. Log::debug('使用提取的options', [
  578. 'question_id' => $q['id'],
  579. 'extracted_options' => $extractedOptions,
  580. ]);
  581. }
  582. }
  583. // 从数据库 paper_questions 表中获取 question_type(已在前面设置,这里确保有值)
  584. if (! isset($q['question_type']) || empty($q['question_type'])) {
  585. $dbQuestion = $paperQuestions->firstWhere('question_bank_id', $q['id']);
  586. if ($dbQuestion && $dbQuestion->question_type) {
  587. $q['question_type'] = $dbQuestion->question_type;
  588. }
  589. }
  590. return $q;
  591. }, $questionsData);
  592. }
  593. }
  594. }
  595. // 按题型分类(使用标准的中学数学试卷格式)
  596. $questions = ['choice' => [], 'fill' => [], 'answer' => []];
  597. foreach ($questionsData as $q) {
  598. // 题库API返回的是 stem 字段,不是 content
  599. $rawContent = $q['stem'] ?? $q['content'] ?? '题目内容缺失';
  600. // 分离题干和选项
  601. [$content, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  602. // 如果从题库API获取了选项,优先使用
  603. $options = $q['options'] ?? $extractedOptions;
  604. $answer = $q['answer'] ?? '';
  605. $solution = $q['solution'] ?? '';
  606. // 优先使用 question_type 字段,如果没有则根据内容智能判断
  607. $type = isset($q['question_type'])
  608. ? $this->normalizeQuestionTypeValue((string) $q['question_type'])
  609. : $this->determineQuestionType($q);
  610. // 详细调试:记录题目类型判断结果
  611. Log::info('题目类型判断', [
  612. 'question_id' => $q['id'] ?? '',
  613. 'has_question_type' => isset($q['question_type']),
  614. 'question_type_value' => $q['question_type'] ?? null,
  615. 'tags' => $q['tags'] ?? '',
  616. 'stem_length' => mb_strlen($content),
  617. 'stem_preview' => mb_substr($content, 0, 100),
  618. 'has_extracted_options' => ! empty($extractedOptions),
  619. 'extracted_options_count' => count($extractedOptions),
  620. 'has_api_options' => isset($q['options']) && ! empty($q['options']),
  621. 'api_options_count' => isset($q['options']) ? count($q['options']) : 0,
  622. 'final_options_count' => count($options),
  623. 'determined_type' => $type,
  624. ]);
  625. if (! isset($questions[$type])) {
  626. $type = 'answer';
  627. }
  628. // 统一处理数学公式和选项数据
  629. $questionData = [
  630. 'id' => $q['id'] ?? $q['question_bank_id'] ?? null,
  631. 'question_number' => $q['question_number'] ?? null, // 【关键】保留题目序号
  632. 'content' => $content,
  633. 'stem' => $content, // 同时提供stem字段
  634. 'answer' => $answer,
  635. 'solution' => $solution,
  636. 'difficulty' => $q['difficulty'] ?? 0.5,
  637. 'kp_code' => $q['kp_code'] ?? '',
  638. 'tags' => $q['tags'] ?? '',
  639. 'options' => $options, // 使用分离后的选项
  640. 'score' => $q['score'] ?? $this->getQuestionScore($type),
  641. 'question_type' => $type,
  642. ];
  643. // 统一处理数学公式 - 标记已处理,避免模板中重复处理
  644. $questionData = \App\Services\MathFormulaProcessor::processQuestionData($questionData);
  645. $questionData['math_processed'] = true; // 添加标记
  646. $qData = (object) $questionData;
  647. $questions[$type][] = $qData;
  648. }
  649. // 【关键】确保每个题型内的题目按 question_number 排序
  650. foreach (['choice', 'fill', 'answer'] as $type) {
  651. if (! empty($questions[$type])) {
  652. usort($questions[$type], function ($a, $b) {
  653. $aNum = $a->question_number ?? 0;
  654. $bNum = $b->question_number ?? 0;
  655. return $aNum <=> $bNum;
  656. });
  657. }
  658. }
  659. // 调试:记录最终分类结果
  660. Log::info('最终分类结果', [
  661. 'paper_id' => $paper_id,
  662. 'choice_count' => count($questions['choice']),
  663. 'fill_count' => count($questions['fill']),
  664. 'answer_count' => count($questions['answer']),
  665. 'total' => count($questions['choice']) + count($questions['fill']) + count($questions['answer']),
  666. ]);
  667. // 渲染视图
  668. $viewName = $includeAnswer ? 'pdf.exam-grading' : 'pdf.exam-paper';
  669. return view($viewName, [
  670. 'paper' => $paper,
  671. 'questions' => $questions,
  672. 'student' => $this->getStudentInfo($paper->student_id),
  673. 'teacher' => $this->getTeacherInfo($paper->teacher_id),
  674. 'includeAnswer' => $includeAnswer,
  675. ]);
  676. }
  677. /**
  678. * 判卷视图:题目前带方框,题后附"正确答案+解题思路"
  679. */
  680. public function showGrading(Request $request, $paper_id)
  681. {
  682. // 复用现有逻辑获取题目分类
  683. $includeAnswer = true;
  684. // 直接调用 show 的前置逻辑(简化复用)
  685. $request->merge(['answer' => 'true']);
  686. // 复用 show() 内逻辑获取 questions/paper
  687. // 为避免重复代码,简单调用 showData 方法(拆分为私有方法?暂直接重用现有方法流程)
  688. // 这里直接复制 show 的主体以保持兼容
  689. // 使用 Eloquent 模型获取试卷数据
  690. $paper = \App\Models\Paper::where('paper_id', $paper_id)->first();
  691. if (! $paper) {
  692. $cached = Cache::get('generated_exam_'.$paper_id);
  693. if (! $cached) {
  694. abort(404, '试卷未找到');
  695. }
  696. $paper = (object) [
  697. 'paper_id' => $paper_id,
  698. 'paper_name' => $cached['paper_name'] ?? 'Demo Paper',
  699. 'student_id' => $cached['student_id'] ?? null,
  700. 'teacher_id' => $cached['teacher_id'] ?? null,
  701. ];
  702. $questionsData = $cached['questions'] ?? [];
  703. $totalQuestions = $cached['total_questions'] ?? count($questionsData);
  704. $difficultyCategory = $cached['difficulty_category'] ?? '中等';
  705. if (! empty($questionsData)) {
  706. $questionBankService = app(QuestionBankService::class);
  707. $questionIds = array_column($questionsData, 'id');
  708. $questionsResponse = $questionBankService->getQuestionsByIds($questionIds);
  709. $responseData = $questionsResponse['data'] ?? [];
  710. if (! empty($responseData)) {
  711. $responseDataMap = [];
  712. foreach ($responseData as $respQ) {
  713. $responseDataMap[$respQ['id']] = $respQ;
  714. }
  715. $questionsData = array_map(function ($q) use ($responseDataMap) {
  716. if (isset($responseDataMap[$q['id']])) {
  717. $apiData = $responseDataMap[$q['id']];
  718. $rawContent = $apiData['stem'] ?? $q['stem'] ?? $q['content'] ?? '';
  719. [$stem, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  720. $q['stem'] = $stem;
  721. $q['content'] = $stem; // 同时设置content字段
  722. $q['answer'] = $apiData['answer'] ?? $q['answer'] ?? '';
  723. $q['solution'] = $apiData['solution'] ?? $q['solution'] ?? '';
  724. $q['tags'] = $apiData['tags'] ?? $q['tags'] ?? '';
  725. // 优先使用API选项,支持多种数据格式
  726. $apiOptions = $apiData['options'] ?? null;
  727. if (! empty($apiOptions)) {
  728. // 标准化options格式为数组值列表
  729. $q['options'] = $this->normalizeOptions($apiOptions);
  730. Log::debug('使用标准化API options', [
  731. 'question_id' => $q['id'],
  732. 'raw_options' => $apiOptions,
  733. 'normalized_options' => $q['options'],
  734. ]);
  735. } else {
  736. // 备选:从题干中提取的选项
  737. $q['options'] = $extractedOptions;
  738. Log::debug('使用提取的options', [
  739. 'question_id' => $q['id'],
  740. 'extracted_options' => $extractedOptions,
  741. ]);
  742. }
  743. }
  744. return $q;
  745. }, $questionsData);
  746. }
  747. }
  748. if (count($questionsData) > $totalQuestions) {
  749. $questionsData = $this->selectBestQuestionsForPdf($questionsData, $totalQuestions, $difficultyCategory);
  750. }
  751. } else {
  752. $paperQuestions = \App\Models\PaperQuestion::where('paper_id', $paper_id)
  753. ->orderBy('question_number')
  754. ->get();
  755. $questionsData = [];
  756. foreach ($paperQuestions as $pq) {
  757. $questionsData[] = [
  758. 'id' => $pq->question_bank_id,
  759. 'question_number' => $pq->question_number, // 【关键】保留题目序号
  760. 'kp_code' => $pq->knowledge_point,
  761. 'question_type' => $pq->question_type ?? 'answer',
  762. 'stem' => $pq->question_text ?? '题目内容缺失',
  763. 'solution' => $pq->solution ?? '', // 保存解题思路!
  764. 'answer' => $pq->correct_answer ?? '', // 保存正确答案
  765. 'difficulty' => $pq->difficulty ?? 0.5,
  766. 'score' => $pq->score ?? 5,
  767. 'tags' => '',
  768. 'content' => $pq->question_text ?? '',
  769. ];
  770. }
  771. if (! empty($questionsData)) {
  772. $questionBankService = app(QuestionBankService::class);
  773. $questionIds = array_column($questionsData, 'id');
  774. $questionsResponse = $questionBankService->getQuestionsByIds($questionIds);
  775. $responseData = $questionsResponse['data'] ?? [];
  776. if (! empty($responseData)) {
  777. $responseDataMap = [];
  778. foreach ($responseData as $respQ) {
  779. $responseDataMap[$respQ['id']] = $respQ;
  780. }
  781. $questionsData = array_map(function ($q) use ($responseDataMap, $paperQuestions) {
  782. if (isset($responseDataMap[$q['id']])) {
  783. $apiData = $responseDataMap[$q['id']];
  784. $rawContent = $apiData['stem'] ?? $q['stem'] ?? '题目内容缺失';
  785. [$stem, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  786. $q['stem'] = $stem;
  787. $q['content'] = $stem; // 同时设置content字段
  788. $q['answer'] = $apiData['answer'] ?? $q['answer'] ?? '';
  789. $q['solution'] = $apiData['solution'] ?? $q['solution'] ?? '';
  790. $q['tags'] = $apiData['tags'] ?? $q['tags'] ?? '';
  791. // 优先使用API选项,支持多种数据格式
  792. $apiOptions = $apiData['options'] ?? null;
  793. if (! empty($apiOptions)) {
  794. // 标准化options格式为数组值列表
  795. $q['options'] = $this->normalizeOptions($apiOptions);
  796. Log::debug('使用标准化API options', [
  797. 'question_id' => $q['id'],
  798. 'raw_options' => $apiOptions,
  799. 'normalized_options' => $q['options'],
  800. ]);
  801. } else {
  802. // 备选:从题干中提取的选项
  803. $q['options'] = $extractedOptions;
  804. Log::debug('使用提取的options', [
  805. 'question_id' => $q['id'],
  806. 'extracted_options' => $extractedOptions,
  807. ]);
  808. }
  809. }
  810. if (! isset($q['question_type']) || empty($q['question_type'])) {
  811. $dbQuestion = $paperQuestions->firstWhere('question_bank_id', $q['id']);
  812. if ($dbQuestion && $dbQuestion->question_type) {
  813. $q['question_type'] = $dbQuestion->question_type;
  814. }
  815. }
  816. return $q;
  817. }, $questionsData);
  818. }
  819. }
  820. }
  821. $questions = ['choice' => [], 'fill' => [], 'answer' => []];
  822. foreach ($questionsData as $q) {
  823. $rawContent = $q['stem'] ?? $q['content'] ?? '题目内容缺失';
  824. [$content, $extractedOptions] = $this->separateStemAndOptions($rawContent);
  825. $options = $q['options'] ?? $extractedOptions;
  826. $answer = $q['answer'] ?? '';
  827. $solution = $q['solution'] ?? '';
  828. $type = isset($q['question_type'])
  829. ? $this->normalizeQuestionTypeValue((string) $q['question_type'])
  830. : $this->determineQuestionType($q);
  831. if (! isset($questions[$type])) {
  832. $type = 'answer';
  833. }
  834. // 统一处理数学公式和选项数据
  835. $questionData = [
  836. 'id' => $q['id'] ?? $q['question_bank_id'] ?? null,
  837. 'question_number' => $q['question_number'] ?? null, // 【关键】保留题目序号
  838. 'content' => $content,
  839. 'stem' => $content, // 同时提供stem字段
  840. 'answer' => $answer,
  841. 'solution' => $solution,
  842. 'difficulty' => $q['difficulty'] ?? 0.5,
  843. 'kp_code' => $q['kp_code'] ?? '',
  844. 'tags' => $q['tags'] ?? '',
  845. 'options' => $options,
  846. 'score' => $q['score'] ?? $this->getQuestionScore($type),
  847. 'question_type' => $type,
  848. ];
  849. // 统一处理数学公式 - 标记已处理,避免模板中重复处理
  850. $questionData = \App\Services\MathFormulaProcessor::processQuestionData($questionData);
  851. $questionData['math_processed'] = true; // 添加标记
  852. $qData = (object) $questionData;
  853. $questions[$type][] = $qData;
  854. }
  855. // 【关键】确保每个题型内的题目按 question_number 排序
  856. foreach (['choice', 'fill', 'answer'] as $type) {
  857. if (! empty($questions[$type])) {
  858. usort($questions[$type], function ($a, $b) {
  859. $aNum = $a->question_number ?? 0;
  860. $bNum = $b->question_number ?? 0;
  861. return $aNum <=> $bNum;
  862. });
  863. }
  864. }
  865. return view('pdf.exam-grading', [
  866. 'paper' => $paper,
  867. 'questions' => $questions,
  868. 'student' => $this->getStudentInfo($paper->student_id),
  869. 'teacher' => $this->getTeacherInfo($paper->teacher_id),
  870. 'includeAnswer' => true,
  871. ]);
  872. }
  873. /**
  874. * 知识点讲解视图
  875. */
  876. public function showKnowledgeExplanation(Request $request, $paper_id)
  877. {
  878. // 获取试卷数据
  879. $paper = \App\Models\Paper::where('paper_id', $paper_id)->first();
  880. if (! $paper) {
  881. abort(404, '试卷未找到');
  882. }
  883. // 提取15位paper_id数字部分作为学案编号
  884. $rawPaperId = $paper->paper_id ?? $paper_id;
  885. preg_match('/paper_(\d{15})/', (string) $rawPaperId, $matches);
  886. $examCode = $matches[1] ?? preg_replace('/[^0-9]/', '', (string) $rawPaperId);
  887. // 生成日期
  888. $generateDate = now()->locale('zh_CN')->isoFormat('M月D日');
  889. // 提取并去重知识点代码(优先 paper_questions.knowledge_point,缺失时回退到题库 kp_code)
  890. $paperQuestions = \App\Models\PaperQuestion::where('paper_id', $paper_id)->get();
  891. $kpCodes = [];
  892. $seen = [];
  893. $questionBankIds = $paperQuestions
  894. ->pluck('question_bank_id')
  895. ->filter()
  896. ->unique()
  897. ->values();
  898. $questionKpMap = [];
  899. if ($questionBankIds->isNotEmpty()) {
  900. $questionKpMap = \App\Models\Question::whereIn('id', $questionBankIds)
  901. ->pluck('kp_code', 'id')
  902. ->toArray();
  903. }
  904. foreach ($paperQuestions as $pq) {
  905. $kpCode = trim((string) ($pq->knowledge_point ?? ''));
  906. if ($kpCode === '' && ! empty($pq->question_bank_id)) {
  907. $kpCode = trim((string) ($questionKpMap[$pq->question_bank_id] ?? ''));
  908. }
  909. if ($kpCode === '') {
  910. continue;
  911. }
  912. if (isset($seen[$kpCode])) {
  913. continue;
  914. }
  915. $seen[$kpCode] = true;
  916. $kpCodes[] = $kpCode;
  917. }
  918. // 使用 ExamPdfExportService 构建知识点数据
  919. $pdfService = app(\App\Services\ExamPdfExportService::class);
  920. // 批量获取知识点讲解
  921. $knowledgePoints = $pdfService->buildExplanations($kpCodes);
  922. return view('pdf.exam-knowledge-explanation', [
  923. 'paperId' => $paper_id,
  924. 'examCode' => $examCode ?: $paper_id,
  925. 'generateDate' => $generateDate,
  926. 'knowledgePoints' => $knowledgePoints,
  927. ]);
  928. }
  929. /**
  930. * 重新生成 PDF(统一生成卷子和判卷)
  931. *
  932. * @param string $paper_id
  933. * @return \Illuminate\Http\JsonResponse
  934. */
  935. public function regeneratePdf(Request $request, $paper_id)
  936. {
  937. Log::info('RegeneratePdf: 开始重新生成PDF', ['paper_id' => $paper_id]);
  938. // 验证 paper_id 格式
  939. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  940. return response()->json([
  941. 'success' => false,
  942. 'message' => '无效的试卷ID格式',
  943. 'paper_id' => $paper_id,
  944. ], 400);
  945. }
  946. try {
  947. // 【修复】首先检查试卷是否存在
  948. $paperModel = Paper::with('questions')->find($paper_id);
  949. if (! $paperModel || $paperModel->questions->isEmpty()) {
  950. return response()->json([
  951. 'success' => false,
  952. 'message' => '无效的试卷',
  953. 'paper_id' => $paper_id,
  954. ], 400);
  955. }
  956. // 根据 config 或 env 配置决定是否包含知识点讲解
  957. // 还需要判断如果摸底(paper_type =0)的时候也是不需要插入知识点讲解内容
  958. $includeKpExplain = null;
  959. if ($request->has('include_kp_explain')) {
  960. $includeKpExplain = filter_var(
  961. $request->input('include_kp_explain'),
  962. FILTER_VALIDATE_BOOLEAN,
  963. FILTER_NULL_ON_FAILURE
  964. );
  965. } elseif ($paperModel->paper_type === 0) {
  966. $includeKpExplain = false;
  967. }
  968. info("includekpexplain", [$includeKpExplain]);
  969. // 调用 PDF 生成服务
  970. $pdfService = app(\App\Services\ExamPdfExportService::class);
  971. // 生成统一 PDF(卷子 + 判卷)
  972. $pdfUrl = $pdfService->generateUnifiedPdf($paper_id, $includeKpExplain);
  973. if ($pdfUrl) {
  974. Log::info('RegeneratePdf: PDF重新生成成功', [
  975. 'paper_id' => $paper_id,
  976. 'url' => $pdfUrl,
  977. ]);
  978. return response()->json([
  979. 'success' => true,
  980. 'message' => 'PDF重新生成成功',
  981. 'paper_id' => $paper_id,
  982. 'pdf_url' => $pdfUrl,
  983. ]);
  984. }
  985. Log::error('RegeneratePdf: PDF生成失败', ['paper_id' => $paper_id]);
  986. return response()->json([
  987. 'success' => false,
  988. 'message' => 'PDF生成失败',
  989. 'paper_id' => $paper_id,
  990. ], 500);
  991. } catch (\Exception $e) {
  992. Log::error('RegeneratePdf: 异常错误', [
  993. 'paper_id' => $paper_id,
  994. 'error' => $e->getMessage(),
  995. 'trace' => $e->getTraceAsString(),
  996. ]);
  997. return response()->json([
  998. 'success' => false,
  999. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1000. 'paper_id' => $paper_id,
  1001. ], 500);
  1002. }
  1003. }
  1004. /**
  1005. * 重新生成试卷 PDF(不含答案)
  1006. *
  1007. * @param string $paper_id
  1008. * @return \Illuminate\Http\JsonResponse
  1009. */
  1010. public function regenerateExamPdf(Request $request, $paper_id)
  1011. {
  1012. Log::info('RegenerateExamPdf: 开始重新生成试卷PDF', ['paper_id' => $paper_id]);
  1013. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  1014. return response()->json([
  1015. 'success' => false,
  1016. 'message' => '无效的试卷ID格式',
  1017. 'paper_id' => $paper_id,
  1018. ], 400);
  1019. }
  1020. try {
  1021. $pdfService = app(\App\Services\ExamPdfExportService::class);
  1022. $pdfUrl = $pdfService->generateExamPdf($paper_id);
  1023. if ($pdfUrl) {
  1024. return response()->json([
  1025. 'success' => true,
  1026. 'message' => '试卷PDF重新生成成功',
  1027. 'paper_id' => $paper_id,
  1028. 'pdf_url' => $pdfUrl,
  1029. ]);
  1030. }
  1031. return response()->json([
  1032. 'success' => false,
  1033. 'message' => '试卷PDF生成失败',
  1034. 'paper_id' => $paper_id,
  1035. ], 500);
  1036. } catch (\Exception $e) {
  1037. Log::error('RegenerateExamPdf: 异常错误', [
  1038. 'paper_id' => $paper_id,
  1039. 'error' => $e->getMessage(),
  1040. ]);
  1041. return response()->json([
  1042. 'success' => false,
  1043. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1044. 'paper_id' => $paper_id,
  1045. ], 500);
  1046. }
  1047. }
  1048. /**
  1049. * 重新生成判卷 PDF(含答案)
  1050. *
  1051. * @param string $paper_id
  1052. * @return \Illuminate\Http\JsonResponse
  1053. */
  1054. public function regenerateGradingPdf(Request $request, $paper_id)
  1055. {
  1056. Log::info('RegenerateGradingPdf: 开始重新生成判卷PDF', ['paper_id' => $paper_id]);
  1057. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  1058. return response()->json([
  1059. 'success' => false,
  1060. 'message' => '无效的试卷ID格式',
  1061. 'paper_id' => $paper_id,
  1062. ], 400);
  1063. }
  1064. try {
  1065. $pdfService = app(\App\Services\ExamPdfExportService::class);
  1066. $pdfUrl = $pdfService->generateGradingPdf($paper_id);
  1067. if ($pdfUrl) {
  1068. return response()->json([
  1069. 'success' => true,
  1070. 'message' => '判卷PDF重新生成成功',
  1071. 'paper_id' => $paper_id,
  1072. 'pdf_url' => $pdfUrl,
  1073. ]);
  1074. }
  1075. return response()->json([
  1076. 'success' => false,
  1077. 'message' => '判卷PDF生成失败',
  1078. 'paper_id' => $paper_id,
  1079. ], 500);
  1080. } catch (\Exception $e) {
  1081. Log::error('RegenerateGradingPdf: 异常错误', [
  1082. 'paper_id' => $paper_id,
  1083. 'error' => $e->getMessage(),
  1084. ]);
  1085. return response()->json([
  1086. 'success' => false,
  1087. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1088. 'paper_id' => $paper_id,
  1089. ], 500);
  1090. }
  1091. }
  1092. }