IntelligentExamGeneration.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeGraphService;
  4. use App\Services\LearningAnalyticsService;
  5. use App\Services\QuestionBankService;
  6. use BackedEnum;
  7. use Filament\Notifications\Notification;
  8. use Filament\Pages\Page;
  9. use UnitEnum;
  10. use Livewire\Attributes\Computed;
  11. use Livewire\Attributes\On;
  12. use Livewire\Component;
  13. use Illuminate\Support\Facades\Cache; // Add Cache import
  14. class IntelligentExamGeneration extends Page
  15. {
  16. protected static ?string $title = '智能出卷';
  17. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
  18. protected static ?string $navigationLabel = '智能出卷';
  19. protected static string|UnitEnum|null $navigationGroup = '题库系统';
  20. protected static ?int $navigationSort = 3;
  21. protected string $view = 'filament.pages.intelligent-exam-generation-simple';
  22. // 基本配置
  23. public ?string $paperName = '';
  24. public ?string $paperDescription = '';
  25. public ?string $difficultyCategory = '基础'; // 基础/进阶/竞赛
  26. public int $totalQuestions = 20;
  27. public int $totalScore = 100;
  28. // 知识点和技能点选择
  29. public array $selectedKpCodes = [];
  30. public array $selectedSkills = [];
  31. // 题型配比
  32. public array $questionTypeRatio = [
  33. '选择题' => 40, // 百分比
  34. '填空题' => 30,
  35. '解答题' => 30,
  36. ];
  37. // 难度配比
  38. public array $difficultyRatio = [
  39. '基础' => 50, // 百分比
  40. '中等' => 35,
  41. '拔高' => 15,
  42. ];
  43. // 教师和学生相关
  44. public ?string $selectedTeacherId = null;
  45. public ?string $selectedStudentId = null;
  46. public bool $filterByStudentWeakness = false;
  47. // 状态
  48. public bool $isGenerating = false;
  49. public array $generatedQuestions = [];
  50. public ?string $generatedPaperId = null;
  51. #[Computed(cache: false)]
  52. public function knowledgePoints(): array
  53. {
  54. $result = app(KnowledgeGraphService::class)->listKnowledgePoints(1, 1000);
  55. return $result['data'] ?? [];
  56. }
  57. #[Computed(cache: false)]
  58. public function skills(): array
  59. {
  60. if (empty($this->selectedKpCodes)) {
  61. return [];
  62. }
  63. $allSkills = [];
  64. foreach ($this->selectedKpCodes as $kpCode) {
  65. $kpSkills = app(KnowledgeGraphService::class)->getSkillsByKnowledgePoint($kpCode);
  66. $allSkills = array_merge($allSkills, $kpSkills);
  67. }
  68. return $allSkills;
  69. }
  70. #[Computed(cache: false)]
  71. public function teachers(): array
  72. {
  73. try {
  74. // 首先获取teachers表中的老师
  75. $teachers = \App\Models\Teacher::query()->from('teachers as t')
  76. ->leftJoin('users as u', 't.teacher_id', '=', 'u.user_id')
  77. ->select(
  78. 't.teacher_id',
  79. 't.name',
  80. 't.subject',
  81. 'u.username',
  82. 'u.email'
  83. )
  84. ->orderBy('t.name')
  85. ->orderBy('t.name')
  86. ->get();
  87. // 如果有学生但没有对应的老师记录,添加一个"未知老师"条目
  88. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  89. $missingTeacherIds = \App\Models\Student::query()->from('students as s')
  90. ->distinct()
  91. ->whereNotIn('s.teacher_id', $teacherIds)
  92. ->pluck('teacher_id')
  93. ->toArray();
  94. // 转换 Collection 为数组以便合并和排序
  95. $teachersArray = $teachers->all();
  96. if (!empty($missingTeacherIds)) {
  97. foreach ($missingTeacherIds as $missingId) {
  98. $teachersArray[] = (object) [
  99. 'teacher_id' => $missingId,
  100. 'name' => '未知老师 (' . $missingId . ')',
  101. 'subject' => '未知',
  102. 'username' => null,
  103. 'email' => null
  104. ];
  105. }
  106. // 重新排序
  107. usort($teachersArray, function($a, $b) {
  108. return strcmp($a->name, $b->name);
  109. });
  110. return $teachersArray;
  111. }
  112. return $teachersArray;
  113. } catch (\Exception $e) {
  114. \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
  115. 'error' => $e->getMessage()
  116. ]);
  117. return [];
  118. }
  119. }
  120. #[Computed(cache: false)]
  121. public function students(): array
  122. {
  123. if (empty($this->selectedTeacherId)) {
  124. return [];
  125. }
  126. try {
  127. return \App\Models\Student::query()->from('students as s')
  128. ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
  129. ->where('s.teacher_id', $this->selectedTeacherId)
  130. ->select(
  131. 's.student_id',
  132. 's.name',
  133. 's.grade',
  134. 's.class_name',
  135. 'u.username',
  136. 'u.email'
  137. )
  138. ->orderBy('s.name')
  139. ->orderBy('s.name')
  140. ->get()
  141. ->all();
  142. } catch (\Exception $e) {
  143. \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
  144. 'teacher_id' => $this->selectedTeacherId,
  145. 'error' => $e->getMessage()
  146. ]);
  147. return [];
  148. }
  149. }
  150. #[Computed(cache: false)]
  151. public function studentWeaknesses(): array
  152. {
  153. if (!$this->selectedStudentId || !$this->filterByStudentWeakness) {
  154. return [];
  155. }
  156. try {
  157. return app(LearningAnalyticsService::class)->getStudentWeaknesses($this->selectedStudentId);
  158. } catch (\Exception $e) {
  159. \Illuminate\Support\Facades\Log::error('获取学生薄弱点失败', ['student_id' => $this->selectedStudentId, 'error' => $e->getMessage()]);
  160. return [];
  161. }
  162. }
  163. public function updatedSelectedTeacherId($value)
  164. {
  165. // 当教师选择变化时,清空之前选择的学生
  166. $this->selectedStudentId = null;
  167. }
  168. public function updatedSelectedStudentId($value)
  169. {
  170. if ($this->filterByStudentWeakness && $value) {
  171. // 根据学生薄弱点自动选择知识点
  172. $weaknesses = $this->studentWeaknesses;
  173. if (empty($weaknesses)) {
  174. Notification::make()
  175. ->title('提示')
  176. ->body('该学生暂无薄弱点数据,将随机生成题目或根据年级推荐')
  177. ->warning()
  178. ->send();
  179. // 保持选中状态,但不自动勾选知识点,或者可以选择取消勾选
  180. // $this->filterByStudentWeakness = false;
  181. } else {
  182. $this->selectedKpCodes = array_slice(array_column($weaknesses, 'kp_code'), 0, 5);
  183. }
  184. }
  185. }
  186. public function generateExam()
  187. {
  188. \Illuminate\Support\Facades\Log::info('generateExam called with studentId=' . $this->selectedStudentId);
  189. $this->validate([
  190. // 'paperName' => 'required|string|max:255', // 已移除必填
  191. 'totalQuestions' => 'required|integer|min:6|max:100',
  192. 'selectedStudentId' => 'required', // 必选学生
  193. ]);
  194. // 确保题目数量至少6题
  195. if ($this->totalQuestions < 6) {
  196. \Illuminate\Support\Facades\Log::warning('题目数量少于6题,已自动调整为6题', ['original' => $this->totalQuestions]);
  197. $this->totalQuestions = 6;
  198. }
  199. // 自动生成试卷名称
  200. if (empty($this->paperName)) {
  201. $studentName = '学生' . $this->selectedStudentId;
  202. // 尝试从 students 列表中获取真实姓名
  203. foreach ($this->students as $student) {
  204. if (is_array($student)) {
  205. $sId = $student['student_id'] ?? '';
  206. if ($sId == $this->selectedStudentId) {
  207. $studentName = $student['name'] ?? $studentName;
  208. break;
  209. }
  210. } elseif (is_object($student)) {
  211. if ($student->student_id == $this->selectedStudentId) {
  212. $studentName = $student->name ?? $studentName;
  213. break;
  214. }
  215. }
  216. }
  217. $this->paperName = $studentName . '_' . now()->format('Ymd_His') . '_智能试卷';
  218. }
  219. $this->isGenerating = true;
  220. try {
  221. // 使用LearningAnalyticsService进行智能出卷
  222. $learningAnalyticsService = app(LearningAnalyticsService::class);
  223. // 准备出卷参数
  224. $examParams = [
  225. 'student_id' => $this->selectedStudentId,
  226. 'total_questions' => $this->totalQuestions,
  227. 'kp_codes' => $this->selectedKpCodes,
  228. 'skills' => $this->selectedSkills,
  229. 'question_type_ratio' => $this->questionTypeRatio,
  230. 'difficulty_ratio' => $this->difficultyRatio,
  231. ];
  232. // 调用智能出卷API
  233. $result = $learningAnalyticsService->generateIntelligentExam($examParams);
  234. if (!$result['success']) {
  235. throw new \Exception($result['message']);
  236. }
  237. $questions = $result['questions'];
  238. if (count($questions) < $this->totalQuestions) {
  239. // 题库不足时,批量生成题目(使用题库的多AI模型并行生成功能)
  240. $neededCount = $this->totalQuestions - count($questions);
  241. // 生成比需求更多的题目,储备起来供后续使用
  242. $generateCount = max($neededCount, 20); // 至少生成20道题作为储备
  243. \Illuminate\Support\Facades\Log::info("题库题目不足,需要补充 {$neededCount} 道题,准备批量生成 {$generateCount} 道题", [
  244. 'current_count' => count($questions),
  245. 'needed' => $neededCount,
  246. 'will_generate' => $generateCount
  247. ]);
  248. // 只生成一次,生成足够多的题目(题库服务支持多AI模型并行)
  249. $this->batchGenerateQuestions($generateCount);
  250. // 重新从题库获取题目
  251. $questionBankService = app(QuestionBankService::class);
  252. $params = [
  253. 'kp_codes' => implode(',', $this->selectedKpCodes),
  254. 'limit' => $this->totalQuestions * 2 // 获取更多题目用于筛选
  255. ];
  256. if (!empty($this->selectedSkills)) {
  257. $params['skills'] = implode(',', $this->selectedSkills);
  258. }
  259. if ($this->selectedStudentId) {
  260. $params['exclude_student_questions'] = $this->selectedStudentId;
  261. }
  262. $newResponse = $questionBankService->filterQuestions($params);
  263. // 合并题目并去重
  264. if (!empty($newResponse['data'])) {
  265. $existingIds = array_column($questions, 'id');
  266. foreach ($newResponse['data'] as $newQ) {
  267. if (!in_array($newQ['id'], $existingIds)) {
  268. $questions[] = $newQ;
  269. }
  270. }
  271. }
  272. \Illuminate\Support\Facades\Log::info("批量生成完成,当前题库题目数量: " . count($questions), [
  273. 'generated_count' => $generateCount,
  274. 'total_in_bank' => count($questions)
  275. ]);
  276. }
  277. // 2. 限制试卷题目数量为用户要求的数量
  278. if (count($questions) > $this->totalQuestions) {
  279. // 根据题型配比和难度配比对题目进行筛选和排序
  280. $questions = $this->selectBestQuestions(
  281. $questions,
  282. $this->totalQuestions,
  283. $this->difficultyCategory,
  284. $this->totalScore,
  285. $this->questionTypeRatio
  286. );
  287. \Illuminate\Support\Facades\Log::info("从 " . count($questions) . " 道题中筛选出 " . count($questions) . " 道题,难度分类: {$this->difficultyCategory}, 总分: {$this->totalScore}");
  288. }
  289. // 3. 检查题型完整性(至少保证每种题型都有题目)
  290. $checkResult = $this->ensureQuestionTypeCompleteness($questions, $this->totalQuestions);
  291. if ($checkResult['missing_types']) {
  292. \Illuminate\Support\Facades\Log::warning("检测到缺失题型,将自动生成", [
  293. 'missing_types' => $checkResult['missing_types'],
  294. 'current_count' => $checkResult['current_count']
  295. ]);
  296. // 批量生成缺失题型的题目
  297. $this->batchGenerateMissingTypes($checkResult['missing_types']);
  298. // 重新获取题目
  299. $questionBankService = app(QuestionBankService::class);
  300. $params = [
  301. 'kp_codes' => implode(',', $this->selectedKpCodes),
  302. 'limit' => $this->totalQuestions * 2
  303. ];
  304. if (!empty($this->selectedSkills)) {
  305. $params['skills'] = implode(',', $this->selectedSkills);
  306. }
  307. if ($this->selectedStudentId) {
  308. $params['exclude_student_questions'] = $this->selectedStudentId;
  309. }
  310. $newResponse = $questionBankService->filterQuestions($params);
  311. if (!empty($newResponse['data'])) {
  312. $questions = array_merge($questions, $newResponse['data']);
  313. }
  314. // 再次筛选
  315. $questions = $this->selectBestQuestions(
  316. $questions,
  317. $this->totalQuestions,
  318. $this->difficultyCategory,
  319. $this->totalScore,
  320. $this->questionTypeRatio
  321. );
  322. }
  323. // 2. 为题目添加类型信息(如果缺失)
  324. foreach ($questions as &$question) {
  325. if (!isset($question['question_type'])) {
  326. $question['question_type'] = $this->determineQuestionType($question);
  327. \Illuminate\Support\Facades\Log::debug('为题目添加类型', [
  328. 'question_id' => $question['id'] ?? '',
  329. 'added_type' => $question['question_type']
  330. ]);
  331. }
  332. }
  333. unset($question); // 释放引用
  334. // 3. 生成试卷数据
  335. $examData = [
  336. 'paper_name' => $this->paperName,
  337. 'paper_description' => $this->paperDescription,
  338. 'difficulty_category' => $this->difficultyCategory,
  339. 'questions' => $questions,
  340. 'total_score' => $this->totalScore,
  341. 'total_questions' => count($questions),
  342. 'student_id' => $this->selectedStudentId,
  343. 'teacher_id' => $this->selectedTeacherId,
  344. ];
  345. // 4. 保存到数据库
  346. $questionBankService = app(QuestionBankService::class);
  347. $paperId = $questionBankService->saveExamToDatabase($examData);
  348. // 如果保存返回 null,使用默认占位 ID,防止 UI 不显示
  349. if (empty($paperId)) {
  350. $paperId = 'demo_' . $this->selectedStudentId . '_' . now()->format('YmdHis');
  351. }
  352. \Illuminate\Support\Facades\Log::info('Generated paper ID: ' . $paperId);
  353. $this->generatedPaperId = $paperId;
  354. // 将生成的试卷数据缓存,以便 PDF 预览时使用(缓存 1 小时)
  355. \Illuminate\Support\Facades\Log::info('缓存试卷数据', [
  356. 'paper_id' => $paperId,
  357. 'question_count' => count($questions),
  358. 'question_types' => array_column($questions, 'question_type')
  359. ]);
  360. Cache::put('generated_exam_' . $paperId, $examData, now()->addHour());
  361. $this->generatedQuestions = $questions;
  362. $stats = $result['stats'] ?? [];
  363. $message = "已生成包含 " . count($questions) . " 道题的试卷";
  364. if (!empty($stats['weakness_targeted'])) {
  365. $message .= ",其中针对薄弱点 " . $stats['weakness_targeted'] . " 题";
  366. }
  367. Notification::make()
  368. ->title('试卷生成成功')
  369. ->body($message)
  370. ->success()
  371. ->send();
  372. } catch (\Exception $e) {
  373. // 记录错误并提供回退的试卷 ID,防止 UI 无显示
  374. \Illuminate\Support\Facades\Log::error('生成试卷失败', ['error' => $e->getMessage()]);
  375. $fallbackId = 'demo_' . $this->selectedStudentId . '_' . now()->format('YmdHis');
  376. $this->generatedPaperId = $fallbackId;
  377. $this->generatedQuestions = [];
  378. Notification::make()
  379. ->title('试卷生成失败,使用默认试卷')
  380. ->body('错误: ' . $e->getMessage() . "\n已生成默认试卷 ID: $fallbackId")
  381. ->warning()
  382. ->send();
  383. } finally {
  384. $this->isGenerating = false;
  385. }
  386. }
  387. /**
  388. * 批量生成题目(使用题库的多AI模型并行功能)
  389. */
  390. protected function batchGenerateQuestions(int $count)
  391. {
  392. $questionBankService = app(QuestionBankService::class);
  393. $generatedTasks = [];
  394. // 只生成一次,使用所有选中的知识点
  395. $allKpCodes = $this->selectedKpCodes;
  396. if (empty($allKpCodes)) {
  397. // 如果没有选中知识点,使用默认知识点
  398. $allKpCodes = ['R01']; // 默认知识点
  399. }
  400. // 对每个知识点生成题目
  401. foreach ($allKpCodes as $kpCode) {
  402. \Illuminate\Support\Facades\Log::info("为知识点 {$kpCode} 生成题目", [
  403. 'count' => $count,
  404. 'skills' => $this->selectedSkills
  405. ]);
  406. $result = $questionBankService->generateIntelligentQuestions([
  407. 'kp_code' => $kpCode,
  408. 'skills' => $this->selectedSkills,
  409. 'count' => $count,
  410. 'difficulty_distribution' => $this->difficultyRatio,
  411. ]);
  412. if ($result['success'] && isset($result['task_id'])) {
  413. $generatedTasks[] = [
  414. 'task_id' => $result['task_id'],
  415. 'kp_code' => $kpCode
  416. ];
  417. \Illuminate\Support\Facades\Log::info("已启动生成任务: {$result['task_id']} for {$kpCode}");
  418. } else {
  419. \Illuminate\Support\Facades\Log::warning("生成任务启动失败", [
  420. 'kp_code' => $kpCode,
  421. 'result' => $result
  422. ]);
  423. }
  424. }
  425. // 等待所有任务完成(最多等待60秒)
  426. if (!empty($generatedTasks)) {
  427. $maxWaitTime = 60; // 增加最大等待时间
  428. $startTime = time();
  429. \Illuminate\Support\Facades\Log::info("等待 {$maxWaitTime} 秒,所有生成任务完成", [
  430. 'tasks' => array_column($generatedTasks, 'task_id')
  431. ]);
  432. while (time() - $startTime < $maxWaitTime) {
  433. $allCompleted = true;
  434. $completedTasks = [];
  435. $runningTasks = [];
  436. foreach ($generatedTasks as $task) {
  437. $taskStatus = $questionBankService->getTaskStatus($task['task_id']);
  438. if (!$taskStatus) {
  439. $allCompleted = false;
  440. $runningTasks[] = $task['task_id'];
  441. continue;
  442. }
  443. $status = $taskStatus['status'] ?? '';
  444. if ($status === 'completed') {
  445. $completedTasks[] = $task['task_id'];
  446. } elseif ($status === 'failed') {
  447. \Illuminate\Support\Facades\Log::error("生成任务失败", [
  448. 'task_id' => $task['task_id'],
  449. 'error' => $taskStatus['error'] ?? '未知错误'
  450. ]);
  451. // 任务失败继续等待其他任务
  452. } else {
  453. $allCompleted = false;
  454. $runningTasks[] = $task['task_id'];
  455. }
  456. }
  457. if ($allCompleted) {
  458. \Illuminate\Support\Facades\Log::info('所有AI生成任务已完成', [
  459. 'completed' => $completedTasks,
  460. 'tasks' => $generatedTasks
  461. ]);
  462. break;
  463. }
  464. // 每10秒输出一次进度
  465. $elapsed = time() - $startTime;
  466. if ($elapsed % 10 < 2) {
  467. \Illuminate\Support\Facades\Log::info("生成进度", [
  468. 'elapsed' => $elapsed,
  469. 'completed' => count($completedTasks),
  470. 'running' => count($runningTasks),
  471. 'total' => count($generatedTasks)
  472. ]);
  473. }
  474. // 等待3秒后重试
  475. sleep(3);
  476. }
  477. $waitTime = time() - $startTime;
  478. \Illuminate\Support\Facades\Log::info('AI生成任务等待完成', [
  479. 'wait_time' => $waitTime,
  480. 'tasks' => $generatedTasks
  481. ]);
  482. }
  483. }
  484. /**
  485. * 根据题型配比和难度配比,从大量题目中筛选出最佳题目
  486. */
  487. protected function selectBestQuestions(
  488. array $questions,
  489. int $targetCount,
  490. string $difficultyCategory,
  491. float $totalScore,
  492. array $questionTypeRatio
  493. ): array {
  494. if (count($questions) <= $targetCount) {
  495. return $questions;
  496. }
  497. \Illuminate\Support\Facades\Log::info("开始筛选题目", [
  498. 'total_available' => count($questions),
  499. 'target_count' => $targetCount,
  500. 'difficulty_category' => $difficultyCategory,
  501. 'total_score' => $totalScore,
  502. 'type_ratio' => $questionTypeRatio
  503. ]);
  504. // 1. 按题型分类题目
  505. $categorizedQuestions = [
  506. 'choice' => [], // 选择题
  507. 'fill' => [], // 填空题
  508. 'answer' => [], // 解答题
  509. ];
  510. foreach ($questions as $question) {
  511. $type = $this->determineQuestionType($question);
  512. if (!isset($categorizedQuestions[$type])) {
  513. $type = 'answer';
  514. }
  515. $categorizedQuestions[$type][] = $question;
  516. }
  517. // 2. 根据难度分类筛选题目
  518. $difficultyFilteredQuestions = $this->filterByDifficulty($categorizedQuestions, $difficultyCategory);
  519. // 3. 根据题型配比计算每种题型应选择的题目数量
  520. // 先确保每种题型至少有1题(如果题目数量>=3)
  521. $selectedQuestions = [];
  522. $totalSelected = 0;
  523. // 优先保证每种题型至少一题(适用于总题目数>=3的情况)
  524. if ($targetCount >= 3) {
  525. foreach (['choice', 'fill', 'answer'] as $typeKey) {
  526. if (!empty($difficultyFilteredQuestions[$typeKey])) {
  527. // 随机选择1道该题型的题目
  528. $randomIndex = array_rand($difficultyFilteredQuestions[$typeKey]);
  529. $selectedQuestions[] = $difficultyFilteredQuestions[$typeKey][$randomIndex];
  530. $totalSelected++;
  531. \Illuminate\Support\Facades\Log::info("保证题型最少题目: {$typeKey}", [
  532. 'selected_index' => $randomIndex
  533. ]);
  534. } else {
  535. // 如果某题型没有题目,标记为缺失
  536. \Illuminate\Support\Facades\Log::warning("题型缺失: {$typeKey},需要从其他题型补充");
  537. }
  538. }
  539. }
  540. // 如果题目数量不足3题,则跳过最少保证
  541. // 根据题型配比计算每种题型应选择的题目数量
  542. foreach ($questionTypeRatio as $type => $ratio) {
  543. $typeKey = $type === '选择题' ? 'choice' : ($type === '填空题' ? 'fill' : 'answer');
  544. $countForType = floor($targetCount * $ratio / 100);
  545. // 如果总题目数>=3,已经为每种题型分配了1题,需要从剩余数量中扣除
  546. if ($targetCount >= 3 && $totalSelected > 0) {
  547. // 重新计算:确保至少1题 + 按比例分配的额外题目
  548. $baseCount = 1; // 最少1题
  549. $extraCount = floor(($targetCount - 3) * $ratio / 100); // 剩余题目按比例分配
  550. $countForType = $baseCount + $extraCount;
  551. }
  552. if ($countForType > 0 && !empty($difficultyFilteredQuestions[$typeKey])) {
  553. // 按难度排序后选择该题型的一部分题目
  554. $availableCount = count($difficultyFilteredQuestions[$typeKey]);
  555. $takeCount = min($countForType, $availableCount, $targetCount - $totalSelected);
  556. // 如果该题型已经在最少保证中分配过,需要排除已分配的题目
  557. if ($targetCount >= 3 && isset($selectedQuestions)) {
  558. // 重新获取题目,排除已选择的
  559. $availableQuestions = $difficultyFilteredQuestions[$typeKey];
  560. $takeCount = min($takeCount, $availableCount, $targetCount - $totalSelected);
  561. if ($takeCount > 0) {
  562. $selectedFromType = array_rand(array_flip(array_keys($availableQuestions)), $takeCount);
  563. if (!is_array($selectedFromType)) {
  564. $selectedFromType = [$selectedFromType];
  565. }
  566. foreach ($selectedFromType as $index) {
  567. $selectedQuestions[] = $availableQuestions[$index];
  568. }
  569. $totalSelected += $takeCount;
  570. }
  571. } else {
  572. // 正常分配
  573. if ($takeCount > 0) {
  574. $selectedFromType = array_rand(array_flip(array_keys($difficultyFilteredQuestions[$typeKey])), $takeCount);
  575. if (!is_array($selectedFromType)) {
  576. $selectedFromType = [$selectedFromType];
  577. }
  578. foreach ($selectedFromType as $index) {
  579. $selectedQuestions[] = $difficultyFilteredQuestions[$typeKey][$index];
  580. }
  581. $totalSelected += $takeCount;
  582. }
  583. }
  584. \Illuminate\Support\Facades\Log::info("{$type}题型筛选结果", [
  585. 'available' => $availableCount,
  586. 'take' => $takeCount,
  587. 'ratio' => $ratio,
  588. 'type' => $typeKey
  589. ]);
  590. }
  591. }
  592. // 4. 如果还有空缺,随机补充其他题型
  593. while ($totalSelected < $targetCount && count($selectedQuestions) < count($questions)) {
  594. $randomQuestion = $questions[array_rand($questions)];
  595. if (!in_array($randomQuestion, $selectedQuestions)) {
  596. $selectedQuestions[] = $randomQuestion;
  597. $totalSelected++;
  598. }
  599. }
  600. // 5. 打乱题目顺序
  601. shuffle($selectedQuestions);
  602. $finalQuestions = array_slice($selectedQuestions, 0, $targetCount);
  603. \Illuminate\Support\Facades\Log::info("题目筛选完成", [
  604. 'selected_count' => count($finalQuestions),
  605. 'difficulty_category' => $difficultyCategory,
  606. 'total_score' => $totalScore
  607. ]);
  608. return $finalQuestions;
  609. }
  610. /**
  611. * 检查题型完整性,确保每种题型至少有一题
  612. */
  613. protected function ensureQuestionTypeCompleteness(array $questions, int $targetCount): array
  614. {
  615. $result = [
  616. 'missing_types' => [],
  617. 'current_count' => count($questions),
  618. 'has_choice' => false,
  619. 'has_fill' => false,
  620. 'has_answer' => false,
  621. ];
  622. // 统计各题型数量
  623. $choiceCount = $fillCount = $answerCount = 0;
  624. foreach ($questions as $q) {
  625. $type = $this->determineQuestionType($q);
  626. if ($type === 'choice') {
  627. $choiceCount++;
  628. $result['has_choice'] = true;
  629. } elseif ($type === 'fill') {
  630. $fillCount++;
  631. $result['has_fill'] = true;
  632. } elseif ($type === 'answer') {
  633. $answerCount++;
  634. $result['has_answer'] = true;
  635. }
  636. }
  637. // 如果题目数量>=3,确保每种题型至少1题
  638. if ($targetCount >= 3) {
  639. if (!$result['has_choice']) {
  640. $result['missing_types'][] = 'choice';
  641. }
  642. if (!$result['has_fill']) {
  643. $result['missing_types'][] = 'fill';
  644. }
  645. if (!$result['has_answer']) {
  646. $result['missing_types'][] = 'answer';
  647. }
  648. }
  649. \Illuminate\Support\Facades\Log::info("题型完整性检查", [
  650. 'choice_count' => $choiceCount,
  651. 'fill_count' => $fillCount,
  652. 'answer_count' => $answerCount,
  653. 'missing_types' => $result['missing_types']
  654. ]);
  655. return $result;
  656. }
  657. /**
  658. * 批量生成缺失题型的题目
  659. */
  660. protected function batchGenerateMissingTypes(array $missingTypes): void
  661. {
  662. if (empty($missingTypes)) {
  663. return;
  664. }
  665. \Illuminate\Support\Facades\Log::info("开始生成缺失题型题目", ['missing_types' => $missingTypes]);
  666. // 为每个缺失题型生成3-5道题
  667. foreach ($missingTypes as $type) {
  668. $generateCount = 5; // 每个缺失题型生成5道题
  669. \Illuminate\Support\Facades\Log::info("为缺失题型 {$type} 生成 {$generateCount} 道题");
  670. foreach ($this->selectedKpCodes as $kpCode) {
  671. $questionBankService = app(QuestionBankService::class);
  672. // 根据题型设置特定的技能点
  673. $skills = $this->selectedSkills;
  674. if ($type === 'choice') {
  675. $skills[] = '选择题专项练习';
  676. } elseif ($type === 'fill') {
  677. $skills[] = '填空题专项练习';
  678. } elseif ($type === 'answer') {
  679. $skills[] = '解答题专项练习';
  680. }
  681. $result = $questionBankService->generateIntelligentQuestions([
  682. 'kp_code' => $kpCode,
  683. 'skills' => $skills,
  684. 'count' => $generateCount,
  685. 'difficulty_distribution' => $this->difficultyRatio,
  686. ]);
  687. if ($result['success'] && isset($result['task_id'])) {
  688. \Illuminate\Support\Facades\Log::info("已启动生成任务", [
  689. 'type' => $type,
  690. 'task_id' => $result['task_id'],
  691. 'kp_code' => $kpCode
  692. ]);
  693. }
  694. }
  695. }
  696. // 缺失题型生成任务已启动,由于题目生成是异步的,
  697. // 将在预览时动态获取最新生成的题目
  698. \Illuminate\Support\Facades\Log::info("缺失题型生成任务已启动,将在预览时动态获取");
  699. }
  700. /**
  701. * 根据难度分类筛选题目
  702. */
  703. protected function filterByDifficulty(array $categorizedQuestions, string $difficultyCategory): array
  704. {
  705. $filtered = [];
  706. $difficultyRanges = [
  707. '基础' => [0, 0.4],
  708. '中等' => [0.3, 0.7],
  709. '拔高' => [0.6, 1.0]
  710. ];
  711. $targetRange = $difficultyRanges[$difficultyCategory] ?? [0, 1.0];
  712. foreach ($categorizedQuestions as $type => $questions) {
  713. $filtered[$type] = [];
  714. foreach ($questions as $question) {
  715. $difficulty = floatval($question['difficulty'] ?? 0.5);
  716. if ($difficulty >= $targetRange[0] && $difficulty <= $targetRange[1]) {
  717. $filtered[$type][] = $question;
  718. } else {
  719. // 保留部分越界题目(如果该难度题目不足)
  720. if (count($filtered[$type]) < 2) {
  721. $filtered[$type][] = $question;
  722. }
  723. }
  724. }
  725. }
  726. \Illuminate\Support\Facades\Log::info("难度筛选结果", [
  727. 'difficulty_category' => $difficultyCategory,
  728. 'difficulty_range' => $targetRange,
  729. 'choice_count' => count($filtered['choice']),
  730. 'fill_count' => count($filtered['fill']),
  731. 'answer_count' => count($filtered['answer'])
  732. ]);
  733. return $filtered;
  734. }
  735. /**
  736. * 根据题目标签或内容判断题型
  737. */
  738. protected function determineQuestionType(array $question): string
  739. {
  740. $tags = $question['tags'] ?? '';
  741. $stem = $question['stem'] ?? '';
  742. // 1. 根据标签判断
  743. if (is_string($tags)) {
  744. if (strpos($tags, '选择') !== false || strpos($tags, '选择题') !== false) {
  745. return 'choice';
  746. }
  747. if (strpos($tags, '填空') !== false || strpos($tags, '填空题') !== false) {
  748. return 'fill';
  749. }
  750. if (strpos($tags, '解答') !== false || strpos($tags, '简答') !== false || strpos($tags, '证明') !== false) {
  751. return 'answer';
  752. }
  753. }
  754. // 2. 根据题干内容判断 - 选择题(有括号的或包含选项A.B.C.D.)
  755. if (is_string($stem)) {
  756. // 检查全角括号
  757. if (strpos($stem, '()') !== false) {
  758. return 'choice';
  759. }
  760. // 检查半角括号
  761. if (strpos($stem, '()') !== false) {
  762. return 'choice';
  763. }
  764. // 检查选项格式 A. B. C. D.(支持跨行匹配)
  765. if (preg_match('/[A-D]\.\s/m', $stem)) {
  766. return 'choice';
  767. }
  768. }
  769. // 3. 根据题干内容判断 - 填空题(有下划线)
  770. if (is_string($stem) && (strpos($stem, '____') !== false || strpos($stem, '______') !== false)) {
  771. return 'fill';
  772. }
  773. // 4. 根据题干长度和内容判断(启发式)
  774. if (is_string($stem)) {
  775. $shortQuestions = ['下列', '判断', '选择', '计算', '求'];
  776. $isShort = false;
  777. foreach ($shortQuestions as $keyword) {
  778. if (strpos($stem, $keyword) !== false) {
  779. $isShort = true;
  780. break;
  781. }
  782. }
  783. // 短题目通常是选择题或填空题
  784. if ($isShort && mb_strlen($stem) < 100) {
  785. return 'choice';
  786. }
  787. // 有证明、解答等关键词的是解答题
  788. if (strpos($stem, '证明') !== false || strpos($stem, '分析') !== false || strpos($stem, '求证') !== false) {
  789. return 'answer';
  790. }
  791. }
  792. // 默认是解答题
  793. return 'answer';
  794. }
  795. /**
  796. * 保留旧方法以兼容(但不再使用)
  797. */
  798. protected function autoGenerateQuestions(int $count)
  799. {
  800. // 调用新的批量生成方法
  801. $this->batchGenerateQuestions($count);
  802. }
  803. public function exportToPdf()
  804. {
  805. if (!$this->generatedPaperId) {
  806. Notification::make()
  807. ->title('错误')
  808. ->body('请先生成试卷')
  809. ->danger()
  810. ->send();
  811. return;
  812. }
  813. // 调用PDF导出API
  814. return redirect()->route('filament.admin.auth.intelligent-exam.pdf', [
  815. 'paper_id' => $this->generatedPaperId
  816. ]);
  817. }
  818. public function resetForm()
  819. {
  820. $this->reset([
  821. 'paperName', 'paperDescription', 'selectedKpCodes', 'selectedSkills',
  822. 'selectedTeacherId', 'selectedStudentId', 'filterByStudentWeakness', 'generatedQuestions', 'generatedPaperId'
  823. ]);
  824. $this->questionTypeRatio = [
  825. '选择题' => 40,
  826. '填空题' => 30,
  827. '解答题' => 30,
  828. ];
  829. $this->difficultyRatio = [
  830. '基础' => 50,
  831. '中等' => 35,
  832. '拔高' => 15,
  833. ];
  834. }
  835. }