IntelligentExamGeneration.php 56 KB

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