IntelligentExamGeneration.php 70 KB

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