ExamHistory.php 17 KB

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