KnowledgeMasteryController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\KnowledgeMasteryService;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 知识点掌握情况 API 控制器
  9. *
  10. * 提供:
  11. * 1. 获取学生知识点掌握情况统计
  12. * 2. 获取知识点图谱数据(考试快照)
  13. * 3. 获取知识点图谱快照列表
  14. */
  15. class KnowledgeMasteryController extends Controller
  16. {
  17. protected KnowledgeMasteryService $service;
  18. public function __construct(KnowledgeMasteryService $service)
  19. {
  20. $this->service = $service;
  21. }
  22. /**
  23. * 获取学生知识点掌握情况统计
  24. *
  25. * GET /api/knowledge-mastery/stats/{studentId}
  26. *
  27. * @param string $studentId 学生ID
  28. * @return JsonResponse
  29. */
  30. public function stats(string $studentId): JsonResponse
  31. {
  32. $result = $this->service->getStats($studentId);
  33. if (!$result['success']) {
  34. return response()->json([
  35. 'success' => false,
  36. 'message' => $result['error'] ?? '获取知识点掌握情况失败',
  37. ], 500);
  38. }
  39. return response()->json([
  40. 'success' => true,
  41. 'data' => $result['data'],
  42. ]);
  43. }
  44. /**
  45. * 获取学生知识点掌握摘要(简化版)
  46. *
  47. * GET /api/knowledge-mastery/summary/{studentId}
  48. *
  49. * @param string $studentId 学生ID
  50. * @return JsonResponse
  51. */
  52. public function summary(string $studentId): JsonResponse
  53. {
  54. $result = $this->service->getSummary($studentId);
  55. if (!$result['success']) {
  56. return response()->json([
  57. 'success' => false,
  58. 'message' => $result['error'] ?? '获取知识点掌握摘要失败',
  59. ], 500);
  60. }
  61. return response()->json([
  62. 'success' => true,
  63. 'data' => $result['data'],
  64. ]);
  65. }
  66. /**
  67. * 获取学生知识点图谱数据
  68. *
  69. * GET /api/knowledge-mastery/graph/{studentId}
  70. *
  71. * @param Request $request
  72. * @param string $studentId 学生ID
  73. * @return JsonResponse
  74. */
  75. public function graph(Request $request, string $studentId): JsonResponse
  76. {
  77. $examId = $request->query('exam_id');
  78. $result = $this->service->getGraph($studentId, $examId);
  79. if (!$result['success']) {
  80. return response()->json([
  81. 'success' => false,
  82. 'message' => $result['error'] ?? '获取知识点图谱失败',
  83. ], 500);
  84. }
  85. return response()->json([
  86. 'success' => true,
  87. 'data' => $result['data'],
  88. ]);
  89. }
  90. /**
  91. * 获取学生知识点图谱快照列表
  92. *
  93. * GET /api/knowledge-mastery/graph/snapshots/{studentId}
  94. *
  95. * @param Request $request
  96. * @param string $studentId 学生ID
  97. * @return JsonResponse
  98. */
  99. public function graphSnapshots(Request $request, string $studentId): JsonResponse
  100. {
  101. $limit = (int) $request->query('limit', 10);
  102. $result = $this->service->getGraphSnapshots($studentId, $limit);
  103. if (!$result['success']) {
  104. return response()->json([
  105. 'success' => false,
  106. 'message' => $result['error'] ?? '获取知识点图谱快照列表失败',
  107. ], 500);
  108. }
  109. return response()->json([
  110. 'success' => true,
  111. 'data' => $result['data'],
  112. ]);
  113. }
  114. /**
  115. * 获取学生知识点快照列表(简化路径)
  116. *
  117. * GET /api/knowledge-mastery/snapshots/{studentId}
  118. *
  119. * @param Request $request
  120. * @param string $studentId 学生ID
  121. * @return JsonResponse
  122. */
  123. public function snapshots(Request $request, string $studentId): JsonResponse
  124. {
  125. $limit = (int) $request->query('limit', 10);
  126. $result = $this->service->getGraphSnapshots($studentId, $limit);
  127. if (!$result['success']) {
  128. return response()->json([
  129. 'success' => false,
  130. 'message' => $result['error'] ?? '获取知识点快照列表失败',
  131. ], 500);
  132. }
  133. return response()->json([
  134. 'success' => true,
  135. 'data' => $result['data'],
  136. ]);
  137. }
  138. /**
  139. * 创建知识点掌握度快照
  140. *
  141. * POST /api/knowledge-mastery/snapshot/{studentId}
  142. *
  143. * @param Request $request
  144. * @param string $studentId 学生ID
  145. * @return JsonResponse
  146. */
  147. public function createSnapshot(Request $request, string $studentId): JsonResponse
  148. {
  149. $snapshotType = $request->input('snapshot_type', 'report');
  150. $sourceId = $request->input('source_id');
  151. $sourceName = $request->input('source_name');
  152. $notes = $request->input('notes');
  153. $result = $this->service->createSnapshot(
  154. $studentId,
  155. $snapshotType,
  156. $sourceId,
  157. $sourceName,
  158. $notes
  159. );
  160. if (!$result['success']) {
  161. return response()->json([
  162. 'success' => false,
  163. 'message' => $result['error'] ?? '创建知识点掌握度快照失败',
  164. ], 500);
  165. }
  166. return response()->json([
  167. 'success' => true,
  168. 'message' => '快照创建成功',
  169. 'data' => $result['data'],
  170. ]);
  171. }
  172. }