IntelligentExamGeneration.php 45 KB

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