MasteryCalculator.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Collection;
  6. /**
  7. * 知识掌握度计算引擎(PHP版本)
  8. * 基于学案基准难度的动态加减逻辑计算掌握度
  9. *
  10. * 新算法特点:
  11. * 1. 根据学案基准难度(筑基、提分、培优、竞赛)动态调整权重
  12. * 2. 难度映射:0.0-1.0 → 1-4级
  13. * 3. 权重计算:越级、适应、降级三种情况
  14. * 4. 父节点掌握度:子节点平均值
  15. */
  16. class MasteryCalculator
  17. {
  18. /**
  19. * 掌握度阈值配置
  20. */
  21. private const MASTERY_THRESHOLD_WEAK = 0.50; // 薄弱点阈值
  22. private const MASTERY_THRESHOLD_GOOD = 0.70; // 良好阈值
  23. private const MASTERY_THRESHOLD_MASTER = 0.85; // 掌握阈值
  24. /**
  25. * 最小正确率要求
  26. */
  27. private const MIN_CORRECT_RATE = 0.60;
  28. /**
  29. * 计算学生对指定知识点的掌握度
  30. *
  31. * @param string $studentId 学生ID
  32. * @param string $kpCode 知识点编码
  33. * @param array|null $attempts 答题记录(可选,默认从数据库查询)
  34. * @param int|null $examBaseDifficulty 学案基准难度(1-4级,1=筑基, 2=提分, 3=培优, 4=竞赛)
  35. * @return array 返回['mastery' => 掌握度, 'confidence' => 置信度, 'trend' => 趋势]
  36. */
  37. public function calculateMasteryLevel(string $studentId, string $kpCode, ?array $attempts = null, ?int $examBaseDifficulty = null): array
  38. {
  39. // 优先使用传递的答题记录,如果没有则从数据库查询
  40. if ($attempts === null || empty($attempts)) {
  41. $attempts = $this->getStudentAttempts($studentId, $kpCode);
  42. }
  43. if (empty($attempts)) {
  44. Log::warning('没有答题记录,无法计算掌握度', [
  45. 'student_id' => $studentId,
  46. 'kp_code' => $kpCode,
  47. 'attempts_provided' => $attempts !== null,
  48. ]);
  49. // 【公司要求】返回值:只返回核心掌握度数据
  50. return [
  51. 'mastery' => 0.0,
  52. 'total_attempts' => 0,
  53. 'correct_attempts' => 0,
  54. 'accuracy_rate' => 0.0,
  55. ];
  56. }
  57. // 如果没有学案基准难度,使用默认值2(提分)
  58. if ($examBaseDifficulty === null) {
  59. Log::warning('缺少学案基准难度,使用默认值2(提分)', [
  60. 'student_id' => $studentId,
  61. 'kp_code' => $kpCode,
  62. 'attempts_count' => count($attempts),
  63. ]);
  64. $examBaseDifficulty = 2; // 默认提分难度
  65. }
  66. $masteryData = $this->calculateMasteryWithExamDifficulty($studentId, $kpCode, $attempts, $examBaseDifficulty);
  67. // 【公司要求】日志输出:只记录核心掌握度数据
  68. Log::info('掌握度计算完成', [
  69. 'student_id' => $studentId,
  70. 'kp_code' => $kpCode,
  71. 'exam_base_difficulty' => $examBaseDifficulty,
  72. 'difficulty_name' => $this->getDifficultyName($examBaseDifficulty),
  73. 'total_attempts' => count($attempts),
  74. 'correct_attempts' => $masteryData['correct_attempts'],
  75. 'final_mastery' => $masteryData['mastery'],
  76. ]);
  77. return $masteryData;
  78. }
  79. /**
  80. * 获取难度等级名称
  81. */
  82. private function getDifficultyName(int $difficultyLevel): string
  83. {
  84. return match ($difficultyLevel) {
  85. 1 => '筑基',
  86. 2 => '提分',
  87. 3 => '培优',
  88. 4 => '竞赛',
  89. default => '未知',
  90. };
  91. }
  92. /**
  93. * 【新算法】使用学案基准难度的动态加减逻辑计算掌握度
  94. */
  95. private function calculateMasteryWithExamDifficulty(string $studentId, string $kpCode, array $attempts, int $examBaseDifficulty): array
  96. {
  97. // 获取历史掌握度
  98. $historyMastery = DB::table('student_knowledge_mastery')
  99. ->where('student_id', $studentId)
  100. ->where('kp_code', $kpCode)
  101. ->first();
  102. $oldMastery = $historyMastery->mastery_level ?? 0.0; // 默认 0.0
  103. // 统计正确和错误次数
  104. $totalAttempts = count($attempts);
  105. $correctAttempts = 0;
  106. $incorrectAttempts = 0;
  107. // 计算每次答题的权重变化
  108. $totalChange = 0.0;
  109. foreach ($attempts as $attempt) {
  110. $isCorrect = boolval($attempt['is_correct'] ?? false);
  111. $questionDifficulty = floatval($attempt['question_difficulty'] ?? 0.6);
  112. // 难度映射:将0.0-1.0的浮点数难度映射为 1-4 等级
  113. $questionLevel = $this->mapDifficultyToLevel($questionDifficulty);
  114. // 根据难度关系计算权重变化
  115. $change = $this->calculateWeightByDifficultyRelation($questionLevel, $examBaseDifficulty, $isCorrect);
  116. $totalChange += $change;
  117. if ($isCorrect) {
  118. $correctAttempts++;
  119. } else {
  120. $incorrectAttempts++;
  121. }
  122. Log::debug('掌握度变化计算', [
  123. 'question_id' => $attempt['question_id'] ?? '',
  124. 'question_difficulty' => $questionDifficulty,
  125. 'question_level' => $questionLevel,
  126. 'exam_base_difficulty' => $examBaseDifficulty,
  127. 'is_correct' => $isCorrect,
  128. 'change' => $change,
  129. 'running_total' => $totalChange
  130. ]);
  131. }
  132. // 【公司要求】数值更新:newMastery = oldMastery + change
  133. $newMastery = $oldMastery + $totalChange;
  134. // 【公司要求】边界限制:0.0 ~ 1.0
  135. $newMastery = max(0.0, min(1.0, $newMastery));
  136. // 【公司要求】保存到数据库(只保存核心掌握度数据)
  137. DB::table('student_knowledge_mastery')
  138. ->updateOrInsert(
  139. ['student_id' => $studentId, 'kp_code' => $kpCode],
  140. [
  141. 'mastery_level' => $newMastery,
  142. 'confidence_level' => 0.0, // 不再计算置信度
  143. 'total_attempts' => ($historyMastery->total_attempts ?? 0) + $totalAttempts,
  144. 'correct_attempts' => ($historyMastery->correct_attempts ?? 0) + $correctAttempts,
  145. 'mastery_trend' => 'stable', // 不再判断趋势,统一设为stable
  146. 'last_mastery_update' => now(),
  147. 'updated_at' => now(),
  148. ]
  149. );
  150. // 【公司要求】返回值:只返回核心掌握度数据
  151. return [
  152. 'mastery' => round($newMastery, 4),
  153. 'total_attempts' => $totalAttempts,
  154. 'correct_attempts' => $correctAttempts,
  155. 'accuracy_rate' => round(($correctAttempts / $totalAttempts) * 100, 2),
  156. 'old_mastery' => $oldMastery,
  157. 'change' => round($totalChange, 4),
  158. 'details' => [
  159. 'exam_base_difficulty' => $examBaseDifficulty,
  160. 'total_change' => round($totalChange, 4)
  161. ],
  162. ];
  163. }
  164. /**
  165. * 【公司要求】难度映射:将题目中0.0-1.0的浮点数难度映射为1-4等级
  166. *
  167. * 公司要求:
  168. * 0.0 ~ 0.25 -> 1级(筑基)
  169. * 0.25 ~ 0.5 -> 2级(提分)
  170. * 0.5 ~ 0.75 -> 3级(培优)
  171. * 0.75 ~ 1.0 -> 4级(竞赛)
  172. *
  173. * @param float $difficulty 题目难度(0.0-1.0浮点数)
  174. * @return int 难度等级(1-4级)
  175. */
  176. private function mapDifficultyToLevel(float $difficulty): int
  177. {
  178. if ($difficulty >= 0.0 && $difficulty < 0.25) {
  179. return 1; // 1级(筑基)
  180. } elseif ($difficulty >= 0.25 && $difficulty < 0.5) {
  181. return 2; // 2级(提分)
  182. } elseif ($difficulty >= 0.5 && $difficulty < 0.75) {
  183. return 3; // 3级(培优)
  184. } else {
  185. return 4; // 4级(竞赛)
  186. }
  187. }
  188. /**
  189. * 【公司要求】根据难度关系计算权重变化
  190. *
  191. * 公司要求的三种难度关系及权重:
  192. * 1. 越级(Question > {学案基准难度}):对 +0.15 / 错 -0.05
  193. * 2. 适应(Question = {学案基准难度}):对 +0.10 / 错 -0.10
  194. * 3. 降级(Question < {学案基准难度}):对 +0.05 / 错 -0.15
  195. *
  196. * 学案基准难度获取:
  197. * - 来源:试卷表(papers.difficulty_category)
  198. * - 映射:筑基→1级,提分→2级,培优→3级,竞赛→4级
  199. *
  200. * @param int $questionLevel 题目难度等级(1-4)
  201. * @param int $examBaseDifficulty 学案基准难度(1-4)
  202. * @param bool $isCorrect 答题是否正确
  203. * @return float 权重变化值
  204. */
  205. private function calculateWeightByDifficultyRelation(int $questionLevel, int $examBaseDifficulty, bool $isCorrect): float
  206. {
  207. if ($questionLevel > $examBaseDifficulty) {
  208. // 越级:题目难度 > 学案基准难度
  209. return $isCorrect ? 0.15 : -0.05;
  210. } elseif ($questionLevel == $examBaseDifficulty) {
  211. // 适应:题目难度 = 学案基准难度
  212. return $isCorrect ? 0.10 : -0.10;
  213. } else {
  214. // 降级:题目难度 < 学案基准难度
  215. return $isCorrect ? 0.05 : -0.15;
  216. }
  217. }
  218. /**
  219. * 获取学生的答题记录
  220. */
  221. private function getStudentAttempts(string $studentId, string $kpCode): array
  222. {
  223. $attempts = [];
  224. // 优先从 student_answer_steps 表获取(步骤级记录)
  225. $stepAttempts = DB::table('student_answer_steps')
  226. ->where('student_id', $studentId)
  227. ->where('kp_id', $kpCode)
  228. ->orderBy('created_at', 'asc')
  229. ->get();
  230. foreach ($stepAttempts as $step) {
  231. // 【关键修复】从题目表查询题目难度
  232. $questionDifficulty = 0.6; // 默认难度
  233. if ($step->question_id) {
  234. $question = DB::table('questions')
  235. ->where('id', $step->question_id)
  236. ->orWhere('question_code', $step->question_id)
  237. ->first();
  238. if ($question && $question->difficulty !== null) {
  239. $questionDifficulty = floatval($question->difficulty);
  240. }
  241. }
  242. $attempts[] = [
  243. 'student_id' => $step->student_id,
  244. 'paper_id' => $step->exam_id,
  245. 'question_id' => $step->question_id,
  246. 'kp_code' => $step->kp_id,
  247. 'is_correct' => (bool) $step->is_correct,
  248. 'score_obtained' => $step->step_score,
  249. 'max_score' => $step->step_score, // 步骤分数本身就是满分
  250. 'question_difficulty' => $questionDifficulty, // 【新增】题目难度
  251. 'created_at' => $step->created_at,
  252. ];
  253. }
  254. // 如果没有步骤级记录,从 student_answer_questions 表获取(题目级记录)
  255. if (empty($attempts)) {
  256. $questionAttempts = DB::table('student_answer_questions')
  257. ->where('student_id', $studentId)
  258. ->orderBy('created_at', 'asc')
  259. ->get();
  260. foreach ($questionAttempts as $question) {
  261. // 【关键修复】从题目表查询题目难度
  262. $questionDifficulty = 0.6; // 默认难度
  263. if ($question->question_id) {
  264. $questionInfo = DB::table('questions')
  265. ->where('id', $question->question_id)
  266. ->orWhere('question_code', $question->question_id)
  267. ->first();
  268. if ($questionInfo && $questionInfo->difficulty !== null) {
  269. $questionDifficulty = floatval($questionInfo->difficulty);
  270. }
  271. }
  272. $attempts[] = [
  273. 'student_id' => $question->student_id,
  274. 'paper_id' => $question->exam_id,
  275. 'question_id' => $question->question_id,
  276. 'kp_code' => $kpCode, // 使用传入的kpCode
  277. 'is_correct' => ($question->score_obtained ?? 0) > 0,
  278. 'score_obtained' => $question->score_obtained ?? 0,
  279. 'max_score' => $question->max_score ?? 0,
  280. 'question_difficulty' => $questionDifficulty, // 【新增】题目难度
  281. 'created_at' => $question->created_at,
  282. ];
  283. }
  284. }
  285. return $attempts;
  286. }
  287. /**
  288. * 批量更新学生掌握度
  289. */
  290. public function batchUpdateMastery(string $studentId, array $kpCodes): array
  291. {
  292. $results = [];
  293. foreach ($kpCodes as $kpCode) {
  294. $masteryData = $this->calculateMasteryLevel($studentId, $kpCode);
  295. // 保存到数据库
  296. DB::table('student_knowledge_mastery')
  297. ->updateOrInsert(
  298. ['student_id' => $studentId, 'kp_code' => $kpCode],
  299. [
  300. 'mastery_level' => $masteryData['mastery'],
  301. 'confidence_level' => $masteryData['confidence'],
  302. 'total_attempts' => $masteryData['total_attempts'],
  303. 'correct_attempts' => $masteryData['correct_attempts'],
  304. 'mastery_trend' => $masteryData['trend'],
  305. 'last_mastery_update' => now(),
  306. 'updated_at' => now(),
  307. ]
  308. );
  309. $results[$kpCode] = $masteryData;
  310. }
  311. return $results;
  312. }
  313. /**
  314. * 获取学生所有知识点的掌握度概览
  315. */
  316. public function getStudentMasteryOverview(string $studentId): array
  317. {
  318. $masteryList = DB::table('student_knowledge_mastery')
  319. ->where('student_id', $studentId)
  320. ->get();
  321. if ($masteryList->isEmpty()) {
  322. return [
  323. 'total_knowledge_points' => 0,
  324. 'average_mastery_level' => 0.0,
  325. 'mastered_knowledge_points' => 0,
  326. 'good_knowledge_points' => 0,
  327. 'weak_knowledge_points' => 0,
  328. 'weak_knowledge_points_list' => [],
  329. 'details' => [],
  330. ];
  331. }
  332. $masteryArray = $masteryList->toArray();
  333. $total = count($masteryArray);
  334. $average = $masteryArray ? array_sum(array_column($masteryArray, 'mastery_level')) / $total : 0;
  335. $mastered = [];
  336. $good = [];
  337. $weak = [];
  338. foreach ($masteryArray as $item) {
  339. $level = floatval($item->mastery_level);
  340. if ($level >= self::MASTERY_THRESHOLD_MASTER) {
  341. $mastered[] = $item;
  342. } elseif ($level >= self::MASTERY_THRESHOLD_GOOD) {
  343. $good[] = $item;
  344. } else {
  345. $weak[] = $item;
  346. }
  347. }
  348. return [
  349. 'total_knowledge_points' => $total,
  350. 'average_mastery_level' => round($average, 4),
  351. 'mastered_knowledge_points' => count($mastered),
  352. 'good_knowledge_points' => count($good),
  353. 'weak_knowledge_points' => count($weak),
  354. 'weak_knowledge_points_list' => $weak,
  355. 'details' => $masteryArray,
  356. ];
  357. }
  358. /**
  359. * 【新功能】获取知识点层级关系
  360. */
  361. private function getKnowledgePointHierarchy(string $kpCode): ?array
  362. {
  363. try {
  364. $kp = DB::table('knowledge_points')
  365. ->where('kp_code', $kpCode)
  366. ->first();
  367. if (!$kp) {
  368. return null;
  369. }
  370. return [
  371. 'kp_code' => $kp->kp_code,
  372. 'parent_kp_code' => $kp->parent_kp_code,
  373. 'level' => $kp->level ?? 1,
  374. ];
  375. } catch (\Exception $e) {
  376. Log::warning('获取知识点层级关系失败', [
  377. 'kp_code' => $kpCode,
  378. 'error' => $e->getMessage(),
  379. ]);
  380. return null;
  381. }
  382. }
  383. /**
  384. * 【公司要求】计算父节点掌握度(所有子节点算术平均值)
  385. *
  386. * 公司要求:
  387. * 1. 查询结构:通过 knowledge_points 表获取知识点的层级关系(parent_kp_code)
  388. * 2. 平均值计算:如果一个知识点是父节点,它的掌握度不再从数据库直接读,
  389. * 而是实时计算其下所有子节点掌握度的算术平均数
  390. * 3. 多级递归:支持多级结构,能够从最底层逐级向上求平均
  391. *
  392. * 递归计算流程:
  393. * - 从最底层叶子节点开始,逐级向上计算每个父节点的掌握度
  394. * - 父节点掌握度 = 所有直接子节点掌握度的算术平均数
  395. *
  396. * @param string $studentId 学生ID
  397. * @param string $parentKpCode 父节点编码
  398. * @param int $maxDepth 最大递归深度(默认10级)
  399. * @return float 父节点掌握度(0.0-1.0)
  400. */
  401. public function calculateParentMastery(string $studentId, string $parentKpCode, int $maxDepth = 3): float
  402. {
  403. return $this->calculateParentMasteryRecursive($studentId, $parentKpCode, 1, $maxDepth);
  404. }
  405. /**
  406. * 【公司要求】递归计算父节点掌握度(多级结构支持)
  407. *
  408. * 多级递归计算流程:
  409. * 1. 获取父节点的所有直接子节点
  410. * 2. 对每个子节点:
  411. * - 如果子节点也是父节点,递归计算其掌握度
  412. * - 如果子节点是叶子节点,从 student_knowledge_mastery 表读取掌握度
  413. * 3. 计算所有子节点掌握度的算术平均数
  414. * 4. 返回父节点掌握度
  415. *
  416. * @param string $studentId 学生ID
  417. * @param string $parentKpCode 父节点编码
  418. * @param int $currentDepth 当前递归深度
  419. * @param int $maxDepth 最大递归深度(防止无限递归)
  420. * @return float 父节点掌握度
  421. */
  422. private function calculateParentMasteryRecursive(string $studentId, string $parentKpCode, int $currentDepth, int $maxDepth): float
  423. {
  424. try {
  425. // 【公司要求】防止无限递归
  426. if ($currentDepth > $maxDepth) {
  427. Log::warning('父节点掌握度计算达到最大递归深度', [
  428. 'student_id' => $studentId,
  429. 'parent_kp_code' => $parentKpCode,
  430. 'current_depth' => $currentDepth,
  431. 'max_depth' => $maxDepth
  432. ]);
  433. return 0.0;
  434. }
  435. // 【公司要求】查询结构:通过 knowledge_points 表获取知识点的层级关系(parent_kp_code)
  436. // 获取所有直接子节点
  437. $childKps = DB::table('knowledge_points')
  438. ->where('parent_kp_code', $parentKpCode)
  439. ->pluck('kp_code')
  440. ->toArray();
  441. if (empty($childKps)) {
  442. // 如果没有子节点,返回0
  443. Log::debug('父节点没有子节点,返回0', [
  444. 'student_id' => $studentId,
  445. 'parent_kp_code' => $parentKpCode
  446. ]);
  447. return 0.0;
  448. }
  449. // 【公司要求】多级递归:支持多级结构,能够从最底层逐级向上求平均
  450. // 获取所有子节点的掌握度(递归计算)
  451. $masteryLevels = [];
  452. foreach ($childKps as $childKpCode) {
  453. // 【公司要求】多级递归:递归计算子节点掌握度
  454. $childMastery = $this->calculateParentMasteryRecursive($studentId, $childKpCode, $currentDepth + 1, $maxDepth);
  455. // 如果子节点没有子节点,则从student_knowledge_mastery表读取
  456. if ($childMastery == 0.0) {
  457. $mastery = DB::table('student_knowledge_mastery')
  458. ->where('student_id', $studentId)
  459. ->where('kp_code', $childKpCode)
  460. ->value('mastery_level');
  461. if ($mastery !== null) {
  462. $masteryLevels[] = floatval($mastery);
  463. }
  464. } else {
  465. // 子节点有子节点,使用递归计算的结果
  466. $masteryLevels[] = $childMastery;
  467. }
  468. }
  469. if (empty($masteryLevels)) {
  470. Log::warning('父节点所有子节点都没有掌握度数据', [
  471. 'student_id' => $studentId,
  472. 'parent_kp_code' => $parentKpCode,
  473. 'child_count' => count($childKps)
  474. ]);
  475. return 0.0;
  476. }
  477. // 【公司要求】平均值计算:父节点掌握度 = 所有子节点掌握度的算术平均数
  478. $averageMastery = array_sum($masteryLevels) / count($masteryLevels);
  479. $result = round($averageMastery, 4);
  480. Log::info('父节点掌握度计算完成', [
  481. 'student_id' => $studentId,
  482. 'parent_kp_code' => $parentKpCode,
  483. 'child_count' => count($childKps),
  484. 'mastery_count' => count($masteryLevels),
  485. 'average_mastery' => $result,
  486. 'child_masteries' => $masteryLevels,
  487. 'current_depth' => $currentDepth
  488. ]);
  489. return $result;
  490. } catch (\Exception $e) {
  491. Log::error('计算父节点掌握度失败', [
  492. 'student_id' => $studentId,
  493. 'parent_kp_code' => $parentKpCode,
  494. 'current_depth' => $currentDepth,
  495. 'error' => $e->getMessage(),
  496. ]);
  497. return 0.0;
  498. }
  499. }
  500. /**
  501. * 【增强】获取学生所有知识点的掌握度概览(支持父节点计算)
  502. */
  503. public function getStudentMasteryOverviewWithHierarchy(string $studentId): array
  504. {
  505. $masteryList = DB::table('student_knowledge_mastery')
  506. ->where('student_id', $studentId)
  507. ->get();
  508. if ($masteryList->isEmpty()) {
  509. return [
  510. 'total_knowledge_points' => 0,
  511. 'average_mastery_level' => 0.0,
  512. 'mastered_knowledge_points' => 0,
  513. 'good_knowledge_points' => 0,
  514. 'weak_knowledge_points' => 0,
  515. 'weak_knowledge_points_list' => [],
  516. 'details' => [],
  517. 'parent_mastery_levels' => [], // 新增:父节点掌握度
  518. ];
  519. }
  520. $masteryArray = $masteryList->toArray();
  521. $total = count($masteryArray);
  522. $average = $masteryArray ? array_sum(array_column($masteryArray, 'mastery_level')) / $total : 0;
  523. $mastered = [];
  524. $good = [];
  525. $weak = [];
  526. foreach ($masteryArray as $item) {
  527. $level = floatval($item->mastery_level);
  528. if ($level >= self::MASTERY_THRESHOLD_MASTER) {
  529. $mastered[] = $item;
  530. } elseif ($level >= self::MASTERY_THRESHOLD_GOOD) {
  531. $good[] = $item;
  532. } else {
  533. $weak[] = $item;
  534. }
  535. }
  536. // 【新功能】计算父节点掌握度
  537. $parentMasteryLevels = [];
  538. $parentKpCodes = DB::table('knowledge_points')
  539. ->whereNotNull('parent_kp_code')
  540. ->distinct()
  541. ->pluck('parent_kp_code')
  542. ->toArray();
  543. foreach ($parentKpCodes as $parentKpCode) {
  544. $parentMastery = $this->calculateParentMastery($studentId, $parentKpCode);
  545. $parentMasteryLevels[$parentKpCode] = $parentMastery;
  546. }
  547. return [
  548. 'total_knowledge_points' => $total,
  549. 'average_mastery_level' => round($average, 4),
  550. 'mastered_knowledge_points' => count($mastered),
  551. 'good_knowledge_points' => count($good),
  552. 'weak_knowledge_points' => count($weak),
  553. 'weak_knowledge_points_list' => $weak,
  554. 'details' => $masteryArray,
  555. 'parent_mastery_levels' => $parentMasteryLevels, // 新增:父节点掌握度
  556. ];
  557. }
  558. }