IntelligentExamGeneration.php 56 KB

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