IntelligentExamGeneration.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeGraphService;
  4. use App\Services\LearningAnalyticsService;
  5. use App\Services\QuestionBankService;
  6. use BackedEnum;
  7. use Filament\Notifications\Notification;
  8. use Filament\Pages\Page;
  9. use UnitEnum;
  10. use Livewire\Attributes\Computed;
  11. use Livewire\Attributes\On;
  12. use Livewire\Attributes\Reactive;
  13. use Livewire\Component;
  14. use Illuminate\Support\Facades\Cache; // Add Cache import
  15. class IntelligentExamGeneration extends Page
  16. {
  17. protected static ?string $title = '智能出卷';
  18. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
  19. protected static ?string $navigationLabel = '智能出卷';
  20. protected static string|UnitEnum|null $navigationGroup = '操作';
  21. protected static ?int $navigationSort = 1;
  22. protected string $view = 'filament.pages.intelligent-exam-generation-simple';
  23. // 基本配置
  24. public ?string $paperName = '';
  25. public ?string $paperDescription = '';
  26. public ?string $difficultyCategory = '基础'; // 基础/进阶/竞赛
  27. public int $totalQuestions = 20;
  28. public int $totalScore = 100;
  29. // 知识点和技能点选择
  30. public array $selectedKpCodes = [];
  31. public array $selectedSkills = [];
  32. // 题型配比
  33. public array $questionTypeRatio = [
  34. '选择题' => 40, // 百分比
  35. '填空题' => 30,
  36. '解答题' => 30,
  37. ];
  38. // 难度配比
  39. public array $difficultyRatio = [
  40. '基础' => 50, // 百分比
  41. '中等' => 35,
  42. '拔高' => 15,
  43. ];
  44. // 教师和学生相关
  45. public ?string $selectedTeacherId = null;
  46. public ?string $selectedStudentId = null;
  47. public bool $filterByStudentWeakness = false;
  48. // 状态
  49. public bool $isGenerating = false;
  50. public array $generatedQuestions = [];
  51. public ?string $generatedPaperId = null;
  52. #[Computed(cache: false)]
  53. public function knowledgePoints(): array
  54. {
  55. try {
  56. $result = app(KnowledgeGraphService::class)->listKnowledgePoints(1, 1000);
  57. $knowledgePoints = $result['data'] ?? [];
  58. \Illuminate\Support\Facades\Log::info('知识点列表获取成功', [
  59. 'count' => count($knowledgePoints),
  60. 'first_item' => !empty($knowledgePoints) ? $knowledgePoints[0] : null
  61. ]);
  62. return $knowledgePoints;
  63. } catch (\Exception $e) {
  64. \Illuminate\Support\Facades\Log::error('获取知识点列表失败', [
  65. 'error' => $e->getMessage()
  66. ]);
  67. return [];
  68. }
  69. }
  70. #[Computed(cache: false)]
  71. public function skills(): array
  72. {
  73. if (empty($this->selectedKpCodes)) {
  74. return [];
  75. }
  76. $allSkills = [];
  77. foreach ($this->selectedKpCodes as $kpCode) {
  78. $kpSkills = app(KnowledgeGraphService::class)->getSkillsByKnowledgePoint($kpCode);
  79. $allSkills = array_merge($allSkills, $kpSkills);
  80. }
  81. return $allSkills;
  82. }
  83. /**
  84. * 获取当前选择的教师名称
  85. */
  86. public function getSelectedTeacherName(): string
  87. {
  88. if (empty($this->selectedTeacherId)) {
  89. return '未选择';
  90. }
  91. try {
  92. $teacher = \App\Models\Teacher::query()
  93. ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
  94. ->where('teachers.teacher_id', $this->selectedTeacherId)
  95. ->select(
  96. 'teachers.name',
  97. 'teachers.subject',
  98. 'u.username'
  99. )
  100. ->first();
  101. if ($teacher) {
  102. $name = trim($teacher->name ?? $this->selectedTeacherId);
  103. $subject = $teacher->subject ? " ({$teacher->subject})" : '';
  104. $username = $teacher->username ? " [{$teacher->username}]" : '';
  105. return "{$name}{$subject}{$username}";
  106. }
  107. return $this->selectedTeacherId;
  108. } catch (\Exception $e) {
  109. return $this->selectedTeacherId;
  110. }
  111. }
  112. /**
  113. * 获取当前选择的学生名称
  114. */
  115. public function getSelectedStudentName(): string
  116. {
  117. if (empty($this->selectedStudentId) || empty($this->selectedTeacherId)) {
  118. return '未选择';
  119. }
  120. try {
  121. $student = \App\Models\Student::query()
  122. ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
  123. ->where('students.student_id', $this->selectedStudentId)
  124. ->where('students.teacher_id', $this->selectedTeacherId)
  125. ->select(
  126. 'students.name',
  127. 'students.grade',
  128. 'students.class_name',
  129. 'u.username'
  130. )
  131. ->first();
  132. if ($student) {
  133. $name = trim($student->name ?? $this->selectedStudentId);
  134. $gradeClass = trim("{$student->grade} - {$student->class_name}");
  135. $username = $student->username ? " [{$student->username}]" : '';
  136. return "{$name} ({$gradeClass}){$username}";
  137. }
  138. return $this->selectedStudentId;
  139. } catch (\Exception $e) {
  140. return $this->selectedStudentId;
  141. }
  142. }
  143. #[Computed(cache: false)]
  144. public function teachers(): array
  145. {
  146. try {
  147. // 首先获取teachers表中的老师
  148. $teachers = \App\Models\Teacher::query()->from('teachers as t')
  149. ->leftJoin('users as u', 't.teacher_id', '=', 'u.user_id')
  150. ->select(
  151. 't.teacher_id',
  152. 't.name',
  153. 't.subject',
  154. 'u.username',
  155. 'u.email'
  156. )
  157. ->orderBy('t.name')
  158. ->orderBy('t.name')
  159. ->get();
  160. // 如果有学生但没有对应的老师记录,添加一个"未知老师"条目
  161. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  162. $missingTeacherIds = \App\Models\Student::query()->from('students as s')
  163. ->distinct()
  164. ->whereNotIn('s.teacher_id', $teacherIds)
  165. ->pluck('teacher_id')
  166. ->toArray();
  167. // 转换 Collection 为数组以便合并和排序
  168. $teachersArray = $teachers->all();
  169. if (!empty($missingTeacherIds)) {
  170. foreach ($missingTeacherIds as $missingId) {
  171. $teachersArray[] = (object) [
  172. 'teacher_id' => $missingId,
  173. 'name' => '未知老师 (' . $missingId . ')',
  174. 'subject' => '未知',
  175. 'username' => null,
  176. 'email' => null
  177. ];
  178. }
  179. // 重新排序
  180. usort($teachersArray, function($a, $b) {
  181. return strcmp($a->name, $b->name);
  182. });
  183. return $teachersArray;
  184. }
  185. return $teachersArray;
  186. } catch (\Exception $e) {
  187. \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
  188. 'error' => $e->getMessage()
  189. ]);
  190. return [];
  191. }
  192. }
  193. #[Computed(cache: false)]
  194. public function students(): array
  195. {
  196. if (empty($this->selectedTeacherId)) {
  197. return [];
  198. }
  199. try {
  200. $students = \App\Models\Student::query()->from('students as s')
  201. ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
  202. ->where('s.teacher_id', $this->selectedTeacherId)
  203. ->select(
  204. 's.student_id',
  205. 's.name',
  206. 's.grade',
  207. 's.class_name',
  208. 'u.username',
  209. 'u.email'
  210. )
  211. ->orderBy('s.grade')
  212. ->orderBy('s.class_name')
  213. ->orderBy('s.name')
  214. ->get()
  215. ->all();
  216. \Illuminate\Support\Facades\Log::info('智能出题页面加载学生列表', [
  217. 'teacher_id' => $this->selectedTeacherId,
  218. 'student_count' => count($students)
  219. ]);
  220. return $students;
  221. } catch (\Exception $e) {
  222. \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
  223. 'teacher_id' => $this->selectedTeacherId,
  224. 'error' => $e->getMessage()
  225. ]);
  226. return [];
  227. }
  228. }
  229. #[Computed(cache: false)]
  230. public function studentWeaknesses(): array
  231. {
  232. if (!$this->selectedStudentId || !$this->filterByStudentWeakness) {
  233. \Illuminate\Support\Facades\Log::info('学生薄弱点未加载', [
  234. 'student_id' => $this->selectedStudentId,
  235. 'filter_enabled' => $this->filterByStudentWeakness
  236. ]);
  237. return [];
  238. }
  239. try {
  240. $weaknesses = app(LearningAnalyticsService::class)->getStudentWeaknesses($this->selectedStudentId);
  241. \Illuminate\Support\Facades\Log::info('获取学生薄弱点成功', [
  242. 'student_id' => $this->selectedStudentId,
  243. 'weakness_count' => count($weaknesses),
  244. 'weaknesses' => $weaknesses
  245. ]);
  246. return $weaknesses;
  247. } catch (\Exception $e) {
  248. \Illuminate\Support\Facades\Log::error('获取学生薄弱点失败', [
  249. 'student_id' => $this->selectedStudentId,
  250. 'error' => $e->getMessage()
  251. ]);
  252. return [];
  253. }
  254. }
  255. public function updatedSelectedTeacherId($value)
  256. {
  257. // 当教师选择变化时,清空之前选择的学生
  258. $this->selectedStudentId = null;
  259. }
  260. /**
  261. * 全选所有薄弱知识点
  262. */
  263. public function selectAllWeaknesses(): void
  264. {
  265. $weaknesses = $this->studentWeaknesses;
  266. if (empty($weaknesses)) {
  267. Notification::make()
  268. ->title('提示')
  269. ->body('暂无薄弱知识点数据')
  270. ->warning()
  271. ->send();
  272. return;
  273. }
  274. // 获取所有薄弱知识点的代码
  275. $kpCodes = array_column($weaknesses, 'kp_code');
  276. // 合并到已选择的知识点中(去重)
  277. $this->selectedKpCodes = array_unique(array_merge($this->selectedKpCodes, $kpCodes));
  278. Notification::make()
  279. ->title('成功')
  280. ->body('已全选 ' . count($kpCodes) . ' 个薄弱知识点')
  281. ->success()
  282. ->send();
  283. \Illuminate\Support\Facades\Log::info('全选薄弱知识点', [
  284. 'student_id' => $this->selectedStudentId,
  285. 'selected_kp_codes' => $kpCodes,
  286. 'total_selected' => count($this->selectedKpCodes)
  287. ]);
  288. }
  289. /**
  290. * 清空所有选择的知识点
  291. */
  292. public function clearSelection(): void
  293. {
  294. $this->selectedKpCodes = [];
  295. Notification::make()
  296. ->title('成功')
  297. ->body('已清空所有选择的知识点')
  298. ->info()
  299. ->send();
  300. \Illuminate\Support\Facades\Log::info('清空知识点选择', [
  301. 'student_id' => $this->selectedStudentId
  302. ]);
  303. }
  304. public function updatedSelectedStudentId($value)
  305. {
  306. // 选择学生后,清空之前选择的知识点
  307. $this->selectedKpCodes = [];
  308. // 如果启用了薄弱点筛选,加载但不自动勾选
  309. if ($this->filterByStudentWeakness && $value) {
  310. $weaknesses = $this->studentWeaknesses;
  311. if (empty($weaknesses)) {
  312. Notification::make()
  313. ->title('提示')
  314. ->body('该学生暂无薄弱点数据,请手动选择知识点或根据年级推荐')
  315. ->warning()
  316. ->send();
  317. } else {
  318. Notification::make()
  319. ->title('提示')
  320. ->body('已加载' . count($weaknesses) . '个薄弱知识点,请手动选择要练习的知识点')
  321. ->info()
  322. ->send();
  323. }
  324. }
  325. }
  326. public function generateExam()
  327. {
  328. \Illuminate\Support\Facades\Log::info('generateExam called with studentId=' . ($this->selectedStudentId ?? 'null'));
  329. $this->validate([
  330. // 'paperName' => 'required|string|max:255', // 已移除必填
  331. 'totalQuestions' => 'required|integer|min:6|max:100',
  332. 'selectedTeacherId' => 'nullable|string', // 可选老师
  333. 'selectedStudentId' => 'nullable|string', // 可选学生
  334. ]);
  335. // 确保题目数量至少6题
  336. if ($this->totalQuestions < 6) {
  337. \Illuminate\Support\Facades\Log::warning('题目数量少于6题,已自动调整为6题', ['original' => $this->totalQuestions]);
  338. $this->totalQuestions = 6;
  339. }
  340. // 自动生成试卷名称
  341. if (empty($this->paperName)) {
  342. $studentName = '学生' . ($this->selectedStudentId ?? '未选择');
  343. // 如果有选择学生,尝试从数据库获取真实姓名
  344. if ($this->selectedStudentId) {
  345. try {
  346. $student = \App\Models\Student::where('student_id', $this->selectedStudentId)->first();
  347. if ($student && $student->name) {
  348. $studentName = $student->name;
  349. }
  350. } catch (\Exception $e) {
  351. \Illuminate\Support\Facades\Log::warning('获取学生姓名失败', [
  352. 'student_id' => $this->selectedStudentId,
  353. 'error' => $e->getMessage()
  354. ]);
  355. }
  356. }
  357. $this->paperName = $studentName . '_' . now()->format('Ymd_His') . '_智能试卷';
  358. }
  359. $this->isGenerating = true;
  360. try {
  361. // 使用LearningAnalyticsService进行智能出卷
  362. $learningAnalyticsService = app(LearningAnalyticsService::class);
  363. // 准备出卷参数
  364. $examParams = [
  365. 'student_id' => $this->selectedStudentId,
  366. 'total_questions' => $this->totalQuestions,
  367. 'kp_codes' => $this->selectedKpCodes,
  368. 'skills' => $this->selectedSkills,
  369. 'question_type_ratio' => $this->questionTypeRatio,
  370. 'difficulty_ratio' => $this->difficultyRatio,
  371. ];
  372. // 调用智能出卷API
  373. $result = $learningAnalyticsService->generateIntelligentExam($examParams);
  374. if (!$result['success']) {
  375. throw new \Exception($result['message']);
  376. }
  377. $questions = $result['questions'];
  378. if (count($questions) < $this->totalQuestions) {
  379. // 题库不足时,批量生成题目(使用题库的多AI模型并行生成功能)
  380. $neededCount = $this->totalQuestions - count($questions);
  381. // 生成比需求更多的题目,储备起来供后续使用
  382. $generateCount = max($neededCount, 20); // 至少生成20道题作为储备
  383. \Illuminate\Support\Facades\Log::info("题库题目不足,需要补充 {$neededCount} 道题,准备批量生成 {$generateCount} 道题", [
  384. 'current_count' => count($questions),
  385. 'needed' => $neededCount,
  386. 'will_generate' => $generateCount
  387. ]);
  388. // 只生成一次,生成足够多的题目(题库服务支持多AI模型并行)
  389. $this->batchGenerateQuestions($generateCount);
  390. // 重新从题库获取题目
  391. $questionBankService = app(QuestionBankService::class);
  392. $params = [
  393. 'kp_codes' => implode(',', $this->selectedKpCodes),
  394. 'limit' => $this->totalQuestions * 2 // 获取更多题目用于筛选
  395. ];
  396. if (!empty($this->selectedSkills)) {
  397. $params['skills'] = implode(',', $this->selectedSkills);
  398. }
  399. if ($this->selectedStudentId) {
  400. $params['exclude_student_questions'] = $this->selectedStudentId;
  401. }
  402. $newResponse = $questionBankService->filterQuestions($params);
  403. // 合并题目并去重
  404. if (!empty($newResponse['data'])) {
  405. $existingIds = array_column($questions, 'id');
  406. foreach ($newResponse['data'] as $newQ) {
  407. if (!in_array($newQ['id'], $existingIds)) {
  408. $questions[] = $newQ;
  409. }
  410. }
  411. }
  412. \Illuminate\Support\Facades\Log::info("批量生成完成,当前题库题目数量: " . count($questions), [
  413. 'generated_count' => $generateCount,
  414. 'total_in_bank' => count($questions)
  415. ]);
  416. }
  417. // 2. 限制试卷题目数量为用户要求的数量
  418. if (count($questions) > $this->totalQuestions) {
  419. // 根据题型配比和难度配比对题目进行筛选和排序
  420. $questions = $this->selectBestQuestions(
  421. $questions,
  422. $this->totalQuestions,
  423. $this->difficultyCategory,
  424. $this->totalScore,
  425. $this->questionTypeRatio
  426. );
  427. \Illuminate\Support\Facades\Log::info("从 " . count($questions) . " 道题中筛选出 " . count($questions) . " 道题,难度分类: {$this->difficultyCategory}, 总分: {$this->totalScore}");
  428. }
  429. // 3. 检查题型完整性(至少保证每种题型都有题目)
  430. $checkResult = $this->ensureQuestionTypeCompleteness($questions, $this->totalQuestions);
  431. if ($checkResult['missing_types']) {
  432. \Illuminate\Support\Facades\Log::warning("检测到缺失题型,将自动生成", [
  433. 'missing_types' => $checkResult['missing_types'],
  434. 'current_count' => $checkResult['current_count']
  435. ]);
  436. // 批量生成缺失题型的题目
  437. $this->batchGenerateMissingTypes($checkResult['missing_types']);
  438. // 重新获取题目
  439. $questionBankService = app(QuestionBankService::class);
  440. $params = [
  441. 'kp_codes' => implode(',', $this->selectedKpCodes),
  442. 'limit' => $this->totalQuestions * 2
  443. ];
  444. if (!empty($this->selectedSkills)) {
  445. $params['skills'] = implode(',', $this->selectedSkills);
  446. }
  447. if ($this->selectedStudentId) {
  448. $params['exclude_student_questions'] = $this->selectedStudentId;
  449. }
  450. $newResponse = $questionBankService->filterQuestions($params);
  451. if (!empty($newResponse['data'])) {
  452. $questions = array_merge($questions, $newResponse['data']);
  453. }
  454. // 再次筛选
  455. $questions = $this->selectBestQuestions(
  456. $questions,
  457. $this->totalQuestions,
  458. $this->difficultyCategory,
  459. $this->totalScore,
  460. $this->questionTypeRatio
  461. );
  462. }
  463. // 2. 为题目添加类型信息(如果缺失)
  464. foreach ($questions as &$question) {
  465. if (!isset($question['question_type'])) {
  466. $question['question_type'] = $this->determineQuestionType($question);
  467. \Illuminate\Support\Facades\Log::debug('为题目添加类型', [
  468. 'question_id' => $question['id'] ?? '',
  469. 'added_type' => $question['question_type']
  470. ]);
  471. }
  472. }
  473. unset($question); // 释放引用
  474. // 3. 生成试卷数据
  475. $examData = [
  476. 'paper_name' => $this->paperName,
  477. 'paper_description' => $this->paperDescription,
  478. 'difficulty_category' => $this->difficultyCategory,
  479. 'questions' => $questions,
  480. 'total_score' => $this->totalScore,
  481. 'total_questions' => count($questions),
  482. 'student_id' => $this->selectedStudentId,
  483. 'teacher_id' => $this->selectedTeacherId,
  484. ];
  485. // 4. 保存到数据库
  486. $questionBankService = app(QuestionBankService::class);
  487. $paperId = $questionBankService->saveExamToDatabase($examData);
  488. // 如果保存返回 null,使用默认占位 ID,防止 UI 不显示
  489. if (empty($paperId)) {
  490. $paperId = 'demo_' . $this->selectedStudentId . '_' . now()->format('YmdHis');
  491. }
  492. \Illuminate\Support\Facades\Log::info('Generated paper ID: ' . $paperId);
  493. $this->generatedPaperId = $paperId;
  494. // 将生成的试卷数据缓存,以便 PDF 预览时使用(缓存 1 小时)
  495. \Illuminate\Support\Facades\Log::info('缓存试卷数据', [
  496. 'paper_id' => $paperId,
  497. 'question_count' => count($questions),
  498. 'question_types' => array_column($questions, 'question_type')
  499. ]);
  500. Cache::put('generated_exam_' . $paperId, $examData, now()->addHour());
  501. $this->generatedQuestions = $questions;
  502. $stats = $result['stats'] ?? [];
  503. $message = "已生成包含 " . count($questions) . " 道题的试卷";
  504. if (!empty($stats['weakness_targeted'])) {
  505. $message .= ",其中针对薄弱点 " . $stats['weakness_targeted'] . " 题";
  506. }
  507. Notification::make()
  508. ->title('试卷生成成功')
  509. ->body($message)
  510. ->success()
  511. ->send();
  512. } catch (\Exception $e) {
  513. // 记录错误并提供回退的试卷 ID,防止 UI 无显示
  514. \Illuminate\Support\Facades\Log::error('生成试卷失败', ['error' => $e->getMessage()]);
  515. $fallbackId = 'demo_' . $this->selectedStudentId . '_' . now()->format('YmdHis');
  516. $this->generatedPaperId = $fallbackId;
  517. $this->generatedQuestions = [];
  518. Notification::make()
  519. ->title('试卷生成失败,使用默认试卷')
  520. ->body('错误: ' . $e->getMessage() . "\n已生成默认试卷 ID: $fallbackId")
  521. ->warning()
  522. ->send();
  523. } finally {
  524. $this->isGenerating = false;
  525. }
  526. }
  527. /**
  528. * 批量生成题目(使用题库的多AI模型并行功能)
  529. */
  530. protected function batchGenerateQuestions(int $count)
  531. {
  532. $questionBankService = app(QuestionBankService::class);
  533. $generatedTasks = [];
  534. // 只生成一次,使用所有选中的知识点
  535. $allKpCodes = $this->selectedKpCodes;
  536. if (empty($allKpCodes)) {
  537. // 如果没有选中知识点,使用默认知识点
  538. $allKpCodes = ['R01']; // 默认知识点
  539. }
  540. // 对每个知识点生成题目
  541. foreach ($allKpCodes as $kpCode) {
  542. \Illuminate\Support\Facades\Log::info("为知识点 {$kpCode} 生成题目", [
  543. 'count' => $count,
  544. 'skills' => $this->selectedSkills
  545. ]);
  546. $result = $questionBankService->generateIntelligentQuestions([
  547. 'kp_code' => $kpCode,
  548. 'skills' => $this->selectedSkills,
  549. 'count' => $count,
  550. 'difficulty_distribution' => $this->difficultyRatio,
  551. ]);
  552. if ($result['success'] && isset($result['task_id'])) {
  553. $generatedTasks[] = [
  554. 'task_id' => $result['task_id'],
  555. 'kp_code' => $kpCode
  556. ];
  557. \Illuminate\Support\Facades\Log::info("已启动生成任务: {$result['task_id']} for {$kpCode}");
  558. } else {
  559. \Illuminate\Support\Facades\Log::warning("生成任务启动失败", [
  560. 'kp_code' => $kpCode,
  561. 'result' => $result
  562. ]);
  563. }
  564. }
  565. // 等待所有任务完成(最多等待60秒)
  566. if (!empty($generatedTasks)) {
  567. $maxWaitTime = 60; // 增加最大等待时间
  568. $startTime = time();
  569. \Illuminate\Support\Facades\Log::info("等待 {$maxWaitTime} 秒,所有生成任务完成", [
  570. 'tasks' => array_column($generatedTasks, 'task_id')
  571. ]);
  572. while (time() - $startTime < $maxWaitTime) {
  573. $allCompleted = true;
  574. $completedTasks = [];
  575. $runningTasks = [];
  576. foreach ($generatedTasks as $task) {
  577. $taskStatus = $questionBankService->getTaskStatus($task['task_id']);
  578. if (!$taskStatus) {
  579. $allCompleted = false;
  580. $runningTasks[] = $task['task_id'];
  581. continue;
  582. }
  583. $status = $taskStatus['status'] ?? '';
  584. if ($status === 'completed') {
  585. $completedTasks[] = $task['task_id'];
  586. } elseif ($status === 'failed') {
  587. \Illuminate\Support\Facades\Log::error("生成任务失败", [
  588. 'task_id' => $task['task_id'],
  589. 'error' => $taskStatus['error'] ?? '未知错误'
  590. ]);
  591. // 任务失败继续等待其他任务
  592. } else {
  593. $allCompleted = false;
  594. $runningTasks[] = $task['task_id'];
  595. }
  596. }
  597. if ($allCompleted) {
  598. \Illuminate\Support\Facades\Log::info('所有AI生成任务已完成', [
  599. 'completed' => $completedTasks,
  600. 'tasks' => $generatedTasks
  601. ]);
  602. break;
  603. }
  604. // 每10秒输出一次进度
  605. $elapsed = time() - $startTime;
  606. if ($elapsed % 10 < 2) {
  607. \Illuminate\Support\Facades\Log::info("生成进度", [
  608. 'elapsed' => $elapsed,
  609. 'completed' => count($completedTasks),
  610. 'running' => count($runningTasks),
  611. 'total' => count($generatedTasks)
  612. ]);
  613. }
  614. // 等待3秒后重试
  615. sleep(3);
  616. }
  617. $waitTime = time() - $startTime;
  618. \Illuminate\Support\Facades\Log::info('AI生成任务等待完成', [
  619. 'wait_time' => $waitTime,
  620. 'tasks' => $generatedTasks
  621. ]);
  622. }
  623. }
  624. /**
  625. * 根据题型配比和难度配比,从大量题目中筛选出最佳题目
  626. */
  627. protected function selectBestQuestions(
  628. array $questions,
  629. int $targetCount,
  630. string $difficultyCategory,
  631. float $totalScore,
  632. array $questionTypeRatio
  633. ): array {
  634. if (count($questions) <= $targetCount) {
  635. return $questions;
  636. }
  637. \Illuminate\Support\Facades\Log::info("开始筛选题目", [
  638. 'total_available' => count($questions),
  639. 'target_count' => $targetCount,
  640. 'difficulty_category' => $difficultyCategory,
  641. 'total_score' => $totalScore,
  642. 'type_ratio' => $questionTypeRatio
  643. ]);
  644. // 1. 按题型分类题目
  645. $categorizedQuestions = [
  646. 'choice' => [], // 选择题
  647. 'fill' => [], // 填空题
  648. 'answer' => [], // 解答题
  649. ];
  650. foreach ($questions as $question) {
  651. $type = $this->determineQuestionType($question);
  652. if (!isset($categorizedQuestions[$type])) {
  653. $type = 'answer';
  654. }
  655. $categorizedQuestions[$type][] = $question;
  656. }
  657. // 2. 根据难度分类筛选题目
  658. $difficultyFilteredQuestions = $this->filterByDifficulty($categorizedQuestions, $difficultyCategory);
  659. // 3. 根据题型配比计算每种题型应选择的题目数量
  660. // 先确保每种题型至少有1题(如果题目数量>=3)
  661. $selectedQuestions = [];
  662. $totalSelected = 0;
  663. // 优先保证每种题型至少一题(适用于总题目数>=3的情况)
  664. if ($targetCount >= 3) {
  665. foreach (['choice', 'fill', 'answer'] as $typeKey) {
  666. if (!empty($difficultyFilteredQuestions[$typeKey])) {
  667. // 随机选择1道该题型的题目
  668. $randomIndex = array_rand($difficultyFilteredQuestions[$typeKey]);
  669. $selectedQuestions[] = $difficultyFilteredQuestions[$typeKey][$randomIndex];
  670. $totalSelected++;
  671. \Illuminate\Support\Facades\Log::info("保证题型最少题目: {$typeKey}", [
  672. 'selected_index' => $randomIndex
  673. ]);
  674. } else {
  675. // 如果某题型没有题目,标记为缺失
  676. \Illuminate\Support\Facades\Log::warning("题型缺失: {$typeKey},需要从其他题型补充");
  677. }
  678. }
  679. }
  680. // 如果题目数量不足3题,则跳过最少保证
  681. // 根据题型配比计算每种题型应选择的题目数量
  682. foreach ($questionTypeRatio as $type => $ratio) {
  683. $typeKey = $type === '选择题' ? 'choice' : ($type === '填空题' ? 'fill' : 'answer');
  684. $countForType = floor($targetCount * $ratio / 100);
  685. // 如果总题目数>=3,已经为每种题型分配了1题,需要从剩余数量中扣除
  686. if ($targetCount >= 3 && $totalSelected > 0) {
  687. // 重新计算:确保至少1题 + 按比例分配的额外题目
  688. $baseCount = 1; // 最少1题
  689. $extraCount = floor(($targetCount - 3) * $ratio / 100); // 剩余题目按比例分配
  690. $countForType = $baseCount + $extraCount;
  691. }
  692. if ($countForType > 0 && !empty($difficultyFilteredQuestions[$typeKey])) {
  693. // 按难度排序后选择该题型的一部分题目
  694. $availableCount = count($difficultyFilteredQuestions[$typeKey]);
  695. $takeCount = min($countForType, $availableCount, $targetCount - $totalSelected);
  696. // 如果该题型已经在最少保证中分配过,需要排除已分配的题目
  697. if ($targetCount >= 3 && isset($selectedQuestions)) {
  698. // 重新获取题目,排除已选择的
  699. $availableQuestions = $difficultyFilteredQuestions[$typeKey];
  700. $takeCount = min($takeCount, $availableCount, $targetCount - $totalSelected);
  701. if ($takeCount > 0) {
  702. $selectedFromType = array_rand(array_flip(array_keys($availableQuestions)), $takeCount);
  703. if (!is_array($selectedFromType)) {
  704. $selectedFromType = [$selectedFromType];
  705. }
  706. foreach ($selectedFromType as $index) {
  707. $selectedQuestions[] = $availableQuestions[$index];
  708. }
  709. $totalSelected += $takeCount;
  710. }
  711. } else {
  712. // 正常分配
  713. if ($takeCount > 0) {
  714. $selectedFromType = array_rand(array_flip(array_keys($difficultyFilteredQuestions[$typeKey])), $takeCount);
  715. if (!is_array($selectedFromType)) {
  716. $selectedFromType = [$selectedFromType];
  717. }
  718. foreach ($selectedFromType as $index) {
  719. $selectedQuestions[] = $difficultyFilteredQuestions[$typeKey][$index];
  720. }
  721. $totalSelected += $takeCount;
  722. }
  723. }
  724. \Illuminate\Support\Facades\Log::info("{$type}题型筛选结果", [
  725. 'available' => $availableCount,
  726. 'take' => $takeCount,
  727. 'ratio' => $ratio,
  728. 'type' => $typeKey
  729. ]);
  730. }
  731. }
  732. // 4. 如果还有空缺,随机补充其他题型
  733. while ($totalSelected < $targetCount && count($selectedQuestions) < count($questions)) {
  734. $randomQuestion = $questions[array_rand($questions)];
  735. if (!in_array($randomQuestion, $selectedQuestions)) {
  736. $selectedQuestions[] = $randomQuestion;
  737. $totalSelected++;
  738. }
  739. }
  740. // 5. 打乱题目顺序
  741. shuffle($selectedQuestions);
  742. $finalQuestions = array_slice($selectedQuestions, 0, $targetCount);
  743. \Illuminate\Support\Facades\Log::info("题目筛选完成", [
  744. 'selected_count' => count($finalQuestions),
  745. 'difficulty_category' => $difficultyCategory,
  746. 'total_score' => $totalScore
  747. ]);
  748. return $finalQuestions;
  749. }
  750. /**
  751. * 检查题型完整性,确保每种题型至少有一题
  752. */
  753. protected function ensureQuestionTypeCompleteness(array $questions, int $targetCount): array
  754. {
  755. $result = [
  756. 'missing_types' => [],
  757. 'current_count' => count($questions),
  758. 'has_choice' => false,
  759. 'has_fill' => false,
  760. 'has_answer' => false,
  761. ];
  762. // 统计各题型数量
  763. $choiceCount = $fillCount = $answerCount = 0;
  764. foreach ($questions as $q) {
  765. $type = $this->determineQuestionType($q);
  766. if ($type === 'choice') {
  767. $choiceCount++;
  768. $result['has_choice'] = true;
  769. } elseif ($type === 'fill') {
  770. $fillCount++;
  771. $result['has_fill'] = true;
  772. } elseif ($type === 'answer') {
  773. $answerCount++;
  774. $result['has_answer'] = true;
  775. }
  776. }
  777. // 如果题目数量>=3,确保每种题型至少1题
  778. if ($targetCount >= 3) {
  779. if (!$result['has_choice']) {
  780. $result['missing_types'][] = 'choice';
  781. }
  782. if (!$result['has_fill']) {
  783. $result['missing_types'][] = 'fill';
  784. }
  785. if (!$result['has_answer']) {
  786. $result['missing_types'][] = 'answer';
  787. }
  788. }
  789. \Illuminate\Support\Facades\Log::info("题型完整性检查", [
  790. 'choice_count' => $choiceCount,
  791. 'fill_count' => $fillCount,
  792. 'answer_count' => $answerCount,
  793. 'missing_types' => $result['missing_types']
  794. ]);
  795. return $result;
  796. }
  797. /**
  798. * 批量生成缺失题型的题目
  799. */
  800. protected function batchGenerateMissingTypes(array $missingTypes): void
  801. {
  802. if (empty($missingTypes)) {
  803. return;
  804. }
  805. \Illuminate\Support\Facades\Log::info("开始生成缺失题型题目", ['missing_types' => $missingTypes]);
  806. // 为每个缺失题型生成3-5道题
  807. foreach ($missingTypes as $type) {
  808. $generateCount = 5; // 每个缺失题型生成5道题
  809. \Illuminate\Support\Facades\Log::info("为缺失题型 {$type} 生成 {$generateCount} 道题");
  810. foreach ($this->selectedKpCodes as $kpCode) {
  811. $questionBankService = app(QuestionBankService::class);
  812. // 根据题型设置特定的技能点
  813. $skills = $this->selectedSkills;
  814. if ($type === 'choice') {
  815. $skills[] = '选择题专项练习';
  816. } elseif ($type === 'fill') {
  817. $skills[] = '填空题专项练习';
  818. } elseif ($type === 'answer') {
  819. $skills[] = '解答题专项练习';
  820. }
  821. $result = $questionBankService->generateIntelligentQuestions([
  822. 'kp_code' => $kpCode,
  823. 'skills' => $skills,
  824. 'count' => $generateCount,
  825. 'difficulty_distribution' => $this->difficultyRatio,
  826. ]);
  827. if ($result['success'] && isset($result['task_id'])) {
  828. \Illuminate\Support\Facades\Log::info("已启动生成任务", [
  829. 'type' => $type,
  830. 'task_id' => $result['task_id'],
  831. 'kp_code' => $kpCode
  832. ]);
  833. }
  834. }
  835. }
  836. // 缺失题型生成任务已启动,由于题目生成是异步的,
  837. // 将在预览时动态获取最新生成的题目
  838. \Illuminate\Support\Facades\Log::info("缺失题型生成任务已启动,将在预览时动态获取");
  839. }
  840. /**
  841. * 根据难度分类筛选题目
  842. */
  843. protected function filterByDifficulty(array $categorizedQuestions, string $difficultyCategory): array
  844. {
  845. $filtered = [];
  846. $difficultyRanges = [
  847. '基础' => [0, 0.4],
  848. '中等' => [0.3, 0.7],
  849. '拔高' => [0.6, 1.0]
  850. ];
  851. $targetRange = $difficultyRanges[$difficultyCategory] ?? [0, 1.0];
  852. foreach ($categorizedQuestions as $type => $questions) {
  853. $filtered[$type] = [];
  854. foreach ($questions as $question) {
  855. $difficulty = floatval($question['difficulty'] ?? 0.5);
  856. if ($difficulty >= $targetRange[0] && $difficulty <= $targetRange[1]) {
  857. $filtered[$type][] = $question;
  858. } else {
  859. // 保留部分越界题目(如果该难度题目不足)
  860. if (count($filtered[$type]) < 2) {
  861. $filtered[$type][] = $question;
  862. }
  863. }
  864. }
  865. }
  866. \Illuminate\Support\Facades\Log::info("难度筛选结果", [
  867. 'difficulty_category' => $difficultyCategory,
  868. 'difficulty_range' => $targetRange,
  869. 'choice_count' => count($filtered['choice']),
  870. 'fill_count' => count($filtered['fill']),
  871. 'answer_count' => count($filtered['answer'])
  872. ]);
  873. return $filtered;
  874. }
  875. /**
  876. * 根据题目标签或内容判断题型
  877. */
  878. protected function determineQuestionType(array $question): string
  879. {
  880. $tags = $question['tags'] ?? '';
  881. $stem = $question['stem'] ?? '';
  882. // 1. 根据标签判断
  883. if (is_string($tags)) {
  884. if (strpos($tags, '选择') !== false || strpos($tags, '选择题') !== false) {
  885. return 'choice';
  886. }
  887. if (strpos($tags, '填空') !== false || strpos($tags, '填空题') !== false) {
  888. return 'fill';
  889. }
  890. if (strpos($tags, '解答') !== false || strpos($tags, '简答') !== false || strpos($tags, '证明') !== false) {
  891. return 'answer';
  892. }
  893. }
  894. // 2. 根据题干内容判断 - 选择题(有括号的或包含选项A.B.C.D.)
  895. if (is_string($stem)) {
  896. // 检查全角括号
  897. if (strpos($stem, '()') !== false) {
  898. return 'choice';
  899. }
  900. // 检查半角括号
  901. if (strpos($stem, '()') !== false) {
  902. return 'choice';
  903. }
  904. // 检查选项格式 A. B. C. D.(支持跨行匹配)
  905. if (preg_match('/[A-D]\.\s/m', $stem)) {
  906. return 'choice';
  907. }
  908. }
  909. // 3. 根据题干内容判断 - 填空题(有下划线)
  910. if (is_string($stem) && (strpos($stem, '____') !== false || strpos($stem, '______') !== false)) {
  911. return 'fill';
  912. }
  913. // 4. 根据题干长度和内容判断(启发式)
  914. if (is_string($stem)) {
  915. $shortQuestions = ['下列', '判断', '选择', '计算', '求'];
  916. $isShort = false;
  917. foreach ($shortQuestions as $keyword) {
  918. if (strpos($stem, $keyword) !== false) {
  919. $isShort = true;
  920. break;
  921. }
  922. }
  923. // 短题目通常是选择题或填空题
  924. if ($isShort && mb_strlen($stem) < 100) {
  925. return 'choice';
  926. }
  927. // 有证明、解答等关键词的是解答题
  928. if (strpos($stem, '证明') !== false || strpos($stem, '分析') !== false || strpos($stem, '求证') !== false) {
  929. return 'answer';
  930. }
  931. }
  932. // 默认是解答题
  933. return 'answer';
  934. }
  935. /**
  936. * 保留旧方法以兼容(但不再使用)
  937. */
  938. protected function autoGenerateQuestions(int $count)
  939. {
  940. // 调用新的批量生成方法
  941. $this->batchGenerateQuestions($count);
  942. }
  943. public function exportToPdf()
  944. {
  945. if (!$this->generatedPaperId) {
  946. Notification::make()
  947. ->title('错误')
  948. ->body('请先生成试卷')
  949. ->danger()
  950. ->send();
  951. return;
  952. }
  953. // 调用PDF导出API
  954. return redirect()->route('filament.admin.auth.intelligent-exam.pdf', [
  955. 'paper_id' => $this->generatedPaperId
  956. ]);
  957. }
  958. public function resetForm()
  959. {
  960. $this->reset([
  961. 'paperName', 'paperDescription', 'selectedKpCodes', 'selectedSkills',
  962. 'selectedTeacherId', 'selectedStudentId', 'filterByStudentWeakness', 'generatedQuestions', 'generatedPaperId'
  963. ]);
  964. $this->questionTypeRatio = [
  965. '选择题' => 40,
  966. '填空题' => 30,
  967. '解答题' => 30,
  968. ];
  969. $this->difficultyRatio = [
  970. '基础' => 50,
  971. '中等' => 35,
  972. '拔高' => 15,
  973. ];
  974. }
  975. /**
  976. * 监听TeacherStudentSelector组件的老师变化事件
  977. */
  978. #[On('teacherChanged')]
  979. public function onTeacherChanged(string $teacherId): void
  980. {
  981. \Illuminate\Support\Facades\Log::info('智能出题页面收到教师变更事件', [
  982. 'teacher_id' => $teacherId
  983. ]);
  984. $this->selectedTeacherId = $teacherId;
  985. // 清空学生选择和相关的筛选
  986. $this->selectedStudentId = null;
  987. $this->filterByStudentWeakness = false;
  988. }
  989. /**
  990. * 监听TeacherStudentSelector组件的学生变化事件
  991. */
  992. #[On('studentChanged')]
  993. public function onStudentChanged(string $teacherId, string $studentId): void
  994. {
  995. \Illuminate\Support\Facades\Log::info('智能出题页面收到学生变更事件', [
  996. 'teacher_id' => $teacherId,
  997. 'student_id' => $studentId
  998. ]);
  999. $this->selectedTeacherId = $teacherId;
  1000. $this->selectedStudentId = $studentId;
  1001. // ✅ 如果有学生选择,自动启用学生薄弱点筛选(但暂不勾选知识点)
  1002. if ($studentId) {
  1003. $this->filterByStudentWeakness = true;
  1004. \Illuminate\Support\Facades\Log::info('已自动启用薄弱点筛选', [
  1005. 'student_id' => $studentId,
  1006. 'filter_enabled' => $this->filterByStudentWeakness
  1007. ]);
  1008. // 不立即触发,让用户自己选择
  1009. } else {
  1010. // 如果清空了学生选择,也清空薄弱点筛选
  1011. $this->filterByStudentWeakness = false;
  1012. }
  1013. }
  1014. }