ExamHistory.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 ExamHistory extends Page
  10. {
  11. protected static ?string $title = '卷子历史记录';
  12. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
  13. protected static ?string $navigationLabel = '卷子历史';
  14. protected static string|UnitEnum|null $navigationGroup = '管理';
  15. protected static ?int $navigationSort = 14;
  16. protected string $view = 'filament.pages.exam-history-simple';
  17. // 分页
  18. public int $currentPage = 1;
  19. public int $perPage = 20;
  20. // 筛选
  21. public ?string $search = null;
  22. public ?string $statusFilter = null;
  23. public ?string $difficultyFilter = null;
  24. // 编辑功能
  25. public ?string $editingExamId = null;
  26. public array $editForm = [];
  27. #[Computed(cache: false)]
  28. public function exams(): array
  29. {
  30. try {
  31. // 从本地数据库读取试卷列表 - 使用 papers 表
  32. // 只显示有对应题目数据的试卷
  33. $query = \App\Models\Paper::whereHas('questions');
  34. // 应用搜索过滤
  35. if ($this->search) {
  36. $query->where('paper_name', 'like', '%' . $this->search . '%');
  37. }
  38. // 应用状态过滤
  39. if ($this->statusFilter) {
  40. $query->where('status', $this->statusFilter);
  41. }
  42. // 应用难度过滤
  43. if ($this->difficultyFilter) {
  44. $query->where('difficulty_category', $this->difficultyFilter);
  45. }
  46. // 分页
  47. $total = $query->count();
  48. $papers = $query->withCount('questions')
  49. ->orderBy('created_at', 'desc')
  50. ->skip(($this->currentPage - 1) * $this->perPage)
  51. ->take($this->perPage)
  52. ->get()
  53. ->map(function ($paper) {
  54. return [
  55. 'id' => $paper->paper_id,
  56. 'paper_name' => $paper->paper_name,
  57. 'question_count' => $paper->questions_count, // 使用实际的题目数量
  58. 'total_score' => $paper->total_score,
  59. 'difficulty_category' => $paper->difficulty_category,
  60. 'status' => $paper->status,
  61. 'created_at' => $paper->created_at,
  62. ];
  63. })
  64. ->toArray();
  65. return [
  66. 'data' => $papers,
  67. 'meta' => [
  68. 'page' => $this->currentPage,
  69. 'per_page' => $this->perPage,
  70. 'total' => $total,
  71. 'total_pages' => ceil($total / $this->perPage),
  72. ]
  73. ];
  74. } catch (\Exception $e) {
  75. \Illuminate\Support\Facades\Log::error('获取试卷列表失败', ['error' => $e->getMessage()]);
  76. return [
  77. 'data' => [],
  78. 'meta' => ['page' => 1, 'per_page' => 20, 'total' => 0, 'total_pages' => 0]
  79. ];
  80. }
  81. }
  82. #[Computed(cache: false)]
  83. public function meta(): array
  84. {
  85. $examsData = $this->exams();
  86. return $examsData['meta'] ?? ['page' => 1, 'per_page' => 20, 'total' => 0, 'total_pages' => 0];
  87. }
  88. // 侧边栏详情功能已移除,使用独立页面
  89. // public function viewExamDetail(string $examId)
  90. // {
  91. // return redirect()->route('filament.admin.auth.exam-detail', ['paperId' => $examId]);
  92. // }
  93. public function exportPdf(string $examId)
  94. {
  95. try {
  96. $questionBankService = app(QuestionBankService::class);
  97. $pdfUrl = $questionBankService->exportExamToPdf($examId);
  98. if ($pdfUrl) {
  99. // TODO: 实际下载PDF
  100. Notification::make()
  101. ->title('PDF导出成功')
  102. ->body('试卷已导出为PDF格式')
  103. ->success()
  104. ->send();
  105. } else {
  106. Notification::make()
  107. ->title('PDF导出暂时不可用')
  108. ->body('外部题库服务暂时不可用,请稍后重试或联系管理员')
  109. ->warning()
  110. ->send();
  111. }
  112. } catch (\Exception $e) {
  113. \Illuminate\Support\Facades\Log::error('PDF导出异常', [
  114. 'exam_id' => $examId,
  115. 'error' => $e->getMessage()
  116. ]);
  117. Notification::make()
  118. ->title('PDF导出失败')
  119. ->body('导出过程中发生错误')
  120. ->danger()
  121. ->send();
  122. }
  123. }
  124. public function duplicateExam(array $examData)
  125. {
  126. // 复制试卷配置,用于快速生成类似试卷
  127. $learningService = app(\App\Services\LearningAnalyticsService::class);
  128. // 提取试卷配置
  129. $examConfig = [
  130. 'paper_name' => $examData['paper_name'] . ' (副本)',
  131. 'total_questions' => $examData['question_count'],
  132. 'difficulty_category' => $examData['difficulty_category'] ?? '基础',
  133. 'question_type_ratio' => [
  134. '选择题' => 40,
  135. '填空题' => 30,
  136. '解答题' => 30,
  137. ],
  138. 'difficulty_ratio' => [
  139. '基础' => 50,
  140. '中等' => 35,
  141. '拔高' => 15,
  142. ],
  143. ];
  144. // TODO: 跳转到智能出卷页面并预填充配置
  145. // 这里可以通过session传递配置,或者使用URL参数
  146. Notification::make()
  147. ->title('试卷配置已复制')
  148. ->body('请前往智能出卷页面查看并使用该配置')
  149. ->success()
  150. ->send();
  151. }
  152. public function deleteExam(string $examId)
  153. {
  154. try {
  155. \Illuminate\Support\Facades\DB::beginTransaction();
  156. $paper = \App\Models\Paper::find($examId);
  157. if (!$paper) {
  158. throw new \Exception('试卷不存在');
  159. }
  160. // 删除关联的题目
  161. $deletedQuestions = $paper->questions()->delete();
  162. // 删除试卷
  163. $paper->delete();
  164. \Illuminate\Support\Facades\DB::commit();
  165. Notification::make()
  166. ->title('删除成功')
  167. ->body("试卷及其 {$deletedQuestions} 道关联题目已删除")
  168. ->success()
  169. ->send();
  170. $this->reset('selectedExamId', 'selectedExamDetail');
  171. } catch (\Exception $e) {
  172. \Illuminate\Support\Facades\DB::rollBack();
  173. \Illuminate\Support\Facades\Log::error('删除试卷失败', [
  174. 'exam_id' => $examId,
  175. 'error' => $e->getMessage()
  176. ]);
  177. Notification::make()
  178. ->title('删除失败')
  179. ->body($e->getMessage())
  180. ->danger()
  181. ->send();
  182. }
  183. }
  184. public function startEditExam(string $examId)
  185. {
  186. $paper = \App\Models\Paper::find($examId);
  187. if ($paper) {
  188. $this->editingExamId = $examId;
  189. $this->editForm = [
  190. 'paper_name' => $paper->paper_name,
  191. 'difficulty_category' => $paper->difficulty_category,
  192. 'status' => $paper->status,
  193. ];
  194. }
  195. }
  196. public function saveExamEdit()
  197. {
  198. try {
  199. $paper = \App\Models\Paper::find($this->editingExamId);
  200. if (!$paper) {
  201. throw new \Exception('试卷不存在');
  202. }
  203. // 验证表单数据
  204. $validated = \Illuminate\Support\Facades\Validator::make($this->editForm, [
  205. 'paper_name' => 'required|string|max:255',
  206. 'difficulty_category' => 'required|in:基础,进阶,竞赛',
  207. 'status' => 'required|in:draft,completed,graded',
  208. ])->validate();
  209. $paper->update($validated);
  210. Notification::make()
  211. ->title('修改成功')
  212. ->body('试卷信息已更新')
  213. ->success()
  214. ->send();
  215. $this->reset('editingExamId', 'editForm');
  216. } catch (\Illuminate\Validation\ValidationException $e) {
  217. Notification::make()
  218. ->title('验证失败')
  219. ->body('请检查输入的数据是否正确')
  220. ->danger()
  221. ->send();
  222. } catch (\Exception $e) {
  223. \Illuminate\Support\Facades\Log::error('修改试卷失败', [
  224. 'exam_id' => $this->editingExamId,
  225. 'error' => $e->getMessage()
  226. ]);
  227. Notification::make()
  228. ->title('修改失败')
  229. ->body($e->getMessage())
  230. ->danger()
  231. ->send();
  232. }
  233. }
  234. public function cancelEdit()
  235. {
  236. $this->reset('editingExamId', 'editForm');
  237. }
  238. public function getStatusColor(string $status): string
  239. {
  240. return match($status) {
  241. 'draft' => 'ghost',
  242. 'completed' => 'success',
  243. 'graded' => 'primary',
  244. default => 'ghost',
  245. };
  246. }
  247. public function getStatusLabel(string $status): string
  248. {
  249. return match($status) {
  250. 'draft' => '草稿',
  251. 'completed' => '已完成',
  252. 'graded' => '已评分',
  253. default => '未知',
  254. };
  255. }
  256. public function getDifficultyColor(string $difficulty): string
  257. {
  258. return match($difficulty) {
  259. '基础' => 'success',
  260. '进阶' => 'warning',
  261. '竞赛' => 'error',
  262. default => 'ghost',
  263. };
  264. }
  265. /**
  266. * 删除试卷中的题目
  267. */
  268. public function deleteQuestion(string $paperId, int $questionId)
  269. {
  270. try {
  271. \Illuminate\Support\Facades\DB::beginTransaction();
  272. $paperQuestion = \App\Models\PaperQuestion::where('paper_id', $paperId)
  273. ->where('id', $questionId)
  274. ->first();
  275. if (!$paperQuestion) {
  276. throw new \Exception('题目不存在');
  277. }
  278. $paperQuestion->delete();
  279. // 更新试卷的题目数量
  280. $paper = \App\Models\Paper::find($paperId);
  281. if ($paper) {
  282. $paper->question_count = $paper->questions()->count();
  283. $paper->save();
  284. }
  285. \Illuminate\Support\Facades\DB::commit();
  286. Notification::make()
  287. ->title('删除成功')
  288. ->body('题目已从试卷中删除')
  289. ->success()
  290. ->send();
  291. // 刷新试卷详情
  292. if ($this->selectedExamId === $paperId) {
  293. $this->loadExamDetail();
  294. }
  295. } catch (\Exception $e) {
  296. \Illuminate\Support\Facades\DB::rollBack();
  297. \Illuminate\Support\Facades\Log::error('删除题目失败', [
  298. 'paper_id' => $paperId,
  299. 'question_id' => $questionId,
  300. 'error' => $e->getMessage()
  301. ]);
  302. Notification::make()
  303. ->title('删除失败')
  304. ->body($e->getMessage())
  305. ->danger()
  306. ->send();
  307. }
  308. }
  309. /**
  310. * 获取可选题目列表
  311. */
  312. #[Computed(cache: false)]
  313. public function availableQuestions(): array
  314. {
  315. if (!$this->selectedKnowledgePoint && !$this->selectedQuestionType) {
  316. return [];
  317. }
  318. try {
  319. $query = \App\Models\Question::query();
  320. // 按知识点过滤
  321. if ($this->selectedKnowledgePoint) {
  322. $query->where('kp_code', 'like', '%' . $this->selectedKnowledgePoint . '%');
  323. }
  324. // 按题型过滤
  325. if ($this->selectedQuestionType) {
  326. // 这里需要根据实际的数据结构来过滤
  327. // PaperQuestion 表中有 question_type,但 Question 表中没有
  328. // 可能需要通过 join 或其他方式处理
  329. }
  330. $questions = $query->limit(50)->get()->map(function ($question) {
  331. return [
  332. 'id' => $question->id,
  333. 'question_code' => $question->question_code,
  334. 'kp_code' => $question->kp_code,
  335. 'stem' => \Illuminate\Support\Str::limit($question->stem, 100),
  336. 'difficulty' => $question->difficulty,
  337. 'difficulty_label' => $question->difficulty_label,
  338. ];
  339. })->toArray();
  340. return $questions;
  341. } catch (\Exception $e) {
  342. \Illuminate\Support\Facades\Log::error('获取可选题目列表失败', ['error' => $e->getMessage()]);
  343. return [];
  344. }
  345. }
  346. /**
  347. * 添加题目到试卷
  348. */
  349. public function addQuestion(string $paperId, int $questionBankId)
  350. {
  351. try {
  352. \Illuminate\Support\Facades\DB::beginTransaction();
  353. $paper = \App\Models\Paper::find($paperId);
  354. if (!$paper) {
  355. throw new \Exception('试卷不存在');
  356. }
  357. $question = \App\Models\Question::find($questionBankId);
  358. if (!$question) {
  359. throw new \Exception('题目不存在');
  360. }
  361. // 检查题目是否已在试卷中
  362. $exists = \App\Models\PaperQuestion::where('paper_id', $paperId)
  363. ->where('question_bank_id', $questionBankId)
  364. ->exists();
  365. if ($exists) {
  366. throw new \Exception('题目已在试卷中');
  367. }
  368. // 获取当前试卷的最大题号
  369. $maxQuestionNumber = \App\Models\PaperQuestion::where('paper_id', $paperId)
  370. ->max('question_number') ?? 0;
  371. // 创建新的试卷题目记录
  372. \App\Models\PaperQuestion::create([
  373. 'paper_id' => $paperId,
  374. 'question_id' => 'PQ_' . uniqid(),
  375. 'question_bank_id' => $questionBankId,
  376. 'knowledge_point' => $question->kp_code,
  377. 'question_type' => $this->getQuestionTypeFromQuestion($question),
  378. 'question_text' => $question->stem,
  379. 'difficulty' => $question->difficulty,
  380. 'score' => $this->calculateScore($question->difficulty),
  381. 'estimated_time' => $this->calculateEstimatedTime($question->difficulty),
  382. 'question_number' => $maxQuestionNumber + 1,
  383. ]);
  384. // 更新试卷的题目数量和总分
  385. $paper->question_count = $paper->questions()->count();
  386. $paper->total_score = $paper->questions()->sum('score');
  387. $paper->save();
  388. \Illuminate\Support\Facades\DB::commit();
  389. Notification::make()
  390. ->title('添加成功')
  391. ->body('题目已添加到试卷中')
  392. ->success()
  393. ->send();
  394. // 刷新试卷详情
  395. if ($this->selectedExamId === $paperId) {
  396. $this->loadExamDetail();
  397. }
  398. // 清空搜索条件
  399. $this->reset('selectedKnowledgePoint', 'selectedQuestionType', 'availableQuestions');
  400. } catch (\Exception $e) {
  401. \Illuminate\Support\Facades\DB::rollBack();
  402. \Illuminate\Support\Facades\Log::error('添加题目失败', [
  403. 'paper_id' => $paperId,
  404. 'question_bank_id' => $questionBankId,
  405. 'error' => $e->getMessage()
  406. ]);
  407. Notification::make()
  408. ->title('添加失败')
  409. ->body($e->getMessage())
  410. ->danger()
  411. ->send();
  412. }
  413. }
  414. /**
  415. * 根据题目确定题型
  416. */
  417. private function getQuestionTypeFromQuestion(\App\Models\Question $question): string
  418. {
  419. // 这里需要根据题目的特征来判断题型
  420. // 临时返回默认值,可以根据实际需求调整
  421. return 'choice';
  422. }
  423. /**
  424. * 根据难度计算分数
  425. */
  426. private function calculateScore(float $difficulty): float
  427. {
  428. return match (true) {
  429. $difficulty <= 0.4 => 5.0,
  430. $difficulty <= 0.7 => 10.0,
  431. default => 15.0,
  432. };
  433. }
  434. /**
  435. * 根据难度计算预计用时(秒)
  436. */
  437. private function calculateEstimatedTime(float $difficulty): int
  438. {
  439. return match (true) {
  440. $difficulty <= 0.4 => 120,
  441. $difficulty <= 0.7 => 180,
  442. default => 300,
  443. };
  444. }
  445. }