ExamPdfController.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. $studentName = $this->getStudentInfo($paper->student_id)['name'] ?? ($paper->student_id ?? '________');
  888. // 生成时间(格式:2026年01月30日 15:04:05)
  889. $generateDateTime = now()->format('Y年m月d日 H:i:s');
  890. // 优先使用 paper 中保存的 explanation_kp_codes(组卷时指定的知识点,最多2个)
  891. $kpCodes = $paper->explanation_kp_codes ?? [];
  892. // 如果没有保存 explanation_kp_codes,回退到从题目中提取(兼容旧数据)
  893. if (empty($kpCodes)) {
  894. $paperQuestions = \App\Models\PaperQuestion::where('paper_id', $paper_id)->get();
  895. $seen = [];
  896. $questionBankIds = $paperQuestions
  897. ->pluck('question_bank_id')
  898. ->filter()
  899. ->unique()
  900. ->values();
  901. $questionKpMap = [];
  902. if ($questionBankIds->isNotEmpty()) {
  903. $questionKpMap = \App\Models\Question::whereIn('id', $questionBankIds)
  904. ->pluck('kp_code', 'id')
  905. ->toArray();
  906. }
  907. foreach ($paperQuestions as $pq) {
  908. $kpCode = trim((string) ($pq->knowledge_point ?? ''));
  909. if ($kpCode === '' && ! empty($pq->question_bank_id)) {
  910. $kpCode = trim((string) ($questionKpMap[$pq->question_bank_id] ?? ''));
  911. }
  912. if ($kpCode === '') {
  913. continue;
  914. }
  915. if (isset($seen[$kpCode])) {
  916. continue;
  917. }
  918. $seen[$kpCode] = true;
  919. $kpCodes[] = $kpCode;
  920. }
  921. }
  922. // 使用 ExamPdfExportService 构建知识点数据
  923. $pdfService = app(\App\Services\ExamPdfExportService::class);
  924. // 批量获取知识点讲解
  925. $knowledgePoints = $pdfService->buildExplanations($kpCodes);
  926. return view('pdf.exam-knowledge-explanation', [
  927. 'paperId' => $paper_id,
  928. 'examCode' => $examCode ?: $paper_id,
  929. 'studentName' => $studentName,
  930. 'generateDateTime' => $generateDateTime,
  931. 'knowledgePoints' => $knowledgePoints,
  932. ]);
  933. }
  934. /**
  935. * 服务端渲染预览 - 试卷(与 PDF 生成效果一致,用于调试)
  936. */
  937. public function showServerRendered(Request $request, $paper_id)
  938. {
  939. // 强制不显示答案(试卷模式)
  940. $request->merge(['answer' => 'false']);
  941. // 复用 show() 获取 HTML
  942. $view = $this->show($request, $paper_id);
  943. $html = $view->render();
  944. // 使用 KatexRenderer 服务端渲染公式
  945. $katexRenderer = new \App\Services\KatexRenderer();
  946. $rendered = $katexRenderer->disableCache()->renderHtml($html);
  947. return response($rendered);
  948. }
  949. /**
  950. * 服务端渲染预览 - 判卷(与 PDF 生成效果一致,用于调试)
  951. */
  952. public function showGradingServerRendered(Request $request, $paper_id)
  953. {
  954. // 复用 showGrading() 获取 HTML
  955. $view = $this->showGrading($request, $paper_id);
  956. $html = $view->render();
  957. // 使用 KatexRenderer 服务端渲染公式
  958. $katexRenderer = new \App\Services\KatexRenderer();
  959. $rendered = $katexRenderer->disableCache()->renderHtml($html);
  960. return response($rendered);
  961. }
  962. /**
  963. * 重新生成 PDF(统一生成卷子和判卷)
  964. *
  965. * @param string $paper_id
  966. * @return \Illuminate\Http\JsonResponse
  967. */
  968. public function regeneratePdf(Request $request, $paper_id)
  969. {
  970. Log::info('RegeneratePdf: 开始重新生成PDF', ['paper_id' => $paper_id]);
  971. // 验证 paper_id 格式
  972. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  973. return response()->json([
  974. 'success' => false,
  975. 'message' => '无效的试卷ID格式',
  976. 'paper_id' => $paper_id,
  977. ], 400);
  978. }
  979. try {
  980. // 【修复】首先检查试卷是否存在
  981. $paperModel = Paper::with('questions')->find($paper_id);
  982. if (! $paperModel || $paperModel->questions->isEmpty()) {
  983. return response()->json([
  984. 'success' => false,
  985. 'message' => '无效的试卷',
  986. 'paper_id' => $paper_id,
  987. ], 400);
  988. }
  989. // 根据 config 或 env 配置决定是否包含知识点讲解
  990. // 还需要判断如果摸底(paper_type =0)的时候也是不需要插入知识点讲解内容
  991. $includeKpExplain = null;
  992. if ($request->has('include_kp_explain')) {
  993. $includeKpExplain = filter_var(
  994. $request->input('include_kp_explain'),
  995. FILTER_VALIDATE_BOOLEAN,
  996. FILTER_NULL_ON_FAILURE
  997. );
  998. } elseif ($paperModel->paper_type === 0) {
  999. $includeKpExplain = false;
  1000. }
  1001. info("includekpexplain", [$includeKpExplain]);
  1002. // 调用 PDF 生成服务
  1003. $pdfService = app(\App\Services\ExamPdfExportService::class);
  1004. // 生成统一 PDF(卷子 + 判卷)
  1005. $pdfUrl = $pdfService->generateUnifiedPdf($paper_id, $includeKpExplain);
  1006. if ($pdfUrl) {
  1007. Log::info('RegeneratePdf: PDF重新生成成功', [
  1008. 'paper_id' => $paper_id,
  1009. 'url' => $pdfUrl,
  1010. ]);
  1011. return response()->json([
  1012. 'success' => true,
  1013. 'message' => 'PDF重新生成成功',
  1014. 'paper_id' => $paper_id,
  1015. 'pdf_url' => $pdfUrl,
  1016. ]);
  1017. }
  1018. Log::error('RegeneratePdf: PDF生成失败', ['paper_id' => $paper_id]);
  1019. return response()->json([
  1020. 'success' => false,
  1021. 'message' => 'PDF生成失败',
  1022. 'paper_id' => $paper_id,
  1023. ], 500);
  1024. } catch (\Exception $e) {
  1025. Log::error('RegeneratePdf: 异常错误', [
  1026. 'paper_id' => $paper_id,
  1027. 'error' => $e->getMessage(),
  1028. 'trace' => $e->getTraceAsString(),
  1029. ]);
  1030. return response()->json([
  1031. 'success' => false,
  1032. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1033. 'paper_id' => $paper_id,
  1034. ], 500);
  1035. }
  1036. }
  1037. /**
  1038. * 重新生成试卷 PDF(不含答案)
  1039. *
  1040. * @param string $paper_id
  1041. * @return \Illuminate\Http\JsonResponse
  1042. */
  1043. public function regenerateExamPdf(Request $request, $paper_id)
  1044. {
  1045. Log::info('RegenerateExamPdf: 开始重新生成试卷PDF', ['paper_id' => $paper_id]);
  1046. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  1047. return response()->json([
  1048. 'success' => false,
  1049. 'message' => '无效的试卷ID格式',
  1050. 'paper_id' => $paper_id,
  1051. ], 400);
  1052. }
  1053. try {
  1054. $pdfService = app(\App\Services\ExamPdfExportService::class);
  1055. $pdfUrl = $pdfService->generateExamPdf($paper_id);
  1056. if ($pdfUrl) {
  1057. return response()->json([
  1058. 'success' => true,
  1059. 'message' => '试卷PDF重新生成成功',
  1060. 'paper_id' => $paper_id,
  1061. 'pdf_url' => $pdfUrl,
  1062. ]);
  1063. }
  1064. return response()->json([
  1065. 'success' => false,
  1066. 'message' => '试卷PDF生成失败',
  1067. 'paper_id' => $paper_id,
  1068. ], 500);
  1069. } catch (\Exception $e) {
  1070. Log::error('RegenerateExamPdf: 异常错误', [
  1071. 'paper_id' => $paper_id,
  1072. 'error' => $e->getMessage(),
  1073. ]);
  1074. return response()->json([
  1075. 'success' => false,
  1076. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1077. 'paper_id' => $paper_id,
  1078. ], 500);
  1079. }
  1080. }
  1081. /**
  1082. * 重新生成判卷 PDF(含答案)
  1083. *
  1084. * @param string $paper_id
  1085. * @return \Illuminate\Http\JsonResponse
  1086. */
  1087. public function regenerateGradingPdf(Request $request, $paper_id)
  1088. {
  1089. Log::info('RegenerateGradingPdf: 开始重新生成判卷PDF', ['paper_id' => $paper_id]);
  1090. if (empty($paper_id) || ! preg_match('/^paper_\d+$/', $paper_id)) {
  1091. return response()->json([
  1092. 'success' => false,
  1093. 'message' => '无效的试卷ID格式',
  1094. 'paper_id' => $paper_id,
  1095. ], 400);
  1096. }
  1097. try {
  1098. $pdfService = app(\App\Services\ExamPdfExportService::class);
  1099. $pdfUrl = $pdfService->generateGradingPdf($paper_id);
  1100. if ($pdfUrl) {
  1101. return response()->json([
  1102. 'success' => true,
  1103. 'message' => '判卷PDF重新生成成功',
  1104. 'paper_id' => $paper_id,
  1105. 'pdf_url' => $pdfUrl,
  1106. ]);
  1107. }
  1108. return response()->json([
  1109. 'success' => false,
  1110. 'message' => '判卷PDF生成失败',
  1111. 'paper_id' => $paper_id,
  1112. ], 500);
  1113. } catch (\Exception $e) {
  1114. Log::error('RegenerateGradingPdf: 异常错误', [
  1115. 'paper_id' => $paper_id,
  1116. 'error' => $e->getMessage(),
  1117. ]);
  1118. return response()->json([
  1119. 'success' => false,
  1120. 'message' => 'PDF生成异常:'.$e->getMessage(),
  1121. 'paper_id' => $paper_id,
  1122. ], 500);
  1123. }
  1124. }
  1125. }