| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace App\Filament\Pages;
- use App\Filament\Traits\HandlesMindmapDetails;
- use App\Filament\Traits\HasUserRole;
- use App\Models\Student;
- use App\Models\Teacher;
- use App\Services\LearningAnalyticsService;
- use BackedEnum;
- use Filament\Pages\Page;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use UnitEnum;
- use Livewire\Attributes\Layout;
- use Livewire\Attributes\Title;
- use Livewire\Attributes\On;
- use Livewire\Attributes\Computed;
- use App\Models\Student as StudentModel;
- use App\Services\MistakeBookService;
- class StudentDashboard extends Page
- {
- use HasUserRole, HandlesMindmapDetails;
- use \Filament\Pages\Concerns\InteractsWithFormActions;
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
- protected static string|UnitEnum|null $navigationGroup = '操作';
- protected static ?string $navigationLabel = '学生仪表板';
- protected static ?int $navigationSort = 4;
- protected ?string $heading = '学生仪表板';
- protected string $view = 'filament.pages.student-dashboard';
- public string $studentId = '';
- public string $teacherId = '';
- public array $dashboardData = [];
- public bool $isLoading = false;
- public string $errorMessage = '';
- public array $mindmapMasteryData = [];
- public bool $mindmapDrawerOpen = false;
- public array $mindmapNodeDetails = [];
- public ?string $mindmapSelectedNode = null;
- // teachers 和 students 现在是 Computed 属性,不再需要声明
- public array $mistakePanel = [];
- public function mount(Request $request): void
- {
- // 初始化用户角色检查
- $this->initializeUserRole();
- // 如果是老师,自动选择当前老师
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $this->teacherId = $teacherId;
- }
- } else {
- // 从请求中获取老师ID
- $this->teacherId = (string) ($request->input('teacher_id') ?? '');
- }
- // 从请求中获取学生ID
- $this->studentId = (string) ($request->input('student_id') ?? '');
- if ($this->studentId && empty($this->teacherId)) {
- $student = StudentModel::find($this->studentId);
- if ($student && $student->teacher_id) {
- $this->teacherId = (string) $student->teacher_id;
- }
- }
- // 若已通过 URL 传入学生,自动加载仪表盘数据,减少手动刷新
- if ($this->studentId && $this->teacherId) {
- $this->loadDashboardData();
- }
- }
- #[Computed]
- public function teachers(): array
- {
- try {
- $query = Teacher::query()
- ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
- ->select(
- 'teachers.teacher_id',
- 'teachers.name',
- 'teachers.subject',
- 'u.username',
- 'u.email'
- );
- // 如果是老师,只返回自己
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $query->where('teachers.teacher_id', $teacherId);
- }
- }
- $teachers = $query->orderBy('teachers.name')->get();
- // 检查是否有学生没有对应的老师记录
- $teacherIds = $teachers->pluck('teacher_id')->toArray();
- $missingTeacherIds = Student::query()
- ->distinct()
- ->whereNotIn('teacher_id', $teacherIds)
- ->pluck('teacher_id')
- ->toArray();
- $teachersArray = $teachers->all();
- if (!empty($missingTeacherIds)) {
- foreach ($missingTeacherIds as $missingId) {
- $teachersArray[] = (object) [
- 'teacher_id' => $missingId,
- 'name' => '未知老师 (' . $missingId . ')',
- 'subject' => '未知',
- 'username' => null,
- 'email' => null
- ];
- }
- usort($teachersArray, function($a, $b) {
- return strcmp($a->name, $b->name);
- });
- }
- return $teachersArray;
- } catch (\Exception $e) {
- Log::error('加载老师列表失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function students(): array
- {
- if (empty($this->teacherId)) {
- return [];
- }
- try {
- return Student::query()
- ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
- ->where('students.teacher_id', $this->teacherId)
- ->select(
- 'students.student_id',
- 'students.name',
- 'students.grade',
- 'students.class_name',
- 'u.username',
- 'u.email'
- )
- ->orderBy('students.grade')
- ->orderBy('students.class_name')
- ->orderBy('students.name')
- ->get()
- ->all();
- } catch (\Exception $e) {
- Log::error('加载学生列表失败', [
- 'teacher_id' => $this->teacherId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 老师改变时重新加载学生列表
- */
- public function updatedTeacherId(): void
- {
- // 清空之前选中的学生ID
- $this->studentId = '';
- }
- /**
- * 学生改变时重新加载数据
- */
- public function updatedStudentId(): void
- {
- if (!empty($this->studentId)) {
- $this->loadDashboardData();
- } else {
- $this->mindmapMasteryData = [];
- $this->dispatch('mastery-updated', data: []);
- $this->mistakePanel = [];
- }
- }
- public function loadDashboardData(): void
- {
- // 检查是否选择了学生
- if (empty($this->studentId)) {
- $this->errorMessage = '请先选择学生';
- $this->isLoading = false;
- return;
- }
- $this->isLoading = true;
- $this->errorMessage = '';
- try {
- $service = app(LearningAnalyticsService::class);
- // 检查服务健康状态
- if (!$service->checkHealth()) {
- $this->errorMessage = '学习分析系统当前不可用,请稍后重试';
- $this->isLoading = false;
- return;
- }
- Log::info('开始加载仪表板数据', ['student_id' => $this->studentId]);
- // 获取各项数据
- $masteryOverview = $service->getStudentMasteryOverview($this->studentId);
- $skillProficiency = $service->getStudentSkillProficiency($this->studentId);
- $skillSummary = $service->getStudentSkillSummary($this->studentId);
- $predictions = $service->getStudentPredictions($this->studentId, 5);
- $learningPaths = $service->getStudentLearningPaths($this->studentId, 3);
- $predictionAnalytics = $service->getPredictionAnalytics($this->studentId);
- $pathAnalytics = $service->getLearningPathAnalytics($this->studentId);
- $quickPrediction = $service->quickScorePrediction($this->studentId);
- Log::info('快速预测结果', [
- 'student_id' => $this->studentId,
- 'quick_prediction' => $quickPrediction
- ]);
- $recommendations = $service->recommendLearningPaths($this->studentId, 3);
- // 组合数据
- $this->dashboardData = [
- 'mastery' => [
- 'overview' => $masteryOverview,
- 'list' => $service->getStudentMasteryList($this->studentId),
- ],
- 'skill' => [
- 'proficiency' => $skillProficiency,
- 'summary' => $skillSummary,
- ],
- 'prediction' => [
- 'list' => $predictions,
- 'analytics' => $predictionAnalytics,
- 'quick' => $quickPrediction,
- ],
- 'learning_path' => [
- 'list' => $learningPaths,
- 'analytics' => $pathAnalytics,
- 'recommendations' => $recommendations,
- ],
- ];
- $this->mindmapMasteryData = $this->buildMasteryMap(
- $this->dashboardData['mastery']['list'] ?? []
- );
- $this->dispatch('mastery-updated', data: $this->mindmapMasteryData);
- Log::info('仪表板数据加载完成', [
- 'student_id' => $this->studentId,
- 'dashboard_data_keys' => array_keys($this->dashboardData)
- ]);
- try {
- $mistakeService = app(MistakeBookService::class);
- $this->mistakePanel = $mistakeService->getPanelSnapshot($this->studentId, 5);
- } catch (\Exception $e) {
- Log::warning('加载错题本面板数据失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- $this->mistakePanel = [];
- }
- } catch (\Exception $e) {
- $this->errorMessage = '加载数据时发生错误:' . $e->getMessage();
- Log::error('学生仪表板数据加载失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- $this->mindmapMasteryData = [];
- $this->dispatch('mastery-updated', data: []);
- $this->mistakePanel = [];
- } finally {
- $this->isLoading = false;
- }
- }
- public function recalculateMastery(string $kpCode): void
- {
- try {
- $service = app(LearningAnalyticsService::class);
- $result = $service->recalculateMastery($this->studentId, $kpCode);
- if ($result) {
- $this->dispatch('notify', message: '掌握度重新计算完成', type: 'success');
- $this->loadDashboardData(); // 刷新数据
- } else {
- $this->dispatch('notify', message: '掌握度重新计算失败', type: 'danger');
- }
- } catch (\Exception $e) {
- Log::error('重新计算掌握度失败', [
- 'student_id' => $this->studentId,
- 'kp_code' => $kpCode,
- 'error' => $e->getMessage()
- ]);
- $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
- }
- }
- public function batchUpdateSkills(): void
- {
- try {
- $service = app(LearningAnalyticsService::class);
- $result = $service->batchUpdateSkillProficiency($this->studentId);
- if ($result) {
- $this->dispatch('notify', message: '技能熟练度更新完成', type: 'success');
- $this->loadDashboardData(); // 刷新数据
- } else {
- $this->dispatch('notify', message: '技能熟练度更新失败', type: 'danger');
- }
- } catch (\Exception $e) {
- Log::error('批量更新技能熟练度失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
- }
- }
- public function generateQuickPrediction(): void
- {
- try {
- $service = app(LearningAnalyticsService::class);
- $result = $service->quickScorePrediction($this->studentId);
- if ($result) {
- $this->dispatch('notify', message: '快速预测生成完成', type: 'success');
- $this->loadDashboardData(); // 刷新数据
- } else {
- $this->dispatch('notify', message: '快速预测生成失败', type: 'danger');
- }
- } catch (\Exception $e) {
- Log::error('生成快速预测失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
- }
- }
- protected function buildMasteryMap(array $list): array
- {
- $map = [];
- $items = $list['data'] ?? $list['masteries'] ?? $list;
- if (!is_array($items)) {
- return $map;
- }
- foreach ($items as $item) {
- if (!is_array($item)) {
- continue;
- }
- $code = $item['kp_code'] ?? $item['code'] ?? null;
- if (!$code) {
- continue;
- }
- $map[$code] = $item;
- }
- return $map;
- }
- public function openMindmapDrawer(string $nodeId): void
- {
- $this->mindmapSelectedNode = $nodeId;
- $this->mindmapNodeDetails = $this->getNodeDetails($nodeId, $this->mindmapMasteryData);
- $this->mindmapDrawerOpen = true;
- }
- public function closeMindmapDrawer(): void
- {
- $this->mindmapDrawerOpen = false;
- $this->mindmapSelectedNode = null;
- $this->mindmapNodeDetails = [];
- }
- /**
- * 监听TeacherStudentSelector组件的老师变化事件
- */
- #[On('teacherChanged')]
- public function onTeacherChanged(string $teacherId): void
- {
- $this->teacherId = $teacherId;
- $this->loadStudentsByTeacher();
- $this->studentId = $this->getDefaultStudentId();
- }
- /**
- * 监听TeacherStudentSelector组件的学生变化事件
- */
- #[On('studentChanged')]
- public function onStudentChanged(string $teacherId, string $studentId): void
- {
- $this->teacherId = $teacherId;
- $this->studentId = $studentId;
- $this->loadDashboardData();
- }
- }
|