ExamDetail.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionBankService;
  4. use BackedEnum;
  5. use Filament\Notifications\Notification;
  6. use Filament\Pages\Page;
  7. use UnitEnum;
  8. use Livewire\Attributes\Computed;
  9. class ExamDetail extends Page
  10. {
  11. protected static ?string $title = '试卷详情';
  12. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-text';
  13. protected static ?string $navigationLabel = '试卷详情';
  14. protected static string|UnitEnum|null $navigationGroup = '管理';
  15. protected static ?int $navigationSort = 15;
  16. protected string $view = 'filament.pages.exam-detail';
  17. // 试卷ID(从URL参数获取)
  18. public ?string $paperId = null;
  19. // 试卷详情
  20. public array $paperDetail = [];
  21. // 编辑功能
  22. public ?string $editingExamId = null;
  23. public array $editForm = [];
  24. // 题目编辑功能
  25. public array $availableQuestions = [];
  26. public ?string $selectedKnowledgePoint = null;
  27. public ?string $selectedQuestionType = null;
  28. public bool $showAddQuestionModal = false;
  29. // 是否正在加载
  30. public bool $isLoading = false;
  31. public function mount()
  32. {
  33. // 从URL查询参数获取paperId
  34. $this->paperId = request()->query('paperId');
  35. if ($this->paperId) {
  36. $this->loadPaperDetail();
  37. }
  38. }
  39. protected function loadPaperDetail()
  40. {
  41. if (!$this->paperId) {
  42. $this->paperDetail = [];
  43. return;
  44. }
  45. $paper = \App\Models\Paper::with(['questions' => function($query) {
  46. $query->orderBy('question_number');
  47. }])->find($this->paperId);
  48. if ($paper) {
  49. $this->paperDetail = [
  50. 'paper_id' => $paper->paper_id,
  51. 'paper_name' => $paper->paper_name,
  52. 'question_count' => $paper->questions->count(),
  53. 'total_score' => $paper->total_score,
  54. 'difficulty_category' => $paper->difficulty_category,
  55. 'status' => $paper->status,
  56. 'created_at' => $paper->created_at,
  57. 'updated_at' => $paper->updated_at,
  58. 'questions' => $paper->questions->map(function($question) {
  59. // 直接使用paper_questions表中的数据,不依赖外部Question模型
  60. return [
  61. 'id' => $question->id,
  62. 'question_id' => $question->question_id,
  63. 'question_number' => $question->question_number,
  64. 'question_bank_id' => $question->question_bank_id,
  65. 'question_type' => $question->question_type,
  66. 'score' => $question->score,
  67. 'knowledge_point' => $question->knowledge_point,
  68. 'difficulty' => $question->difficulty,
  69. 'estimated_time' => $question->estimated_time,
  70. // 使用question_text字段(如果有的话)
  71. 'stem' => $question->question_text ?: '题目详情请查看题库系统',
  72. 'answer' => '',
  73. 'solution' => '',
  74. 'question_code' => 'QB_' . $question->question_bank_id,
  75. 'difficulty_label' => $this->getDifficultyLabel($question->difficulty),
  76. ];
  77. })->toArray(),
  78. ];
  79. } else {
  80. $this->paperDetail = [];
  81. }
  82. }
  83. /**
  84. * 根据难度值获取难度标签
  85. */
  86. private function getDifficultyLabel(?float $difficulty): string
  87. {
  88. if ($difficulty === null) {
  89. return '未知';
  90. }
  91. return match (true) {
  92. $difficulty <= 0.4 => '基础',
  93. $difficulty <= 0.7 => '中等',
  94. default => '拔高',
  95. };
  96. }
  97. public function startEditExam()
  98. {
  99. $paper = \App\Models\Paper::find($this->paperId);
  100. if ($paper) {
  101. $this->editingExamId = $this->paperId;
  102. $this->editForm = [
  103. 'paper_name' => $paper->paper_name,
  104. 'difficulty_category' => $paper->difficulty_category,
  105. 'status' => $paper->status,
  106. ];
  107. }
  108. }
  109. public function saveExamEdit()
  110. {
  111. try {
  112. $paper = \App\Models\Paper::find($this->paperId);
  113. if (!$paper) {
  114. throw new \Exception('试卷不存在');
  115. }
  116. // 验证表单数据
  117. $validated = \Illuminate\Support\Facades\Validator::make($this->editForm, [
  118. 'paper_name' => 'required|string|max:255',
  119. 'difficulty_category' => 'required|in:基础,进阶,竞赛',
  120. 'status' => 'required|in:draft,completed,graded',
  121. ])->validate();
  122. $paper->update($validated);
  123. Notification::make()
  124. ->title('修改成功')
  125. ->body('试卷信息已更新')
  126. ->success()
  127. ->send();
  128. $this->reset('editingExamId', 'editForm');
  129. $this->loadPaperDetail();
  130. } catch (\Illuminate\Validation\ValidationException $e) {
  131. Notification::make()
  132. ->title('验证失败')
  133. ->body('请检查输入的数据是否正确')
  134. ->danger()
  135. ->send();
  136. } catch (\Exception $e) {
  137. \Illuminate\Support\Facades\Log::error('修改试卷失败', [
  138. 'paper_id' => $this->paperId,
  139. 'error' => $e->getMessage()
  140. ]);
  141. Notification::make()
  142. ->title('修改失败')
  143. ->body($e->getMessage())
  144. ->danger()
  145. ->send();
  146. }
  147. }
  148. public function cancelEdit()
  149. {
  150. $this->reset('editingExamId', 'editForm');
  151. }
  152. /**
  153. * 删除试卷中的题目
  154. */
  155. public function deleteQuestion(int $questionId)
  156. {
  157. try {
  158. \Illuminate\Support\Facades\DB::beginTransaction();
  159. $paperQuestion = \App\Models\PaperQuestion::where('paper_id', $this->paperId)
  160. ->where('id', $questionId)
  161. ->first();
  162. if (!$paperQuestion) {
  163. throw new \Exception('题目不存在');
  164. }
  165. $paperQuestion->delete();
  166. // 更新试卷的题目数量
  167. $paper = \App\Models\Paper::find($this->paperId);
  168. if ($paper) {
  169. $paper->question_count = $paper->questions()->count();
  170. $paper->total_score = $paper->questions()->sum('score');
  171. $paper->save();
  172. }
  173. \Illuminate\Support\Facades\DB::commit();
  174. Notification::make()
  175. ->title('删除成功')
  176. ->body('题目已从试卷中删除')
  177. ->success()
  178. ->send();
  179. $this->loadPaperDetail();
  180. } catch (\Exception $e) {
  181. \Illuminate\Support\Facades\DB::rollBack();
  182. \Illuminate\Support\Facades\Log::error('删除题目失败', [
  183. 'paper_id' => $this->paperId,
  184. 'question_id' => $questionId,
  185. 'error' => $e->getMessage()
  186. ]);
  187. Notification::make()
  188. ->title('删除失败')
  189. ->body($e->getMessage())
  190. ->danger()
  191. ->send();
  192. }
  193. }
  194. /**
  195. * 搜索可选题目
  196. * 注意:由于没有questions表,这里返回空数组
  197. * 实际使用时需要连接外部题库API
  198. */
  199. public function searchQuestions()
  200. {
  201. if (!$this->selectedKnowledgePoint && !$this->selectedQuestionType) {
  202. $this->availableQuestions = [];
  203. return;
  204. }
  205. try {
  206. // TODO: 连接外部题库API获取题目
  207. // 这里暂时返回空数组,实际项目中需要调用题库服务
  208. $this->availableQuestions = [];
  209. // 如果需要,可以显示提示信息
  210. Notification::make()
  211. ->title('提示')
  212. ->body('题库功能需要连接外部API,当前显示模拟数据')
  213. ->info()
  214. ->send();
  215. } catch (\Exception $e) {
  216. \Illuminate\Support\Facades\Log::error('搜索题目失败', ['error' => $e->getMessage()]);
  217. $this->availableQuestions = [];
  218. }
  219. }
  220. /**
  221. * 添加题目到试卷
  222. * 注意:由于没有questions表,这里暂时禁用了添加功能
  223. */
  224. public function addQuestion(int $questionBankId)
  225. {
  226. try {
  227. // TODO: 连接外部题库API获取题目详情
  228. // 这里暂时返回错误提示
  229. Notification::make()
  230. ->title('功能暂未开放')
  231. ->body('添加题目功能需要连接外部题库API,当前暂未开放')
  232. ->warning()
  233. ->send();
  234. } catch (\Exception $e) {
  235. \Illuminate\Support\Facades\Log::error('添加题目失败', [
  236. 'paper_id' => $this->paperId,
  237. 'question_bank_id' => $questionBankId,
  238. 'error' => $e->getMessage()
  239. ]);
  240. Notification::make()
  241. ->title('添加失败')
  242. ->body($e->getMessage())
  243. ->danger()
  244. ->send();
  245. }
  246. }
  247. /**
  248. * 根据题目确定题型
  249. * 已移除对Question模型的依赖
  250. */
  251. /**
  252. * 根据难度计算分数
  253. */
  254. private function calculateScore(float $difficulty): float
  255. {
  256. return match (true) {
  257. $difficulty <= 0.4 => 5.0,
  258. $difficulty <= 0.7 => 10.0,
  259. default => 15.0,
  260. };
  261. }
  262. /**
  263. * 根据难度计算预计用时(秒)
  264. */
  265. private function calculateEstimatedTime(float $difficulty): int
  266. {
  267. return match (true) {
  268. $difficulty <= 0.4 => 120,
  269. $difficulty <= 0.7 => 180,
  270. default => 300,
  271. };
  272. }
  273. /**
  274. * 导出PDF
  275. * 注意:当前外部API可能不稳定,可能导致导出失败
  276. */
  277. public function exportPdf()
  278. {
  279. try {
  280. $questionBankService = app(QuestionBankService::class);
  281. $pdfUrl = $questionBankService->exportExamToPdf($this->paperId);
  282. if ($pdfUrl) {
  283. Notification::make()
  284. ->title('PDF导出成功')
  285. ->body('试卷已导出为PDF格式')
  286. ->success()
  287. ->send();
  288. } else {
  289. Notification::make()
  290. ->title('PDF导出暂时不可用')
  291. ->body('外部题库服务暂时不可用,请稍后重试或联系管理员')
  292. ->warning()
  293. ->send();
  294. }
  295. } catch (\Exception $e) {
  296. Log::error('PDF导出异常', [
  297. 'paper_id' => $this->paperId,
  298. 'error' => $e->getMessage()
  299. ]);
  300. Notification::make()
  301. ->title('PDF导出失败')
  302. ->body('导出过程中发生错误:' . $e->getMessage())
  303. ->danger()
  304. ->send();
  305. }
  306. }
  307. /**
  308. * 复制试卷配置
  309. */
  310. public function duplicateExam()
  311. {
  312. $learningService = app(\App\Services\LearningAnalyticsService::class);
  313. // 提取试卷配置
  314. $examConfig = [
  315. 'paper_name' => $this->paperDetail['paper_name'] . ' (副本)',
  316. 'total_questions' => $this->paperDetail['question_count'],
  317. 'difficulty_category' => $this->paperDetail['difficulty_category'] ?? '基础',
  318. 'question_type_ratio' => [
  319. '选择题' => 40,
  320. '填空题' => 30,
  321. '解答题' => 30,
  322. ],
  323. 'difficulty_ratio' => [
  324. '基础' => 50,
  325. '中等' => 35,
  326. '拔高' => 15,
  327. ],
  328. ];
  329. Notification::make()
  330. ->title('试卷配置已复制')
  331. ->body('请前往智能出卷页面查看并使用该配置')
  332. ->success()
  333. ->send();
  334. }
  335. public function getStatusColor(string $status): string
  336. {
  337. return match($status) {
  338. 'draft' => 'ghost',
  339. 'completed' => 'success',
  340. 'graded' => 'primary',
  341. default => 'ghost',
  342. };
  343. }
  344. public function getStatusLabel(string $status): string
  345. {
  346. return match($status) {
  347. 'draft' => '草稿',
  348. 'completed' => '已完成',
  349. 'graded' => '已评分',
  350. default => '未知',
  351. };
  352. }
  353. public function getDifficultyColor(string $difficulty): string
  354. {
  355. return match($difficulty) {
  356. '基础' => 'success',
  357. '进阶' => 'warning',
  358. '竞赛' => 'error',
  359. default => 'ghost',
  360. };
  361. }
  362. }