StudentDashboard.php 12 KB

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