StudentDashboard.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\LearningAnalyticsService;
  4. use BackedEnum;
  5. use Filament\Pages\Page;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Http;
  9. use Illuminate\Support\Facades\Log;
  10. use UnitEnum;
  11. use Livewire\Attributes\Layout;
  12. use Livewire\Attributes\Title;
  13. use Livewire\Attributes\On;
  14. class StudentDashboard extends Page
  15. {
  16. use \Filament\Pages\Concerns\InteractsWithFormActions;
  17. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
  18. protected static string|UnitEnum|null $navigationGroup = '操作';
  19. protected static ?string $navigationLabel = '学生仪表板';
  20. protected static ?int $navigationSort = 3;
  21. protected ?string $heading = '学生仪表板';
  22. protected string $view = 'filament.pages.student-dashboard';
  23. public string $studentId = '';
  24. public string $teacherId = '';
  25. public array $dashboardData = [];
  26. public bool $isLoading = false;
  27. public string $errorMessage = '';
  28. public array $teachers = [];
  29. public array $students = [];
  30. public function mount(Request $request): void
  31. {
  32. // 加载老师列表
  33. $this->loadTeachers();
  34. // 从请求中获取老师ID或使用默认值
  35. $this->teacherId = $request->input('teacher_id', $this->getDefaultTeacherId());
  36. // 根据老师ID加载学生列表
  37. $this->loadStudentsByTeacher();
  38. // 从请求中获取学生ID或使用默认值
  39. $this->studentId = $request->input('student_id', $this->getDefaultStudentId());
  40. }
  41. /**
  42. * 获取默认老师ID(列表中的第一个老师)
  43. */
  44. private function getDefaultTeacherId(): string
  45. {
  46. return !empty($this->teachers) ? $this->teachers[0]->teacher_id : '';
  47. }
  48. /**
  49. * 获取默认学生ID(列表中的第一个学生)
  50. */
  51. private function getDefaultStudentId(): string
  52. {
  53. return !empty($this->students) ? $this->students[0]->student_id : '';
  54. }
  55. /**
  56. * 从MySQL加载老师列表
  57. */
  58. public function loadTeachers(): void
  59. {
  60. try {
  61. // 首先获取teachers表中的老师
  62. $this->teachers = DB::connection('remote_mysql')
  63. ->table('teachers as t')
  64. ->leftJoin('users as u', 't.teacher_id', '=', 'u.user_id')
  65. ->select(
  66. 't.teacher_id',
  67. 't.name',
  68. 't.subject',
  69. 'u.username',
  70. 'u.email'
  71. )
  72. ->orderBy('t.name')
  73. ->get()
  74. ->toArray();
  75. // 如果有学生但没有对应的老师记录,添加一个"未知老师"条目
  76. $teacherIds = array_column($this->teachers, 'teacher_id');
  77. $missingTeacherIds = DB::connection('remote_mysql')
  78. ->table('students as s')
  79. ->distinct()
  80. ->whereNotIn('s.teacher_id', $teacherIds)
  81. ->pluck('teacher_id')
  82. ->toArray();
  83. if (!empty($missingTeacherIds)) {
  84. foreach ($missingTeacherIds as $missingId) {
  85. $this->teachers[] = (object) [
  86. 'teacher_id' => $missingId,
  87. 'name' => '未知老师 (' . $missingId . ')',
  88. 'subject' => '未知',
  89. 'username' => null,
  90. 'email' => null
  91. ];
  92. }
  93. // 重新排序
  94. usort($this->teachers, function($a, $b) {
  95. return strcmp($a->name, $b->name);
  96. });
  97. }
  98. } catch (\Exception $e) {
  99. Log::error('加载老师列表失败', [
  100. 'error' => $e->getMessage()
  101. ]);
  102. $this->teachers = [];
  103. }
  104. }
  105. /**
  106. * 根据老师ID加载学生列表
  107. */
  108. public function loadStudentsByTeacher(): void
  109. {
  110. try {
  111. if (empty($this->teacherId)) {
  112. $this->students = [];
  113. return;
  114. }
  115. $this->students = DB::connection('remote_mysql')
  116. ->table('students as s')
  117. ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
  118. ->where('s.teacher_id', $this->teacherId)
  119. ->select(
  120. 's.student_id',
  121. 's.name',
  122. 's.grade',
  123. 's.class_name',
  124. 'u.username',
  125. 'u.email'
  126. )
  127. ->orderBy('s.name')
  128. ->get()
  129. ->toArray();
  130. } catch (\Exception $e) {
  131. Log::error('加载学生列表失败', [
  132. 'teacher_id' => $this->teacherId,
  133. 'error' => $e->getMessage()
  134. ]);
  135. $this->students = [];
  136. }
  137. }
  138. /**
  139. * 老师改变时重新加载学生列表
  140. */
  141. public function updatedTeacherId(): void
  142. {
  143. $this->loadStudentsByTeacher();
  144. // 清空之前选中的学生ID
  145. $this->studentId = '';
  146. // 自动加载第一个学生的数据
  147. $this->studentId = $this->getDefaultStudentId();
  148. if (!empty($this->studentId)) {
  149. $this->loadDashboardData();
  150. }
  151. }
  152. public function loadDashboardData(): void
  153. {
  154. $this->isLoading = true;
  155. $this->errorMessage = '';
  156. try {
  157. $service = app(LearningAnalyticsService::class);
  158. // 检查服务健康状态
  159. if (!$service->checkHealth()) {
  160. $this->errorMessage = '学习分析系统当前不可用,请稍后重试';
  161. $this->isLoading = false;
  162. return;
  163. }
  164. Log::info('开始加载仪表板数据', ['student_id' => $this->studentId]);
  165. // 获取各项数据
  166. $masteryOverview = $service->getStudentMasteryOverview($this->studentId);
  167. $skillProficiency = $service->getStudentSkillProficiency($this->studentId);
  168. $skillSummary = $service->getStudentSkillSummary($this->studentId);
  169. $predictions = $service->getStudentPredictions($this->studentId, 5);
  170. $learningPaths = $service->getStudentLearningPaths($this->studentId, 3);
  171. $predictionAnalytics = $service->getPredictionAnalytics($this->studentId);
  172. $pathAnalytics = $service->getLearningPathAnalytics($this->studentId);
  173. $quickPrediction = $service->quickScorePrediction($this->studentId);
  174. Log::info('快速预测结果', [
  175. 'student_id' => $this->studentId,
  176. 'quick_prediction' => $quickPrediction
  177. ]);
  178. $recommendations = $service->recommendLearningPaths($this->studentId, 3);
  179. // 组合数据
  180. $this->dashboardData = [
  181. 'mastery' => [
  182. 'overview' => $masteryOverview,
  183. 'list' => $service->getStudentMasteryList($this->studentId),
  184. ],
  185. 'skill' => [
  186. 'proficiency' => $skillProficiency,
  187. 'summary' => $skillSummary,
  188. ],
  189. 'prediction' => [
  190. 'list' => $predictions,
  191. 'analytics' => $predictionAnalytics,
  192. 'quick' => $quickPrediction,
  193. ],
  194. 'learning_path' => [
  195. 'list' => $learningPaths,
  196. 'analytics' => $pathAnalytics,
  197. 'recommendations' => $recommendations,
  198. ],
  199. ];
  200. Log::info('仪表板数据加载完成', [
  201. 'student_id' => $this->studentId,
  202. 'dashboard_data_keys' => array_keys($this->dashboardData)
  203. ]);
  204. } catch (\Exception $e) {
  205. $this->errorMessage = '加载数据时发生错误:' . $e->getMessage();
  206. Log::error('学生仪表板数据加载失败', [
  207. 'student_id' => $this->studentId,
  208. 'error' => $e->getMessage()
  209. ]);
  210. } finally {
  211. $this->isLoading = false;
  212. }
  213. }
  214. public function updatedStudentId(): void
  215. {
  216. // 学生ID更新后自动刷新数据
  217. $this->loadDashboardData();
  218. }
  219. public function recalculateMastery(string $kpCode): void
  220. {
  221. try {
  222. $service = app(LearningAnalyticsService::class);
  223. $result = $service->recalculateMastery($this->studentId, $kpCode);
  224. if ($result) {
  225. $this->dispatch('notify', message: '掌握度重新计算完成', type: 'success');
  226. $this->loadDashboardData(); // 刷新数据
  227. } else {
  228. $this->dispatch('notify', message: '掌握度重新计算失败', type: 'danger');
  229. }
  230. } catch (\Exception $e) {
  231. Log::error('重新计算掌握度失败', [
  232. 'student_id' => $this->studentId,
  233. 'kp_code' => $kpCode,
  234. 'error' => $e->getMessage()
  235. ]);
  236. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  237. }
  238. }
  239. public function batchUpdateSkills(): void
  240. {
  241. try {
  242. $service = app(LearningAnalyticsService::class);
  243. $result = $service->batchUpdateSkillProficiency($this->studentId);
  244. if ($result) {
  245. $this->dispatch('notify', message: '技能熟练度更新完成', type: 'success');
  246. $this->loadDashboardData(); // 刷新数据
  247. } else {
  248. $this->dispatch('notify', message: '技能熟练度更新失败', type: 'danger');
  249. }
  250. } catch (\Exception $e) {
  251. Log::error('批量更新技能熟练度失败', [
  252. 'student_id' => $this->studentId,
  253. 'error' => $e->getMessage()
  254. ]);
  255. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  256. }
  257. }
  258. public function generateQuickPrediction(): void
  259. {
  260. try {
  261. $service = app(LearningAnalyticsService::class);
  262. $result = $service->quickScorePrediction($this->studentId);
  263. if ($result) {
  264. $this->dispatch('notify', message: '快速预测生成完成', type: 'success');
  265. $this->loadDashboardData(); // 刷新数据
  266. } else {
  267. $this->dispatch('notify', message: '快速预测生成失败', type: 'danger');
  268. }
  269. } catch (\Exception $e) {
  270. Log::error('生成快速预测失败', [
  271. 'student_id' => $this->studentId,
  272. 'error' => $e->getMessage()
  273. ]);
  274. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  275. }
  276. }
  277. /**
  278. * 监听TeacherStudentSelector组件的老师变化事件
  279. */
  280. #[On('teacherChanged')]
  281. public function onTeacherChanged(string $teacherId): void
  282. {
  283. $this->teacherId = $teacherId;
  284. $this->loadStudentsByTeacher();
  285. $this->studentId = $this->getDefaultStudentId();
  286. }
  287. /**
  288. * 监听TeacherStudentSelector组件的学生变化事件
  289. */
  290. #[On('studentChanged')]
  291. public function onStudentChanged(string $teacherId, string $studentId): void
  292. {
  293. $this->teacherId = $teacherId;
  294. $this->studentId = $studentId;
  295. $this->loadDashboardData();
  296. }
  297. }