IntelligentExamGeneration.php 47 KB

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