KnowledgeMindmapController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Student;
  5. use App\Models\Teacher;
  6. use Illuminate\Http\Request;
  7. class KnowledgeMindmapController extends Controller
  8. {
  9. public function getTeachers()
  10. {
  11. // For demonstration, return all teachers or a subset
  12. // In a real app, this might be filtered by the logged-in user's permissions
  13. $teachers = Teacher::query()
  14. ->select('id as teacher_id', 'name')
  15. ->limit(10)
  16. ->get();
  17. // If no teachers found, return some mock data for testing
  18. if ($teachers->isEmpty()) {
  19. return response()->json([
  20. ['teacher_id' => 1, 'name' => '张老师'],
  21. ['teacher_id' => 2, 'name' => '李老师'],
  22. ['teacher_id' => 3, 'name' => '王老师'],
  23. ]);
  24. }
  25. return response()->json($teachers);
  26. }
  27. public function getStudents($teacherId)
  28. {
  29. // Return students associated with the teacher
  30. // Mocking logic if no relationship exists in DB for this demo
  31. $students = Student::query()
  32. ->select('id as student_id', 'name')
  33. ->limit(20)
  34. ->get();
  35. if ($students->isEmpty()) {
  36. return response()->json([
  37. ['student_id' => 101, 'name' => '学生A'],
  38. ['student_id' => 102, 'name' => '学生B'],
  39. ['student_id' => 103, 'name' => '学生C'],
  40. ]);
  41. }
  42. return response()->json($students);
  43. }
  44. public function getMastery($studentId)
  45. {
  46. try {
  47. $service = app(\App\Services\LearningAnalyticsService::class);
  48. $response = $service->getStudentMasteryList($studentId);
  49. if (isset($response['error'])) {
  50. return response()->json([]);
  51. }
  52. // The service returns data in a 'data' key or directly as an array depending on the endpoint
  53. // Based on getStudentMastery implementation, it returns the full JSON response from the API
  54. // which likely contains a 'data' key.
  55. $data = $response['data'] ?? [];
  56. // Transform if necessary, but the view expects { kp_code: '...', mastery_level: ... }
  57. // The API response format should be verified, but assuming it matches what we need
  58. // or we map it here.
  59. // Let's ensure we return a list of { kp_code, mastery_level }
  60. return response()->json($data);
  61. } catch (\Exception $e) {
  62. \Illuminate\Support\Facades\Log::error('Failed to fetch mastery data for mindmap', [
  63. 'student_id' => $studentId,
  64. 'error' => $e->getMessage()
  65. ]);
  66. return response()->json([]);
  67. }
  68. }
  69. }