| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- use App\Services\ExamTypeStrategy;
- use App\Services\QuestionExpansionService;
- /**
- * 学习分析服务(本地化重构版)
- *
- * 原先调用外部 LearningAnalytics API (localhost:5016)
- * 现已重构为使用本地服务:MasteryCalculator、KnowledgeMasteryService 等
- */
- class LearningAnalyticsService
- {
- protected string $baseUrl;
- protected int $timeout = 10;
- protected ?QuestionExpansionService $questionExpansionService;
- protected ?MasteryCalculator $masteryCalculator;
- protected ?LocalAIAnalysisService $aiAnalysisService;
- public function __construct(
- ?QuestionExpansionService $questionExpansionService = null,
- ?MasteryCalculator $masteryCalculator = null,
- ?LocalAIAnalysisService $aiAnalysisService = null
- ) {
- // 保留baseUrl用于兼容,但不再实际调用
- $this->baseUrl = config('services.learning_analytics.url', env('LEARNING_ANALYTICS_API_BASE', 'http://localhost:5016'));
- $this->questionExpansionService = $questionExpansionService;
- $this->masteryCalculator = $masteryCalculator ?? app(MasteryCalculator::class);
- $this->aiAnalysisService = $aiAnalysisService ?? app(LocalAIAnalysisService::class);
- }
- /**
- * 获取学生掌握度(本地化)
- */
- public function getStudentMastery(string $studentId, string $kpCode = null): array
- {
- try {
- Log::info('LearningAnalyticsService: 获取学生掌握度 (本地)', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode
- ]);
- if ($kpCode) {
- // 获取特定知识点掌握度
- $result = $this->masteryCalculator->calculateMasteryLevel($studentId, $kpCode);
- return [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- 'mastery_level' => $result['mastery'],
- 'confidence' => $result['confidence'],
- 'trend' => $result['trend'],
- 'total_attempts' => $result['total_attempts'],
- 'correct_attempts' => $result['correct_attempts'],
- ];
- }
- // 获取全部知识点掌握度概览
- $overview = $this->masteryCalculator->getStudentMasteryOverview($studentId);
- return [
- 'student_id' => $studentId,
- 'total_knowledge_points' => $overview['total_knowledge_points'],
- 'average_mastery' => $overview['average_mastery_level'],
- 'mastered_count' => $overview['mastered_knowledge_points'],
- 'good_count' => $overview['good_knowledge_points'],
- 'weak_count' => $overview['weak_knowledge_points'],
- 'weak_list' => $overview['weak_knowledge_points_list'],
- 'details' => $overview['details'],
- ];
- } catch (\Exception $e) {
- Log::error('LearningAnalyticsService: 获取掌握度失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage(),
- ]);
- return [
- 'error' => true,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 更新学生掌握度(本地化)
- */
- public function updateMastery(array $data): array
- {
- try {
- $studentId = $data['student_id'];
- $kpCode = $data['kp_code'];
- $isCorrect = $data['is_correct'] ?? false;
- $difficulty = $data['difficulty_level'] ?? 0.5;
- Log::info('LearningAnalyticsService: 更新掌握度 (本地)', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- ]);
- // 获取当前掌握度
- $currentMastery = DB::table('student_knowledge_mastery')
- ->where('student_id', $studentId)
- ->where('kp_code', $kpCode)
- ->value('mastery_level') ?? 0.5;
- // 使用LocalAIAnalysisService更新掌握度
- $result = $this->aiAnalysisService->updateMastery(
- $studentId,
- $kpCode,
- $currentMastery,
- $isCorrect,
- $difficulty
- );
- return [
- 'success' => true,
- 'data' => $result
- ];
- } catch (\Exception $e) {
- Log::error('LearningAnalyticsService: 更新掌握度失败', [
- 'data' => $data,
- 'error' => $e->getMessage(),
- ]);
- 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
- {
- Log::warning('submitOCRAnalysis 已停用:分析项目已下线', [
- 'student_id' => $data['student_id'] ?? 'unknown',
- 'exam_id' => $data['exam_id'] ?? 'unknown',
- ]);
- return [
- 'success' => false,
- 'message' => 'analysis_api_disabled',
- ];
- }
- /**
- * 获取分析结果详情
- */
- public function getAnalysisResult(string $analysisId): array
- {
- Log::warning('getAnalysisResult 已停用:分析项目已下线', [
- 'analysis_id' => $analysisId,
- ]);
- return [
- 'success' => false,
- 'message' => 'analysis_api_disabled',
- ];
- }
- /**
- * 检查服务健康状态(本地化)
- */
- public function checkHealth(): bool
- {
- // 检查本地服务是否可用
- try {
- $this->masteryCalculator->getStudentMasteryOverview('test');
- return true;
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 获取学生掌握度概览(本地化)
- */
- public function getStudentMasteryOverview(string $studentId): array
- {
- try {
- // 直接使用MasteryCalculator
- $overview = $this->masteryCalculator->getStudentMasteryOverview($studentId);
- return [
- 'total_knowledge_points' => $overview['total_knowledge_points'],
- 'average_mastery_level' => $overview['average_mastery_level'],
- 'mastered_knowledge_points' => $overview['mastered_knowledge_points'],
- 'good_knowledge_points' => $overview['good_knowledge_points'],
- 'weak_knowledge_points' => $overview['weak_knowledge_points'],
- 'weak_knowledge_points_list' => $overview['weak_knowledge_points_list'],
- 'details' => $overview['details']
- ];
- } 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' => []
- ];
- }
- }
- /**
- * 获取学生预测数据(本地化 - 已停用外部API)
- */
- public function getStudentPredictions(string $studentId, int $count = 5): array
- {
- // 外部API已停用,基于本地数据生成预测
- try {
- Log::info('LearningAnalyticsService: 获取学生预测 (本地)', [
- 'student_id' => $studentId,
- ]);
- $overview = $this->masteryCalculator->getStudentMasteryOverview($studentId);
- $avgMastery = $overview['average_mastery_level'] ?? 0;
- // 基于掌握度生成简单预测
- $predictions = [];
- if ($avgMastery > 0) {
- $predictions[] = [
- 'type' => 'score_improvement',
- 'current_level' => round($avgMastery * 100),
- 'predicted_improvement' => rand(5, 15),
- 'confidence' => 0.75,
- ];
- }
- return [
- 'predictions' => $predictions
- ];
- } catch (\Exception $e) {
- Log::error('Get Student Predictions Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'predictions' => []
- ];
- }
- }
- /**
- * 获取学生学习路径(本地化 - 已停用外部API)
- */
- public function getStudentLearningPaths(string $studentId, int $count = 3): array
- {
- // 外部API已停用,基于薄弱点生成学习路径
- try {
- Log::info('LearningAnalyticsService: 获取学习路径 (本地)', [
- 'student_id' => $studentId,
- ]);
- $overview = $this->masteryCalculator->getStudentMasteryOverview($studentId);
- $weakPoints = $overview['weak_knowledge_points_list'] ?? [];
- $paths = [];
- foreach (array_slice($weakPoints, 0, $count) as $weak) {
- $paths[] = [
- 'kp_code' => $weak->kp_code ?? $weak['kp_code'] ?? '',
- 'current_mastery' => $weak->mastery_level ?? $weak['mastery_level'] ?? 0,
- 'target_mastery' => 0.85,
- 'recommended_practice' => 5,
- ];
- }
- return [
- 'paths' => $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('LearningAnalyticsService: 推荐学习路径 (本地)', [
- 'student_id' => $studentId,
- ]);
- $overview = $this->masteryCalculator->getStudentMasteryOverview($studentId);
- $weakPoints = $overview['weak_knowledge_points_list'] ?? [];
- $recommendations = [];
- foreach (array_slice($weakPoints, 0, $count) as $weak) {
- $kpCode = is_object($weak) ? $weak->kp_code : ($weak['kp_code'] ?? '');
- $masteryLevel = is_object($weak) ? $weak->mastery_level : ($weak['mastery_level'] ?? 0);
- $recommendations[] = [
- 'kp_code' => $kpCode,
- 'kp_name' => $this->getKnowledgePointName($kpCode),
- 'current_mastery' => $masteryLevel,
- 'target_mastery' => 0.85,
- 'priority' => 1 - $masteryLevel,
- 'recommended_practice_count' => max(3, intval((0.85 - $masteryLevel) * 10)),
- 'estimated_time_minutes' => max(15, intval((0.85 - $masteryLevel) * 60)),
- ];
- }
- return [
- 'recommendations' => $recommendations
- ];
- } catch (\Exception $e) {
- Log::error('Recommend Learning Paths Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'recommendations' => []
- ];
- }
- }
- /**
- * 获取知识点名称
- */
- private function getKnowledgePointName(string $kpCode): string
- {
- try {
- $kp = DB::table('knowledge_points')
- ->where('kp_code', $kpCode)
- ->value('name');
- return $kp ?? $kpCode;
- } catch (\Exception $e) {
- return $kpCode;
- }
- }
- /**
- * 重新计算掌握度(本地化)
- */
- public function recalculateMastery(string $studentId, string $kpCode): bool
- {
- try {
- Log::info('LearningAnalyticsService: 重新计算掌握度 (本地)', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- ]);
- // 使用MasteryCalculator重新计算
- $result = $this->masteryCalculator->calculateMasteryLevel($studentId, $kpCode);
- // 更新到数据库
- DB::table('student_knowledge_mastery')
- ->updateOrInsert(
- ['student_id' => $studentId, 'kp_code' => $kpCode],
- [
- 'mastery_level' => $result['mastery'],
- 'confidence_level' => $result['confidence'],
- 'total_attempts' => $result['total_attempts'],
- 'correct_attempts' => $result['correct_attempts'],
- 'mastery_trend' => $result['trend'],
- 'last_mastery_update' => now(),
- 'updated_at' => now(),
- ]
- );
- return true;
- } 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 {
- Log::info('LearningAnalyticsService: 批量更新技能熟练度 (本地)', [
- 'student_id' => $studentId,
- ]);
- // 获取学生所有知识点
- $kpCodes = DB::table('student_knowledge_mastery')
- ->where('student_id', $studentId)
- ->pluck('kp_code')
- ->toArray();
- // 批量更新
- $this->masteryCalculator->batchUpdateMastery($studentId, $kpCodes);
- return true;
- } 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 {
- Log::info('LearningAnalyticsService: 清空学生数据 (本地)', [
- 'student_id' => $studentId,
- ]);
- // 清空MySQL中的数据
- $this->clearStudentMySQLData($studentId);
- Log::info('Student Data Cleared Successfully', [
- 'student_id' => $studentId,
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('Clear Student Data Error', [
- 'student_id' => $studentId,
- 'error' => $e->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 [];
- }
- }
- /**
- * 获取学生薄弱点列表
- * 策略:MySQL作为权威数据源,LearningAnalytics API仅作为辅助/缓存
- */
- public function getStudentWeaknesses(string $studentId, int $limit = 10): array
- {
- try {
- // 从本地MySQL数据库获取学生薄弱点
- Log::info('从本地MySQL数据库获取学生薄弱点', [
- 'student_id' => $studentId,
- 'limit' => $limit
- ]);
- $weaknesses = $this->getStudentWeaknessesFromMySQL($studentId, $limit);
- if (!empty($weaknesses)) {
- Log::info('从本地数据库获取到薄弱点数据', [
- 'student_id' => $studentId,
- 'count' => count($weaknesses)
- ]);
- return $weaknesses;
- }
- Log::warning('本地数据库中无该学生薄弱点数据', [
- 'student_id' => $studentId
- ]);
- return [];
- } 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 {
- // 优先从 student_knowledge_mastery 表读取(更完整的掌握度数据)
- $weaknesses = DB::table('student_knowledge_mastery as skm')
- ->where('skm.student_id', $studentId)
- ->where('skm.mastery_level', '<', 0.7) // 掌握度低于70%视为薄弱点
- ->orderBy('skm.mastery_level', 'asc')
- ->limit($limit)
- ->select([
- 'skm.kp_code',
- 'skm.mastery_level',
- 'skm.total_attempts',
- 'skm.correct_attempts',
- 'skm.incorrect_attempts',
- 'skm.confidence_level',
- 'skm.mastery_trend'
- ])
- ->get()
- ->toArray();
- // 如果student_knowledge_mastery表没有数据,尝试从student_mastery表读取
- if (empty($weaknesses)) {
- Log::info('student_knowledge_mastery表无数据,尝试从student_mastery表读取', [
- 'student_id' => $studentId
- ]);
- $weaknesses = DB::table('student_mastery as sm')
- ->leftJoin('knowledge_points as kp', 'sm.kp', '=', 'kp.kp_code')
- ->where('sm.student_id', $studentId)
- ->where('sm.mastery', '<', 0.7) // 掌握度低于70%视为薄弱点
- ->orderBy('sm.mastery', 'asc')
- ->limit($limit)
- ->select([
- 'sm.kp as kp_code',
- 'kp.name as kp_name',
- 'sm.mastery',
- 'sm.attempts',
- 'sm.correct'
- ])
- ->get()
- ->toArray();
- // 转换为统一格式
- return array_map(function ($item) {
- $mastery = (float) ($item->mastery ?? 0);
- $attempts = (int) ($item->attempts ?? 0);
- $correct = (int) ($item->correct ?? 0);
- return [
- 'kp_code' => $item->kp_code,
- 'kp_name' => $item->kp_name ?? $item->kp_code,
- 'mastery' => $mastery,
- 'stability' => 0.5, // 默认稳定性
- 'weakness_level' => 1.0 - $mastery, // 薄弱程度
- 'practice_count' => $attempts,
- 'success_rate' => $attempts > 0 ? ($correct / $attempts) : 0,
- 'priority' => $mastery < 0.3 ? '高' : ($mastery < 0.5 ? '中' : '低'),
- 'suggested_questions' => max(5, (int)((0.7 - $mastery) * 20)) // 掌握度越低,建议题目越多
- ];
- }, $weaknesses);
- }
- // 转换student_knowledge_mastery表的数据格式
- return array_map(function ($item) {
- $mastery = (float) ($item->mastery_level ?? 0);
- $totalAttempts = (int) ($item->total_attempts ?? 0);
- $correctAttempts = (int) ($item->correct_attempts ?? 0);
- $incorrectAttempts = (int) ($item->incorrect_attempts ?? 0);
- $confidence = (float) ($item->confidence_level ?? 0.5);
- $trend = $item->mastery_trend ?? 'stable';
- // 计算成功率
- $successRate = $totalAttempts > 0 ? ($correctAttempts / $totalAttempts) : 0;
- // 确定优先级
- $priority = '中';
- if ($mastery < 0.3) {
- $priority = '高';
- } elseif ($mastery < 0.5) {
- $priority = '中';
- } else {
- $priority = '低';
- }
- return [
- 'kp_code' => $item->kp_code,
- 'kp_name' => $item->kp_code, // 如果没有中文名,使用代码作为名称
- 'mastery' => $mastery,
- 'stability' => $confidence,
- 'weakness_level' => 1.0 - $mastery, // 薄弱程度
- 'practice_count' => $totalAttempts,
- 'success_rate' => $successRate,
- 'priority' => $priority,
- 'suggested_questions' => max(5, (int)((0.7 - $mastery) * 20)), // 掌握度越低,建议题目越多
- 'trend' => $trend,
- 'correct_attempts' => $correctAttempts,
- 'incorrect_attempts' => $incorrectAttempts
- ];
- }, $weaknesses);
- } catch (\Exception $e) {
- Log::error('Get Student Weaknesses From MySQL Error', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- /**
- * 智能出卷:根据学生掌握度智能选择题目
- */
- public function generateIntelligentExam(array $params): array
- {
- $startTime = microtime(true);
- try {
- // 应用组卷类型策略
- $assembleType = (int) ($params['assemble_type'] ?? 4); // 默认为通用类型(4)
- $examTypeLegacy = $params['exam_type'] ?? 'general'; // 兼容旧版参数
- Log::info('LearningAnalyticsService: 检查组卷策略', [
- 'assemble_type' => $assembleType,
- 'exam_type_legacy' => $examTypeLegacy,
- 'has_question_expansion_service' => !empty($this->questionExpansionService)
- ]);
- // 如果有 assemble_type 参数,优先使用新的参数系统
- if (isset($params['assemble_type'])) {
- try {
- // 确保QuestionExpansionService和QuestionLocalService可用
- $questionExpansionService = $this->questionExpansionService ?? app(QuestionExpansionService::class);
- $questionLocalService = app(QuestionLocalService::class);
- Log::info('LearningAnalyticsService: 从容器获取服务实例');
- $strategy = new ExamTypeStrategy($questionExpansionService, $questionLocalService);
- $params = $strategy->buildParams($params, $assembleType);
- Log::info('LearningAnalyticsService: 已应用组卷策略', [
- 'assemble_type' => $assembleType,
- 'enhanced_params_keys' => array_keys($params)
- ]);
- } catch (Exception $e) {
- Log::warning('LearningAnalyticsService: 组卷策略应用失败,使用默认策略', [
- 'assemble_type' => $assembleType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- } elseif ($examTypeLegacy !== 'general') {
- // 兼容旧版 exam_type 参数
- try {
- $questionExpansionService = $this->questionExpansionService ?? app(QuestionExpansionService::class);
- $questionLocalService = app(QuestionLocalService::class);
- Log::info('LearningAnalyticsService: 从容器获取服务实例(兼容模式)');
- $strategy = new ExamTypeStrategy($questionExpansionService, $questionLocalService);
- $params = $strategy->buildParamsLegacy($params, $examTypeLegacy);
- Log::info('LearningAnalyticsService: 已应用组卷策略(兼容模式)', [
- 'exam_type' => $examTypeLegacy,
- 'enhanced_params_keys' => array_keys($params)
- ]);
- } catch (Exception $e) {
- Log::warning('LearningAnalyticsService: 组卷策略应用失败,使用默认策略', [
- 'exam_type' => $examTypeLegacy,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- } else {
- Log::info('LearningAnalyticsService: 跳过组卷策略', [
- 'reason' => '通用类型不需要特殊策略'
- ]);
- }
- $studentId = $params['student_id'] ?? null;
- $grade = $params['grade'] ?? null; // 用户选择的年级
- $totalQuestions = $params['total_questions'] ?? 20;
- // 【修复】参数映射:支持 kp_codes 和 kp_code_list 两种参数名
- $kpCodes = $params['kp_codes'] ?? $params['kp_code_list'] ?? [];
- if (!is_array($kpCodes)) {
- $kpCodes = [];
- }
- $skills = $params['skills'] ?? [];
- $questionTypeRatio = $params['question_type_ratio'] ?? [
- '选择题' => 40,
- '填空题' => 30,
- '解答题' => 30,
- ];
- // 新增:题目分类筛选
- $questionCategory = $params['question_category'] ?? null;
- // 注意: difficulty_ratio 参数已废弃,使用 difficulty_category 控制难度分布
- $difficultyLevels = $params['difficulty_levels'] ?? [];
- // 如果用户没有选择任何难度,difficultyLevels 为空数组,表示随机难度
- Log::info("generateIntelligentExam 开始", [
- 'student_id' => $studentId,
- 'total_questions' => $totalQuestions,
- 'kp_codes' => $kpCodes,
- 'skills' => $skills,
- 'assemble_type' => $assembleType,
- 'exam_type_legacy' => $examTypeLegacy,
- 'question_category' => $questionCategory,
- ]);
- // 1. 如果指定了学生,获取学生的薄弱点
- $weaknessFilter = [];
- if ($studentId) {
- Log::info("获取学生薄弱点: $studentId");
- $weaknesses = $this->getStudentWeaknesses($studentId, 20);
- Log::info("薄弱点数量: " . count($weaknesses), [
- '薄弱点' => $weaknesses,
- ]);
- $weaknessFilter = array_column($weaknesses, 'kp_code');
- // 如果用户没有指定知识点,使用学生的薄弱点
- if (empty($kpCodes)) {
- $kpCodes = $weaknessFilter;
- Log::info("用户未选择知识点,使用薄弱点作为kp_codes", [
- '最终kp_codes' => $kpCodes,
- ]);
- }
- }
- Log::info("准备调用 getQuestionsFromBank", [
- 'kp_codes' => $kpCodes,
- 'skills' => $skills,
- ]);
- // 2. 优先使用学生错题(如果存在)
- $mistakeQuestionIds = $params['mistake_question_ids'] ?? [];
- $priorityQuestions = [];
- $maxQuestions = 50; // 全局最大题目数限制
- if (!empty($mistakeQuestionIds)) {
- Log::info('LearningAnalyticsService: 优先获取学生错题', [
- 'mistake_question_ids' => $mistakeQuestionIds,
- 'count' => count($mistakeQuestionIds),
- 'max_limit' => $maxQuestions
- ]);
- // 如果错题超过最大值,截取到最大值
- $truncatedMistakeIds = $mistakeQuestionIds;
- if (count($mistakeQuestionIds) > $maxQuestions) {
- Log::warning('LearningAnalyticsService: 错题数量超过最大值限制,已截取', [
- 'mistake_count' => count($mistakeQuestionIds),
- 'max_limit' => $maxQuestions,
- 'truncated_count' => $maxQuestions
- ]);
- $truncatedMistakeIds = array_slice($mistakeQuestionIds, 0, $maxQuestions);
- }
- // 获取学生错题的详细信息
- $priorityQuestions = $this->getQuestionsFromBank([], [], $studentId, $questionTypeRatio, $maxQuestions, $truncatedMistakeIds, [], null, null);
- Log::info('LearningAnalyticsService: 错题获取完成', [
- 'priority_questions_count' => count($priorityQuestions),
- 'expected_count' => count($truncatedMistakeIds)
- ]);
- // 如果获取的错题数量少于预期,记录警告
- if (count($priorityQuestions) < count($truncatedMistakeIds)) {
- Log::warning('LearningAnalyticsService: 错题获取不完整', [
- 'expected' => count($truncatedMistakeIds),
- 'actual' => count($priorityQuestions),
- 'missing_ids' => array_diff($truncatedMistakeIds, array_column($priorityQuestions, 'id'))
- ]);
- }
- }
- // 3. 处理错题本逻辑
- $allQuestions = $priorityQuestions;
- $isMistakeBook = ($assembleType === 5); // 错题本类型
- // 如果是错题本类型,且有错题但数量不足,获取原卷子的所有题目
- if ($isMistakeBook && !empty($priorityQuestions) && count($priorityQuestions) < 5 && !empty($params['paper_ids'])) {
- Log::info('LearningAnalyticsService: 错题本错题不足,获取原卷子题目补充', [
- 'mistake_count' => count($priorityQuestions),
- 'paper_ids' => $params['paper_ids']
- ]);
- // 获取原卷子的所有题目
- $paperQuestionIds = $this->getPaperAllQuestions($params['paper_ids']);
- if (!empty($paperQuestionIds)) {
- Log::info('LearningAnalyticsService: 获取原卷子题目', [
- 'paper_question_count' => count($paperQuestionIds)
- ]);
- // 使用原卷子题目补充到目标数量
- $additionalNeeded = max(5, count($priorityQuestions)) - count($priorityQuestions);
- $paperQuestionIds = array_diff($paperQuestionIds, array_column($priorityQuestions, 'id'));
- $additionalPaperQuestions = $this->getQuestionsFromBank([], [], $studentId, $questionTypeRatio, $additionalNeeded, $paperQuestionIds, [], null, null);
- $allQuestions = array_merge($priorityQuestions, $additionalPaperQuestions);
- Log::info('LearningAnalyticsService: 错题本题目补充完成', [
- 'final_count' => count($allQuestions)
- ]);
- }
- }
- // 如果是错题本类型,但完全没有错题,使用原卷子的所有题目
- if ($isMistakeBook && empty($priorityQuestions) && !empty($params['paper_ids'])) {
- Log::info('LearningAnalyticsService: 错题本无错题,使用原卷子题目', [
- 'paper_ids' => $params['paper_ids']
- ]);
- // 获取原卷子的所有题目
- $paperQuestionIds = $this->getPaperAllQuestions($params['paper_ids']);
- if (!empty($paperQuestionIds)) {
- $allQuestions = $this->getQuestionsFromBank([], [], $studentId, $questionTypeRatio, $maxQuestions, $paperQuestionIds, [], null, null);
- // 检查是否获取到题目
- if (empty($allQuestions)) {
- Log::warning('LearningAnalyticsService: 错题本无法获取任何题目', [
- 'paper_ids' => $params['paper_ids'],
- 'paper_question_ids' => $paperQuestionIds,
- 'reason' => '原卷子题目在题库中不存在或已删除'
- ]);
- throw new \Exception('抱歉,您选择的卷子中的题目在题库中不存在或已被删除,无法生成错题本。请选择其他卷子或联系管理员更新题库。');
- }
- Log::info('LearningAnalyticsService: 错题本使用原卷子题目', [
- 'question_count' => count($allQuestions)
- ]);
- } else {
- throw new \Exception('抱歉,指定的卷子中没有找到题目,无法生成错题本。');
- }
- }
- // 错题本类型但既无错题又无卷子题目
- if ($isMistakeBook && empty($priorityQuestions) && empty($allQuestions)) {
- Log::warning('LearningAnalyticsService: 错题本既无错题又无卷子题目', [
- 'student_id' => $studentId,
- 'paper_ids' => $params['paper_ids'] ?? []
- ]);
- throw new \Exception('抱歉,您在这个卷子中没有错题记录,且卷子题目无法获取,无法生成错题本。请确认卷子ID是否正确或联系管理员。');
- }
- if (!$isMistakeBook && count($priorityQuestions) < $totalQuestions) {
- try {
- Log::info('开始调用 getQuestionsFromBank 补充题目', [
- 'kp_codes_count' => count($kpCodes),
- 'skills_count' => count($skills),
- 'has_mistake_priority' => !empty($mistakeQuestionIds),
- 'need_more' => $totalQuestions - count($priorityQuestions),
- 'assemble_type' => $assembleType
- ]);
- // 【优化】获取textbook_catalog_node_ids参数(教材组卷时使用)
- $textbookCatalogNodeIds = $params['textbook_catalog_node_ids'] ?? null;
- $additionalQuestions = $this->getQuestionsFromBank($kpCodes, $skills, $studentId, $questionTypeRatio, $maxQuestions, [], [], $questionCategory, $textbookCatalogNodeIds);
- $allQuestions = array_merge($priorityQuestions, $additionalQuestions);
- Log::info('getQuestionsFromBank 调用完成', [
- 'questions_count' => count($allQuestions),
- 'is_array' => is_array($allQuestions),
- 'first_question_id' => !empty($allQuestions) ? ($allQuestions[0]['id'] ?? 'N/A') : 'N/A',
- '耗时' => round((microtime(true) - $startTime) * 1000, 2) . 'ms',
- ]);
- 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;
- }
- } elseif ($isMistakeBook) {
- // 错题本类型:不补充题目,只使用错题
- Log::info('错题本类型:不补充题目,只使用错题', [
- 'assemble_type' => $assembleType,
- 'mistake_questions_count' => count($priorityQuestions),
- 'total_questions_requested' => $totalQuestions
- ]);
- }
- 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. 根据掌握度对题目进行筛选和排序
- // 错题本类型:使用所有错题,但不超过最大值限制
- $targetQuestionCount = $isMistakeBook ? min(count($allQuestions), $maxQuestions) : $totalQuestions;
- Log::info('开始调用 selectQuestionsByMastery', [
- 'input_count' => count($allQuestions),
- 'target_count' => $targetQuestionCount,
- 'is_mistake_book' => $isMistakeBook,
- 'assemble_type' => $assembleType,
- 'total_questions_param' => $totalQuestions
- ]);
- $startTime = microtime(true);
- $selectedQuestions = $this->selectQuestionsByMastery(
- $allQuestions,
- $studentId,
- $targetQuestionCount,
- $questionTypeRatio,
- $difficultyLevels,
- $weaknessFilter,
- $assembleType // 新增assembleType参数
- );
- $selectTime = (microtime(true) - $startTime) * 1000;
- Log::info('题目筛选结果', [
- 'input_count' => count($allQuestions),
- 'selected_count' => count($selectedQuestions),
- 'target_count' => $targetQuestionCount,
- 'is_mistake_book' => $isMistakeBook,
- 'select_time_ms' => round($selectTime, 2)
- ]);
- if (empty($selectedQuestions)) {
- return [
- 'success' => false,
- 'message' => '题目筛选失败',
- 'questions' => []
- ];
- }
- // 【恢复】简化难度分布检查
- $difficultyCategory = $params['difficulty_category'] ?? 1;
- $enableDistribution = $params['enable_difficulty_distribution'] ?? false;
- $isExcludedType = ($assembleType === 5); // 只有错题本类型(assembleType=5)不应用难度分布
- // 【重要】添加详细的difficulty_category追踪日志
- Log::info('LearningAnalyticsService: 难度分布检查开始', [
- 'input_difficulty_category' => $difficultyCategory,
- 'assemble_type' => $assembleType,
- 'enable_distribution' => $enableDistribution,
- 'is_excluded_type' => $isExcludedType,
- 'selected_questions_before' => count($selectedQuestions),
- 'params_keys' => array_keys($params)
- ]);
- if ($enableDistribution && !$isExcludedType) {
- Log::info('LearningAnalyticsService: 应用难度系数分布', [
- 'difficulty_category_before' => $difficultyCategory,
- 'assemble_type' => $assembleType,
- 'before_count' => count($selectedQuestions)
- ]);
- try {
- // 使用 ExamTypeStrategy 的独立方法应用难度分布
- $questionExpansionService = $this->questionExpansionService ?? app(QuestionExpansionService::class);
- $examStrategy = new ExamTypeStrategy($questionExpansionService);
- $selectedQuestions = $examStrategy->applyDifficultyDistributionToQuestions(
- $selectedQuestions,
- $totalQuestions,
- $difficultyCategory,
- $params
- );
- Log::info('LearningAnalyticsService: 难度分布应用完成', [
- 'difficulty_category_after' => $difficultyCategory,
- 'after_count' => count($selectedQuestions)
- ]);
- } catch (\Exception $e) {
- Log::warning('LearningAnalyticsService: 难度分布应用失败,继续使用原结果', [
- 'error' => $e->getMessage(),
- 'difficulty_category_when_error' => $difficultyCategory
- ]);
- }
- }
- 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,
- 'difficulty_distribution_applied' => $enableDistribution && !$isExcludedType,
- 'difficulty_category' => $difficultyCategory,
- // 【新增】章节知识点数量统计(教材组卷时)
- 'chapter_knowledge_point_stats' => $params['chapter_knowledge_point_stats'] ?? null,
- 'textbook_catalog_node_ids' => $params['textbook_catalog_node_ids'] ?? null
- ]
- ];
- } catch (\Exception $e) {
- Log::error('Generate Intelligent Exam Error', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- 'success' => false,
- 'message' => '智能出卷异常: ' . $e->getMessage(),
- 'questions' => []
- ];
- }
- }
- /**
- * 从本地题库获取题目(错题回顾优先)
- * 支持优先获取指定题目ID的题目
- * 【优化】新增textbookCatalogNodeIds参数,支持按textbook_catalog_node_id筛选题目
- */
- private function getQuestionsFromBank(array $kpCodes, array $skills, ?string $studentId, array $questionTypeRatio = [], int $totalNeeded = 100, array $priorityQuestionIds = [], array $excludeQuestionIds = [], ?int $questionCategory = null, ?array $textbookCatalogNodeIds = null): array
- {
- $startTime = microtime(true);
- try {
- // 错题回顾:优先获取指定的学生错题
- if (!empty($priorityQuestionIds)) {
- Log::info('getQuestionsFromBank: 优先获取学生错题', [
- 'priority_count' => count($priorityQuestionIds),
- 'priority_ids' => $priorityQuestionIds
- ]);
- $priorityQuestions = $this->getLocalQuestionsByIds($priorityQuestionIds);
- if (!empty($priorityQuestions)) {
- Log::info('getQuestionsFromBank: 优先错题获取成功', [
- 'count' => count($priorityQuestions)
- ]);
- return $priorityQuestions;
- } else {
- Log::warning('getQuestionsFromBank: 优先错题获取失败,返回空数组让上层处理');
- // 错题本类型获取不到错题时,返回空数组,不回退到题库随机选题
- return [];
- }
- }
- // 从本地数据库查询题目
- Log::info('getQuestionsFromBank: 从本地数据库查询题目', [
- 'kp_codes' => $kpCodes,
- 'skills' => $skills,
- 'total_needed' => $totalNeeded,
- 'question_type_ratio' => $questionTypeRatio,
- 'note' => '难度筛选由 QuestionLocalService 处理'
- ]);
- $query = \App\Models\Question::query();
- // 按知识点筛选
- if (!empty($kpCodes)) {
- $query->whereIn('kp_code', $kpCodes);
- Log::info('应用知识点筛选', ['kp_codes' => $kpCodes]);
- }
- // 按技能筛选(这里使用 tags 字段模拟技能筛选)
- if (!empty($skills)) {
- $query->where(function ($q) use ($skills) {
- foreach ($skills as $skill) {
- $q->orWhere('tags', 'like', "%{$skill}%");
- }
- });
- Log::info('应用技能筛选', ['skills' => $skills]);
- }
- // 排除学生已做过的题目
- if (!empty($excludeQuestionIds)) {
- $query->whereNotIn('id', $excludeQuestionIds);
- Log::info('应用排除筛选', ['exclude_count' => count($excludeQuestionIds)]);
- }
- // 按题目分类筛选(如果指定了 question_category)
- if ($questionCategory !== null) {
- $query->where('question_category', $questionCategory);
- Log::info('应用题目分类筛选', ['question_category' => $questionCategory]);
- }
- // 【优化】按教材章节节点筛选(textbook_catalog_nodes_id)
- if (!empty($textbookCatalogNodeIds)) {
- $query->whereIn('textbook_catalog_nodes_id', $textbookCatalogNodeIds);
- Log::info('应用教材章节节点筛选', ['textbook_catalog_nodes_ids' => $textbookCatalogNodeIds]);
- }
- // 筛选有解题思路的题目
- $query->whereNotNull('solution')
- ->where('solution', '!=', '')
- ->where('solution', '!=', '[]');
- // 注意: 难度筛选由 QuestionLocalService 的难度分布系统处理
- // 不在这里进行难度筛选,让 QuestionLocalService 做精确的难度分布
- // 【重要】移除数量限制,获取所有符合条件的题目
- // 不使用limit()限制查询结果,让后续处理逻辑决定最终数量
- $query->inRandomOrder();
- $questions = $query->get();
- Log::info('getQuestionsFromBank: 查询完成', [
- 'raw_count' => $questions->count(),
- 'total_needed' => $totalNeeded,
- 'note' => '移除limit限制,获取所有符合条件的题目',
- 'time_ms' => round((microtime(true) - $startTime) * 1000, 2)
- ]);
- // 【重要】添加更详细的日志来追踪题目筛选过程
- Log::info('getQuestionsFromBank: 题目筛选过程详情', [
- 'database_query_count' => $questions->count(),
- 'kp_codes_filter' => $kpCodes,
- 'skills_filter' => $skills,
- 'exclude_count' => count($excludeQuestionIds),
- 'question_category' => $questionCategory,
- 'textbook_catalog_node_ids_filter' => $textbookCatalogNodeIds,
- 'note' => '将返回所有符合条件的题目,不限制数量'
- ]);
- // 转换为标准格式
- $formattedQuestions = $questions->map(function ($q) {
- return [
- 'id' => $q->id,
- 'question_code' => $q->question_code,
- 'kp_code' => $q->kp_code,
- 'question_type' => $q->question_type,
- 'difficulty' => (float) $q->difficulty,
- 'stem' => $q->stem,
- 'solution' => $q->solution,
- 'metadata' => [
- 'has_solution' => true,
- 'is_choice' => $q->question_type === 'choice',
- 'is_fill' => $q->question_type === 'fill',
- 'is_answer' => $q->question_type === 'answer',
- 'difficulty_label' => $this->getDifficultyLabel($q->difficulty),
- 'question_type_label' => $this->getQuestionTypeLabel($q->question_type)
- ]
- ];
- })->toArray();
- // 【重要】返回所有符合条件的题目,不限制数量
- // 让上层调用者根据需要选择题目数量
- $selectedQuestions = $formattedQuestions;
- Log::info('getQuestionsFromBank 完成', [
- 'final_count' => count($selectedQuestions),
- 'raw_database_count' => $questions->count(),
- 'time_ms' => round((microtime(true) - $startTime) * 1000, 2)
- ]);
- return $selectedQuestions;
- } catch (\Exception $e) {
- Log::error('getQuestionsFromBank 查询失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 获取指定ID的题目详情
- */
- private function getLocalQuestionsByIds(array $questionIds): array
- {
- try {
- // 通过QuestionBankService获取题目详情
- $questionBankService = $this->questionBankService ?? app(\App\Services\QuestionBankService::class);
- // 批量获取题目详情
- $response = $questionBankService->getQuestionsByIds($questionIds);
- if (empty($response['data'])) {
- Log::warning('getLocalQuestionsByIds: 未获取到任何题目', [
- 'requested_ids' => $questionIds
- ]);
- return [];
- }
- $questions = $response['data'];
- // 转换为标准格式
- $result = array_map(function ($q) {
- $difficulty = $q['difficulty'] ?? 0.5;
- return [
- 'id' => $q['id'],
- 'question_code' => $q['question_code'] ?? '',
- 'kp_code' => $q['kp_code'] ?? '',
- 'question_type' => $q['question_type'] ?? 'choice',
- 'difficulty' => (float) $difficulty,
- 'stem' => $q['stem'] ?? '',
- 'solution' => $q['solution'] ?? '',
- 'answer' => $q['answer'] ?? '',
- 'metadata' => [
- 'has_solution' => !empty($q['solution']),
- 'is_choice' => ($q['question_type'] ?? 'choice') === 'choice',
- 'is_fill' => ($q['question_type'] ?? 'choice') === 'fill',
- 'is_answer' => ($q['question_type'] ?? 'choice') === 'answer',
- 'difficulty_label' => $this->getDifficultyLabel((float) $difficulty),
- 'question_type_label' => $this->getQuestionTypeLabel($q['question_type'] ?? 'choice')
- ]
- ];
- }, $questions);
- Log::info('getLocalQuestionsByIds 获取成功', [
- 'requested_count' => count($questionIds),
- 'found_count' => count($result),
- 'missing_ids' => array_diff($questionIds, array_column($result, 'id')),
- 'question_ids' => array_slice($questionIds, 0, 20),
- 'found_question_ids' => array_slice(array_column($result, 'id'), 0, 20)
- ]);
- return $result;
- } catch (\Exception $e) {
- Log::error('getLocalQuestionsByIds 获取失败', [
- 'question_ids' => $questionIds,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- /**
- * 获取卷子的所有题目ID(不区分对错)
- */
- private function getPaperAllQuestions(array $paperIds): array
- {
- try {
- // 查询 paper_questions 表获取所有题目ID
- $questionIds = DB::table('paper_questions')
- ->whereIn('paper_id', $paperIds)
- ->pluck('question_bank_id')
- ->unique()
- ->filter()
- ->values()
- ->toArray();
- Log::debug('LearningAnalyticsService: 获取卷子所有题目', [
- 'paper_ids' => $paperIds,
- 'question_count' => count($questionIds)
- ]);
- return $questionIds;
- } catch (\Exception $e) {
- Log::error('LearningAnalyticsService: 获取卷子题目失败', [
- 'paper_ids' => $paperIds,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取难度标签
- */
- private function getDifficultyLabel(float $difficulty): string
- {
- return match (true) {
- $difficulty < 0.4 => '基础',
- $difficulty < 0.7 => '中等',
- default => '拔高'
- };
- }
- /**
- * 获取题型标签
- */
- private function getQuestionTypeLabel(string $questionType): string
- {
- return match ($questionType) {
- 'choice' => '选择题',
- 'fill' => '填空题',
- 'answer' => '解答题',
- default => '未知题型'
- };
- }
- /**
- * 根据题型配比选择题目
- * 注意: 难度配比调整由 QuestionLocalService 处理
- */
- private function selectQuestionsByRatio(
- array $questions,
- int $totalNeeded,
- array $questionTypeRatio = []
- ): array {
- if (empty($questions)) {
- return [];
- }
- // 如果没有配比要求,直接返回
- if (empty($questionTypeRatio)) {
- return array_slice($questions, 0, $totalNeeded);
- }
- $selected = [];
- $usedIndices = [];
- // 按题型配比选择
- if (!empty($questionTypeRatio)) {
- foreach ($questionTypeRatio as $type => $ratio) {
- $count = (int) round(($ratio / 100) * $totalNeeded);
- if ($count <= 0) continue;
- $typeQuestions = [];
- foreach ($questions as $idx => $q) {
- if (in_array($idx, $usedIndices)) continue;
- $qType = $q['question_type'] ?? '';
- $label = $this->getQuestionTypeLabel($qType);
- if ($label === $type) {
- $typeQuestions[] = ['idx' => $idx, 'question' => $q];
- }
- }
- // 随机选择
- shuffle($typeQuestions);
- $selectedCount = min($count, count($typeQuestions));
- for ($i = 0; $i < $selectedCount; $i++) {
- $selected[] = $typeQuestions[$i]['question'];
- $usedIndices[] = $typeQuestions[$i]['idx'];
- }
- }
- }
- // 如果还有空缺,补充剩余题目
- // 注意: 难度配比调整由 QuestionLocalService 处理
- if (count($selected) < $totalNeeded) {
- foreach ($questions as $idx => $q) {
- if (count($selected) >= $totalNeeded) break;
- if (!in_array($idx, $usedIndices)) {
- $selected[] = $q;
- $usedIndices[] = $idx;
- }
- }
- }
- return array_slice($selected, 0, $totalNeeded);
- }
- /**
- * 根据学生掌握度筛选题目
- */
- private function selectQuestionsByMastery(
- array $questions,
- ?string $studentId,
- int $totalQuestions,
- array $questionTypeRatio,
- array $difficultyLevels,
- array $weaknessFilter,
- int $assembleType // 新增assembleType参数
- ): array {
- Log::info('selectQuestionsByMastery 开始', [
- 'question_count' => count($questions),
- 'student_id' => $studentId,
- 'total_questions' => $totalQuestions
- ]);
- // 【修复】题目数量处理逻辑:无论题目数量多少,都要进行权重分配和筛选
- // 如果题目数量超过目标,则截取到目标数量
- // 如果题目数量不足,则使用所有题目,并记录警告
- if (count($questions) > $totalQuestions) {
- Log::info('题目数量超过目标,进行截取', [
- 'question_count' => count($questions),
- 'total_questions' => $totalQuestions,
- 'note' => '将按权重选择最合适的题目'
- ]);
- } else {
- Log::warning('题目数量不足,将使用所有可用题目', [
- 'available_count' => count($questions),
- 'requested_count' => $totalQuestions,
- 'note' => '可能需要补充题库或放宽筛选条件'
- ]);
- }
- // 【移除】删除多余的难度筛选逻辑
- // 题目本身就有难度系数,QuestionLocalService的难度分布系统会处理题目分布
- // 不需要额外的难度筛选,让题目保持原始的难度分布
- Log::info('跳过多余的难度筛选,使用题目原始难度分布', [
- 'question_count' => count($questions),
- 'note' => '由QuestionLocalService处理难度分布'
- ]);
- // 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. 按题型分配题目数量(权重用于题型内排序)
- $selectedQuestions = [];
- // 将所有题目合并
- $weightedQuestions = [];
- foreach ($questionsByKp as $kpCode => $kpQuestions) {
- foreach ($kpQuestions as $q) {
- $q['kp_code'] = $kpCode;
- $weightedQuestions[] = $q;
- }
- }
- // ========== 知识点优先选择机制 ==========
- // 摸底测试的核心目标是最大化知识点覆盖
- // 题型只是约束条件(每种至少1题)
- // 首先按题型分组
- $questionsByType = [
- 'choice' => [],
- 'fill' => [],
- 'answer' => [],
- ];
- foreach ($weightedQuestions 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($questionsByType[$type])) {
- $type = 'answer'; // 默认归类为answer
- }
- $questionsByType[$type][] = $q;
- }
- // ========== 步骤1:按题型分配题目 ==========
- $selectedQuestions = [];
- // 【区分】根据assembleType决定是否使用知识点优先机制
- $useKnowledgePointPriority = ($assembleType === 0); // 摸底测试需要知识点优先
- $kpSelected = []; // 已选知识点记录
- Log::info('selectQuestionsByMastery: 知识点优先策略', [
- 'assemble_type' => $assembleType,
- 'use_knowledge_point_priority' => $useKnowledgePointPriority,
- 'note' => $useKnowledgePointPriority ? '摸底测试:需要均衡分配知识点' : '知识点组卷:允许同一知识点选多题'
- ]);
- // 确保每种题型至少选1题
- foreach (['choice', 'fill', 'answer'] as $type) {
- if (empty($questionsByType[$type])) {
- Log::warning('题型分配:题型无题目', ['type' => $type]);
- continue;
- }
- // 按权重排序该题型的题目
- usort($questionsByType[$type], function ($a, $b) use ($kpWeights) {
- $kpA = $a['kp_code'] ?? '';
- $kpB = $b['kp_code'] ?? '';
- $weightA = $kpWeights[$kpA] ?? 1.0;
- $weightB = $kpWeights[$kpB] ?? 1.0;
- return $weightB <=> $weightA;
- });
- // 根据策略选择题目
- if ($useKnowledgePointPriority) {
- // 摸底测试:选择第一个未选过知识点的题目
- foreach ($questionsByType[$type] as $q) {
- $kpCode = $q['kp_code'] ?? '';
- if (!isset($kpSelected[$kpCode])) {
- $selectedQuestions[] = $q;
- $kpSelected[$kpCode] = true;
- Log::debug('题型基础分配(知识点优先)', ['type' => $type, 'kp' => $kpCode]);
- break;
- }
- }
- } else {
- // 【修复】知识点组卷:随机选择该题型的一道题,避免固定选择第一个导致知识点分布不均
- $randomIndex = array_rand($questionsByType[$type]);
- $selectedQuestions[] = $questionsByType[$type][$randomIndex];
- Log::debug('题型基础分配(随机选择)', [
- 'type' => $type,
- 'kp' => $questionsByType[$type][$randomIndex]['kp_code'] ?? 'unknown',
- 'random_index' => $randomIndex,
- 'total_in_type' => count($questionsByType[$type])
- ]);
- }
- }
- // ========== 步骤2:继续选题目,直到达到目标数量 ==========
- $allQuestions = array_merge($questionsByType['choice'], $questionsByType['fill'], $questionsByType['answer']);
- // 【重要】添加排序前的知识点分布日志
- $preSortKpDistribution = [];
- foreach ($allQuestions as $q) {
- $kpCode = $q['kp_code'] ?? '';
- if (!isset($preSortKpDistribution[$kpCode])) {
- $preSortKpDistribution[$kpCode] = 0;
- }
- $preSortKpDistribution[$kpCode]++;
- }
- Log::info('selectQuestionsByMastery: 排序前知识点分布', [
- 'total_questions' => count($allQuestions),
- 'kp_distribution' => $preSortKpDistribution,
- 'note' => '用于对比排序后的分布变化'
- ]);
- // 【修复】添加随机因子到排序中,避免因ID排序导致知识点分布不均
- usort($allQuestions, function ($a, $b) use ($kpWeights) {
- $kpA = $a['kp_code'] ?? '';
- $kpB = $b['kp_code'] ?? '';
- $weightA = $kpWeights[$kpA] ?? 1.0;
- $weightB = $kpWeights[$kpB] ?? 1.0;
- // 主要按权重排序
- if ($weightA != $weightB) {
- return $weightB <=> $weightA;
- }
- // 权重相同时,添加随机排序而不是按ID排序
- // 使用随机因子确保相同权重的知识点有公平的选中机会
- return mt_rand(-1, 1);
- });
- // 【重要】添加排序后的知识点分布日志
- $postSortKpDistribution = [];
- $postSortFirst50 = []; // 记录前50题的知识点分布
- foreach ($allQuestions as $idx => $q) {
- $kpCode = $q['kp_code'] ?? '';
- if (!isset($postSortKpDistribution[$kpCode])) {
- $postSortKpDistribution[$kpCode] = 0;
- }
- $postSortKpDistribution[$kpCode]++;
- // 记录前50题的知识点
- if ($idx < 50) {
- $postSortFirst50[] = $kpCode;
- }
- }
- Log::info('selectQuestionsByMastery: 排序后知识点分布', [
- 'total_questions' => count($allQuestions),
- 'kp_distribution' => $postSortKpDistribution,
- 'first_50_kp_codes' => $postSortFirst50,
- 'note' => '观察排序是否均衡分布A07和A08'
- ]);
- // 根据策略继续选择题目
- if ($useKnowledgePointPriority) {
- // 摸底测试:选择未选过知识点的题目(优先)
- foreach ($allQuestions as $q) {
- if (count($selectedQuestions) >= $totalQuestions) break;
- $kpCode = $q['kp_code'] ?? '';
- if (!isset($kpSelected[$kpCode])) {
- $selectedQuestions[] = $q;
- $kpSelected[$kpCode] = true;
- Log::debug('继续选择题目(知识点优先)', ['kp' => $kpCode, 'id' => $q['id'] ?? 'unknown']);
- }
- }
- } else {
- // 知识点组卷:选择未选过的题目(不要求知识点不重复)
- $selectedIds = array_column($selectedQuestions, 'id');
- foreach ($allQuestions as $q) {
- if (count($selectedQuestions) >= $totalQuestions) break;
- $qid = $q['id'] ?? null;
- if ($qid && !in_array($qid, $selectedIds)) {
- $selectedQuestions[] = $q;
- $selectedIds[] = $qid;
- Log::debug('继续选择题目(无知识点限制)', ['kp' => $q['kp_code'] ?? 'unknown', 'id' => $qid]);
- }
- }
- }
- // 【移除】删除步骤3的多余逻辑
- // 前面的逻辑已经能选够题目,不需要额外的补充步骤
- Log::info('selectQuestionsByMastery: 题目选择完成', [
- 'total_questions' => $totalQuestions,
- 'selected_count' => count($selectedQuestions),
- 'success' => count($selectedQuestions) === $totalQuestions,
- 'assemble_type' => $assembleType,
- 'strategy' => $useKnowledgePointPriority ? '知识点优先' : '无知识点限制',
- 'type_distribution' => array_count_values(array_map(function($q) {
- $qid = $q['id'] ?? $q['question_id'] ?? null;
- if ($qid && isset($questionTypeCache[$qid])) {
- return $questionTypeCache[$qid];
- }
- return $this->determineQuestionType($q);
- }, $selectedQuestions)),
- 'kp_distribution' => array_count_values(array_column($selectedQuestions, 'kp_code'))
- ]);
- // 【重要】最终截取到目标数量(如果超过)
- if (count($selectedQuestions) > $totalQuestions) {
- Log::info('题目数量超过目标,进行最终截取', [
- 'before' => count($selectedQuestions),
- 'after' => $totalQuestions
- ]);
- $selectedQuestions = array_slice($selectedQuestions, 0, $totalQuestions);
- }
- // 【重要】添加最终数量验证日志
- Log::info('selectQuestionsByMastery: 最终题目数量验证', [
- 'before_final_check' => count($selectedQuestions),
- 'target_count' => $totalQuestions,
- 'is_array' => is_array($selectedQuestions)
- ]);
- // ========== 最终排查:确保无重复题目且题型分布合理 ==========
- $finalQuestions = [];
- $seenQuestionIds = [];
- $duplicateCount = 0;
- $typeDistribution = ['choice' => 0, 'fill' => 0, 'answer' => 0];
- foreach ($selectedQuestions as $question) {
- $qbId = $question['question_bank_id'] ?? $question['id'];
- if (!in_array($qbId, $seenQuestionIds)) {
- $seenQuestionIds[] = $qbId;
- $finalQuestions[] = $question;
- // 统计题型分布
- $qid = $question['id'] ?? $question['question_id'] ?? null;
- $type = null;
- if ($qid && isset($questionTypeCache[$qid])) {
- $type = $questionTypeCache[$qid];
- } else {
- $type = $this->determineQuestionType($question);
- if ($qid) {
- $questionTypeCache[$qid] = $type;
- }
- }
- if (isset($typeDistribution[$type])) {
- $typeDistribution[$type]++;
- }
- } else {
- $duplicateCount++;
- Log::warning('发现重复题目(已自动移除)', [
- 'question_id' => $qbId,
- 'duplicate_count' => $duplicateCount
- ]);
- }
- }
- // 【重要】如果去重后数量不足,补充到目标数量
- if (count($finalQuestions) < $totalQuestions) {
- Log::warning('selectQuestionsByMastery: 去重后数量不足,尝试补充', [
- 'final_count' => count($finalQuestions),
- 'target_count' => $totalQuestions,
- 'need_more' => $totalQuestions - count($finalQuestions)
- ]);
- // 从原始题目中选择未选过的题目补充
- $usedIds = array_column($finalQuestions, 'id');
- $supplementCount = 0;
- foreach ($selectedQuestions as $question) {
- if ($supplementCount >= ($totalQuestions - count($finalQuestions))) break;
- $qid = $question['id'] ?? null;
- if ($qid && !in_array($qid, $usedIds)) {
- $finalQuestions[] = $question;
- $usedIds[] = $qid;
- $supplementCount++;
- }
- }
- Log::info('selectQuestionsByMastery: 补充完成', [
- 'supplement_added' => $supplementCount,
- 'final_count_after_supplement' => count($finalQuestions)
- ]);
- }
- Log::info('最终排查完成', [
- 'original_count' => count($selectedQuestions),
- 'final_count' => count($finalQuestions),
- 'duplicate_removed' => $duplicateCount,
- 'final_type_distribution' => $typeDistribution,
- 'target_count' => $totalQuestions
- ]);
- // 【重要】确保返回的题目数量正确
- $finalQuestions = array_slice($finalQuestions, 0, $totalQuestions);
- Log::info('selectQuestionsByMastery: 返回结果', [
- 'return_count' => count($finalQuestions),
- 'target_count' => $totalQuestions,
- 'success' => count($finalQuestions) === $totalQuestions
- ]);
- // 注意:题型平衡已在上面完成,不需要再调用adjustQuestionsByRatio
- // adjustQuestionsByRatio主要处理题型配比,但我们的题型平衡机制已经确保了题型分布符合要求
- return $finalQuestions;
- }
- /**
- * 获取学生对特定知识点的掌握度
- */
- 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, 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 道)
- // 修复:不自动减少目标数量,确保达到用户要求
- $availableCount = count($questions);
- $targets = $this->computeTypeTargets($targetCount, $typeRatio);
- Log::info('题型配比调整前桶统计', [
- 'target_count' => $targetCount,
- 'available_count' => $availableCount,
- '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';
- }
- // 【移除】删除未使用的mapDifficultyLevel方法
- // 难度分布由QuestionLocalService处理,不需要额外的难度映射
- 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
- {
- Log::warning('analyzeStudentAnswers 已停用:分析项目已下线', [
- 'student_id' => $data['student_id'] ?? null,
- 'paper_id' => $data['paper_id'] ?? null,
- ]);
- return [
- 'success' => false,
- 'message' => 'analysis_api_disabled',
- ];
- }
- }
|