| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\KnowledgeGraphService;
- use App\Services\KnowledgeMasteryService;
- use App\Services\MasteryCalculator;
- use BackedEnum;
- use Filament\Pages\Page;
- use UnitEnum;
- class StudentAnalysis extends Page
- {
- protected static ?string $title = '学生掌握度分析';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
- protected static ?string $navigationLabel = '学生分析';
- protected static string|UnitEnum|null $navigationGroup = '其他';
- protected static ?int $navigationSort = 24;
- protected string $view = 'filament.pages.student-analysis-simple';
- /**
- * 禁用导航显示(与"学生仪表板"功能重复)
- */
- public static function shouldRegisterNavigation(): bool
- {
- return false;
- }
- // 当前选中的学生
- public ?string $selectedStudentId = null;
- public array $studentInfo = [];
- public array $masteryData = [];
- public array $weaknesses = [];
- public array $skills = [];
- public array $learningPath = [];
- /**
- * 获取所有学生列表
- */
- public function students(): array
- {
- return \App\Models\Student::all()->toArray();
- }
- public function updatedSelectedStudentId($value)
- {
- if ($value) {
- $this->loadAnalysisData();
- } else {
- $this->reset(['studentInfo', 'masteryData', 'weaknesses', 'skills', 'learningPath']);
- }
- }
- public function loadAnalysisData()
- {
- if (!$this->selectedStudentId) {
- return;
- }
- // 使用本地的KnowledgeMasteryService
- $masteryService = app(KnowledgeMasteryService::class);
- $masteryCalculator = app(MasteryCalculator::class);
- // 1. 获取学生掌握度数据
- $stats = $masteryService->getStats($this->selectedStudentId);
- $this->studentInfo = $stats['success'] ? $stats['data'] : [];
- // 2. 获取掌握度概览
- $overview = $masteryCalculator->getStudentMasteryOverview($this->selectedStudentId);
- // 3. 获取薄弱点列表(掌握度 < 0.5)
- $this->weaknesses = array_filter($overview['details'] ?? [], function($item) {
- return floatval($item->mastery_level ?? 0) < 0.5;
- });
- // 4. 获取技能熟练度(从掌握度数据转换)
- $this->skills = $this->getSkillsData($overview);
- // 5. 获取学习路径建议(基于薄弱点)
- $this->learningPath = $this->generateLearningPath($this->weaknesses);
- // 6. 获取知识点掌握度详情
- $this->masteryData = $overview['details'] ?? [];
- }
- /**
- * 从掌握度数据转换技能数据
- */
- private function getSkillsData(array $overview): array
- {
- $skills = [];
- foreach ($overview['details'] ?? [] as $detail) {
- $skills[] = [
- 'skill_name' => $detail->kp_code,
- 'proficiency' => floatval($detail->mastery_level ?? 0),
- 'mastery_level' => floatval($detail->mastery_level ?? 0),
- ];
- }
- return $skills;
- }
- /**
- * 基于薄弱点生成学习路径
- */
- private function generateLearningPath(array $weaknesses): array
- {
- $path = [];
- foreach (array_slice($weaknesses, 0, 5) as $weakness) {
- $path[] = [
- 'kp_code' => $weakness->kp_code ?? '',
- 'recommendation' => '建议重点练习此知识点',
- 'priority' => 'high',
- 'estimated_hours' => 2,
- ];
- }
- return $path;
- }
- private function getMasteryDetails(string $studentId): array
- {
- try {
- // 从MySQL直接查询学生掌握度详情
- $db = app('db');
- $db->connection('remote_mysql');
- $masteryRecords = \Illuminate\Support\Facades\DB::connection('remote_mysql')
- ->table('student_mastery as sm')
- ->join('knowledge_points as kp', 'sm.kp', '=', 'kp.kp')
- ->where('sm.student_id', $studentId)
- ->select([
- 'sm.kp as kp_code',
- 'kp.cn_name as kp_name',
- 'sm.mastery',
- 'sm.stability',
- 'sm.update_time'
- ])
- ->orderBy('sm.mastery', 'asc')
- ->limit(50)
- ->get()
- ->toArray();
- return array_map(function ($record) {
- return [
- 'kp_code' => $record->kp_code,
- 'kp_name' => $record->kp_name,
- 'mastery' => (float) $record->mastery,
- 'stability' => (float) $record->stability,
- 'update_time' => $record->update_time,
- 'mastery_level' => $this->getMasteryLevel((float) $record->mastery),
- 'weakness_score' => 1.0 - (float) $record->mastery
- ];
- }, $masteryRecords);
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('获取掌握度详情失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- private function getMasteryLevel(float $mastery): string
- {
- if ($mastery >= 0.9) return '优秀';
- if ($mastery >= 0.8) return '良好';
- if ($mastery >= 0.7) return '中等';
- if ($mastery >= 0.6) return '及格';
- return '需提升';
- }
- public function getMasteryColor(float $mastery): string
- {
- if ($mastery >= 0.9) return '#10b981'; // emerald-500
- if ($mastery >= 0.8) return '#34d399'; // emerald-400
- if ($mastery >= 0.7) return '#fbbf24'; // amber-400
- if ($mastery >= 0.6) return '#fb923c'; // orange-400
- return '#ef4444'; // red-500
- }
- public function getMasteryBgColor(float $mastery): string
- {
- if ($mastery >= 0.9) return 'bg-emerald-100';
- if ($mastery >= 0.8) return 'bg-emerald-50';
- if ($mastery >= 0.7) return 'bg-amber-100';
- if ($mastery >= 0.6) return 'bg-orange-100';
- return 'bg-red-100';
- }
- public function generateStudyPlan()
- {
- if (!$this->selectedStudentId || empty($this->weaknesses)) {
- return;
- }
- // TODO: 调用LearningAnalytics服务生成学习计划
- // 这里可以调用专门的API来生成个性化学习计划
- return redirect()->route('filament.admin.auth.learning-plan', [
- 'student_id' => $this->selectedStudentId
- ]);
- }
- public function exportAnalysis()
- {
- if (!$this->selectedStudentId) {
- return;
- }
- // TODO: 导出分析报告为PDF或Excel
- // 可以调用专门的导出服务
- }
- }
|