| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace App\Filament\Pages;
- 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;
- class StudentDashboard extends Page
- {
- 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 = 3;
- 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 $teachers = [];
- public array $students = [];
- public function mount(Request $request): void
- {
- // 加载老师列表
- $this->loadTeachers();
- // 从请求中获取老师ID或使用默认值
- $this->teacherId = $request->input('teacher_id', $this->getDefaultTeacherId());
- // 根据老师ID加载学生列表
- $this->loadStudentsByTeacher();
- // 从请求中获取学生ID或使用默认值
- $this->studentId = $request->input('student_id', $this->getDefaultStudentId());
- }
- /**
- * 获取默认老师ID(列表中的第一个老师)
- */
- private function getDefaultTeacherId(): string
- {
- return !empty($this->teachers) ? $this->teachers[0]->teacher_id : '';
- }
- /**
- * 获取默认学生ID(列表中的第一个学生)
- */
- private function getDefaultStudentId(): string
- {
- return !empty($this->students) ? $this->students[0]->student_id : '';
- }
- /**
- * 从MySQL加载老师列表
- */
- public function loadTeachers(): void
- {
- try {
- // 首先获取teachers表中的老师
- $this->teachers = DB::connection('remote_mysql')
- ->table('teachers as t')
- ->leftJoin('users as u', 't.teacher_id', '=', 'u.user_id')
- ->select(
- 't.teacher_id',
- 't.name',
- 't.subject',
- 'u.username',
- 'u.email'
- )
- ->orderBy('t.name')
- ->get()
- ->toArray();
- // 如果有学生但没有对应的老师记录,添加一个"未知老师"条目
- $teacherIds = array_column($this->teachers, 'teacher_id');
- $missingTeacherIds = DB::connection('remote_mysql')
- ->table('students as s')
- ->distinct()
- ->whereNotIn('s.teacher_id', $teacherIds)
- ->pluck('teacher_id')
- ->toArray();
- if (!empty($missingTeacherIds)) {
- foreach ($missingTeacherIds as $missingId) {
- $this->teachers[] = (object) [
- 'teacher_id' => $missingId,
- 'name' => '未知老师 (' . $missingId . ')',
- 'subject' => '未知',
- 'username' => null,
- 'email' => null
- ];
- }
- // 重新排序
- usort($this->teachers, function($a, $b) {
- return strcmp($a->name, $b->name);
- });
- }
- } catch (\Exception $e) {
- Log::error('加载老师列表失败', [
- 'error' => $e->getMessage()
- ]);
- $this->teachers = [];
- }
- }
- /**
- * 根据老师ID加载学生列表
- */
- public function loadStudentsByTeacher(): void
- {
- try {
- if (empty($this->teacherId)) {
- $this->students = [];
- return;
- }
- $this->students = DB::connection('remote_mysql')
- ->table('students as s')
- ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
- ->where('s.teacher_id', $this->teacherId)
- ->select(
- 's.student_id',
- 's.name',
- 's.grade',
- 's.class_name',
- 'u.username',
- 'u.email'
- )
- ->orderBy('s.name')
- ->get()
- ->toArray();
- } catch (\Exception $e) {
- Log::error('加载学生列表失败', [
- 'teacher_id' => $this->teacherId,
- 'error' => $e->getMessage()
- ]);
- $this->students = [];
- }
- }
- /**
- * 老师改变时重新加载学生列表
- */
- public function updatedTeacherId(): void
- {
- $this->loadStudentsByTeacher();
- // 清空之前选中的学生ID
- $this->studentId = '';
- // 自动加载第一个学生的数据
- $this->studentId = $this->getDefaultStudentId();
- if (!empty($this->studentId)) {
- $this->loadDashboardData();
- }
- }
- public function loadDashboardData(): void
- {
- $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,
- ],
- ];
- Log::info('仪表板数据加载完成', [
- 'student_id' => $this->studentId,
- 'dashboard_data_keys' => array_keys($this->dashboardData)
- ]);
- } catch (\Exception $e) {
- $this->errorMessage = '加载数据时发生错误:' . $e->getMessage();
- Log::error('学生仪表板数据加载失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- } finally {
- $this->isLoading = false;
- }
- }
- public function updatedStudentId(): void
- {
- // 学生ID更新后自动刷新数据
- $this->loadDashboardData();
- }
- 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');
- }
- }
- /**
- * 监听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();
- }
- }
|