IntelligentExamGeneration.php 64 KB

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