StudentDashboard.php 13 KB

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