QuestionPdfController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ExamPdfExportService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Validator;
  9. class QuestionPdfController extends Controller
  10. {
  11. protected ExamPdfExportService $pdfService;
  12. public function __construct(ExamPdfExportService $pdfService)
  13. {
  14. $this->pdfService = $pdfService;
  15. }
  16. /**
  17. * Generate PDF for specified questions
  18. *
  19. * POST /api/questions/pdf
  20. *
  21. * Request body:
  22. * {
  23. * "question_ids": [19903, 17766, ...],
  24. * "student_id": "123456",
  25. * "student_name": "张三", // optional
  26. * "student_grade": "初二", // optional
  27. * "teacher_name": "李老师", // optional
  28. * "paper_name": "专项练习", // optional
  29. * "include_grading": false // optional, whether to generate grading PDF
  30. * }
  31. */
  32. public function generate(Request $request)
  33. {
  34. $validator = Validator::make($request->all(), [
  35. 'question_ids' => 'required|array|min:1|max:100',
  36. 'question_ids.*' => 'required|integer',
  37. 'student_id' => 'required|string',
  38. 'student_name' => 'nullable|string|max:50',
  39. 'student_grade' => 'nullable|string|max:20',
  40. 'teacher_name' => 'nullable|string|max:50',
  41. 'paper_name' => 'nullable|string|max:100',
  42. 'include_grading' => 'nullable|boolean',
  43. 'source' => 'nullable|string|in:default,ai', // 题库来源:default=默认表(questions_tem),ai=ai表(questions_ai)
  44. ]);
  45. if ($validator->fails()) {
  46. return response()->json([
  47. 'success' => false,
  48. 'message' => '参数验证失败',
  49. 'errors' => $validator->errors(),
  50. ], 422);
  51. }
  52. $questionIds = $request->input('question_ids');
  53. $studentId = $request->input('student_id');
  54. $studentName = $request->input('student_name', '');
  55. $studentGrade = $request->input('student_grade', '');
  56. $teacherName = $request->input('teacher_name', '');
  57. $paperName = $request->input('paper_name', '专项练习');
  58. $includeGrading = $request->input('include_grading', false);
  59. $source = $request->input('source', 'default'); // 题库来源:default=questions_tem, ai=questions_ai
  60. Log::info('生成指定题目PDF', [
  61. 'question_ids' => $questionIds,
  62. 'student_id' => $studentId,
  63. 'count' => count($questionIds),
  64. 'source' => $source,
  65. ]);
  66. try {
  67. // 1. Fetch questions from database
  68. $tableName = $source === 'ai' ? 'questions_ai' : 'questions_tem';
  69. $questions = DB::connection('remote_mysql')
  70. ->table($tableName)
  71. ->whereIn('id', $questionIds)
  72. ->get();
  73. if ($questions->isEmpty()) {
  74. return response()->json([
  75. 'success' => false,
  76. 'message' => '未找到指定的题目',
  77. ], 404);
  78. }
  79. // 2. Group questions by type
  80. $groupedQuestions = $this->groupQuestionsByType($questions, $questionIds);
  81. // 3. Build virtual paper structure
  82. $paper = $this->buildVirtualPaper($paperName, $studentId, $groupedQuestions);
  83. // 4. Generate PDF
  84. $result = $this->pdfService->generateByQuestions(
  85. $paper,
  86. $groupedQuestions,
  87. [
  88. 'name' => $studentName,
  89. 'grade' => $studentGrade,
  90. ],
  91. [
  92. 'name' => $teacherName,
  93. ],
  94. $includeGrading
  95. );
  96. Log::info('指定题目PDF生成成功', [
  97. 'student_id' => $studentId,
  98. 'question_count' => count($questionIds),
  99. 'pdf_url' => $result['pdf_url'] ?? null,
  100. ]);
  101. return response()->json([
  102. 'success' => true,
  103. 'message' => 'PDF生成成功',
  104. 'data' => $result,
  105. ]);
  106. } catch (\Exception $e) {
  107. Log::error('生成指定题目PDF失败', [
  108. 'error' => $e->getMessage(),
  109. 'trace' => $e->getTraceAsString(),
  110. ]);
  111. return response()->json([
  112. 'success' => false,
  113. 'message' => '生成PDF失败: ' . $e->getMessage(),
  114. ], 500);
  115. }
  116. }
  117. /**
  118. * Group questions by type and maintain order
  119. */
  120. private function groupQuestionsByType($questions, array $originalOrder): array
  121. {
  122. // Create a map for quick lookup and preserve original order
  123. $questionMap = [];
  124. foreach ($questions as $q) {
  125. $questionMap[$q->id] = $q;
  126. }
  127. $grouped = [
  128. 'choice' => [],
  129. 'fill' => [],
  130. 'answer' => [],
  131. ];
  132. $questionNumber = 1;
  133. // Process in original order
  134. foreach ($originalOrder as $id) {
  135. if (!isset($questionMap[$id])) {
  136. continue;
  137. }
  138. $q = $questionMap[$id];
  139. $type = $this->normalizeQuestionType($q->question_type);
  140. // Build question object for template
  141. $questionObj = (object) [
  142. 'id' => $q->id,
  143. 'question_number' => $questionNumber++,
  144. 'content' => $q->stem,
  145. 'options' => is_string($q->options) ? json_decode($q->options, true) : ($q->options ?? []),
  146. 'answer' => $q->answer,
  147. 'solution' => $q->solution,
  148. 'score' => $this->getDefaultScore($type),
  149. 'difficulty' => $q->difficulty,
  150. 'kp_code' => $q->kp_code,
  151. ];
  152. $grouped[$type][] = $questionObj;
  153. }
  154. return $grouped;
  155. }
  156. /**
  157. * Normalize question type to standard format
  158. */
  159. private function normalizeQuestionType(?string $type): string
  160. {
  161. if (!$type) {
  162. return 'answer';
  163. }
  164. $type = strtolower(trim($type));
  165. $typeMap = [
  166. 'choice' => 'choice',
  167. '选择题' => 'choice',
  168. 'single_choice' => 'choice',
  169. 'multiple_choice' => 'choice',
  170. 'fill' => 'fill',
  171. '填空题' => 'fill',
  172. 'blank' => 'fill',
  173. 'answer' => 'answer',
  174. '解答题' => 'answer',
  175. 'subjective' => 'answer',
  176. 'calculation' => 'answer',
  177. 'proof' => 'answer',
  178. ];
  179. return $typeMap[$type] ?? 'answer';
  180. }
  181. /**
  182. * Get default score by question type
  183. */
  184. private function getDefaultScore(string $type): int
  185. {
  186. return match ($type) {
  187. 'choice' => 5,
  188. 'fill' => 5,
  189. 'answer' => 10,
  190. default => 5,
  191. };
  192. }
  193. /**
  194. * Build virtual paper structure
  195. */
  196. private function buildVirtualPaper(string $paperName, string $studentId, array $groupedQuestions): object
  197. {
  198. $totalScore = 0;
  199. $totalQuestions = 0;
  200. foreach ($groupedQuestions as $type => $questions) {
  201. foreach ($questions as $q) {
  202. $totalScore += $q->score;
  203. $totalQuestions++;
  204. }
  205. }
  206. // Generate unique paper ID
  207. $paperId = 'custom_' . $studentId . '_' . time() . '_' . uniqid();
  208. return (object) [
  209. 'paper_id' => $paperId,
  210. 'paper_name' => $paperName,
  211. 'total_score' => $totalScore,
  212. 'total_questions' => $totalQuestions,
  213. 'created_at' => now()->toDateTimeString(),
  214. ];
  215. }
  216. }