IntelligentExamGeneration.php 56 KB

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