| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\QuestionBankService;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- class ExamHistory extends Page
- {
- protected static ?string $title = '卷子历史记录';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
- protected static ?string $navigationLabel = '卷子历史';
- protected static string|UnitEnum|null $navigationGroup = '题库系统';
- protected static ?int $navigationSort = 4;
- protected string $view = 'filament.pages.exam-history-simple';
- // 分页
- public int $currentPage = 1;
- public int $perPage = 20;
- // 筛选
- public ?string $search = null;
- public ?string $statusFilter = null;
- public ?string $difficultyFilter = null;
- // 详情
- public ?string $selectedExamId = null;
- public array $selectedExamDetail = [];
- #[Computed(cache: false)]
- public function exams(): array
- {
- try {
- // 从本地数据库读取试卷列表 - 使用 papers 表
- $query = \App\Models\Paper::query();
- // 应用搜索过滤
- if ($this->search) {
- $query->where('paper_name', 'like', '%' . $this->search . '%');
- }
- // 应用状态过滤
- if ($this->statusFilter) {
- $query->where('status', $this->statusFilter);
- }
- // 应用难度过滤
- if ($this->difficultyFilter) {
- $query->where('difficulty_category', $this->difficultyFilter);
- }
- // 分页
- $total = $query->count();
- $papers = $query->orderBy('created_at', 'desc')
- ->skip(($this->currentPage - 1) * $this->perPage)
- ->take($this->perPage)
- ->get()
- ->map(function ($paper) {
- return [
- 'id' => $paper->paper_id,
- 'paper_name' => $paper->paper_name,
- 'question_count' => $paper->question_count,
- 'total_score' => $paper->total_score,
- 'difficulty_category' => $paper->difficulty_category,
- 'status' => $paper->status,
- 'created_at' => $paper->created_at,
- ];
- })
- ->toArray();
- return [
- 'data' => $papers,
- 'meta' => [
- 'page' => $this->currentPage,
- 'per_page' => $this->perPage,
- 'total' => $total,
- 'total_pages' => ceil($total / $this->perPage),
- ]
- ];
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('获取试卷列表失败', ['error' => $e->getMessage()]);
- return [
- 'data' => [],
- 'meta' => ['page' => 1, 'per_page' => 20, 'total' => 0, 'total_pages' => 0]
- ];
- }
- }
- #[Computed(cache: false)]
- public function meta(): array
- {
- $examsData = $this->exams();
- return $examsData['meta'] ?? ['page' => 1, 'per_page' => 20, 'total' => 0, 'total_pages' => 0];
- }
- public function updatedCurrentPage()
- {
- $this->reset('selectedExamId', 'selectedExamDetail');
- }
- public function viewExamDetail(string $examId)
- {
- $this->selectedExamId = $examId;
- $this->loadExamDetail();
- }
- protected function loadExamDetail()
- {
- if (!$this->selectedExamId) {
- return;
- }
- $questionBankService = app(QuestionBankService::class);
- $this->selectedExamDetail = $questionBankService->getExamById($this->selectedExamId) ?? [];
- }
- public function exportPdf(string $examId)
- {
- $questionBankService = app(QuestionBankService::class);
- $pdfUrl = $questionBankService->exportExamToPdf($examId);
- if ($pdfUrl) {
- // TODO: 实际下载PDF
- Notification::make()
- ->title('PDF导出成功')
- ->body('试卷已导出为PDF格式')
- ->success()
- ->send();
- } else {
- Notification::make()
- ->title('PDF导出失败')
- ->body('无法导出试卷,请稍后重试')
- ->danger()
- ->send();
- }
- }
- public function duplicateExam(array $examData)
- {
- // 复制试卷配置,用于快速生成类似试卷
- $learningService = app(\App\Services\LearningAnalyticsService::class);
- // 提取试卷配置
- $examConfig = [
- 'paper_name' => $examData['paper_name'] . ' (副本)',
- 'total_questions' => $examData['question_count'],
- 'difficulty_category' => $examData['difficulty_category'] ?? '基础',
- 'question_type_ratio' => [
- '选择题' => 40,
- '填空题' => 30,
- '解答题' => 30,
- ],
- 'difficulty_ratio' => [
- '基础' => 50,
- '中等' => 35,
- '拔高' => 15,
- ],
- ];
- // TODO: 跳转到智能出卷页面并预填充配置
- // 这里可以通过session传递配置,或者使用URL参数
- Notification::make()
- ->title('试卷配置已复制')
- ->body('请前往智能出卷页面查看并使用该配置')
- ->success()
- ->send();
- }
- public function deleteExam(string $examId)
- {
- // TODO: 实现删除试卷功能
- // 需要在QuestionBankService中添加deleteExam方法
- Notification::make()
- ->title('删除成功')
- ->body('试卷记录已删除')
- ->success()
- ->send();
- $this->reset('selectedExamId', 'selectedExamDetail');
- }
- public function getStatusColor(string $status): string
- {
- return match($status) {
- 'draft' => 'ghost',
- 'completed' => 'success',
- 'graded' => 'primary',
- default => 'ghost',
- };
- }
- public function getStatusLabel(string $status): string
- {
- return match($status) {
- 'draft' => '草稿',
- 'completed' => '已完成',
- 'graded' => '已评分',
- default => '未知',
- };
- }
- public function getDifficultyColor(string $difficulty): string
- {
- return match($difficulty) {
- '基础' => 'success',
- '进阶' => 'warning',
- '竞赛' => 'error',
- default => 'ghost',
- };
- }
- }
|