| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737 |
- <?php
- namespace App\Filament\Pages;
- use App\Filament\Traits\HasUserRole;
- use App\Services\KnowledgeGraphService;
- use App\Services\KnowledgeMasteryService;
- use App\Services\MasteryCalculator;
- use App\Services\QuestionBankService;
- use App\Models\Student;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- use Livewire\Attributes\On;
- use Livewire\Attributes\Reactive;
- use Livewire\Component;
- use Illuminate\Support\Facades\Cache; // Add Cache import
- class IntelligentExamGeneration extends Page
- {
- use HasUserRole;
- protected static ?string $title = '智能出卷';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
- protected static ?string $navigationLabel = '智能出卷';
- protected static string|UnitEnum|null $navigationGroup = '卷子生成管理';
- protected static ?int $navigationSort = 1;
- protected string $view = 'filament.pages.intelligent-exam-generation-simple';
- // 基本配置
- public ?string $paperName = '';
- public ?string $paperDescription = '';
- public ?string $selectedGrade = '初中'; // 初中/七年级/八年级/九年级
- public ?string $difficultyCategory = '基础'; // 基础/进阶/竞赛
- public int $totalQuestions = 20;
- public int $totalScore = 100;
- public bool $includeAnswer = false; // 是否生成答案(默认不生成参考答案)
- // 知识点和技能点选择
- public array $selectedKpCodes = [];
- public array $selectedSkills = [];
- // 题型配比
- public array $questionTypeRatio = [
- '选择题' => 40, // 百分比,支持 4:2:4 或 5:2:3
- '填空题' => 20,
- '解答题' => 40,
- ];
- // 难度配比
- public array $difficultyRatio = [
- '基础' => 50, // 百分比
- '中等' => 35,
- '拔高' => 15,
- ];
- // 难度多选(空数组表示随机难度)
- public array $selectedDifficultyLevels = [];
- // 教师和学生相关
- public ?string $selectedTeacherId = null;
- public ?string $selectedStudentId = null;
- public bool $filterByStudentWeakness = false;
- // 状态
- public bool $isGenerating = false;
- public array $generatedQuestions = [];
- public ?string $generatedPaperId = null;
- #[Computed]
- public function canGenerate(): bool
- {
- return !$this->isGenerating
- && $this->totalQuestions >= 6
- && !empty($this->selectedTeacherId)
- && !empty($this->selectedStudentId);
- // 注意:不强制要求选择知识点,没有选择时将按年级或随机生成
- }
- public function mount(): void
- {
- // 初始化用户角色检查
- $this->initializeUserRole();
- // 如果是老师,自动选择当前老师
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $this->selectedTeacherId = $teacherId;
- }
- } else {
- // 管理员模式:从 URL 参数获取
- $this->selectedTeacherId = request()->query('teacher_id', $this->selectedTeacherId);
- }
- $this->selectedStudentId = request()->query('student_id', $this->selectedStudentId);
- // 如果只传了 student_id,补全 teacher_id 以便学生下拉加载
- if ($this->selectedStudentId && !$this->selectedTeacherId) {
- $student = Student::find($this->selectedStudentId);
- if ($student && $student->teacher_id) {
- $this->selectedTeacherId = (string) $student->teacher_id;
- }
- }
- // 注意:不初始化 selectedDifficultyLevels,保持为空数组表示随机
- // 如果用户不选择任何难度,系统将随机生成题目
- }
- #[Computed(cache: false)]
- public function knowledgePoints(): array
- {
- try {
- $result = app(KnowledgeGraphService::class)->listKnowledgePoints(1, 1000);
- $knowledgePoints = $result['data'] ?? [];
- \Illuminate\Support\Facades\Log::info('知识点列表获取成功', [
- 'count' => count($knowledgePoints),
- 'first_item' => !empty($knowledgePoints) ? $knowledgePoints[0] : null
- ]);
- return $knowledgePoints;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('获取知识点列表失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed(cache: false)]
- public function skills(): array
- {
- if (empty($this->selectedKpCodes)) {
- return [];
- }
- $allSkills = [];
- foreach ($this->selectedKpCodes as $kpCode) {
- $kpSkills = app(KnowledgeGraphService::class)->getSkillsByKnowledgePoint($kpCode);
- $allSkills = array_merge($allSkills, $kpSkills);
- }
- return $allSkills;
- }
- /**
- * 获取当前选择的教师名称
- */
- public function getSelectedTeacherName(): string
- {
- if (empty($this->selectedTeacherId)) {
- return '未选择';
- }
- try {
- $teacher = \App\Models\Teacher::query()
- ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
- ->where('teachers.teacher_id', $this->selectedTeacherId)
- ->select(
- 'teachers.name',
- 'teachers.subject',
- 'u.username'
- )
- ->first();
- if ($teacher) {
- $name = trim($teacher->name ?? $this->selectedTeacherId);
- $subject = $teacher->subject ? " ({$teacher->subject})" : '';
- $username = $teacher->username ? " [{$teacher->username}]" : '';
- return "{$name}{$subject}{$username}";
- }
- return $this->selectedTeacherId;
- } catch (\Exception $e) {
- return $this->selectedTeacherId;
- }
- }
- /**
- * 获取当前选择的学生名称
- */
- public function getSelectedStudentName(): string
- {
- if (empty($this->selectedStudentId) || empty($this->selectedTeacherId)) {
- return '未选择';
- }
- try {
- $student = \App\Models\Student::query()
- ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
- ->where('students.student_id', $this->selectedStudentId)
- ->where('students.teacher_id', $this->selectedTeacherId)
- ->select(
- 'students.name',
- 'students.grade',
- 'students.class_name',
- 'u.username'
- )
- ->first();
- if ($student) {
- $name = trim($student->name ?? $this->selectedStudentId);
- $gradeClass = trim("{$student->grade} - {$student->class_name}");
- $username = $student->username ? " [{$student->username}]" : '';
- return "{$name} ({$gradeClass}){$username}";
- }
- return $this->selectedStudentId;
- } catch (\Exception $e) {
- return $this->selectedStudentId;
- }
- }
- #[Computed(cache: false)]
- public function teachers(): array
- {
- try {
- $query = \App\Models\Teacher::query()->from('teachers as t')
- ->leftJoin('users as u', 't.teacher_id', '=', 'u.user_id')
- ->select(
- 't.teacher_id',
- 't.name',
- 't.subject',
- 'u.username',
- 'u.email'
- );
- // 如果是老师,只返回自己
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $query->where('t.teacher_id', $teacherId);
- }
- }
- $teachers = $query->orderBy('t.name')->get();
- // 如果有学生但没有对应的老师记录,添加一个"未知老师"条目
- $teacherIds = $teachers->pluck('teacher_id')->toArray();
- $missingTeacherIds = \App\Models\Student::query()->from('students as s')
- ->distinct()
- ->whereNotIn('s.teacher_id', $teacherIds)
- ->pluck('teacher_id')
- ->toArray();
- // 转换 Collection 为数组以便合并和排序
- $teachersArray = $teachers->all();
- if (!empty($missingTeacherIds)) {
- foreach ($missingTeacherIds as $missingId) {
- $teachersArray[] = (object) [
- 'teacher_id' => $missingId,
- 'name' => '未知老师 (' . $missingId . ')',
- 'subject' => '未知',
- 'username' => null,
- 'email' => null
- ];
- }
- // 重新排序
- usort($teachersArray, function($a, $b) {
- return strcmp($a->name, $b->name);
- });
- return $teachersArray;
- }
- return $teachersArray;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed(cache: false)]
- public function students(): array
- {
- if (empty($this->selectedTeacherId)) {
- return [];
- }
- try {
- $students = \App\Models\Student::query()->from('students as s')
- ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
- ->where('s.teacher_id', $this->selectedTeacherId)
- ->select(
- 's.student_id',
- 's.name',
- 's.grade',
- 's.class_name',
- 'u.username',
- 'u.email'
- )
- ->orderBy('s.grade')
- ->orderBy('s.class_name')
- ->orderBy('s.name')
- ->get()
- ->all();
- \Illuminate\Support\Facades\Log::info('智能出题页面加载学生列表', [
- 'teacher_id' => $this->selectedTeacherId,
- 'student_count' => count($students)
- ]);
- return $students;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
- 'teacher_id' => $this->selectedTeacherId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed(cache: false)]
- public function studentWeaknesses(): array
- {
- if (!$this->selectedStudentId || !$this->filterByStudentWeakness) {
- \Illuminate\Support\Facades\Log::info('学生薄弱点未加载', [
- 'student_id' => $this->selectedStudentId,
- 'filter_enabled' => $this->filterByStudentWeakness
- ]);
- return [];
- }
- try {
- // 使用本地MasteryCalculator替代LearningAnalyticsService
- $masteryCalculator = app(MasteryCalculator::class);
- $overview = $masteryCalculator->getStudentMasteryOverview($this->selectedStudentId);
- // 从概览中提取薄弱点(掌握度 < 0.5)
- $weaknesses = [];
- foreach ($overview['details'] ?? [] as $detail) {
- $masteryLevel = floatval($detail->mastery_level ?? 0);
- if ($masteryLevel < 0.5) {
- $weaknesses[] = [
- 'kp_code' => $detail->kp_code,
- 'mastery' => $masteryLevel
- ];
- }
- }
- \Illuminate\Support\Facades\Log::info('获取学生薄弱点成功(本地服务)', [
- 'student_id' => $this->selectedStudentId,
- 'weakness_count' => count($weaknesses),
- 'weaknesses' => $weaknesses
- ]);
- return $weaknesses;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('获取学生薄弱点失败', [
- 'student_id' => $this->selectedStudentId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 检查学生是否有薄弱知识点(用于 UI 判断)
- */
- #[Computed(cache: false)]
- public function hasStudentWeaknesses(): bool
- {
- if (!$this->selectedStudentId) {
- return false;
- }
- try {
- // 使用本地MasteryCalculator替代LearningAnalyticsService
- $masteryCalculator = app(MasteryCalculator::class);
- $overview = $masteryCalculator->getStudentMasteryOverview($this->selectedStudentId);
- // 检查是否有薄弱点(掌握度 < 0.5)
- foreach ($overview['details'] ?? [] as $detail) {
- $masteryLevel = floatval($detail->mastery_level ?? 0);
- if ($masteryLevel < 0.5) {
- return true;
- }
- }
- return false;
- } catch (\Exception $e) {
- return false;
- }
- }
- public function updatedSelectedTeacherId($value)
- {
- // 当教师选择变化时,清空之前选择的学生
- $this->selectedStudentId = null;
- $this->filterByStudentWeakness = false;
- }
- /**
- * 监听薄弱点筛选开关变化
- * 确保不会重置学生选择
- */
- public function updatedFilterByStudentWeakness($value)
- {
- \Illuminate\Support\Facades\Log::info('薄弱点筛选开关变化', [
- 'enabled' => $value,
- 'student_id' => $this->selectedStudentId,
- 'teacher_id' => $this->selectedTeacherId
- ]);
- // 不需要做任何事情,只是记录日志
- // 学生ID和教师ID保持不变
- }
- /**
- * 全选所有薄弱知识点
- */
- public function selectAllWeaknesses(): void
- {
- $weaknesses = $this->studentWeaknesses;
- if (empty($weaknesses)) {
- Notification::make()
- ->title('提示')
- ->body('暂无薄弱知识点数据')
- ->warning()
- ->send();
- return;
- }
- // 获取所有薄弱知识点的代码
- $kpCodes = array_column($weaknesses, 'kp_code');
- // 合并到已选择的知识点中(去重)
- $this->selectedKpCodes = array_unique(array_merge($this->selectedKpCodes, $kpCodes));
- Notification::make()
- ->title('成功')
- ->body('已全选 ' . count($kpCodes) . ' 个薄弱知识点')
- ->success()
- ->send();
- \Illuminate\Support\Facades\Log::info('全选薄弱知识点', [
- 'student_id' => $this->selectedStudentId,
- 'selected_kp_codes' => $kpCodes,
- 'total_selected' => count($this->selectedKpCodes)
- ]);
- }
- /**
- * 去重题目:基于ID和内容相似度去重
- */
- protected function deduplicateQuestions(array $questions): array
- {
- $uniqueQuestions = [];
- $seenIds = [];
- $seenStems = [];
- foreach ($questions as $question) {
- $id = $question['id'] ?? $question['question_id'] ?? null;
- $stem = $question['stem'] ?? $question['content'] ?? $question['question_text'] ?? '';
- $difficulty = $question['difficulty'] ?? 0.5;
- $kpCode = $question['kp_code'] ?? '';
- // 规范化题干:移除多余空白和换行
- $normalizedStem = preg_replace('/\s+/', ' ', trim($stem));
- $normalizedStem = preg_replace('/^\d+\.\s*/', '', $normalizedStem); // 移除题号
- // 创建唯一键:ID + 题干哈希 + 知识点
- $key = ($id ? "id:{$id}" : "stem:" . md5($normalizedStem)) . "kp:{$kpCode}";
- // 如果是选择题,进一步检查选项是否相同
- if ($this->determineQuestionType($question) === 'choice') {
- $options = $question['options'] ?? [];
- if (!empty($options) && is_array($options)) {
- $optionsKey = md5(implode('|', $options));
- $key .= "opt:{$optionsKey}";
- }
- }
- // 检查是否已存在
- if (isset($seenIds[$key]) || in_array($normalizedStem, $seenStems)) {
- \Illuminate\Support\Facades\Log::debug("跳过重复题目", [
- 'id' => $id,
- 'stem_preview' => mb_substr($normalizedStem, 0, 50)
- ]);
- continue;
- }
- // 记录并保留
- $seenIds[$key] = true;
- $seenStems[] = $normalizedStem;
- $uniqueQuestions[] = $question;
- }
- \Illuminate\Support\Facades\Log::info("题目去重完成", [
- 'original_count' => count($questions),
- 'unique_count' => count($uniqueQuestions),
- 'removed_count' => count($questions) - count($uniqueQuestions)
- ]);
- return array_values($uniqueQuestions);
- }
- /**
- * 清空所有选择的知识点
- */
- public function clearSelection(): void
- {
- $this->selectedKpCodes = [];
- Notification::make()
- ->title('成功')
- ->body('已清空所有选择的知识点')
- ->info()
- ->send();
- \Illuminate\Support\Facades\Log::info('清空知识点选择', [
- 'student_id' => $this->selectedStudentId
- ]);
- }
- public function updatedSelectedStudentId($value)
- {
- // 选择学生后,不要清空知识点选择,保持步骤3的选择有效
- // $this->selectedKpCodes = []; // 注释掉这行,避免清空用户选择
- // 如果启用了薄弱点筛选,加载但不自动勾选
- if ($this->filterByStudentWeakness && $value) {
- $weaknesses = $this->studentWeaknesses;
- if (empty($weaknesses)) {
- Notification::make()
- ->title('提示')
- ->body('该学生暂无薄弱点数据,您可以在步骤3中手动选择知识点')
- ->warning()
- ->send();
- } else {
- Notification::make()
- ->title('提示')
- ->body('已加载' . count($weaknesses) . '个薄弱知识点,您可以在下方或步骤3中选择要练习的知识点')
- ->info()
- ->send();
- }
- }
- }
- /**
- * 监听知识点选择变化
- */
- public function updatedSelectedKpCodes($value)
- {
- // 记录调试信息
- \Illuminate\Support\Facades\Log::info('selectedKpCodes updated', [
- 'count' => count($this->selectedKpCodes),
- 'codes' => $this->selectedKpCodes,
- 'type' => gettype($value),
- 'value' => $value
- ]);
- }
- public function generateExam()
- {
- \Illuminate\Support\Facades\Log::info('generateExam called with studentId=' . ($this->selectedStudentId ?? 'null'));
- $this->validate([
- // 'paperName' => 'required|string|max:255', // 已移除必填
- 'totalQuestions' => 'required|integer|min:6|max:100',
- 'selectedTeacherId' => 'nullable|string', // 可选老师
- 'selectedStudentId' => 'nullable|string', // 可选学生
- ]);
- // 确保题目数量至少6题
- if ($this->totalQuestions < 6) {
- \Illuminate\Support\Facades\Log::warning('题目数量少于6题,已自动调整为6题', ['original' => $this->totalQuestions]);
- $this->totalQuestions = 6;
- }
- if (empty($this->selectedTeacherId) || empty($this->selectedStudentId)) {
- Notification::make()
- ->title('请选择教师与学生')
- ->body('请选择出卷对应的教师与学生后再生成试卷,确保个性化规则生效。')
- ->danger()
- ->send();
- return;
- }
- // 若未选知识点,允许随机(后台会按年级或薄弱点补全)
- // 自动生成试卷名称 - 使用 paper_id 作为唯一标识
- if (empty($this->paperName)) {
- // 先生成一个临时名称,保存后会更新为真实的 paper_id
- $this->paperName = 'paper_' . time() . '_' . bin2hex(random_bytes(4));
- }
- $this->isGenerating = true;
- try {
- // 使用本地QuestionBankService进行智能出卷(generateIntelligentExam是LearningAnalytics的特定方法)
- // TODO: 需要实现本地的智能出卷功能,暂时跳过
- \Illuminate\Support\Facades\Log::warning('跳过LearningAnalytics的generateIntelligentExam调用', [
- 'student_id' => $this->selectedStudentId,
- 'reason' => '功能已迁移到本地KnowledgeMasteryService,但generateIntelligentExam尚未实现'
- ]);
- // 模拟成功结果,返回空题目列表
- $questions = [];
- $result = [
- 'success' => true,
- 'message' => '使用本地题库服务生成',
- 'questions' => $questions
- ];
- \Illuminate\Support\Facades\Log::info('智能出卷使用本地服务', [
- 'success' => $result['success'],
- 'message' => $result['message'],
- 'questions_count' => count($questions),
- 'target_count' => $this->totalQuestions
- ]);
- if (count($questions) < $this->totalQuestions) {
- // 题库不足时,批量生成题目(使用题库的多AI模型并行生成功能)
- $neededCount = $this->totalQuestions - count($questions);
- // 生成比需求更多的题目,储备起来供后续使用
- $generateCount = max($neededCount, 20); // 至少生成20道题作为储备
- \Illuminate\Support\Facades\Log::info("题库题目不足,需要补充 {$neededCount} 道题,准备批量生成 {$generateCount} 道题", [
- 'current_count' => count($questions),
- 'needed' => $neededCount,
- 'will_generate' => $generateCount
- ]);
- // 只生成一次,生成足够多的题目(题库服务支持多AI模型并行)
- $this->batchGenerateQuestions($generateCount);
- // 重新从题库获取题目
- $questionBankService = app(QuestionBankService::class);
- $params = [
- 'kp_codes' => implode(',', $this->selectedKpCodes),
- 'limit' => $this->totalQuestions * 2 // 获取更多题目用于筛选
- ];
- if (!empty($this->selectedSkills)) {
- $params['skills'] = implode(',', $this->selectedSkills);
- }
- if ($this->selectedStudentId) {
- $params['exclude_student_questions'] = $this->selectedStudentId;
- }
- $newResponse = $questionBankService->filterQuestions($params);
- // 合并题目并去重,只保留有解题思路的题目
- if (!empty($newResponse['data'])) {
- $existingIds = array_column($questions, 'id');
- foreach ($newResponse['data'] as $newQ) {
- // 只添加有解题思路的题目
- $sol = $newQ['solution'] ?? '';
- if (is_array($sol)) $sol = json_encode($sol, JSON_UNESCAPED_UNICODE);
- if (!in_array($newQ['id'], $existingIds) && !empty(trim($sol))) {
- $questions[] = $newQ;
- }
- }
- }
- \Illuminate\Support\Facades\Log::info("批量生成完成,当前题库题目数量: " . count($questions), [
- 'generated_count' => $generateCount,
- 'total_in_bank' => count($questions)
- ]);
- }
- // 2. 最终去重:确保没有重复题目
- $questions = $this->deduplicateQuestions($questions);
- \Illuminate\Support\Facades\Log::info("去重后题目数量: " . count($questions));
- // 3. 限制试卷题目数量为用户要求的数量
- if (count($questions) > $this->totalQuestions) {
- // 根据题型配比和难度配比对题目进行筛选和排序
- // 注意:如果用户未选择难度(selectedDifficultyLevels为空),则传入null表示随机难度
- $effectiveDifficultyCategory = !empty($this->selectedDifficultyLevels) ? $this->difficultyCategory : null;
- $questions = $this->selectBestQuestions(
- $questions,
- $this->totalQuestions,
- $effectiveDifficultyCategory,
- $this->totalScore,
- $this->questionTypeRatio
- );
- $difficultyLog = $effectiveDifficultyCategory ?? '随机';
- \Illuminate\Support\Facades\Log::info("从 " . count($questions) . " 道题中筛选出 " . count($questions) . " 道题,难度: {$difficultyLog}, 总分: {$this->totalScore}");
- }
- // 3. 检查题型完整性(至少保证每种题型都有题目)
- $checkResult = $this->ensureQuestionTypeCompleteness($questions, $this->totalQuestions);
- if ($checkResult['missing_types']) {
- \Illuminate\Support\Facades\Log::warning("检测到缺失题型,将自动生成", [
- 'missing_types' => $checkResult['missing_types'],
- 'current_count' => $checkResult['current_count']
- ]);
- // 批量生成缺失题型的题目
- $this->batchGenerateMissingTypes($checkResult['missing_types']);
- // 重新获取题目
- $questionBankService = app(QuestionBankService::class);
- $params = [
- 'kp_codes' => implode(',', $this->selectedKpCodes),
- 'limit' => $this->totalQuestions * 2
- ];
- if (!empty($this->selectedSkills)) {
- $params['skills'] = implode(',', $this->selectedSkills);
- }
- if ($this->selectedStudentId) {
- $params['exclude_student_questions'] = $this->selectedStudentId;
- }
- $newResponse = $questionBankService->filterQuestions($params);
- if (!empty($newResponse['data'])) {
- // 只保留有解题思路的题目
- $questionsWithSolution = array_filter($newResponse['data'], function($q) {
- $sol = $q['solution'] ?? '';
- if (is_array($sol)) $sol = json_encode($sol, JSON_UNESCAPED_UNICODE);
- return !empty(trim($sol));
- });
- $questions = array_merge($questions, array_values($questionsWithSolution));
- }
- // 再次筛选
- $effectiveDifficultyCategory = !empty($this->selectedDifficultyLevels) ? $this->difficultyCategory : null;
- $questions = $this->selectBestQuestions(
- $questions,
- $this->totalQuestions,
- $effectiveDifficultyCategory,
- $this->totalScore,
- $this->questionTypeRatio
- );
- }
- // 2. 最终过滤:确保所有题目都有解题思路
- $questions = array_filter($questions, function($q) {
- $solution = $q['solution'] ?? '';
- // 处理 solution 可能是数组的情况
- if (is_array($solution)) {
- $solution = json_encode($solution, JSON_UNESCAPED_UNICODE);
- }
- return !empty(trim($solution));
- });
- $questions = array_values($questions); // 重新索引数组
- \Illuminate\Support\Facades\Log::info('最终过滤后题目数量', [
- 'total_questions' => count($questions),
- 'filtered_message' => '已过滤掉没有解题思路的题目'
- ]);
- // 3. 为题目添加类型信息(如果缺失)
- foreach ($questions as &$question) {
- if (!isset($question['question_type'])) {
- $question['question_type'] = $this->determineQuestionType($question);
- \Illuminate\Support\Facades\Log::debug('为题目添加类型', [
- 'question_id' => $question['id'] ?? '',
- 'added_type' => $question['question_type']
- ]);
- }
- }
- unset($question); // 释放引用
- // 3. 为题目分配分数(根据题型配比和总分)
- $questions = $this->assignScoresToQuestions($questions, $this->totalScore, $this->questionTypeRatio);
- // 4. 生成试卷数据
- $examData = [
- 'paper_name' => $this->paperName,
- 'paper_description' => $this->paperDescription,
- 'difficulty_category' => $this->difficultyCategory,
- 'questions' => $questions,
- 'total_score' => $this->totalScore,
- 'total_questions' => count($questions),
- 'student_id' => $this->selectedStudentId,
- 'teacher_id' => $this->selectedTeacherId,
- ];
- // 4. 保存到数据库
- $questionBankService = app(QuestionBankService::class);
- $paperId = $questionBankService->saveExamToDatabase($examData);
- // 如果保存返回 null,使用默认占位 ID,防止 UI 不显示
- if (empty($paperId)) {
- $paperId = 'demo_' . $this->selectedStudentId . '_' . now()->format('YmdHis');
- }
- // 使用 paper_id 作为试卷名称
- $this->paperName = $paperId;
- \Illuminate\Support\Facades\Log::info('Generated paper ID: ' . $paperId);
- $this->generatedPaperId = $paperId;
- // 将生成的试卷数据缓存,以便 PDF 预览时使用(缓存 1 小时)
- \Illuminate\Support\Facades\Log::info('缓存试卷数据', [
- 'paper_id' => $paperId,
- 'question_count' => count($questions),
- 'question_types' => array_column($questions, 'question_type')
- ]);
- Cache::put('generated_exam_' . $paperId, $examData, now()->addHour());
- $this->generatedQuestions = $questions;
- $stats = $result['stats'] ?? [];
- $message = "已生成包含 " . count($questions) . " 道题的试卷";
- if (!empty($stats['weakness_targeted'])) {
- $message .= ",其中针对薄弱点 " . $stats['weakness_targeted'] . " 题";
- }
- Notification::make()
- ->title('试卷生成成功')
- ->body($message)
- ->success()
- ->send();
- } catch (\Exception $e) {
- // 记录错误,生成失败时不创建 paper_id,避免生成无效的 PDF 链接
- \Illuminate\Support\Facades\Log::error('生成试卷失败', ['error' => $e->getMessage()]);
- $this->generatedPaperId = null; // 不设置 paper_id,避免显示无效链接
- $this->generatedQuestions = [];
- Notification::make()
- ->title('试卷生成失败')
- ->body('错误: ' . $e->getMessage() . "\n\n请检查题目数量、知识点选择或网络连接后重试")
- ->danger()
- ->send();
- } finally {
- $this->isGenerating = false;
- }
- }
- /**
- * 批量生成题目(使用题库的多AI模型并行功能)
- */
- protected function batchGenerateQuestions(int $count)
- {
- // 增加PHP脚本执行时间到120秒,给足够时间启动异步任务和等待完成
- set_time_limit(120);
- $questionBankService = app(QuestionBankService::class);
- $generatedTasks = [];
- // 只生成一次,使用所有选中的知识点
- $allKpCodes = $this->selectedKpCodes;
- if (empty($allKpCodes)) {
- // 如果没有选中知识点,使用默认知识点
- $allKpCodes = ['R01']; // 默认知识点
- }
- // 对每个知识点生成题目
- foreach ($allKpCodes as $kpCode) {
- \Illuminate\Support\Facades\Log::info("为知识点 {$kpCode} 生成题目", [
- 'count' => $count,
- 'skills' => $this->selectedSkills
- ]);
- $result = $questionBankService->generateIntelligentQuestions([
- 'kp_code' => $kpCode,
- 'skills' => $this->selectedSkills,
- 'count' => $count,
- 'difficulty_distribution' => $this->difficultyRatio,
- ]);
- if ($result['success'] && isset($result['task_id'])) {
- $generatedTasks[] = [
- 'task_id' => $result['task_id'],
- 'kp_code' => $kpCode
- ];
- \Illuminate\Support\Facades\Log::info("已启动生成任务: {$result['task_id']} for {$kpCode}");
- } else {
- \Illuminate\Support\Facades\Log::warning("生成任务启动失败", [
- 'kp_code' => $kpCode,
- 'result' => $result
- ]);
- }
- }
- // 等待所有任务完成(最多等待60秒)
- if (!empty($generatedTasks)) {
- $maxWaitTime = 60; // 增加最大等待时间
- $startTime = time();
- \Illuminate\Support\Facades\Log::info("等待 {$maxWaitTime} 秒,所有生成任务完成", [
- 'tasks' => array_column($generatedTasks, 'task_id')
- ]);
- while (time() - $startTime < $maxWaitTime) {
- $allCompleted = true;
- $completedTasks = [];
- $runningTasks = [];
- foreach ($generatedTasks as $task) {
- $taskStatus = $questionBankService->getTaskStatus($task['task_id']);
- if (!$taskStatus) {
- $allCompleted = false;
- $runningTasks[] = $task['task_id'];
- continue;
- }
- $status = $taskStatus['status'] ?? '';
- if ($status === 'completed') {
- $completedTasks[] = $task['task_id'];
- } elseif ($status === 'failed') {
- \Illuminate\Support\Facades\Log::error("生成任务失败", [
- 'task_id' => $task['task_id'],
- 'error' => $taskStatus['error'] ?? '未知错误'
- ]);
- // 任务失败继续等待其他任务
- } else {
- $allCompleted = false;
- $runningTasks[] = $task['task_id'];
- }
- }
- if ($allCompleted) {
- \Illuminate\Support\Facades\Log::info('所有AI生成任务已完成', [
- 'completed' => $completedTasks,
- 'tasks' => $generatedTasks
- ]);
- break;
- }
- // 每10秒输出一次进度
- $elapsed = time() - $startTime;
- if ($elapsed % 10 < 2) {
- \Illuminate\Support\Facades\Log::info("生成进度", [
- 'elapsed' => $elapsed,
- 'completed' => count($completedTasks),
- 'running' => count($runningTasks),
- 'total' => count($generatedTasks)
- ]);
- }
- // 等待3秒后重试
- sleep(3);
- }
- $waitTime = time() - $startTime;
- \Illuminate\Support\Facades\Log::info('AI生成任务等待完成', [
- 'wait_time' => $waitTime,
- 'tasks' => $generatedTasks
- ]);
- }
- }
- /**
- * 根据题型配比和难度配比,从大量题目中筛选出最佳题目
- */
- protected function selectBestQuestions(
- array $questions,
- int $targetCount,
- ?string $difficultyCategory, // null 表示随机难度
- float $totalScore,
- array $questionTypeRatio
- ): array {
- // 去重并过滤:确保输入题目列表没有重复ID,且都有解题思路
- $uniqueQuestions = [];
- foreach ($questions as $q) {
- $id = $q['id'] ?? $q['question_id'] ?? null;
- // 只保留有解题思路且不重复的题目
- $sol = $q['solution'] ?? '';
- if (is_array($sol)) $sol = json_encode($sol, JSON_UNESCAPED_UNICODE);
- if ($id && !isset($uniqueQuestions[$id]) && !empty(trim($sol))) {
- $uniqueQuestions[$id] = $q;
- }
- }
- $questions = array_values($uniqueQuestions);
- if (count($questions) <= $targetCount) {
- return $questions;
- }
- \Illuminate\Support\Facades\Log::info("开始筛选题目", [
- 'total_available' => count($questions),
- 'target_count' => $targetCount,
- 'difficulty_category' => $difficultyCategory,
- 'total_score' => $totalScore,
- 'type_ratio' => $questionTypeRatio
- ]);
- // 1. 按题型分类题目
- $categorizedQuestions = [
- 'choice' => [], // 选择题
- 'fill' => [], // 填空题
- 'answer' => [], // 解答题
- ];
- // 统计所有题型的原始类型分布
- $rawTypeStats = ['choice' => 0, 'fill' => 0, 'answer' => 0];
- foreach ($questions as $question) {
- $type = $this->determineQuestionType($question);
- if (!isset($categorizedQuestions[$type])) {
- $type = 'answer';
- }
- $categorizedQuestions[$type][] = $question;
- // 统计原始类型
- $rawType = $question['question_type'] ?? $question['type'] ?? 'unknown';
- if (strtolower($rawType) === 'choice') $rawTypeStats['choice']++;
- if (strtolower($rawType) === 'fill') $rawTypeStats['fill']++;
- if (strtolower($rawType) === 'answer') $rawTypeStats['answer']++;
- // 记录选择题的详细信息(用于调试)
- if ($type === 'choice') {
- \Illuminate\Support\Facades\Log::debug('题目被分类为选择题', [
- 'question_id' => $question['id'] ?? '',
- 'question_type' => $question['question_type'] ?? '',
- 'type' => $question['type'] ?? '',
- 'stem_preview' => mb_substr($question['stem'] ?? '', 0, 50)
- ]);
- }
- }
- \Illuminate\Support\Facades\Log::info("题目类型分类统计", [
- 'total_questions' => count($questions),
- 'raw_type_stats' => $rawTypeStats, // 题库中题目的原始类型分布
- 'final_type_stats' => [
- 'choice' => count($categorizedQuestions['choice']),
- 'fill' => count($categorizedQuestions['fill']),
- 'answer' => count($categorizedQuestions['answer'])
- ]
- ]);
- // 2. 根据难度分类筛选题目
- // 如果难度分类为null(用户未选择),则不过滤,保留所有题目(随机难度)
- if ($difficultyCategory === null) {
- \Illuminate\Support\Facades\Log::info("用户未选择难度,将随机生成所有难度的题目");
- $difficultyFilteredQuestions = $categorizedQuestions;
- } else {
- $difficultyFilteredQuestions = $this->filterByDifficulty($categorizedQuestions, $difficultyCategory);
- }
- // 3. 根据题型配比计算每种题型应选择的题目数量
- $selectedQuestions = [];
- $selectedIds = []; // 用于追踪已选题目ID
- $typeTargets = $this->computeTypeTargets($targetCount, $questionTypeRatio);
- // 检查每种题型的可用数量
- $availableCounts = [
- 'choice' => count($difficultyFilteredQuestions['choice'] ?? []),
- 'fill' => count($difficultyFilteredQuestions['fill'] ?? []),
- 'answer' => count($difficultyFilteredQuestions['answer'] ?? []),
- ];
- \Illuminate\Support\Facades\Log::info("各题型可用数量", [
- 'available' => $availableCounts,
- 'targets' => $typeTargets
- ]);
- // 优先保证每种题型至少一题(适用于总题目数>=3的情况)
- if ($targetCount >= 3) {
- foreach (['choice', 'fill', 'answer'] as $typeKey) {
- if (!empty($difficultyFilteredQuestions[$typeKey])) {
- // 随机选择1道该题型的题目
- $randomIndex = array_rand($difficultyFilteredQuestions[$typeKey]);
- $q = $difficultyFilteredQuestions[$typeKey][$randomIndex];
- $id = $q['id'] ?? $q['question_id'];
- if (!in_array($id, $selectedIds)) {
- $selectedQuestions[] = $q;
- $selectedIds[] = $id;
- }
- \Illuminate\Support\Facades\Log::info("保证题型最少题目: {$typeKey}", [
- 'selected_index' => $randomIndex
- ]);
- } else {
- \Illuminate\Support\Facades\Log::warning("题型缺失: {$typeKey},需要从其他题型补充");
- }
- }
- }
- // 根据题型配比计算每种题型应选择的题目数量
- foreach ($typeTargets as $typeKey => $targetTypeCount) {
- if ($targetTypeCount <= 0) continue;
- $availableQuestions = $difficultyFilteredQuestions[$typeKey] ?? [];
- // 过滤掉已选的
- $availableQuestions = array_filter($availableQuestions, function($q) use ($selectedIds) {
- $id = $q['id'] ?? $q['question_id'];
- return !in_array($id, $selectedIds);
- });
- // 还需要选多少:目标数量 - 已选该类型的数量
- $currentTypeCount = 0;
- foreach ($selectedQuestions as $sq) {
- if ($this->determineQuestionType($sq) === $typeKey) {
- $currentTypeCount++;
- }
- }
- $needToSelect = $targetTypeCount - $currentTypeCount;
- if ($needToSelect > 0) {
- $availableCount = count($availableQuestions);
- // 如果该题型可用数量不足目标数量,从其他题型补充
- if ($availableCount < $needToSelect) {
- \Illuminate\Support\Facades\Log::warning("题型 {$typeKey} 数量不足", [
- 'available' => $availableCount,
- 'target' => $needToSelect,
- 'will_supplement' => true
- ]);
- // 先选完该题型的所有可用题目
- if ($availableCount > 0) {
- foreach ($availableQuestions as $q) {
- $selectedQuestions[] = $q;
- $selectedIds[] = $q['id'] ?? $q['question_id'];
- }
- }
- // 剩余题目从其他题型补充
- $remainingNeed = $needToSelect - $availableCount;
- $otherTypes = ['choice', 'fill', 'answer'];
- $otherTypes = array_filter($otherTypes, fn($t) => $t !== $typeKey);
- foreach ($otherTypes as $otherType) {
- if ($remainingNeed <= 0) break;
- $otherQuestions = $difficultyFilteredQuestions[$otherType] ?? [];
- // 过滤掉已选的
- $otherQuestions = array_filter($otherQuestions, function($q) use ($selectedIds) {
- $id = $q['id'] ?? $q['question_id'];
- return !in_array($id, $selectedIds);
- });
- if (!empty($otherQuestions)) {
- $takeCount = min($remainingNeed, count($otherQuestions));
- $randomKeys = array_rand($otherQuestions, $takeCount);
- if (!is_array($randomKeys)) {
- $randomKeys = [$randomKeys];
- }
- foreach ($randomKeys as $key) {
- $q = $otherQuestions[$key];
- $selectedQuestions[] = $q;
- $selectedIds[] = $q['id'] ?? $q['question_id'];
- }
- $remainingNeed -= $takeCount;
- }
- }
- \Illuminate\Support\Facades\Log::info("题型 {$typeKey} 补充完成", [
- 'original_need' => $needToSelect,
- 'available' => $availableCount,
- 'supplemented' => $needToSelect - $remainingNeed
- ]);
- } else {
- // 该题型数量足够,正常选择
- $takeCount = min($needToSelect, $availableCount, $targetCount - count($selectedQuestions));
- if ($takeCount > 0) {
- $randomKeys = array_rand($availableQuestions, $takeCount);
- if (!is_array($randomKeys)) {
- $randomKeys = [$randomKeys];
- }
- foreach ($randomKeys as $key) {
- $q = $availableQuestions[$key];
- $selectedQuestions[] = $q;
- $selectedIds[] = $q['id'] ?? $q['question_id'];
- }
- }
- }
- }
- }
- // 4. 如果还有空缺,随机补充其他题型
- if (count($selectedQuestions) < $targetCount) {
- // 从所有题目中过滤掉已选的
- $remainingQuestions = array_filter($questions, function($q) use ($selectedIds) {
- $id = $q['id'] ?? $q['question_id'];
- return !in_array($id, $selectedIds);
- });
- if (!empty($remainingQuestions)) {
- $needed = $targetCount - count($selectedQuestions);
- $take = min($needed, count($remainingQuestions));
- $randomKeys = array_rand($remainingQuestions, $take);
- if (!is_array($randomKeys)) {
- $randomKeys = [$randomKeys];
- }
- foreach ($randomKeys as $key) {
- $selectedQuestions[] = $remainingQuestions[$key];
- }
- }
- }
- // 5. 打乱题目顺序
- shuffle($selectedQuestions);
- $finalQuestions = array_slice($selectedQuestions, 0, $targetCount);
- \Illuminate\Support\Facades\Log::info("题目筛选完成", [
- 'selected_count' => count($finalQuestions),
- 'difficulty_category' => $difficultyCategory,
- 'total_score' => $totalScore
- ]);
- return $finalQuestions;
- }
- /**
- * 根据比例与总题数计算各题型目标数量,四舍五入并校正总数
- */
- protected function computeTypeTargets(int $targetCount, array $questionTypeRatio): array
- {
- $map = [
- '选择题' => 'choice',
- '填空题' => 'fill',
- '解答题' => 'answer',
- ];
- $targets = ['choice' => 0, 'fill' => 0, 'answer' => 0];
- // 初步分配
- foreach ($questionTypeRatio as $label => $ratio) {
- $key = $map[$label] ?? null;
- if (!$key) {
- continue;
- }
- $count = (int) round($targetCount * ($ratio / 100));
- if ($ratio > 0 && $count < 1) {
- $count = 1; // 有比例就至少 1 道
- }
- $targets[$key] = $count;
- }
- $currentSum = array_sum($targets);
- if ($currentSum === 0) {
- $targets['answer'] = $targetCount;
- return $targets;
- }
- // 总数超出则递减最多的类型(>1 时才减)
- while ($currentSum > $targetCount) {
- arsort($targets);
- foreach ($targets as $k => $v) {
- if ($v > 1) {
- $targets[$k]--;
- $currentSum--;
- break;
- }
- }
- }
- // 总数不足则按比例最高的类型补
- if ($currentSum < $targetCount) {
- $ratioByKey = [
- 'choice' => $questionTypeRatio['选择题'] ?? 0,
- 'fill' => $questionTypeRatio['填空题'] ?? 0,
- 'answer' => $questionTypeRatio['解答题'] ?? 0,
- ];
- while ($currentSum < $targetCount) {
- arsort($ratioByKey);
- $key = array_key_first($ratioByKey);
- $targets[$key]++;
- $currentSum++;
- }
- }
- return $targets;
- }
- /**
- * 检查题型完整性,确保每种题型至少有一题
- */
- protected function ensureQuestionTypeCompleteness(array $questions, int $targetCount): array
- {
- $result = [
- 'missing_types' => [],
- 'current_count' => count($questions),
- 'has_choice' => false,
- 'has_fill' => false,
- 'has_answer' => false,
- ];
- // 统计各题型数量
- $choiceCount = $fillCount = $answerCount = 0;
- foreach ($questions as $q) {
- $type = $this->determineQuestionType($q);
- if ($type === 'choice') {
- $choiceCount++;
- $result['has_choice'] = true;
- } elseif ($type === 'fill') {
- $fillCount++;
- $result['has_fill'] = true;
- } elseif ($type === 'answer') {
- $answerCount++;
- $result['has_answer'] = true;
- }
- }
- // 如果题目数量>=3,确保每种题型至少1题
- if ($targetCount >= 3) {
- if (!$result['has_choice']) {
- $result['missing_types'][] = 'choice';
- }
- if (!$result['has_fill']) {
- $result['missing_types'][] = 'fill';
- }
- if (!$result['has_answer']) {
- $result['missing_types'][] = 'answer';
- }
- }
- \Illuminate\Support\Facades\Log::info("题型完整性检查", [
- 'choice_count' => $choiceCount,
- 'fill_count' => $fillCount,
- 'answer_count' => $answerCount,
- 'missing_types' => $result['missing_types']
- ]);
- return $result;
- }
- /**
- * 批量生成缺失题型的题目
- */
- protected function batchGenerateMissingTypes(array $missingTypes): void
- {
- if (empty($missingTypes)) {
- return;
- }
- // 增加PHP脚本执行时间到120秒,给足够时间启动异步任务
- set_time_limit(120);
- \Illuminate\Support\Facades\Log::info("开始生成缺失题型题目", ['missing_types' => $missingTypes]);
- // 为每个缺失题型生成3-5道题
- foreach ($missingTypes as $type) {
- $generateCount = 5; // 每个缺失题型生成5道题
- \Illuminate\Support\Facades\Log::info("为缺失题型 {$type} 生成 {$generateCount} 道题");
- foreach ($this->selectedKpCodes as $kpCode) {
- $questionBankService = app(QuestionBankService::class);
- // 根据题型设置特定的技能点
- $skills = $this->selectedSkills;
- if ($type === 'choice') {
- $skills[] = '选择题专项练习';
- } elseif ($type === 'fill') {
- $skills[] = '填空题专项练习';
- } elseif ($type === 'answer') {
- $skills[] = '解答题专项练习';
- }
- $result = $questionBankService->generateIntelligentQuestions([
- 'kp_code' => $kpCode,
- 'skills' => $skills,
- 'count' => $generateCount,
- 'difficulty_distribution' => $this->difficultyRatio,
- ]);
- if ($result['success'] && isset($result['task_id'])) {
- \Illuminate\Support\Facades\Log::info("已启动生成任务", [
- 'type' => $type,
- 'task_id' => $result['task_id'],
- 'kp_code' => $kpCode
- ]);
- }
- }
- }
- // 缺失题型生成任务已启动,由于题目生成是异步的,
- // 将在预览时动态获取最新生成的题目
- \Illuminate\Support\Facades\Log::info("缺失题型生成任务已启动,将在预览时动态获取");
- }
- /**
- * 根据难度分类筛选题目
- */
- protected function filterByDifficulty(array $categorizedQuestions, string $difficultyCategory): array
- {
- $filtered = [];
- $difficultyRanges = [
- '基础' => [0, 0.6], // 扩大基础难度上限,从0.4提升到0.6
- '中等' => [0.4, 0.8], // 扩大中等难度范围
- '拔高' => [0.6, 1.0]
- ];
- $targetRange = $difficultyRanges[$difficultyCategory] ?? [0, 1.0];
- foreach ($categorizedQuestions as $type => $questions) {
- $filtered[$type] = [];
- foreach ($questions as $question) {
- $difficulty = floatval($question['difficulty'] ?? 0.5);
- if ($difficulty >= $targetRange[0] && $difficulty <= $targetRange[1]) {
- $filtered[$type][] = $question;
- }
- // 不再保留越界题目,严格按难度范围筛选
- }
- }
- \Illuminate\Support\Facades\Log::info("难度筛选结果", [
- 'difficulty_category' => $difficultyCategory,
- 'difficulty_range' => $targetRange,
- 'choice_count' => count($filtered['choice']),
- 'fill_count' => count($filtered['fill']),
- 'answer_count' => count($filtered['answer'])
- ]);
- return $filtered;
- }
- /**
- * 根据题目标签或内容判断题型
- */
- protected function determineQuestionType(array $question): string
- {
- // 优先根据题目内容判断(而不是数据库字段)
- $stem = $question['stem'] ?? $question['content'] ?? '';
- // 处理 stem 可能是数组的情况
- if (is_array($stem)) {
- $stem = json_encode($stem, JSON_UNESCAPED_UNICODE);
- }
- $tags = $question['tags'] ?? '';
- // 处理 tags 可能是数组的情况
- if (is_array($tags)) {
- $tags = json_encode($tags, JSON_UNESCAPED_UNICODE);
- }
- $skills = $question['skills'] ?? [];
- // 1. 根据题干内容判断 - 选择题特征:必须包含 A. B. C. D. 选项(至少2个)
- if (is_string($stem)) {
- // 选择题特征:必须包含 A. B. C. D. 四个选项(至少2个)
- $hasOptionA = preg_match('/\bA\s*[\.\、\:]/', $stem) || preg_match('/\(A\)/', $stem) || preg_match('/^A[\.\s]/', $stem);
- $hasOptionB = preg_match('/\bB\s*[\.\、\:]/', $stem) || preg_match('/\(B\)/', $stem) || preg_match('/^B[\.\s]/', $stem);
- $hasOptionC = preg_match('/\bC\s*[\.\、\:]/', $stem) || preg_match('/\(C\)/', $stem) || preg_match('/^C[\.\s]/', $stem);
- $hasOptionD = preg_match('/\bD\s*[\.\、\:]/', $stem) || preg_match('/\(D\)/', $stem) || preg_match('/^D[\.\s]/', $stem);
- $hasOptionE = preg_match('/\bE\s*[\.\、\:]/', $stem) || preg_match('/\(E\)/', $stem) || preg_match('/^E[\.\s]/', $stem);
- // 至少有2个选项就认为是选择题(降低阈值)
- $optionCount = ($hasOptionA ? 1 : 0) + ($hasOptionB ? 1 : 0) + ($hasOptionC ? 1 : 0) + ($hasOptionD ? 1 : 0) + ($hasOptionE ? 1 : 0);
- if ($optionCount >= 2) {
- return 'choice';
- }
- // 检查是否有"( )"或"( )"括号,这通常是选择题的标志
- if (preg_match('/(\s*)|\(\s*\)/', $stem) && (strpos($stem, 'A.') !== false || strpos($stem, 'B.') !== false || strpos($stem, 'C.') !== false || strpos($stem, 'D.') !== false)) {
- return 'choice';
- }
- }
- // 2. 根据技能点判断
- if (is_array($skills) && !empty($skills)) {
- // 过滤非字符串元素,避免 implode 报错
- $skillsFiltered = array_filter($skills, 'is_string');
- if (!empty($skillsFiltered)) {
- $skillsStr = implode(',', $skillsFiltered);
- if (strpos($skillsStr, '选择题') !== false) return 'choice';
- if (strpos($skillsStr, '填空题') !== false) return 'fill';
- if (strpos($skillsStr, '解答题') !== false) return 'answer';
- }
- }
- // 3. 根据题目已有类型字段判断(作为后备)
- if (!empty($question['question_type']) && is_string($question['question_type'])) {
- $type = strtolower(trim($question['question_type']));
- if (in_array($type, ['choice', '选择题', 'choice question'])) return 'choice';
- if (in_array($type, ['fill', '填空题', 'fill in the blank'])) return 'fill';
- if (in_array($type, ['answer', '解答题', 'calculation', '简答题'])) return 'answer';
- }
- if (!empty($question['type']) && is_string($question['type'])) {
- $type = strtolower(trim($question['type']));
- if (in_array($type, ['choice', '选择题', 'choice question'])) return 'choice';
- if (in_array($type, ['fill', '填空题', 'fill in the blank'])) return 'fill';
- if (in_array($type, ['answer', '解答题', 'calculation', '简答题'])) return 'answer';
- }
- // 4. 根据标签判断
- if (is_string($tags)) {
- if (strpos($tags, '选择') !== false || strpos($tags, '选择题') !== false) {
- return 'choice';
- }
- if (strpos($tags, '填空') !== false || strpos($tags, '填空题') !== false) {
- return 'fill';
- }
- if (strpos($tags, '解答') !== false || strpos($tags, '简答') !== false || strpos($tags, '证明') !== false) {
- return 'answer';
- }
- }
- // 5. 填空题特征:连续下划线或明显的填空括号
- if (is_string($stem)) {
- // 检查填空题特征:连续下划线
- if (preg_match('/_{3,}/', $stem) || strpos($stem, '____') !== false) {
- return 'fill';
- }
- // 空括号填空
- if (preg_match('/(\s*)/', $stem) || preg_match('/\(\s*\)/', $stem)) {
- return 'fill';
- }
- }
- // 6. 根据题干内容关键词判断
- if (is_string($stem)) {
- // 有证明、解答、计算、求证等关键词的是解答题
- if (preg_match('/(证明|求证|解方程|计算:|求解|推导|说明理由)/', $stem)) {
- return 'answer';
- }
- }
- // 默认是解答题(更安全的默认值)
- return 'answer';
- }
- /**
- * 为题目分配分数,确保总分等于设定值
- */
- protected function assignScoresToQuestions(array $questions, float $totalScore, array $questionTypeRatio): array
- {
- if (empty($questions)) {
- return $questions;
- }
- // 按题型分组统计
- $typeStats = [
- 'choice' => ['count' => 0, 'indices' => []],
- 'fill' => ['count' => 0, 'indices' => []],
- 'answer' => ['count' => 0, 'indices' => []],
- ];
- foreach ($questions as $index => $question) {
- $type = $this->determineQuestionType($question);
- if (!isset($typeStats[$type])) {
- $type = 'answer';
- }
- $typeStats[$type]['count']++;
- $typeStats[$type]['indices'][] = $index;
- }
- \Illuminate\Support\Facades\Log::info("分数分配统计", [
- 'total_score' => $totalScore,
- 'choice_count' => $typeStats['choice']['count'],
- 'fill_count' => $typeStats['fill']['count'],
- 'answer_count' => $typeStats['answer']['count'],
- 'type_ratio' => $questionTypeRatio
- ]);
- // 根据题型配比计算每种题型的分数比例
- $choiceRatio = ($questionTypeRatio['选择题'] ?? 40) / 100;
- $fillRatio = ($questionTypeRatio['填空题'] ?? 30) / 100;
- $answerRatio = ($questionTypeRatio['解答题'] ?? 30) / 100;
- // 计算每种题型的总分
- $choiceTotalScore = $totalScore * $choiceRatio;
- $fillTotalScore = $totalScore * $fillRatio;
- $answerTotalScore = $totalScore * $answerRatio;
- // 计算每道题的分数
- $choiceScore = $typeStats['choice']['count'] > 0
- ? round($choiceTotalScore / $typeStats['choice']['count'], 0)
- : 0;
- $fillScore = $typeStats['fill']['count'] > 0
- ? round($fillTotalScore / $typeStats['fill']['count'], 0)
- : 0;
- $answerScore = $typeStats['answer']['count'] > 0
- ? round($answerTotalScore / $typeStats['answer']['count'], 0)
- : 0;
- // 确保每道题至少有分数
- $choiceScore = max($choiceScore, 2);
- $fillScore = max($fillScore, 3);
- $answerScore = max($answerScore, 5);
- // 分配分数
- $assignedTotal = 0;
- foreach ($questions as $index => &$question) {
- // 题目类型应该在之前确定并存储,避免重复调用
- $type = $question['question_type'] ?? $this->determineQuestionType($question);
- if ($type === 'choice') {
- $question['score'] = $choiceScore;
- } elseif ($type === 'fill') {
- $question['score'] = $fillScore;
- } else {
- $question['score'] = $answerScore;
- }
- $assignedTotal += $question['score'];
- }
- unset($question);
- // 调整分数以确保总分正确
- $diff = $totalScore - $assignedTotal;
- if ($diff != 0 && !empty($questions)) {
- // 优先调整解答题的分数
- $adjustIndices = !empty($typeStats['answer']['indices'])
- ? $typeStats['answer']['indices']
- : (!empty($typeStats['fill']['indices'])
- ? $typeStats['fill']['indices']
- : $typeStats['choice']['indices']);
- if (!empty($adjustIndices)) {
- $adjustPerQuestion = intval($diff / count($adjustIndices));
- $remainder = $diff % count($adjustIndices);
- foreach ($adjustIndices as $i => $idx) {
- $adjustment = $adjustPerQuestion + ($i < abs($remainder) ? ($remainder > 0 ? 1 : -1) : 0);
- $questions[$idx]['score'] = max(1, $questions[$idx]['score'] + $adjustment);
- }
- }
- }
- // 最终验证
- $scores = array_column($questions, 'score');
- // 确保所有分数都是数字
- $scores = array_map(function($s) {
- return is_numeric($s) ? (float)$s : 0;
- }, $scores);
- $finalTotal = array_sum($scores);
- \Illuminate\Support\Facades\Log::info("分数分配完成", [
- 'target_score' => $totalScore,
- 'final_total' => $finalTotal,
- 'choice_score_each' => $choiceScore,
- 'fill_score_each' => $fillScore,
- 'answer_score_each' => $answerScore
- ]);
- return $questions;
- }
- /**
- * 快捷设置题型配比方案
- */
- public function applyRatioPreset(string $preset): void
- {
- if ($preset === '4-2-4') {
- $this->questionTypeRatio = [
- '选择题' => 40,
- '填空题' => 20,
- '解答题' => 40,
- ];
- } elseif ($preset === '5-2-3') {
- $this->questionTypeRatio = [
- '选择题' => 50,
- '填空题' => 20,
- '解答题' => 30,
- ];
- }
- }
- /**
- * 保留旧方法以兼容(但不再使用)
- */
- protected function autoGenerateQuestions(int $count)
- {
- // 调用新的批量生成方法
- $this->batchGenerateQuestions($count);
- }
- public function exportToPdf()
- {
- if (!$this->generatedPaperId) {
- Notification::make()
- ->title('错误')
- ->body('请先生成试卷')
- ->danger()
- ->send();
- return;
- }
- // 调用PDF导出API
- return redirect()->route('filament.admin.auth.intelligent-exam.pdf', [
- 'paper_id' => $this->generatedPaperId,
- 'answer' => 'false'
- ]);
- }
- public function resetForm()
- {
- $this->reset([
- 'paperName', 'paperDescription', 'selectedGrade', 'selectedKpCodes', 'selectedSkills',
- 'selectedTeacherId', 'selectedStudentId', 'filterByStudentWeakness', 'generatedQuestions', 'generatedPaperId'
- ]);
- $this->questionTypeRatio = [
- '选择题' => 40,
- '填空题' => 30,
- '解答题' => 30,
- ];
- $this->difficultyRatio = [
- '基础' => 50,
- '中等' => 35,
- '拔高' => 15,
- ];
- }
- /**
- * 监听TeacherStudentSelector组件的老师变化事件
- */
- #[On('teacherChanged')]
- public function onTeacherChanged(string $teacherId): void
- {
- \Illuminate\Support\Facades\Log::info('智能出题页面收到教师变更事件', [
- 'teacher_id' => $teacherId
- ]);
- $this->selectedTeacherId = $teacherId;
- // 清空学生选择和相关的筛选
- $this->selectedStudentId = null;
- $this->filterByStudentWeakness = false;
- }
- /**
- * 监听TeacherStudentSelector组件的学生变化事件
- */
- #[On('studentChanged')]
- public function onStudentChanged(string $teacherId, string $studentId): void
- {
- \Illuminate\Support\Facades\Log::info('智能出题页面收到学生变更事件', [
- 'teacher_id' => $teacherId,
- 'student_id' => $studentId
- ]);
- $this->selectedTeacherId = $teacherId;
- $this->selectedStudentId = $studentId;
- // ✅ 如果有学生选择,自动启用学生薄弱点筛选(但暂不勾选知识点)
- if ($studentId) {
- $this->filterByStudentWeakness = true;
- \Illuminate\Support\Facades\Log::info('已自动启用薄弱点筛选', [
- 'student_id' => $studentId,
- 'filter_enabled' => $this->filterByStudentWeakness
- ]);
- // 不立即触发,让用户自己选择
- } else {
- // 如果清空了学生选择,也清空薄弱点筛选
- $this->filterByStudentWeakness = false;
- }
- }
- }
|