QuestionPdfController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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,main', // 题库来源:default=questions_tem,ai=questions_ai,main=questions
  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, main=questions
  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 = match ($source) {
  69. 'ai' => 'questions_ai',
  70. 'main' => 'questions',
  71. default => 'questions_tem',
  72. };
  73. $questions = DB::connection('remote_mysql')
  74. ->table($tableName)
  75. ->whereIn('id', $questionIds)
  76. ->get();
  77. if ($questions->isEmpty()) {
  78. return response()->json([
  79. 'success' => false,
  80. 'message' => '未找到指定的题目',
  81. ], 404);
  82. }
  83. // 2. Group questions by type
  84. $groupedQuestions = $this->groupQuestionsByType($questions, $questionIds);
  85. // 3. Build virtual paper structure
  86. $paper = $this->buildVirtualPaper($paperName, $studentId, $groupedQuestions);
  87. // 4. Generate PDF
  88. $result = $this->pdfService->generateByQuestions(
  89. $paper,
  90. $groupedQuestions,
  91. [
  92. 'name' => $studentName,
  93. 'grade' => $studentGrade,
  94. ],
  95. [
  96. 'name' => $teacherName,
  97. ],
  98. $includeGrading
  99. );
  100. Log::info('指定题目PDF生成成功', [
  101. 'student_id' => $studentId,
  102. 'question_count' => count($questionIds),
  103. 'pdf_url' => $result['pdf_url'] ?? null,
  104. ]);
  105. return response()->json([
  106. 'success' => true,
  107. 'message' => 'PDF生成成功',
  108. 'data' => $result,
  109. ]);
  110. } catch (\Exception $e) {
  111. Log::error('生成指定题目PDF失败', [
  112. 'error' => $e->getMessage(),
  113. 'trace' => $e->getTraceAsString(),
  114. ]);
  115. return response()->json([
  116. 'success' => false,
  117. 'message' => '生成PDF失败: ' . $e->getMessage(),
  118. ], 500);
  119. }
  120. }
  121. /**
  122. * Group questions by type and maintain order
  123. */
  124. private function groupQuestionsByType($questions, array $originalOrder): array
  125. {
  126. // Create a map for quick lookup and preserve original order
  127. $questionMap = [];
  128. foreach ($questions as $q) {
  129. $questionMap[$q->id] = $q;
  130. }
  131. $grouped = [
  132. 'choice' => [],
  133. 'fill' => [],
  134. 'answer' => [],
  135. ];
  136. $questionNumber = 1;
  137. // Process in original order
  138. foreach ($originalOrder as $id) {
  139. if (!isset($questionMap[$id])) {
  140. continue;
  141. }
  142. $q = $questionMap[$id];
  143. $type = $this->normalizeQuestionType($q->question_type);
  144. // Build question object for template
  145. $questionObj = (object) [
  146. 'id' => $q->id,
  147. 'question_number' => $questionNumber++,
  148. 'content' => $q->stem,
  149. 'options' => is_string($q->options) ? json_decode($q->options, true) : ($q->options ?? []),
  150. 'answer' => $q->answer,
  151. 'solution' => $q->solution,
  152. 'score' => $this->getDefaultScore($type),
  153. 'difficulty' => $q->difficulty,
  154. 'kp_code' => $q->kp_code,
  155. ];
  156. $grouped[$type][] = $questionObj;
  157. }
  158. return $grouped;
  159. }
  160. /**
  161. * Normalize question type to standard format
  162. */
  163. private function normalizeQuestionType(?string $type): string
  164. {
  165. if (!$type) {
  166. return 'answer';
  167. }
  168. $type = strtolower(trim($type));
  169. $typeMap = [
  170. 'choice' => 'choice',
  171. '选择题' => 'choice',
  172. 'single_choice' => 'choice',
  173. 'multiple_choice' => 'choice',
  174. 'fill' => 'fill',
  175. '填空题' => 'fill',
  176. 'blank' => 'fill',
  177. 'answer' => 'answer',
  178. '解答题' => 'answer',
  179. 'subjective' => 'answer',
  180. 'calculation' => 'answer',
  181. 'proof' => 'answer',
  182. ];
  183. return $typeMap[$type] ?? 'answer';
  184. }
  185. /**
  186. * Get default score by question type
  187. */
  188. private function getDefaultScore(string $type): int
  189. {
  190. return match ($type) {
  191. 'choice' => 5,
  192. 'fill' => 5,
  193. 'answer' => 10,
  194. default => 5,
  195. };
  196. }
  197. /**
  198. * Build virtual paper structure
  199. */
  200. private function buildVirtualPaper(string $paperName, string $studentId, array $groupedQuestions): object
  201. {
  202. $totalScore = 0;
  203. $totalQuestions = 0;
  204. foreach ($groupedQuestions as $type => $questions) {
  205. foreach ($questions as $q) {
  206. $totalScore += $q->score;
  207. $totalQuestions++;
  208. }
  209. }
  210. // Generate unique paper ID
  211. $paperId = 'custom_' . $studentId . '_' . time() . '_' . uniqid();
  212. return (object) [
  213. 'paper_id' => $paperId,
  214. 'paper_name' => $paperName,
  215. 'total_score' => $totalScore,
  216. 'total_questions' => $totalQuestions,
  217. 'created_at' => now()->toDateTimeString(),
  218. ];
  219. }
  220. }