LearningAnalyticsService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 学习分析系统API服务
  7. * 负责与LearningAnalytics系统交互
  8. */
  9. class LearningAnalyticsService
  10. {
  11. /**
  12. * API基础URL
  13. */
  14. private string $baseUrl;
  15. /**
  16. * 超时时间(秒)
  17. */
  18. private int $timeout;
  19. public function __construct()
  20. {
  21. $this->baseUrl = config('services.learning_analytics.url', 'http://localhost:5016');
  22. $this->timeout = config('services.learning_analytics.timeout', 30);
  23. }
  24. /**
  25. * 获取学生掌握度概览
  26. */
  27. public function getStudentMasteryOverview(string $studentId): ?array
  28. {
  29. try {
  30. $response = Http::timeout($this->timeout)
  31. ->get("{$this->baseUrl}/api/v1/mastery/student/{$studentId}/overview");
  32. if ($response->successful()) {
  33. return $response->json();
  34. }
  35. Log::warning("获取学生掌握度概览失败", [
  36. 'student_id' => $studentId,
  37. 'status' => $response->status(),
  38. 'response' => $response->body()
  39. ]);
  40. return null;
  41. } catch (\Exception $e) {
  42. Log::error("获取学生掌握度概览异常", [
  43. 'student_id' => $studentId,
  44. 'error' => $e->getMessage()
  45. ]);
  46. return null;
  47. }
  48. }
  49. /**
  50. * 获取学生掌握度列表
  51. */
  52. public function getStudentMasteryList(string $studentId, array $filters = []): ?array
  53. {
  54. try {
  55. $response = Http::timeout($this->timeout)
  56. ->get("{$this->baseUrl}/api/v1/mastery/student/{$studentId}", $filters);
  57. if ($response->successful()) {
  58. return $response->json();
  59. }
  60. return null;
  61. } catch (\Exception $e) {
  62. Log::error("获取学生掌握度列表异常", [
  63. 'student_id' => $studentId,
  64. 'error' => $e->getMessage()
  65. ]);
  66. return null;
  67. }
  68. }
  69. /**
  70. * 获取学生技能熟练度
  71. */
  72. public function getStudentSkillProficiency(string $studentId): ?array
  73. {
  74. try {
  75. $response = Http::timeout($this->timeout)
  76. ->get("{$this->baseUrl}/api/v1/skill/proficiency/student/{$studentId}");
  77. if ($response->successful()) {
  78. return $response->json();
  79. }
  80. return null;
  81. } catch (\Exception $e) {
  82. Log::error("获取学生技能熟练度异常", [
  83. 'student_id' => $studentId,
  84. 'error' => $e->getMessage()
  85. ]);
  86. return null;
  87. }
  88. }
  89. /**
  90. * 获取学生技能摘要
  91. */
  92. public function getStudentSkillSummary(string $studentId): ?array
  93. {
  94. try {
  95. $response = Http::timeout($this->timeout)
  96. ->get("{$this->baseUrl}/api/v1/skill/proficiency/student/{$studentId}/summary");
  97. if ($response->successful()) {
  98. return $response->json();
  99. }
  100. return null;
  101. } catch (\Exception $e) {
  102. Log::error("获取学生技能摘要异常", [
  103. 'student_id' => $studentId,
  104. 'error' => $e->getMessage()
  105. ]);
  106. return null;
  107. }
  108. }
  109. /**
  110. * 创建提分预测
  111. */
  112. public function createScorePrediction(array $data): ?array
  113. {
  114. try {
  115. $response = Http::timeout($this->timeout)
  116. ->post("{$this->baseUrl}/api/v1/prediction/score", $data);
  117. if ($response->successful()) {
  118. return $response->json();
  119. }
  120. return null;
  121. } catch (\Exception $e) {
  122. Log::error("创建提分预测异常", [
  123. 'data' => $data,
  124. 'error' => $e->getMessage()
  125. ]);
  126. return null;
  127. }
  128. }
  129. /**
  130. * 获取学生历史预测
  131. */
  132. public function getStudentPredictions(string $studentId, int $limit = 10): ?array
  133. {
  134. try {
  135. $response = Http::timeout($this->timeout)
  136. ->get("{$this->baseUrl}/api/v1/prediction/student/{$studentId}", [
  137. 'limit' => $limit
  138. ]);
  139. if ($response->successful()) {
  140. return $response->json();
  141. }
  142. return null;
  143. } catch (\Exception $e) {
  144. Log::error("获取学生预测记录异常", [
  145. 'student_id' => $studentId,
  146. 'error' => $e->getMessage()
  147. ]);
  148. return null;
  149. }
  150. }
  151. /**
  152. * 生成学习路径
  153. */
  154. public function generateLearningPath(array $data): ?array
  155. {
  156. try {
  157. $response = Http::timeout($this->timeout)
  158. ->post("{$this->baseUrl}/api/v1/learning-path/generate", $data);
  159. if ($response->successful()) {
  160. return $response->json();
  161. }
  162. return null;
  163. } catch (\Exception $e) {
  164. Log::error("生成学习路径异常", [
  165. 'data' => $data,
  166. 'error' => $e->getMessage()
  167. ]);
  168. return null;
  169. }
  170. }
  171. /**
  172. * 获取学生学习路径
  173. */
  174. public function getStudentLearningPaths(string $studentId, int $limit = 10): ?array
  175. {
  176. try {
  177. $response = Http::timeout($this->timeout)
  178. ->get("{$this->baseUrl}/api/v1/learning-path/student/{$studentId}", [
  179. 'limit' => $limit
  180. ]);
  181. if ($response->successful()) {
  182. return $response->json();
  183. }
  184. return null;
  185. } catch (\Exception $e) {
  186. Log::error("获取学生学习路径异常", [
  187. 'student_id' => $studentId,
  188. 'error' => $e->getMessage()
  189. ]);
  190. return null;
  191. }
  192. }
  193. /**
  194. * 获取预测分析统计
  195. */
  196. public function getPredictionAnalytics(string $studentId): ?array
  197. {
  198. try {
  199. $response = Http::timeout($this->timeout)
  200. ->get("{$this->baseUrl}/api/v1/prediction/student/{$studentId}/analytics");
  201. if ($response->successful()) {
  202. return $response->json();
  203. }
  204. return null;
  205. } catch (\Exception $e) {
  206. Log::error("获取预测分析统计异常", [
  207. 'student_id' => $studentId,
  208. 'error' => $e->getMessage()
  209. ]);
  210. return null;
  211. }
  212. }
  213. /**
  214. * 获取学习路径分析统计
  215. */
  216. public function getLearningPathAnalytics(string $studentId): ?array
  217. {
  218. try {
  219. $response = Http::timeout($this->timeout)
  220. ->get("{$this->baseUrl}/api/v1/learning-path/student/{$studentId}/analytics");
  221. if ($response->successful()) {
  222. return $response->json();
  223. }
  224. return null;
  225. } catch (\Exception $e) {
  226. Log::error("获取学习路径分析统计异常", [
  227. 'student_id' => $studentId,
  228. 'error' => $e->getMessage()
  229. ]);
  230. return null;
  231. }
  232. }
  233. /**
  234. * 重新计算掌握度
  235. */
  236. public function recalculateMastery(string $studentId, string $kpCode): ?array
  237. {
  238. try {
  239. $response = Http::timeout($this->timeout)
  240. ->post("{$this->baseUrl}/api/v1/mastery/student/{$studentId}/update?kp_code={$kpCode}");
  241. if ($response->successful()) {
  242. return $response->json();
  243. }
  244. return null;
  245. } catch (\Exception $e) {
  246. Log::error("重新计算掌握度异常", [
  247. 'student_id' => $studentId,
  248. 'kp_code' => $kpCode,
  249. 'error' => $e->getMessage()
  250. ]);
  251. return null;
  252. }
  253. }
  254. /**
  255. * 批量更新技能熟练度
  256. */
  257. public function batchUpdateSkillProficiency(string $studentId): ?array
  258. {
  259. try {
  260. $response = Http::timeout($this->timeout)
  261. ->post("{$this->baseUrl}/api/v1/skill/proficiency/student/{$studentId}/batch-update");
  262. if ($response->successful()) {
  263. return $response->json();
  264. }
  265. return null;
  266. } catch (\Exception $e) {
  267. Log::error("批量更新技能熟练度异常", [
  268. 'student_id' => $studentId,
  269. 'error' => $e->getMessage()
  270. ]);
  271. return null;
  272. }
  273. }
  274. /**
  275. * 快速提分预测
  276. */
  277. public function quickScorePrediction(string $studentId): ?array
  278. {
  279. try {
  280. $response = Http::timeout($this->timeout)
  281. ->post("{$this->baseUrl}/api/v1/prediction/student/{$studentId}/quick-prediction");
  282. if ($response->successful()) {
  283. return $response->json();
  284. }
  285. return null;
  286. } catch (\Exception $e) {
  287. Log::error("快速提分预测异常", [
  288. 'student_id' => $studentId,
  289. 'error' => $e->getMessage()
  290. ]);
  291. return null;
  292. }
  293. }
  294. /**
  295. * 推荐学习路径
  296. */
  297. public function recommendLearningPaths(string $studentId, int $limit = 3): ?array
  298. {
  299. try {
  300. $response = Http::timeout($this->timeout)
  301. ->post("{$this->baseUrl}/api/v1/learning-path/student/{$studentId}/recommend", [
  302. 'limit' => $limit
  303. ]);
  304. if ($response->successful()) {
  305. return $response->json();
  306. }
  307. return null;
  308. } catch (\Exception $e) {
  309. Log::error("推荐学习路径异常", [
  310. 'student_id' => $studentId,
  311. 'error' => $e->getMessage()
  312. ]);
  313. return null;
  314. }
  315. }
  316. /**
  317. * ==================== 新增:从MathRecSys迁移的功能 ====================
  318. */
  319. /**
  320. * 获取学生能力画像
  321. */
  322. public function getStudentProfile(string $studentId): ?array
  323. {
  324. try {
  325. $response = Http::timeout($this->timeout)
  326. ->get("{$this->baseUrl}/student/{$studentId}/profile");
  327. if ($response->successful()) {
  328. return $response->json();
  329. }
  330. return null;
  331. } catch (\Exception $e) {
  332. Log::error("获取学生能力画像异常", [
  333. 'student_id' => $studentId,
  334. 'error' => $e->getMessage()
  335. ]);
  336. return null;
  337. }
  338. }
  339. /**
  340. * 智能分析题目/学习
  341. */
  342. public function smartAnalyze(string $studentId, array $data): ?array
  343. {
  344. try {
  345. $response = Http::timeout($this->timeout)
  346. ->post("{$this->baseUrl}/student/{$studentId}/analysis", $data);
  347. if ($response->successful()) {
  348. return $response->json();
  349. }
  350. return null;
  351. } catch (\Exception $e) {
  352. Log::error("智能分析异常", [
  353. 'student_id' => $studentId,
  354. 'error' => $e->getMessage()
  355. ]);
  356. return null;
  357. }
  358. }
  359. /**
  360. * 获取学习轨迹
  361. */
  362. public function getLearningTrajectory(string $studentId): ?array
  363. {
  364. try {
  365. $response = Http::timeout($this->timeout)
  366. ->get("{$this->baseUrl}/student/{$studentId}/trajectory");
  367. if ($response->successful()) {
  368. return $response->json();
  369. }
  370. return null;
  371. } catch (\Exception $e) {
  372. Log::error("获取学习轨迹异常", [
  373. 'student_id' => $studentId,
  374. 'error' => $e->getMessage()
  375. ]);
  376. return null;
  377. }
  378. }
  379. /**
  380. * 获取薄弱知识点
  381. */
  382. public function getWeakPoints(string $studentId): ?array
  383. {
  384. try {
  385. $response = Http::timeout($this->timeout)
  386. ->get("{$this->baseUrl}/student/{$studentId}/weak-points");
  387. if ($response->successful()) {
  388. return $response->json();
  389. }
  390. return null;
  391. } catch (\Exception $e) {
  392. Log::error("获取薄弱知识点异常", [
  393. 'student_id' => $studentId,
  394. 'error' => $e->getMessage()
  395. ]);
  396. return null;
  397. }
  398. }
  399. /**
  400. * 获取学习进度
  401. */
  402. public function getLearningProgress(string $studentId): ?array
  403. {
  404. try {
  405. $response = Http::timeout($this->timeout)
  406. ->get("{$this->baseUrl}/student/{$studentId}/progress");
  407. if ($response->successful()) {
  408. return $response->json();
  409. }
  410. return null;
  411. } catch (\Exception $e) {
  412. Log::error("获取学习进度异常", [
  413. 'student_id' => $studentId,
  414. 'error' => $e->getMessage()
  415. ]);
  416. return null;
  417. }
  418. }
  419. /**
  420. * 获取个性化推荐
  421. */
  422. public function getRecommendations(string $studentId, array $data = []): ?array
  423. {
  424. try {
  425. $response = Http::timeout($this->timeout)
  426. ->post("{$this->baseUrl}/student/{$studentId}/recommendations", $data);
  427. if ($response->successful()) {
  428. return $response->json();
  429. }
  430. return null;
  431. } catch (\Exception $e) {
  432. Log::error("获取个性化推荐异常", [
  433. 'student_id' => $studentId,
  434. 'error' => $e->getMessage()
  435. ]);
  436. return null;
  437. }
  438. }
  439. /**
  440. * 获取班级整体分析
  441. */
  442. public function getClassAnalysis(string $classId): ?array
  443. {
  444. try {
  445. $response = Http::timeout($this->timeout)
  446. ->get("{$this->baseUrl}/class/{$classId}/analysis");
  447. if ($response->successful()) {
  448. return $response->json();
  449. }
  450. return null;
  451. } catch (\Exception $e) {
  452. Log::error("获取班级分析异常", [
  453. 'class_id' => $classId,
  454. 'error' => $e->getMessage()
  455. ]);
  456. return null;
  457. }
  458. }
  459. /**
  460. * 批量分析学生
  461. */
  462. public function batchAnalyzeStudents(string $classId, array $data): ?array
  463. {
  464. try {
  465. $response = Http::timeout($this->timeout)
  466. ->post("{$this->baseUrl}/class/{$classId}/batch-analysis", $data);
  467. if ($response->successful()) {
  468. return $response->json();
  469. }
  470. return null;
  471. } catch (\Exception $e) {
  472. Log::error("批量分析学生异常", [
  473. 'class_id' => $classId,
  474. 'error' => $e->getMessage()
  475. ]);
  476. return null;
  477. }
  478. }
  479. /**
  480. * 获取班级排名
  481. */
  482. public function getClassRanking(string $classId, string $metric = 'mastery'): ?array
  483. {
  484. try {
  485. $response = Http::timeout($this->timeout)
  486. ->get("{$this->baseUrl}/class/{$classId}/ranking", [
  487. 'metric' => $metric
  488. ]);
  489. if ($response->successful()) {
  490. return $response->json();
  491. }
  492. return null;
  493. } catch (\Exception $e) {
  494. Log::error("获取班级排名异常", [
  495. 'class_id' => $classId,
  496. 'error' => $e->getMessage()
  497. ]);
  498. return null;
  499. }
  500. }
  501. /**
  502. * 与其他班级对比
  503. */
  504. public function compareWithOtherClasses(string $classId): ?array
  505. {
  506. try {
  507. $response = Http::timeout($this->timeout)
  508. ->get("{$this->baseUrl}/class/{$classId}/comparison");
  509. if ($response->successful()) {
  510. return $response->json();
  511. }
  512. return null;
  513. } catch (\Exception $e) {
  514. Log::error("获取班级对比异常", [
  515. 'class_id' => $classId,
  516. 'error' => $e->getMessage()
  517. ]);
  518. return null;
  519. }
  520. }
  521. /**
  522. * 提分潜力估算
  523. */
  524. public function estimateScoreGain(array $data): ?array
  525. {
  526. try {
  527. $response = Http::timeout($this->timeout)
  528. ->get("{$this->baseUrl}/ability/gain-estimation", $data);
  529. if ($response->successful()) {
  530. return $response->json();
  531. }
  532. return null;
  533. } catch (\Exception $e) {
  534. Log::error("提分潜力估算异常", [
  535. 'data' => $data,
  536. 'error' => $e->getMessage()
  537. ]);
  538. return null;
  539. }
  540. }
  541. /**
  542. * 评估学生能力
  543. */
  544. public function evaluateStudentAbility(array $data): ?array
  545. {
  546. try {
  547. $response = Http::timeout($this->timeout)
  548. ->post("{$this->baseUrl}/ability/evaluate-student-ability", $data);
  549. if ($response->successful()) {
  550. return $response->json();
  551. }
  552. return null;
  553. } catch (\Exception $e) {
  554. Log::error("评估学生能力异常", [
  555. 'data' => $data,
  556. 'error' => $e->getMessage()
  557. ]);
  558. return null;
  559. }
  560. }
  561. /**
  562. * 获取学生能力档案
  563. */
  564. public function getAbilityProfile(string $studentId): ?array
  565. {
  566. try {
  567. $response = Http::timeout($this->timeout)
  568. ->get("{$this->baseUrl}/ability/student/{$studentId}/ability-profile");
  569. if ($response->successful()) {
  570. return $response->json();
  571. }
  572. return null;
  573. } catch (\Exception $e) {
  574. Log::error("获取学生能力档案异常", [
  575. 'student_id' => $studentId,
  576. 'error' => $e->getMessage()
  577. ]);
  578. return null;
  579. }
  580. }
  581. /**
  582. * 比较学生能力
  583. */
  584. public function compareAbilities(array $data): ?array
  585. {
  586. try {
  587. $response = Http::timeout($this->timeout)
  588. ->post("{$this->baseUrl}/ability/compare-abilities", $data);
  589. if ($response->successful()) {
  590. return $response->json();
  591. }
  592. return null;
  593. } catch (\Exception $e) {
  594. Log::error("比较学生能力异常", [
  595. 'data' => $data,
  596. 'error' => $e->getMessage()
  597. ]);
  598. return null;
  599. }
  600. }
  601. /**
  602. * 学习问题诊断
  603. */
  604. public function diagnosticAnalyze(array $data): ?array
  605. {
  606. try {
  607. $response = Http::timeout($this->timeout)
  608. ->post("{$this->baseUrl}/diagnostic/analyze", $data);
  609. if ($response->successful()) {
  610. return $response->json();
  611. }
  612. return null;
  613. } catch (\Exception $e) {
  614. Log::error("学习问题诊断异常", [
  615. 'data' => $data,
  616. 'error' => $e->getMessage()
  617. ]);
  618. return null;
  619. }
  620. }
  621. /**
  622. * 学习障碍检测
  623. */
  624. public function detectLearningBarriers(array $data): ?array
  625. {
  626. try {
  627. $response = Http::timeout($this->timeout)
  628. ->post("{$this->baseUrl}/diagnostic/learning-barrier-detection", $data);
  629. if ($response->successful()) {
  630. return $response->json();
  631. }
  632. return null;
  633. } catch (\Exception $e) {
  634. Log::error("学习障碍检测异常", [
  635. 'data' => $data,
  636. 'error' => $e->getMessage()
  637. ]);
  638. return null;
  639. }
  640. }
  641. /**
  642. * 错误概念检测
  643. */
  644. public function detectMisconceptions(array $data): ?array
  645. {
  646. try {
  647. $response = Http::timeout($this->timeout)
  648. ->post("{$this->baseUrl}/diagnostic/misconception-detection", $data);
  649. if ($response->successful()) {
  650. return $response->json();
  651. }
  652. return null;
  653. } catch (\Exception $e) {
  654. Log::error("错误概念检测异常", [
  655. 'data' => $data,
  656. 'error' => $e->getMessage()
  657. ]);
  658. return null;
  659. }
  660. }
  661. /**
  662. * 获取诊断历史
  663. */
  664. public function getDiagnosisHistory(string $studentId): ?array
  665. {
  666. try {
  667. $response = Http::timeout($this->timeout)
  668. ->get("{$this->baseUrl}/diagnostic/student/{$studentId}/learning-diagnosis-history");
  669. if ($response->successful()) {
  670. return $response->json();
  671. }
  672. return null;
  673. } catch (\Exception $e) {
  674. Log::error("获取诊断历史异常", [
  675. 'student_id' => $studentId,
  676. 'error' => $e->getMessage()
  677. ]);
  678. return null;
  679. }
  680. }
  681. /**
  682. * 检查服务健康状态
  683. */
  684. public function checkHealth(): bool
  685. {
  686. try {
  687. $response = Http::timeout(5)
  688. ->get("{$this->baseUrl}/health");
  689. return $response->successful();
  690. } catch (\Exception $e) {
  691. Log::warning("学习分析系统健康检查失败", [
  692. 'error' => $e->getMessage()
  693. ]);
  694. return false;
  695. }
  696. }
  697. }