| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- class LearningAnalyticsService
- {
- protected string $baseUrl;
- protected int $timeout = 10;
- protected ?QuestionBankService $questionBankService;
- public function __construct(?QuestionBankService $questionBankService = null)
- {
- $this->baseUrl = config('services.learning_analytics.url', env('LEARNING_ANALYTICS_API_BASE', 'http://localhost:5016'));
- $this->questionBankService = $questionBankService;
- }
- /**
- * 获取学生掌握度
- */
- public function getStudentMastery(string $studentId, string $kpCode = null): array
- {
- try {
- $endpoint = $kpCode
- ? "/api/v1/mastery/student/{$studentId}/kp/{$kpCode}"
- : "/api/v1/mastery/student/{$studentId}";
- Log::info('LearningAnalytics Request: Get Student Mastery', [
- 'endpoint' => $endpoint,
- 'student_id' => $studentId,
- 'kp_code' => $kpCode
- ]);
- $response = Http::timeout($this->timeout)->get($this->baseUrl . $endpoint);
- Log::info('LearningAnalytics Response: Get Student Mastery', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('LearningAnalytics API Error', [
- 'endpoint' => $endpoint,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to fetch mastery data'
- ];
- } catch (\Exception $e) {
- Log::error('LearningAnalytics Service Exception', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 更新学生掌握度
- */
- public function updateMastery(array $data): array
- {
- try {
- Log::info('LearningAnalytics Request: Update Mastery', [
- 'url' => $this->baseUrl . '/api/v1/mastery/student/' . $data['student_id'] . '/update',
- 'data' => $data
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . '/api/v1/mastery/student/' . $data['student_id'] . '/update', $data);
- Log::info('LearningAnalytics Response: Update Mastery', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('LearningAnalytics Update Error', [
- 'data' => $data,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to update mastery'
- ];
- } catch (\Exception $e) {
- Log::error('LearningAnalytics Update Exception', [
- 'error' => $e->getMessage(),
- 'data' => $data
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取老师名下的所有学生
- */
- public function getTeacherStudents(string $teacherId): array
- {
- try {
- // 从本地MySQL获取学生
- $students = DB::table('students as s')
- ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
- ->where('s.teacher_id', $teacherId)
- ->select(
- 's.student_id',
- 's.name',
- 's.grade',
- 's.class_name',
- 'u.username',
- 'u.email'
- )
- ->get()
- ->toArray();
- return $students;
- } catch (\Exception $e) {
- Log::error('Get Teacher Students Error', [
- 'teacher_id' => $teacherId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取学生学习分析
- */
- public function getStudentAnalysis(string $studentId): array
- {
- // 从LearningAnalytics获取掌握度
- $masteryData = $this->getStudentMastery($studentId);
- // 从MySQL获取练习历史
- $exercises = DB::table('student_exercises')
- ->where('student_id', $studentId)
- ->orderBy('created_at', 'desc')
- ->limit(50)
- ->get()
- ->toArray();
- // 从MySQL获取掌握度记录
- $masteryRecords = DB::table('student_mastery')
- ->where('student_id', $studentId)
- ->get()
- ->toArray();
- return [
- 'student_id' => $studentId,
- 'mastery_from_la' => $masteryData,
- 'exercises' => $exercises,
- 'mastery_records' => $masteryRecords,
- 'total_exercises' => count($exercises),
- 'total_mastery_records' => count($masteryRecords),
- ];
- }
- /**
- * 生成学习测试数据
- */
- public function generateLearningData(string $studentId, array $params): array
- {
- $results = [];
- foreach ($params as $param) {
- $data = [
- 'student_id' => $studentId,
- 'kp_code' => $param['kp_code'],
- 'is_correct' => $param['is_correct'],
- 'time_spent_seconds' => $param['time_spent_seconds'] ?? 120,
- 'difficulty_level' => $param['difficulty_level'] ?? 3,
- ];
- $result = $this->updateMastery($data);
- $results[] = $result;
- }
- return $results;
- }
- /**
- * 获取学习推荐
- */
- public function getLearningRecommendations(string $studentId): array
- {
- try {
- Log::info('LearningAnalytics Request: Get Learning Recommendations', [
- 'url' => $this->baseUrl . "/api/v1/learning-path/student/{$studentId}/recommend"
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/learning-path/student/{$studentId}/recommend");
- Log::info('LearningAnalytics Response: Get Learning Recommendations', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- return ['error' => true, 'message' => 'Failed to fetch recommendations'];
- } catch (\Exception $e) {
- return ['error' => true, 'message' => $e->getMessage()];
- }
- }
- /**
- * 获取知识点列表(从知识图谱API)
- */
- public function getKnowledgePoints(array $filters = []): array
- {
- try {
- $kgBaseUrl = config('services.knowledge_api.base_url', 'http://localhost:5011');
- Log::info('LearningAnalytics Request: Get Knowledge Points', [
- 'url' => $kgBaseUrl . '/knowledge-points/',
- 'filters' => $filters
- ]);
- $response = Http::timeout($this->timeout)
- ->get($kgBaseUrl . '/knowledge-points/', $filters);
- Log::info('LearningAnalytics Response: Get Knowledge Points', [
- 'status' => $response->status(),
- 'count' => count($response->json()['data'] ?? [])
- ]);
- if ($response->successful()) {
- return $response->json()['data'] ?? [];
- }
- return [];
- } catch (\Exception $e) {
- Log::error('LearningAnalytics Knowledge Points Error', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取学生技能熟练度
- */
- public function getStudentSkillProficiency(string $studentId): array
- {
- try {
- Log::info('LearningAnalytics Request: Get Student Skill Proficiency', [
- 'url' => $this->baseUrl . "/api/v1/skill/proficiency/student/{$studentId}"
- ]);
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . "/api/v1/skill/proficiency/student/{$studentId}");
- Log::info('LearningAnalytics Response: Get Student Skill Proficiency', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::warning('LearningAnalytics Skill Proficiency API Error', [
- 'student_id' => $studentId,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- // API失败时返回空数据,不报错
- return [
- 'student_id' => $studentId,
- 'total_count' => 0,
- 'data' => []
- ];
- } catch (\Exception $e) {
- Log::warning('LearningAnalytics Skill Proficiency API Exception', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- // 发生异常时返回空数据,不报错
- return [
- 'student_id' => $studentId,
- 'total_count' => 0,
- 'data' => []
- ];
- }
- }
- /**
- * 获取学生掌握度列表(别名方法)
- */
- public function getStudentMasteryList(string $studentId): array
- {
- return $this->getStudentMastery($studentId);
- }
- /**
- * 获取知识点依赖关系
- */
- public function getKnowledgeDependencies(): array
- {
- try {
- Log::info('LearningAnalytics Request: Get Knowledge Dependencies', [
- 'url' => $this->baseUrl . '/knowledge-dependencies/'
- ]);
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . '/knowledge-dependencies/');
- Log::info('LearningAnalytics Response: Get Knowledge Dependencies', [
- 'status' => $response->status(),
- 'count' => count($response->json()['data'] ?? [])
- ]);
- if ($response->successful()) {
- return $response->json()['data'] ?? [];
- }
- return [];
- } catch (\Exception $e) {
- Log::error('LearningAnalytics Knowledge Dependencies Error', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 提交学生答题记录
- */
- public function submitAttempt(string $studentId, array $attemptData): array
- {
- try {
- Log::info('LearningAnalytics Request: Submit Attempt', [
- 'url' => $this->baseUrl . "/api/v1/attempts/student/{$studentId}",
- 'data' => $attemptData
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/attempts/student/{$studentId}", $attemptData);
- Log::info('LearningAnalytics Response: Submit Attempt', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Submit Attempt Error', [
- 'student_id' => $studentId,
- 'data' => $attemptData,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to submit attempt'
- ];
- } catch (\Exception $e) {
- Log::error('Submit Attempt Exception', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage(),
- 'data' => $attemptData
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 批量提交学生答题记录
- */
- public function submitBatchAttempts(string $studentId, array $data): array
- {
- try {
- Log::info('LearningAnalytics Request: Submit Batch Attempts', [
- 'url' => $this->baseUrl . "/api/v1/attempts/batch/student/{$studentId}",
- 'data_count' => count($data['answers'] ?? []),
- 'paper_id' => $data['paper_id'] ?? null
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/attempts/batch/student/{$studentId}", $data);
- Log::info('LearningAnalytics Response: Submit Batch Attempts', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Submit Batch Attempts Error', [
- 'student_id' => $studentId,
- 'data_count' => count($data['answers'] ?? []),
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to submit batch attempts: ' . $response->body()
- ];
- } catch (\Exception $e) {
- Log::error('Submit Batch Attempts Exception', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 提交OCR分析请求
- */
- public function submitOCRAnalysis(array $data): array
- {
- try {
- Log::info('Sending OCR results to LearningAnalytics', [
- 'student_id' => $data['student_id'] ?? 'unknown',
- 'exam_id' => $data['exam_id'] ?? 'unknown',
- 'question_count' => count($data['questions'] ?? [])
- ]);
- $response = Http::timeout(30) // 分析可能需要较长时间
- ->post($this->baseUrl . '/api/analysis/process-answers', $data);
- Log::info('LearningAnalytics Response: Submit OCR Analysis', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- Log::info('Analysis submitted successfully', [
- 'analysis_id' => $response->json('analysis_id')
- ]);
- return $response->json();
- }
- Log::error('Submit OCR Analysis Error', [
- 'status' => $response->status(),
- 'response' => $response->body(),
- 'data_preview' => array_merge($data, ['questions' => count($data['questions'])])
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to submit analysis: ' . $response->body()
- ];
- } catch (\Exception $e) {
- Log::error('Submit OCR Analysis Exception', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取分析结果详情
- */
- public function getAnalysisResult(string $analysisId): array
- {
- try {
- $endpoint = "/api/analysis/analysis/{$analysisId}";
- Log::info('LearningAnalytics Request: Get Analysis Result', [
- 'endpoint' => $endpoint,
- 'analysis_id' => $analysisId
- ]);
- $response = Http::timeout($this->timeout)->get($this->baseUrl . $endpoint);
- Log::info('LearningAnalytics Response: Get Analysis Result', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Get Analysis Result Error', [
- 'analysis_id' => $analysisId,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to fetch analysis result'
- ];
- } catch (\Exception $e) {
- Log::error('Get Analysis Result Exception', [
- 'analysis_id' => $analysisId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 检查服务健康状态
- */
- public function checkHealth(): bool
- {
- try {
- $response = Http::timeout(5)->get($this->baseUrl . '/health');
- return $response->successful();
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 获取学生掌握度概览
- */
- public function getStudentMasteryOverview(string $studentId): array
- {
- try {
- $mastery = $this->getStudentMastery($studentId);
- if (isset($mastery['error'])) {
- return [
- 'total_knowledge_points' => 0,
- 'average_mastery_level' => 0,
- 'mastered_knowledge_points' => 0,
- 'good_knowledge_points' => 0,
- 'weak_knowledge_points' => 0,
- 'weak_knowledge_points_list' => [],
- 'details' => []
- ];
- }
- $data = $mastery['data'] ?? [];
- // **修复**:不过滤total_attempts,与薄弱点API保持一致
- // 这样确保数据一致性
- $attemptedData = $data;
- $total = count($data);
- $attemptedCount = count($attemptedData);
- $average = $attemptedCount > 0
- ? array_sum(array_column($attemptedData, 'mastery_level')) / $attemptedCount
- : 0;
- // 分类知识点
- $mastered = [];
- $good = [];
- $weak = [];
- foreach ($attemptedData as $item) {
- $level = $item['mastery_level'] ?? 0;
- if ($level >= 0.85) {
- $mastered[] = $item;
- } elseif ($level >= 0.70) {
- $good[] = $item;
- } else {
- $weak[] = $item;
- }
- }
- return [
- 'total_knowledge_points' => $total,
- 'average_mastery_level' => $average,
- 'mastered_knowledge_points' => count($mastered),
- 'good_knowledge_points' => count($good),
- 'weak_knowledge_points' => count($weak),
- 'weak_knowledge_points_list' => $weak,
- 'details' => $data
- ];
- } catch (\Exception $e) {
- Log::error('Get Student Mastery Overview Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'total_knowledge_points' => 0,
- 'average_mastery_level' => 0,
- 'mastered_knowledge_points' => 0,
- 'good_knowledge_points' => 0,
- 'weak_knowledge_points' => 0,
- 'weak_knowledge_points_list' => [],
- 'details' => []
- ];
- }
- }
- /**
- * 获取学生技能摘要
- */
- public function getStudentSkillSummary(string $studentId): array
- {
- try {
- $proficiency = $this->getStudentSkillProficiency($studentId);
- // 无论是否有error,都继续处理,返回空数据
- $data = $proficiency['data'] ?? [];
- $totalSkills = count($data);
- $averageLevel = $totalSkills > 0 ? array_sum(array_column($data, 'proficiency_level')) / $totalSkills : 0;
- // 计算总答题数
- $totalQuestions = 0;
- foreach ($data as $skill) {
- $totalQuestions += $skill['total_questions_attempted'] ?? 0;
- }
- return [
- 'total_skills' => $totalSkills,
- 'average_proficiency_level' => $averageLevel,
- 'total_questions_attempted' => $totalQuestions,
- 'skill_list' => $data
- ];
- } catch (\Exception $e) {
- Log::warning('Get Student Skill Summary Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- // 发生异常时返回空数据
- return [
- 'total_skills' => 0,
- 'average_proficiency_level' => 0,
- 'total_questions_attempted' => 0,
- 'skill_list' => []
- ];
- }
- }
- /**
- * 获取学生预测数据
- */
- public function getStudentPredictions(string $studentId, int $count = 5): array
- {
- try {
- Log::info('LearningAnalytics Request: Get Student Predictions', [
- 'url' => $this->baseUrl . "/api/v1/prediction/student/{$studentId}?count={$count}"
- ]);
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . "/api/v1/prediction/student/{$studentId}?count={$count}");
- Log::info('LearningAnalytics Response: Get Student Predictions', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- $data = $response->json();
- $predictions = $data['predictions'] ?? $data['data'] ?? [];
- return [
- 'predictions' => $predictions
- ];
- }
- return [
- 'predictions' => []
- ];
- } catch (\Exception $e) {
- Log::error('Get Student Predictions Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'predictions' => []
- ];
- }
- }
- /**
- * 获取学生学习路径
- */
- public function getStudentLearningPaths(string $studentId, int $count = 3): array
- {
- try {
- Log::info('LearningAnalytics Request: Get Student Learning Paths', [
- 'url' => $this->baseUrl . "/api/v1/learning-path/student/{$studentId}?limit={$count}"
- ]);
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . "/api/v1/learning-path/student/{$studentId}?limit={$count}");
- Log::info('LearningAnalytics Response: Get Student Learning Paths', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- $data = $response->json()['data'] ?? [];
- return [
- 'paths' => $data
- ];
- }
- return [
- 'paths' => []
- ];
- } catch (\Exception $e) {
- Log::error('Get Student Learning Paths Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'paths' => []
- ];
- }
- }
- /**
- * 获取预测分析数据
- */
- public function getPredictionAnalytics(string $studentId): array
- {
- try {
- $predictions = $this->getStudentPredictions($studentId, 10);
- if (empty($predictions)) {
- return ['accuracy' => 0, 'trend' => 'stable', 'confidence' => 0];
- }
- $accuracy = 0;
- $confidence = 0;
- if (!empty($predictions)) {
- $accuracy = rand(75, 95); // 模拟准确率
- $confidence = rand(70, 90); // 模拟置信度
- }
- $trend = 'improving'; // improving, stable, declining
- return [
- 'accuracy' => $accuracy,
- 'trend' => $trend,
- 'confidence' => $confidence,
- 'sample_size' => count($predictions)
- ];
- } catch (\Exception $e) {
- Log::error('Get Prediction Analytics Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return ['accuracy' => 0, 'trend' => 'stable', 'confidence' => 0];
- }
- }
- /**
- * 获取学习路径分析数据
- */
- public function getLearningPathAnalytics(string $studentId): array
- {
- try {
- $paths = $this->getStudentLearningPaths($studentId, 5);
- if (empty($paths)) {
- return [
- 'active_paths' => 0,
- 'completed_paths' => 0,
- 'average_efficiency_score' => 0,
- 'completion_rate' => 0,
- 'average_time' => 0,
- 'total_paths' => 0
- ];
- }
- $activePaths = 0;
- $completedPaths = 0;
- $efficiencyScores = [];
- foreach ($paths as $path) {
- if (($path['status'] ?? '') === 'active') {
- $activePaths++;
- }
- if (($path['status'] ?? '') === 'completed') {
- $completedPaths++;
- }
- if (isset($path['efficiency_score'])) {
- $efficiencyScores[] = $path['efficiency_score'];
- }
- }
- $averageEfficiency = !empty($efficiencyScores)
- ? array_sum($efficiencyScores) / count($efficiencyScores)
- : rand(60, 85) / 100;
- $completionRate = count($paths) > 0
- ? ($completedPaths / count($paths)) * 100
- : 0;
- $averageTime = rand(30, 60); // 模拟平均时间(分钟)
- return [
- 'active_paths' => $activePaths,
- 'completed_paths' => $completedPaths,
- 'average_efficiency_score' => $averageEfficiency,
- 'completion_rate' => $completionRate,
- 'average_time' => $averageTime,
- 'total_paths' => count($paths)
- ];
- } catch (\Exception $e) {
- Log::error('Get Learning Path Analytics Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'active_paths' => 0,
- 'completed_paths' => 0,
- 'average_efficiency_score' => 0,
- 'completion_rate' => 0,
- 'average_time' => 0,
- 'total_paths' => 0
- ];
- }
- }
- /**
- * 快速分数预测
- */
- public function quickScorePrediction(string $studentId): array
- {
- Log::info('开始调用快速预测API', ['student_id' => $studentId]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/prediction/student/{$studentId}/quick-prediction");
- Log::info('快速预测API响应', [
- 'student_id' => $studentId,
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- if (!$response->successful()) {
- throw new \Exception(sprintf(
- '快速预测接口失败: %s %s',
- $response->status(),
- $response->body()
- ));
- }
- $data = $response->json();
- Log::info('快速预测API返回数据', ['student_id' => $studentId, 'data' => $data]);
- // API 返回结构:{ student_id, current_assumption, target_assumption, quick_prediction, prediction_id, message }
- $quickPredictionData = $data['quick_prediction'] ?? [];
- return [
- 'quick_prediction' => [
- 'current_score' => $quickPredictionData['current_score'] ?? $data['current_assumption'] ?? 0,
- 'predicted_score' => $quickPredictionData['predicted_score'] ?? $data['target_assumption'] ?? 0,
- 'improvement_potential' => $quickPredictionData['improvement_potential'] ?? (($data['target_assumption'] ?? 0) - ($data['current_assumption'] ?? 0)),
- 'estimated_study_hours' => $quickPredictionData['estimated_study_hours'] ?? 0,
- 'confidence_level' => $quickPredictionData['confidence_level'] ?? 0,
- 'priority_topics' => $quickPredictionData['priority_topics'] ?? [],
- 'recommended_actions' => $quickPredictionData['recommended_actions'] ?? [],
- 'weak_knowledge_points_count' => $quickPredictionData['weak_knowledge_points_count'] ?? 0,
- 'total_knowledge_points' => $quickPredictionData['total_knowledge_points'] ?? 0
- ],
- 'predicted_score' => $quickPredictionData['predicted_score'] ?? $data['target_assumption'] ?? 0,
- 'confidence' => isset($quickPredictionData['confidence_level']) ? $quickPredictionData['confidence_level'] * 100 : 0,
- 'time_estimate' => $quickPredictionData['estimated_study_hours'] ?? 0,
- 'prediction_id' => $data['prediction_id'] ?? null,
- 'message' => $data['message'] ?? null,
- ];
- }
- /**
- * 推荐学习路径
- */
- public function recommendLearningPaths(string $studentId, int $count = 3): array
- {
- try {
- Log::info('LearningAnalytics Request: Recommend Learning Paths', [
- 'url' => $this->baseUrl . "/api/v1/learning-path/student/{$studentId}/recommend?limit={$count}"
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/learning-path/student/{$studentId}/recommend?limit={$count}");
- Log::info('LearningAnalytics Response: Recommend Learning Paths', [
- 'status' => $response->status(),
- 'body' => $response->json()
- ]);
- if ($response->successful()) {
- $data = $response->json()['recommendations'] ?? $response->json()['data'] ?? [];
- return [
- 'recommendations' => $data
- ];
- }
- return [
- 'recommendations' => []
- ];
- } catch (\Exception $e) {
- Log::error('Recommend Learning Paths Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'recommendations' => []
- ];
- }
- }
- /**
- * 重新计算掌握度
- */
- public function recalculateMastery(string $studentId, string $kpCode): bool
- {
- try {
- Log::info('LearningAnalytics Request: Recalculate Mastery', [
- 'url' => $this->baseUrl . "/api/v1/mastery/recalculate/{$studentId}",
- 'kp_code' => $kpCode
- ]);
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/mastery/recalculate/{$studentId}", [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode
- ]);
- Log::info('LearningAnalytics Response: Recalculate Mastery', [
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- return $response->successful();
- } catch (\Exception $e) {
- Log::error('Recalculate Mastery Error', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 批量更新技能熟练度
- */
- public function batchUpdateSkillProficiency(string $studentId): bool
- {
- try {
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . "/api/v1/skill/proficiency/student/{$studentId}/batch-update", [
- 'student_id' => $studentId
- ]);
- return $response->successful();
- } catch (\Exception $e) {
- Log::error('Batch Update Skill Proficiency Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 清空学生所有答题数据
- */
- public function clearStudentData(string $studentId): bool
- {
- try {
- // 清空LearningAnalytics中的数据(通过API)
- $response = Http::timeout($this->timeout)
- ->delete($this->baseUrl . "/api/v1/student/{$studentId}/clear");
- if (!$response->successful()) {
- Log::error('Clear LearningAnalytics Data Failed', [
- 'student_id' => $studentId,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- }
- // 清空MySQL中的数据
- $this->clearStudentMySQLData($studentId);
- Log::info('Student Data Cleared Successfully', [
- 'student_id' => $studentId,
- 'api_success' => $response->successful()
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('Clear Student Data Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- // 即使API失败,也要尝试清空本地数据
- try {
- $this->clearStudentMySQLData($studentId);
- return true;
- } catch (\Exception $localError) {
- Log::error('Clear Local Data Also Failed', [
- 'student_id' => $studentId,
- 'error' => $localError->getMessage()
- ]);
- return false;
- }
- }
- }
- /**
- * 清空学生MySQL中的答题数据
- */
- private function clearStudentMySQLData(string $studentId): void
- {
- try {
- // 清空student_exercises表
- DB::table('student_exercises')
- ->where('student_id', $studentId)
- ->delete();
- // 清空student_mastery表
- DB::table('student_mastery')
- ->where('student_id', $studentId)
- ->delete();
- Log::info('Student MySQL Data Cleared', [
- 'student_id' => $studentId
- ]);
- } catch (\Exception $e) {
- Log::error('Clear Student MySQL Data Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- throw $e; // 重新抛出异常,让上层处理
- }
- }
- /**
- * 获取学生列表(供智能出卷使用)
- */
- public function getStudentsList(): array
- {
- try {
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . '/api/v1/students/list');
- if ($response->successful()) {
- return $response->json('data', []);
- }
- // 如果API失败,尝试从MySQL直接读取
- return $this->getStudentsFromMySQL();
- } catch (\Exception $e) {
- Log::error('Get Students List Error', [
- 'error' => $e->getMessage()
- ]);
- // 返回模拟数据
- return [
- ['student_id' => 'stu_001', 'name' => '张三'],
- ['student_id' => 'stu_002', 'name' => '李四'],
- ['student_id' => 'stu_003', 'name' => '王五'],
- ];
- }
- }
- /**
- * 从MySQL获取学生列表
- */
- private function getStudentsFromMySQL(): array
- {
- try {
- return DB::table('students')
- ->select('student_id', 'name')
- ->limit(100)
- ->get()
- ->toArray();
- } catch (\Exception $e) {
- Log::error('Get Students From MySQL Error', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取学生薄弱点列表
- */
- public function getStudentWeaknesses(string $studentId, int $limit = 10): array
- {
- try {
- // 使用正确的API路径:/api/v1/student/{student_id}/weak-points
- $response = Http::timeout($this->timeout)
- ->get($this->baseUrl . "/api/v1/student/{$studentId}/weak-points");
- if ($response->successful()) {
- $data = $response->json('data', []);
- $weakPoints = $data['weak_points'] ?? [];
- // 转换为统一的格式
- return array_map(function ($item) use ($studentId) {
- return [
- 'kp_code' => $item['kp'] ?? '',
- 'kp_name' => $item['kp'] ?? '',
- 'mastery' => $item['mastery_level'] ?? 0,
- 'stability' => 0.5, // 默认稳定性
- 'weakness_level' => 1.0 - ($item['mastery_level'] ?? 0.5),
- 'practice_count' => $item['practice_count'] ?? 0,
- 'success_rate' => $item['success_rate'] ?? 0,
- 'priority' => $item['priority'] ?? '中',
- 'suggested_questions' => $item['suggested_questions'] ?? 0
- ];
- }, $weakPoints);
- }
- Log::warning('LearningAnalytics weaknesses API失败,使用本地MySQL数据', [
- 'student_id' => $studentId,
- 'status' => $response->status()
- ]);
- // API失败时,从MySQL直接查询
- return $this->getStudentWeaknessesFromMySQL($studentId, $limit);
- } catch (\Exception $e) {
- Log::error('Get Student Weaknesses Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- // 发生异常时,返回空数组,让前端可以继续使用默认值
- return [];
- }
- }
- /**
- * 从MySQL获取学生薄弱点
- */
- private function getStudentWeaknessesFromMySQL(string $studentId, int $limit = 10): array
- {
- try {
- $weaknesses = DB::table('student_mastery as sm')
- ->join('knowledge_points as kp', 'sm.kp', '=', 'kp.kp')
- ->where('sm.student_id', $studentId)
- ->where('sm.mastery', '<', 0.7) // 掌握度低于70%视为薄弱点
- ->orderBy('sm.mastery', 'asc')
- ->limit($limit)
- ->select([
- 'sm.kp as kp_code',
- 'kp.cn_name as kp_name',
- 'sm.mastery',
- 'sm.stability'
- ])
- ->get()
- ->toArray();
- return array_map(function ($item) {
- return [
- 'kp_code' => $item->kp_code,
- 'kp_name' => $item->kp_name,
- 'mastery' => (float) $item->mastery,
- 'stability' => (float) $item->stability,
- 'weakness_level' => 1.0 - (float) $item->mastery // 薄弱程度
- ];
- }, $weaknesses);
- } catch (\Exception $e) {
- Log::error('Get Student Weaknesses From MySQL Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 智能出卷:根据学生掌握度智能选择题目
- */
- public function generateIntelligentExam(array $params): array
- {
- $logFile = storage_path('logs/learning_analytics.log');
- $startTime = microtime(true);
- try {
- $studentId = $params['student_id'] ?? null;
- $grade = $params['grade'] ?? null; // 用户选择的年级
- $totalQuestions = $params['total_questions'] ?? 20;
- $kpCodes = $params['kp_codes'] ?? [];
- $skills = $params['skills'] ?? [];
- $questionTypeRatio = $params['question_type_ratio'] ?? [
- '选择题' => 40,
- '填空题' => 30,
- '解答题' => 30,
- ];
- $difficultyRatio = $params['difficulty_ratio'] ?? [
- '基础' => 50,
- '中等' => 35,
- '拔高' => 15,
- ];
- $difficultyLevels = $params['difficulty_levels'] ?? [];
- // 如果用户没有选择任何难度,difficultyLevels 为空数组,表示随机难度
- $logMsg = "=== " . date('Y-m-d H:i:s') . " ===\n";
- $logMsg .= "generateIntelligentExam 开始\n";
- $logMsg .= "student_id: $studentId\n";
- $logMsg .= "total_questions: $totalQuestions\n";
- $logMsg .= "kp_codes: " . json_encode($kpCodes) . "\n";
- $logMsg .= "skills: " . json_encode($skills) . "\n\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- // 1. 如果指定了学生,获取学生的薄弱点
- $weaknessFilter = [];
- if ($studentId) {
- $logMsg = "获取学生薄弱点: $studentId\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- $weaknesses = $this->getStudentWeaknesses($studentId, 20);
- $logMsg = "薄弱点数量: " . count($weaknesses) . "\n";
- $logMsg .= "薄弱点: " . json_encode($weaknesses) . "\n\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- $weaknessFilter = array_column($weaknesses, 'kp_code');
- // 如果用户没有指定知识点,使用学生的薄弱点
- if (empty($kpCodes)) {
- $kpCodes = $weaknessFilter;
- $logMsg = "用户未选择知识点,使用薄弱点作为kp_codes\n";
- $logMsg .= "最终kp_codes: " . json_encode($kpCodes) . "\n\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- }
- }
- $logMsg = "准备调用 getQuestionsFromBank\n";
- $logMsg .= "kp_codes: " . json_encode($kpCodes) . "\n";
- $logMsg .= "skills: " . json_encode($skills) . "\n\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- // 2. 调用题库API获取符合条件的所有题目
- try {
- Log::info('开始调用 getQuestionsFromBank', [
- 'kp_codes_count' => count($kpCodes),
- 'skills_count' => count($skills)
- ]);
- $allQuestions = $this->getQuestionsFromBank($kpCodes, $skills, $studentId, $questionTypeRatio, $difficultyRatio, 200);
- Log::info('getQuestionsFromBank 调用完成', [
- 'questions_count' => count($allQuestions),
- 'is_array' => is_array($allQuestions),
- 'first_question_id' => !empty($allQuestions) ? ($allQuestions[0]['id'] ?? 'N/A') : 'N/A'
- ]);
- $logMsg = "getQuestionsFromBank 返回\n";
- $logMsg .= "questions_count: " . count($allQuestions) . "\n";
- $logMsg .= "耗时: " . round((microtime(true) - $startTime) * 1000, 2) . "ms\n\n";
- file_put_contents($logFile, $logMsg, FILE_APPEND);
- Log::info('getQuestionsFromBank 返回', [
- 'questions_count' => count($allQuestions),
- 'time_ms' => round((microtime(true) - $startTime) * 1000, 2)
- ]);
- } catch (\Exception $e) {
- Log::error('getQuestionsFromBank 调用失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- if (empty($allQuestions)) {
- // 如果指定了知识点但题库为空,给出明确提示
- if (!empty($kpCodes)) {
- $message = '所选知识点 [' . implode(', ', $kpCodes) . '] 在题库中暂无可用题目。您可以:1) 选择其他知识点,2) 点击"生成练习题"按钮先补充题库,或 3) 取消知识点选择让系统随机选题。';
- } else {
- // 没有选择知识点时,从所有题目中选择
- // 如果仍然没有题目,说明题库为空,提示补充题库
- $message = '题库为空,请先添加题目到题库。您可以点击"生成练习题"按钮或手动上传题目。';
- }
- Log::warning('智能出卷失败 - 未找到题目', [
- 'student_id' => $studentId,
- 'selected_kp_codes' => $kpCodes,
- 'kp_codes_count' => count($kpCodes),
- 'message' => $message,
- 'hint' => '如果选择了知识点但题库为空,请检查知识点代码是否正确,或尝试取消知识点选择'
- ]);
- return [
- 'success' => false,
- 'message' => $message,
- 'questions' => []
- ];
- }
- // 3. 根据掌握度对题目进行筛选和排序
- Log::info('开始调用 selectQuestionsByMastery', [
- 'input_count' => count($allQuestions),
- 'target_count' => $totalQuestions
- ]);
- $startTime = microtime(true);
- $selectedQuestions = $this->selectQuestionsByMastery(
- $allQuestions,
- $studentId,
- $totalQuestions,
- $questionTypeRatio,
- $difficultyRatio,
- $difficultyLevels,
- $weaknessFilter
- );
- $selectTime = (microtime(true) - $startTime) * 1000;
- Log::info('题目筛选结果', [
- 'input_count' => count($allQuestions),
- 'selected_count' => count($selectedQuestions),
- 'target_count' => $totalQuestions,
- 'select_time_ms' => round($selectTime, 2)
- ]);
- if (empty($selectedQuestions)) {
- return [
- 'success' => false,
- 'message' => '题目筛选失败',
- 'questions' => []
- ];
- }
- return [
- 'success' => true,
- 'message' => '智能出卷成功',
- 'questions' => $selectedQuestions,
- 'stats' => [
- 'total_selected' => count($selectedQuestions),
- 'source_questions' => count($allQuestions),
- 'weakness_targeted' => $studentId && !empty($weaknessFilter) ? count(array_filter($selectedQuestions, function($q) use ($weaknessFilter) {
- return in_array($q['kp_code'] ?? '', $weaknessFilter);
- })) : 0
- ]
- ];
- } catch (\Exception $e) {
- Log::error('Generate Intelligent Exam Error', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- 'success' => false,
- 'message' => '智能出卷异常: ' . $e->getMessage(),
- 'questions' => []
- ];
- }
- }
- /**
- * 从题库获取题目 - 使用智能选题API,直接根据要求筛选
- */
- private function getQuestionsFromBank(array $kpCodes, array $skills, ?string $studentId, array $questionTypeRatio = [], array $difficultyRatio = [], int $totalNeeded = 100): array
- {
- try {
- // 构建筛选条件
- $filters = [];
- // 知识点筛选
- if (!empty($kpCodes)) {
- $filters['kp_codes'] = $kpCodes;
- }
- // 技能筛选
- if (!empty($skills)) {
- $filters['skills'] = $skills;
- }
- // 题型配比
- if (!empty($questionTypeRatio)) {
- $filters['question_type_ratio'] = $questionTypeRatio;
- }
- // 难度配比
- if (!empty($difficultyRatio)) {
- $filters['difficulty_ratio'] = $difficultyRatio;
- }
- // 过滤学生做过的题目
- if ($studentId) {
- $filters['exclude_student_questions'] = $studentId;
- }
- // 从容器动态获取实例
- if (!$this->questionBankService) {
- $this->questionBankService = app(QuestionBankService::class);
- }
- // 调用智能选题API - 直接获取符合要求的题目
- $questions = $this->questionBankService->selectQuestionsForExam($totalNeeded, $filters);
- if (!empty($questions)) {
- Log::info('从题库获取到题目,开始过滤解题思路', [
- 'total_from_bank' => count($questions),
- 'filters' => $filters
- ]);
- $filterStartTime = microtime(true);
- $questionsWithSolution = [];
- $noSolutionCount = 0;
- foreach ($questions as $index => $q) {
- try {
- $solution = $q['solution'] ?? '';
- // 处理 solution 可能是数组的情况
- if (is_array($solution)) {
- $solution = json_encode($solution, JSON_UNESCAPED_UNICODE);
- }
- if (!empty(trim($solution))) {
- $questionsWithSolution[] = $q;
- } else {
- $noSolutionCount++;
- }
- } catch (\Exception $e) {
- Log::error('过滤解题思路时出错', [
- 'question_index' => $index,
- 'error' => $e->getMessage()
- ]);
- }
- }
- $filterTime = (microtime(true) - $filterStartTime) * 1000;
- $hasSolutionCount = count($questionsWithSolution);
- Log::info('从题库智能获取题目', [
- 'total_from_bank' => count($questions),
- 'has_solution' => $hasSolutionCount,
- 'no_solution' => $noSolutionCount,
- 'filter_time_ms' => round($filterTime, 2),
- 'filters' => $filters
- ]);
- if ($hasSolutionCount > 0) {
- Log::info('返回有解题思路的题目', [
- 'count' => $hasSolutionCount
- ]);
- return array_values($questionsWithSolution);
- } else {
- Log::warning('所有题目都没有解题思路,返回空数组', [
- 'total_questions' => count($questions)
- ]);
- return [];
- }
- }
- Log::warning('智能选题返回空结果', [
- 'filters' => $filters
- ]);
- return [];
- } catch (\Exception $e) {
- Log::error('智能选题异常', [
- 'error' => $e->getMessage()
- ]);
- }
- return [];
- }
- /**
- * 根据学生掌握度筛选题目
- */
- private function selectQuestionsByMastery(
- array $questions,
- ?string $studentId,
- int $totalQuestions,
- array $questionTypeRatio,
- array $difficultyRatio,
- array $difficultyLevels,
- array $weaknessFilter
- ): array {
- Log::info('selectQuestionsByMastery 开始', [
- 'question_count' => count($questions),
- 'student_id' => $studentId,
- 'total_questions' => $totalQuestions
- ]);
- // 如果未选择难度,则不过滤(随机生成所有难度)
- if (empty($difficultyLevels)) {
- Log::info('用户未选择难度,将随机生成所有难度的题目');
- // 不过滤任何题目,保留所有难度
- } else {
- // 按难度筛掉不在选择范围内的题目
- $questions = array_values(array_filter($questions, function ($q) use ($difficultyLevels) {
- $d = $q['difficulty'] ?? null;
- if ($d === null) return true; // 无难度信息则保留
- $level = $this->mapDifficultyLevel((float)$d);
- return in_array($level, $difficultyLevels);
- }));
- }
- Log::info('难度筛选完成', [
- 'after_filter_count' => count($questions)
- ]);
- // 1. 按知识点分组
- Log::info('开始按知识点分组', [
- 'question_count' => count($questions)
- ]);
- $groupStartTime = microtime(true);
- $questionsByKp = [];
- foreach ($questions as $question) {
- $kpCode = $question['kp_code'] ?? '';
- if (!isset($questionsByKp[$kpCode])) {
- $questionsByKp[$kpCode] = [];
- }
- $questionsByKp[$kpCode][] = $question;
- }
- $groupTime = (microtime(true) - $groupStartTime) * 1000;
- Log::info('按知识点分组完成', [
- 'kp_count' => count($questionsByKp),
- 'group_time_ms' => round($groupTime, 2)
- ]);
- // 2. 为每个知识点计算权重
- $kpWeights = [];
- $kpCodes = array_keys($questionsByKp);
- Log::info('开始计算知识点权重', [
- 'kp_count' => count($kpCodes),
- 'student_id' => $studentId
- ]);
- $startTime = microtime(true);
- $allMastery = [];
- if ($studentId) {
- // 批量获取所有知识点的掌握度(一次查询)
- $masteryStart = microtime(true);
- try {
- $masteryRecords = DB::table('student_mastery')
- ->where('student_id', $studentId)
- ->whereIn('kp', $kpCodes)
- ->pluck('mastery', 'kp')
- ->all();
- Log::debug('批量获取掌握度', [
- 'student_id' => $studentId,
- 'kp_count' => count($kpCodes),
- 'found_count' => count($masteryRecords),
- 'time_ms' => round((microtime(true) - $masteryStart) * 1000, 2)
- ]);
- $allMastery = $masteryRecords;
- } catch (\Exception $e) {
- Log::warning('批量获取掌握度失败,将使用默认值', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- }
- }
- foreach ($kpCodes as $kpCode) {
- if ($studentId) {
- $mastery = $allMastery[$kpCode] ?? 0.5; // 默认0.5(中等掌握度)
- // 薄弱点权重更高
- if (in_array($kpCode, $weaknessFilter)) {
- $kpWeights[$kpCode] = 2.0; // 薄弱点权重翻倍
- } else {
- // 掌握度越低,权重越高
- $kpWeights[$kpCode] = 1.0 + (1.0 - $mastery) * 1.5;
- }
- Log::debug('计算知识点权重', [
- 'kp_code' => $kpCode,
- 'mastery' => $mastery,
- 'weight' => $kpWeights[$kpCode]
- ]);
- } else {
- $kpWeights[$kpCode] = 1.0; // 未指定学生时平均分配
- }
- }
- $totalWeightTime = (microtime(true) - $startTime) * 1000;
- Log::info('知识点权重计算完成', [
- 'total_kp_count' => count($kpCodes),
- 'total_weight_time_ms' => round($totalWeightTime, 2),
- 'avg_time_per_kp_ms' => count($kpCodes) > 0 ? round($totalWeightTime / count($kpCodes), 2) : 0
- ]);
- // 3. 按权重分配题目数量
- $totalWeight = array_sum($kpWeights);
- $selectedQuestions = [];
- foreach ($questionsByKp as $kpCode => $kpQuestions) {
- // 计算该知识点应该选择的题目数
- $kpQuestionCount = max(1, round(($totalQuestions * $kpWeights[$kpCode]) / $totalWeight));
- // 打乱题目顺序(避免固定模式)
- shuffle($kpQuestions);
- // 选择题目
- $selectedFromKp = array_slice($kpQuestions, 0, $kpQuestionCount);
- $selectedQuestions = array_merge($selectedQuestions, $selectedFromKp);
- }
- // 4. 如果题目过多,按权重排序后截取
- if (count($selectedQuestions) > $totalQuestions) {
- Log::info('开始按权重排序题目', [
- 'before_sort_count' => count($selectedQuestions),
- 'target_count' => $totalQuestions
- ]);
- $startTime = microtime(true);
- usort($selectedQuestions, function ($a, $b) use ($kpWeights) {
- $weightA = $kpWeights[$a['kp_code']] ?? 1.0;
- $weightB = $kpWeights[$b['kp_code']] ?? 1.0;
- return $weightB <=> $weightA;
- });
- $sortTime = (microtime(true) - $startTime) * 1000;
- Log::info('权重排序完成', [
- 'sort_time_ms' => round($sortTime, 2),
- 'after_sort_count' => count($selectedQuestions)
- ]);
- $selectedQuestions = array_slice($selectedQuestions, 0, $totalQuestions);
- }
- Log::info('开始题型配比调整', [
- 'input_count' => count($selectedQuestions),
- 'target_count' => $totalQuestions
- ]);
- // 5. 按题型和难度进行微调
- return $this->adjustQuestionsByRatio($selectedQuestions, $questionTypeRatio, $difficultyRatio, $totalQuestions);
- }
- /**
- * 获取学生对特定知识点的掌握度
- */
- private function getStudentKpMastery(string $studentId, string $kpCode): float
- {
- try {
- $mastery = DB::table('student_mastery')
- ->where('student_id', $studentId)
- ->where('kp', $kpCode)
- ->value('mastery');
- return $mastery ? (float) $mastery : 0.5; // 默认0.5(中等掌握度)
- } catch (\Exception $e) {
- Log::error('Get Student Kp Mastery Error', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- 'error' => $e->getMessage()
- ]);
- return 0.5;
- }
- }
- /**
- * 根据题型和难度配比调整题目
- */
- private function adjustQuestionsByRatio(array $questions, array $typeRatio, array $difficultyRatio, int $targetCount): array
- {
- Log::info('开始题型配比调整', [
- 'input_questions' => count($questions),
- 'target_count' => $targetCount,
- 'type_ratio' => $typeRatio
- ]);
- // 缓存题目类型,避免重复计算
- $questionTypeCache = [];
- // 按题型分桶
- $buckets = [
- 'choice' => [],
- 'fill' => [],
- 'answer' => [],
- ];
- foreach ($questions as $q) {
- $qid = $q['id'] ?? $q['question_id'] ?? null;
- if ($qid && isset($questionTypeCache[$qid])) {
- $type = $questionTypeCache[$qid];
- } else {
- $type = $this->determineQuestionType($q);
- if ($qid) {
- $questionTypeCache[$qid] = $type;
- }
- }
- if (!isset($buckets[$type])) {
- $type = 'answer';
- }
- $buckets[$type][] = $q;
- }
- // 计算目标数(四舍五入,比例>0 则至少 1 道),并校正总数
- $targetCount = min($targetCount, count($questions));
- $targets = $this->computeTypeTargets($targetCount, $typeRatio);
- Log::info('题型配比调整前桶统计', [
- 'target_count' => $targetCount,
- 'targets' => $targets,
- 'bucket_counts' => [
- 'choice' => count($buckets['choice']),
- 'fill' => count($buckets['fill']),
- 'answer' => count($buckets['answer']),
- ],
- 'raw_ratio' => $typeRatio,
- ]);
- // 随机打乱桶
- foreach ($buckets as $k => $v) {
- if (!empty($v)) {
- shuffle($v);
- $buckets[$k] = $v;
- }
- }
- $selected = [];
- $selectedIds = [];
- // 按目标数依次取题
- foreach (['choice', 'fill', 'answer'] as $typeKey) {
- $need = $targets[$typeKey] ?? 0;
- if ($need <= 0 || empty($buckets[$typeKey])) {
- continue;
- }
- $take = min($need, count($buckets[$typeKey]));
- $slice = array_slice($buckets[$typeKey], 0, $take);
- foreach ($slice as $q) {
- $id = $q['id'] ?? $q['question_id'] ?? spl_object_id((object)$q);
- if (isset($selectedIds[$id])) {
- continue;
- }
- $selected[] = $q;
- $selectedIds[$id] = true;
- }
- }
- // 不足则从剩余题中补齐
- if (count($selected) < $targetCount) {
- $remaining = [];
- foreach ($buckets as $v) {
- foreach ($v as $q) {
- $id = $q['id'] ?? $q['question_id'] ?? spl_object_id((object)$q);
- if (!isset($selectedIds[$id])) {
- $remaining[] = $q;
- }
- }
- }
- shuffle($remaining);
- $needMore = $targetCount - count($selected);
- $selected = array_merge($selected, array_slice($remaining, 0, $needMore));
- }
- // 截断至目标数
- $selected = array_slice($selected, 0, $targetCount);
- // 使用缓存统计题型分布
- $selectedCounts = ['choice' => 0, 'fill' => 0, 'answer' => 0];
- foreach ($selected as $q) {
- $qid = $q['id'] ?? $q['question_id'] ?? null;
- if ($qid && isset($questionTypeCache[$qid])) {
- $type = $questionTypeCache[$qid];
- } else {
- $type = $this->determineQuestionType($q);
- }
- if (isset($selectedCounts[$type])) {
- $selectedCounts[$type]++;
- }
- }
- Log::info('题型配比调整完成', [
- 'target_count' => $targetCount,
- 'targets' => $targets,
- 'selected_counts' => $selectedCounts,
- 'final_selected_count' => count($selected)
- ]);
- return $selected;
- }
- private function determineQuestionType(array $q): string
- {
- // 优先根据题目内容判断(而不是数据库字段)
- $stem = $q['stem'] ?? $q['content'] ?? '';
- // 处理 stem 可能是数组的情况
- if (is_array($stem)) {
- $stem = json_encode($stem, JSON_UNESCAPED_UNICODE);
- }
- $tags = $q['tags'] ?? '';
- // 处理 tags 可能是数组的情况
- if (is_array($tags)) {
- $tags = json_encode($tags, JSON_UNESCAPED_UNICODE);
- }
- $skills = $q['skills'] ?? [];
- // 1. 根据题干内容判断 - 选择题特征:必须包含 A. B. C. D. 选项(至少2个)
- if (is_string($stem)) {
- // 选择题特征:必须包含 A. B. C. D. 四个选项(至少2个)
- $hasOptionA = preg_match('/\bA\s*[\.\、\:]/', $stem) || preg_match('/\(A\)/', $stem) || preg_match('/^A[\.\s]/', $stem);
- $hasOptionB = preg_match('/\bB\s*[\.\、\:]/', $stem) || preg_match('/\(B\)/', $stem) || preg_match('/^B[\.\s]/', $stem);
- $hasOptionC = preg_match('/\bC\s*[\.\、\:]/', $stem) || preg_match('/\(C\)/', $stem) || preg_match('/^C[\.\s]/', $stem);
- $hasOptionD = preg_match('/\bD\s*[\.\、\:]/', $stem) || preg_match('/\(D\)/', $stem) || preg_match('/^D[\.\s]/', $stem);
- $hasOptionE = preg_match('/\bE\s*[\.\、\:]/', $stem) || preg_match('/\(E\)/', $stem) || preg_match('/^E[\.\s]/', $stem);
- // 至少有2个选项就认为是选择题(降低阈值)
- $optionCount = ($hasOptionA ? 1 : 0) + ($hasOptionB ? 1 : 0) + ($hasOptionC ? 1 : 0) + ($hasOptionD ? 1 : 0) + ($hasOptionE ? 1 : 0);
- if ($optionCount >= 2) {
- return 'choice';
- }
- // 检查是否有"( )"或"( )"括号,这通常是选择题的标志
- if (preg_match('/(\s*)|\(\s*\)/', $stem) && (strpos($stem, 'A.') !== false || strpos($stem, 'B.') !== false || strpos($stem, 'C.') !== false || strpos($stem, 'D.') !== false)) {
- return 'choice';
- }
- }
- // 2. 根据技能点判断
- if (is_array($skills) && !empty($skills)) {
- // 过滤非字符串元素,避免 implode 报错
- $skillsFiltered = array_filter($skills, 'is_string');
- if (!empty($skillsFiltered)) {
- $skillsStr = implode(',', $skillsFiltered);
- if (strpos($skillsStr, '选择题') !== false) return 'choice';
- if (strpos($skillsStr, '填空题') !== false) return 'fill';
- if (strpos($skillsStr, '解答题') !== false) return 'answer';
- }
- }
- // 3. 根据题目已有类型字段判断(作为后备)
- $typeField = $q['question_type'] ?? $q['type'] ?? '';
- if (is_string($typeField)) {
- $t = strtolower($typeField);
- if (in_array($t, ['choice', 'single_choice', 'multiple_choice', '选择题', 'choice', 'single_choice', 'multiple_choice'])) {
- return 'choice';
- }
- if (in_array($t, ['fill', 'blank', 'fill_blank', 'fill_in_the_blank', '填空题'])) {
- return 'fill';
- }
- if (in_array($t, ['answer', 'calculation', 'word_problem', 'proof', '解答题'])) {
- return 'answer';
- }
- }
- // 4. 根据标签判断
- if (is_string($tags)) {
- if (strpos($tags, '选择') !== false || strpos($tags, '选择题') !== false) {
- return 'choice';
- }
- if (strpos($tags, '填空') !== false || strpos($tags, '填空题') !== false) {
- return 'fill';
- }
- if (strpos($tags, '解答') !== false || strpos($tags, '简答') !== false || strpos($tags, '证明') !== false) {
- return 'answer';
- }
- }
- // 5. 根据options字段判断
- if (!empty($q['options']) && is_array($q['options'])) {
- return 'choice';
- }
- // 6. 填空题特征:连续下划线或明显的填空括号
- if (is_string($stem)) {
- // 检查填空题特征:连续下划线
- if (preg_match('/_{3,}/', $stem) || strpos($stem, '____') !== false) {
- return 'fill';
- }
- // 空括号填空
- if (preg_match('/(\s*)/', $stem) || preg_match('/\(\s*\)/', $stem)) {
- return 'fill';
- }
- }
- // 7. 根据题干内容关键词判断
- if (is_string($stem)) {
- // 有证明、解答、计算、求证等关键词的是解答题
- if (preg_match('/(证明|求证|解方程|计算:|求解|推导|说明理由)/', $stem)) {
- return 'answer';
- }
- }
- // 默认是解答题(更安全的默认值)
- return 'answer';
- }
- private function mapDifficultyLevel(float $d): string
- {
- if ($d <= 0.4) {
- return '基础';
- }
- if ($d <= 0.7) {
- return '中等';
- }
- return '拔高';
- }
- private function computeTypeTargets(int $targetCount, array $questionTypeRatio): array
- {
- $map = [
- '选择题' => 'choice',
- '填空题' => 'fill',
- '解答题' => 'answer',
- ];
- $targets = ['choice' => 0, 'fill' => 0, 'answer' => 0];
- foreach ($questionTypeRatio as $label => $ratio) {
- $key = $map[$label] ?? null;
- if (!$key) {
- continue;
- }
- $cnt = (int) round($targetCount * ($ratio / 100));
- if ($ratio > 0 && $cnt < 1) {
- $cnt = 1;
- }
- $targets[$key] = $cnt;
- }
- $sum = array_sum($targets);
- if ($sum === 0) {
- $targets['answer'] = $targetCount;
- return $targets;
- }
- while ($sum > $targetCount) {
- arsort($targets);
- foreach ($targets as $k => $v) {
- if ($v > 1) {
- $targets[$k]--;
- $sum--;
- break;
- }
- }
- }
- if ($sum < $targetCount) {
- $ratioByKey = [
- 'choice' => $questionTypeRatio['选择题'] ?? 0,
- 'fill' => $questionTypeRatio['填空题'] ?? 0,
- 'answer' => $questionTypeRatio['解答题'] ?? 0,
- ];
- while ($sum < $targetCount) {
- arsort($ratioByKey);
- $k = array_key_first($ratioByKey);
- $targets[$k]++;
- $sum++;
- }
- }
- return $targets;
- }
- /**
- * 提交手动评分结果到 LearningAnalytics
- *
- * @param array $data 包含 student_id, paper_id, grades 的数组
- * @return array
- */
- public function submitManualGrading(array $data): array
- {
- try {
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . '/api/ocr/analyze', [
- 'student_id' => $data['student_id'],
- 'paper_id' => $data['paper_id'],
- 'answers' => $data['grades'],
- ]);
- if ($response->successful()) {
- Log::info('Manual grading submitted successfully', [
- 'student_id' => $data['student_id'],
- 'paper_id' => $data['paper_id'],
- 'question_count' => count($data['grades'])
- ]);
- return $response->json();
- }
- Log::error('Submit Manual Grading Error', [
- 'data' => $data,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'error' => true,
- 'message' => 'Failed to submit manual grading'
- ];
- } catch (\Exception $e) {
- Log::error('Submit Manual Grading Exception', [
- 'error' => $e->getMessage(),
- 'data' => $data
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 分析学生作答结果
- *
- * @param array $data 包含 paper_id, student_id, answers 等
- * @return array
- */
- public function analyzeStudentAnswers(array $data): array
- {
- try {
- $response = Http::timeout($this->timeout)
- ->post($this->baseUrl . '/api/v1/analysis/submit-answers', [
- 'paper_id' => $data['paper_id'],
- 'student_id' => $data['student_id'],
- 'answers' => $data['answers'],
- 'submit_time' => $data['submit_time'] ?? now()->toISOString(),
- ]);
- if ($response->successful()) {
- Log::info('Student answers analyzed successfully', [
- 'student_id' => $data['student_id'],
- 'paper_id' => $data['paper_id'],
- 'answer_count' => count($data['answers'])
- ]);
- return [
- 'success' => true,
- 'data' => $response->json()
- ];
- }
- Log::error('Analyze Student Answers Error', [
- 'data' => $data,
- 'status' => $response->status(),
- 'response' => $response->body()
- ]);
- return [
- 'success' => false,
- 'message' => '分析失败:' . $response->body()
- ];
- } catch (\Exception $e) {
- Log::error('Analyze Student Answers Exception', [
- 'error' => $e->getMessage(),
- 'data' => $data
- ]);
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- }
|