QuestionPdfController.php 7.4 KB

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