StudentDashboard.php 11 KB

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