StudentDashboard.php 12 KB

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