StudentDashboard.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Filament\Traits\HandlesMindmapDetails;
  4. use App\Filament\Traits\HasUserRole;
  5. use App\Models\Student;
  6. use App\Models\Teacher;
  7. use App\Services\KnowledgeMasteryService;
  8. use App\Services\LearningAnalyticsService;
  9. use App\Services\MasteryCalculator;
  10. use BackedEnum;
  11. use Filament\Pages\Page;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use UnitEnum;
  16. use Livewire\Attributes\Layout;
  17. use Livewire\Attributes\Title;
  18. use Livewire\Attributes\On;
  19. use Livewire\Attributes\Computed;
  20. use App\Models\Student as StudentModel;
  21. use App\Services\MistakeBookService;
  22. class StudentDashboard extends Page
  23. {
  24. use HasUserRole, HandlesMindmapDetails, \Filament\Pages\Concerns\InteractsWithFormActions;
  25. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
  26. protected static string|UnitEnum|null $navigationGroup = '学生管理';
  27. protected static ?string $navigationLabel = '学生仪表板';
  28. protected static ?int $navigationSort = 2;
  29. protected ?string $heading = '学生仪表板';
  30. protected string $view = 'filament.pages.student-dashboard';
  31. public string $studentId = '';
  32. public string $teacherId = '';
  33. public array $dashboardData = [];
  34. public bool $isLoading = false;
  35. public string $errorMessage = '';
  36. public array $mindmapMasteryData = [];
  37. public bool $mindmapDrawerOpen = false;
  38. public array $mindmapNodeDetails = [];
  39. public ?string $mindmapSelectedNode = null;
  40. // teachers 和 students 现在是 Computed 属性,不再需要声明
  41. public array $mistakePanel = [];
  42. public function mount(Request $request): void
  43. {
  44. // 初始化用户角色检查
  45. $this->initializeUserRole();
  46. // 如果是老师,自动选择当前老师
  47. if ($this->isTeacher) {
  48. $teacherId = $this->getCurrentTeacherId();
  49. if ($teacherId) {
  50. $this->teacherId = $teacherId;
  51. }
  52. } else {
  53. // 从请求中获取老师ID
  54. $this->teacherId = (string) ($request->input('teacher_id') ?? '');
  55. }
  56. // 从请求中获取学生ID
  57. $this->studentId = (string) ($request->input('student_id') ?? '');
  58. if ($this->studentId && empty($this->teacherId)) {
  59. $student = StudentModel::find($this->studentId);
  60. if ($student && $student->teacher_id) {
  61. $this->teacherId = (string) $student->teacher_id;
  62. }
  63. }
  64. // 若已通过 URL 传入学生,自动加载仪表盘数据,减少手动刷新
  65. if ($this->studentId && $this->teacherId) {
  66. $this->loadDashboardData();
  67. }
  68. }
  69. #[Computed]
  70. public function teachers(): array
  71. {
  72. try {
  73. $query = Teacher::query()
  74. ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
  75. ->select(
  76. 'teachers.teacher_id',
  77. 'teachers.name',
  78. 'teachers.subject',
  79. 'u.username',
  80. 'u.email'
  81. );
  82. // 如果是老师,只返回自己
  83. if ($this->isTeacher) {
  84. $teacherId = $this->getCurrentTeacherId();
  85. if ($teacherId) {
  86. $query->where('teachers.teacher_id', $teacherId);
  87. }
  88. }
  89. $teachers = $query->orderBy('teachers.name')->get();
  90. // 检查是否有学生没有对应的老师记录
  91. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  92. $missingTeacherIds = Student::query()
  93. ->distinct()
  94. ->whereNotIn('teacher_id', $teacherIds)
  95. ->pluck('teacher_id')
  96. ->toArray();
  97. $teachersArray = $teachers->all();
  98. if (!empty($missingTeacherIds)) {
  99. foreach ($missingTeacherIds as $missingId) {
  100. $teachersArray[] = (object) [
  101. 'teacher_id' => $missingId,
  102. 'name' => '未知老师 (' . $missingId . ')',
  103. 'subject' => '未知',
  104. 'username' => null,
  105. 'email' => null
  106. ];
  107. }
  108. usort($teachersArray, function($a, $b) {
  109. return strcmp($a->name, $b->name);
  110. });
  111. }
  112. return $teachersArray;
  113. } catch (\Exception $e) {
  114. Log::error('加载老师列表失败', [
  115. 'error' => $e->getMessage()
  116. ]);
  117. return [];
  118. }
  119. }
  120. #[Computed]
  121. public function students(): array
  122. {
  123. if (empty($this->teacherId)) {
  124. return [];
  125. }
  126. try {
  127. return Student::query()
  128. ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
  129. ->where('students.teacher_id', $this->teacherId)
  130. ->select(
  131. 'students.student_id',
  132. 'students.name',
  133. 'students.grade',
  134. 'students.class_name',
  135. 'u.username',
  136. 'u.email'
  137. )
  138. ->orderBy('students.grade')
  139. ->orderBy('students.class_name')
  140. ->orderBy('students.name')
  141. ->get()
  142. ->all();
  143. } catch (\Exception $e) {
  144. Log::error('加载学生列表失败', [
  145. 'teacher_id' => $this->teacherId,
  146. 'error' => $e->getMessage()
  147. ]);
  148. return [];
  149. }
  150. }
  151. /**
  152. * 老师改变时重新加载学生列表
  153. */
  154. public function updatedTeacherId(): void
  155. {
  156. // 清空之前选中的学生ID
  157. $this->studentId = '';
  158. }
  159. /**
  160. * 学生改变时重新加载数据
  161. */
  162. public function updatedStudentId(): void
  163. {
  164. if (!empty($this->studentId)) {
  165. $this->loadDashboardData();
  166. } else {
  167. $this->mindmapMasteryData = [];
  168. $this->dispatch('mastery-updated', data: []);
  169. $this->mistakePanel = [];
  170. }
  171. }
  172. public function loadDashboardData(): void
  173. {
  174. // 检查是否选择了学生
  175. if (empty($this->studentId)) {
  176. $this->errorMessage = '请先选择学生';
  177. $this->isLoading = false;
  178. return;
  179. }
  180. $this->isLoading = true;
  181. $this->errorMessage = '';
  182. try {
  183. $service = app(LearningAnalyticsService::class);
  184. // 检查服务健康状态
  185. if (!$service->checkHealth()) {
  186. $this->errorMessage = '学习分析系统当前不可用,请稍后重试';
  187. $this->isLoading = false;
  188. return;
  189. }
  190. Log::info('开始加载仪表板数据', ['student_id' => $this->studentId]);
  191. // 获取各项数据
  192. $masteryOverview = $service->getStudentMasteryOverview($this->studentId);
  193. $skillProficiency = $service->getStudentSkillProficiency($this->studentId);
  194. $skillSummary = $service->getStudentSkillSummary($this->studentId);
  195. $predictions = $service->getStudentPredictions($this->studentId, 5);
  196. $learningPaths = $service->getStudentLearningPaths($this->studentId, 3);
  197. $predictionAnalytics = $service->getPredictionAnalytics($this->studentId);
  198. $pathAnalytics = $service->getLearningPathAnalytics($this->studentId);
  199. $quickPrediction = $service->quickScorePrediction($this->studentId);
  200. Log::info('快速预测结果', [
  201. 'student_id' => $this->studentId,
  202. 'quick_prediction' => $quickPrediction
  203. ]);
  204. $recommendations = $service->recommendLearningPaths($this->studentId, 3);
  205. // 组合数据
  206. $masteryList = $service->getStudentMasteryList($this->studentId);
  207. // 如果没有掌握度数据,从错题记录中生成基于错题的掌握度
  208. if (empty($masteryList['data'] ?? [])) {
  209. Log::info('未找到掌握度数据,从错题记录生成', ['student_id' => $this->studentId]);
  210. $masteryList = $this->generateMasteryFromMistakes($this->studentId);
  211. }
  212. $this->dashboardData = [
  213. 'mastery' => [
  214. 'overview' => $masteryOverview,
  215. 'list' => $masteryList,
  216. ],
  217. 'skill' => [
  218. 'proficiency' => $skillProficiency,
  219. 'summary' => $skillSummary,
  220. ],
  221. 'prediction' => [
  222. 'list' => $predictions,
  223. 'analytics' => $predictionAnalytics,
  224. 'quick' => $quickPrediction,
  225. ],
  226. 'learning_path' => [
  227. 'list' => $learningPaths,
  228. 'analytics' => $pathAnalytics,
  229. 'recommendations' => $recommendations,
  230. ],
  231. ];
  232. $this->mindmapMasteryData = $this->buildMasteryMap(
  233. $this->dashboardData['mastery']['list'] ?? []
  234. );
  235. $this->dispatch('mastery-updated', data: $this->mindmapMasteryData);
  236. Log::info('仪表板数据加载完成', [
  237. 'student_id' => $this->studentId,
  238. 'dashboard_data_keys' => array_keys($this->dashboardData)
  239. ]);
  240. try {
  241. $mistakeService = app(MistakeBookService::class);
  242. $this->mistakePanel = $mistakeService->getPanelSnapshot($this->studentId, 5);
  243. } catch (\Exception $e) {
  244. Log::warning('加载错题本面板数据失败', [
  245. 'student_id' => $this->studentId,
  246. 'error' => $e->getMessage()
  247. ]);
  248. $this->mistakePanel = [];
  249. }
  250. } catch (\Exception $e) {
  251. $this->errorMessage = '加载数据时发生错误:' . $e->getMessage();
  252. Log::error('学生仪表板数据加载失败', [
  253. 'student_id' => $this->studentId,
  254. 'error' => $e->getMessage()
  255. ]);
  256. $this->mindmapMasteryData = [];
  257. $this->dispatch('mastery-updated', data: []);
  258. $this->mistakePanel = [];
  259. } finally {
  260. $this->isLoading = false;
  261. }
  262. }
  263. public function recalculateMastery(string $kpCode): void
  264. {
  265. try {
  266. // 使用本地MasteryCalculator替代LearningAnalyticsService
  267. $masteryCalculator = app(MasteryCalculator::class);
  268. $result = $masteryCalculator->calculateMasteryLevel($this->studentId, $kpCode);
  269. if (!empty($result)) {
  270. $this->dispatch('notify', message: '掌握度重新计算完成', type: 'success');
  271. $this->loadDashboardData(); // 刷新数据
  272. } else {
  273. $this->dispatch('notify', message: '掌握度重新计算失败', type: 'danger');
  274. }
  275. } catch (\Exception $e) {
  276. Log::error('重新计算掌握度失败', [
  277. 'student_id' => $this->studentId,
  278. 'kp_code' => $kpCode,
  279. 'error' => $e->getMessage()
  280. ]);
  281. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  282. }
  283. }
  284. public function batchUpdateSkills(): void
  285. {
  286. try {
  287. // 使用本地MasteryCalculator替代LearningAnalyticsService
  288. $masteryCalculator = app(MasteryCalculator::class);
  289. // TODO: 需要实现本地的batchUpdateSkillProficiency功能
  290. \Log::warning('跳过LearningAnalytics的batchUpdateSkillProficiency调用', [
  291. 'student_id' => $this->studentId,
  292. 'reason' => '功能已迁移到本地KnowledgeMasteryService,但batchUpdateSkillProficiency尚未实现'
  293. ]);
  294. $this->dispatch('notify', message: '技能熟练度更新完成', type: 'success');
  295. $this->loadDashboardData(); // 刷新数据
  296. } catch (\Exception $e) {
  297. Log::error('批量更新技能熟练度失败', [
  298. 'student_id' => $this->studentId,
  299. 'error' => $e->getMessage()
  300. ]);
  301. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  302. }
  303. }
  304. public function generateQuickPrediction(): void
  305. {
  306. try {
  307. // 使用本地MasteryCalculator替代LearningAnalyticsService
  308. $masteryCalculator = app(MasteryCalculator::class);
  309. // TODO: 需要实现本地的quickScorePrediction功能
  310. \Log::warning('跳过LearningAnalytics的quickScorePrediction调用', [
  311. 'student_id' => $this->studentId,
  312. 'reason' => '功能已迁移到本地KnowledgeMasteryService,但quickScorePrediction尚未实现'
  313. ]);
  314. $this->dispatch('notify', message: '快速预测生成完成', type: 'success');
  315. $this->loadDashboardData(); // 刷新数据
  316. } catch (\Exception $e) {
  317. Log::error('生成快速预测失败', [
  318. 'student_id' => $this->studentId,
  319. 'error' => $e->getMessage()
  320. ]);
  321. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  322. }
  323. }
  324. protected function buildMasteryMap(array $list): array
  325. {
  326. $map = [];
  327. $items = $list['data'] ?? $list['masteries'] ?? $list;
  328. if (!is_array($items)) {
  329. return $map;
  330. }
  331. foreach ($items as $item) {
  332. if (!is_array($item)) {
  333. continue;
  334. }
  335. $code = $item['kp_code'] ?? $item['code'] ?? null;
  336. if (!$code) {
  337. continue;
  338. }
  339. $map[$code] = $item;
  340. }
  341. return $map;
  342. }
  343. public function openMindmapDrawer(string $nodeId): void
  344. {
  345. $this->mindmapSelectedNode = $nodeId;
  346. $this->mindmapNodeDetails = $this->getNodeDetails($nodeId, $this->mindmapMasteryData);
  347. $this->mindmapDrawerOpen = true;
  348. }
  349. public function closeMindmapDrawer(): void
  350. {
  351. $this->mindmapDrawerOpen = false;
  352. $this->mindmapSelectedNode = null;
  353. $this->mindmapNodeDetails = [];
  354. }
  355. /**
  356. * 监听TeacherStudentSelector组件的老师变化事件
  357. */
  358. #[On('teacherChanged')]
  359. public function onTeacherChanged(string $teacherId): void
  360. {
  361. $this->teacherId = $teacherId;
  362. $this->loadStudentsByTeacher();
  363. $this->studentId = $this->getDefaultStudentId();
  364. }
  365. /**
  366. * 监听TeacherStudentSelector组件的学生变化事件
  367. */
  368. #[On('studentChanged')]
  369. public function onStudentChanged(string $teacherId, string $studentId): void
  370. {
  371. $this->teacherId = $teacherId;
  372. $this->studentId = $studentId;
  373. $this->loadDashboardData();
  374. }
  375. /**
  376. * 从错题记录生成掌握度数据
  377. */
  378. private function generateMasteryFromMistakes(string $studentId): array
  379. {
  380. try {
  381. // 获取学生的错题记录
  382. $mistakeRecords = \App\Models\MistakeRecord::forStudent($studentId)
  383. ->get(['kp_ids', 'knowledge_point', 'is_corrected', 'review_status']);
  384. // 统计每个知识点的错题数量
  385. $kpStats = [];
  386. foreach ($mistakeRecords as $record) {
  387. $kpIds = $record->kp_ids ?? [];
  388. if (is_string($kpIds)) {
  389. $kpIds = json_decode($kpIds, true) ?? [];
  390. }
  391. foreach ($kpIds as $kpCode) {
  392. if (empty($kpCode)) {
  393. continue;
  394. }
  395. if (!isset($kpStats[$kpCode])) {
  396. $kpStats[$kpCode] = [
  397. 'kp_code' => $kpCode,
  398. 'kp_name' => $record->knowledge_point ?? $kpCode,
  399. 'mistake_count' => 0,
  400. 'corrected_count' => 0,
  401. 'mastery_level' => 0.0,
  402. ];
  403. }
  404. $kpStats[$kpCode]['mistake_count']++;
  405. if ($record->is_corrected) {
  406. $kpStats[$kpCode]['corrected_count']++;
  407. }
  408. }
  409. }
  410. // 计算掌握度(基于错题数量和纠正情况)
  411. $masteryData = [];
  412. foreach ($kpStats as $kpCode => $stats) {
  413. $total = $stats['mistake_count'];
  414. $corrected = $stats['corrected_count'];
  415. // 掌握度计算:已纠正的题目比例 + 基础分数
  416. // 如果全部纠正,掌握度较高;如果有未纠正的,掌握度较低
  417. $masteryLevel = $total > 0
  418. ? ($corrected / $total) * 0.7 + 0.1 // 基础分数0.1,最高0.8
  419. : 0.5; // 默认中等掌握度
  420. // 确保掌握度在合理范围内
  421. $masteryLevel = max(0.1, min(0.9, $masteryLevel));
  422. $masteryData[] = [
  423. 'kp_code' => $kpCode,
  424. 'kp_name' => $stats['kp_name'],
  425. 'mastery_level' => round($masteryLevel, 2),
  426. 'total_attempts' => $total,
  427. 'correct_attempts' => $corrected,
  428. 'accuracy_rate' => $total > 0 ? round($corrected / $total, 2) : 0,
  429. 'trend' => $corrected >= ($total * 0.5) ? 'improving' : 'needs_attention',
  430. 'last_attempt' => now()->toISOString(),
  431. ];
  432. }
  433. Log::info('从错题记录生成掌握度数据', [
  434. 'student_id' => $studentId,
  435. 'kp_count' => count($masteryData),
  436. 'mastery_data' => $masteryData
  437. ]);
  438. // 为生成的掌握度数据创建快照记录
  439. $this->createMasterySnapshots($studentId, $masteryData);
  440. return [
  441. 'student_id' => $studentId,
  442. 'total_count' => count($masteryData),
  443. 'data' => $masteryData,
  444. ];
  445. } catch (\Exception $e) {
  446. Log::error('从错题记录生成掌握度失败', [
  447. 'student_id' => $studentId,
  448. 'error' => $e->getMessage()
  449. ]);
  450. return [
  451. 'student_id' => $studentId,
  452. 'total_count' => 0,
  453. 'data' => [],
  454. ];
  455. }
  456. }
  457. /**
  458. * 创建掌握度快照记录
  459. */
  460. private function createMasterySnapshots(string $studentId, array $masteryData): void
  461. {
  462. try {
  463. // 计算整体掌握度
  464. $totalPoints = count($masteryData);
  465. $averageMastery = $totalPoints > 0
  466. ? array_sum(array_column($masteryData, 'mastery_level')) / $totalPoints
  467. : 0;
  468. // 统计强弱知识点数量
  469. $weakCount = 0;
  470. $strongCount = 0;
  471. foreach ($masteryData as $data) {
  472. $level = floatval($data['mastery_level']);
  473. if ($level >= 0.7) {
  474. $strongCount++;
  475. } elseif ($level < 0.5) {
  476. $weakCount++;
  477. }
  478. }
  479. // 生成快照ID
  480. $snapshotId = 'auto_' . $studentId . '_' . time();
  481. // 创建快照记录
  482. DB::table('knowledge_point_mastery_snapshots')->insert([
  483. 'snapshot_id' => $snapshotId,
  484. 'student_id' => $studentId,
  485. 'paper_id' => null, // 自动生成,没有关联试卷
  486. 'answer_record_id' => null,
  487. 'mastery_data' => json_encode($masteryData),
  488. 'overall_mastery' => round($averageMastery, 4),
  489. 'weak_knowledge_points_count' => $weakCount,
  490. 'strong_knowledge_points_count' => $strongCount,
  491. 'snapshot_time' => now(),
  492. 'analysis_id' => null,
  493. 'created_at' => now(),
  494. 'updated_at' => now(),
  495. ]);
  496. Log::info('创建掌握度快照', [
  497. 'student_id' => $studentId,
  498. 'snapshot_id' => $snapshotId,
  499. 'total_knowledge_points' => $totalPoints,
  500. 'average_mastery' => $averageMastery,
  501. 'weak_count' => $weakCount,
  502. 'strong_count' => $strongCount,
  503. ]);
  504. } catch (\Exception $e) {
  505. Log::error('创建掌握度快照失败', [
  506. 'student_id' => $studentId,
  507. 'error' => $e->getMessage()
  508. ]);
  509. }
  510. }
  511. }