IntelligentExamGeneration.php 67 KB

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