@php $choiceQuestions = $questions['choice'] ?? []; $fillQuestions = $questions['fill'] ?? []; $answerQuestions = $questions['answer'] ?? []; $gradingMode = $grading ?? false; // 【新增】动态计算大题号 - 根据有题目的题型分配序号 $sectionNumbers = [ 'choice' => null, 'fill' => null, 'answer' => null ]; $currentSectionNumber = 1; // 只给有题目的题型分配序号 if (!empty($choiceQuestions)) { $sectionNumbers['choice'] = $currentSectionNumber++; } if (!empty($fillQuestions)) { $sectionNumbers['fill'] = $currentSectionNumber++; } if (!empty($answerQuestions)) { $sectionNumbers['answer'] = $currentSectionNumber++; } // 获取题型名称的辅助函数 $getSectionTitle = function($type, $sectionNumber) { $typeNames = [ 'choice' => '选择题', 'fill' => '填空题', 'answer' => '解答题' ]; // 将数字转换为中文数字 $chineseNumbers = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']; $chineseNumber = $chineseNumbers[$sectionNumber] ?? (string)$sectionNumber; return $chineseNumber . '、' . $typeNames[$type]; }; // 检查是否有数学公式处理标记,避免重复处理 $mathProcessed = false; // 检查所有题型中是否有任何题目包含 math_processed 标记 foreach ([$choiceQuestions, $fillQuestions, $answerQuestions] as $questionType) { foreach ($questionType as $q) { if (isset($q->math_processed) && $q->math_processed) { $mathProcessed = true; break 2; // 找到标记就退出两层循环 } } } // 计算填空空格数量 $countBlanks = function($text) { $count = 0; $count += preg_match_all('/_{2,}/u', $text, $m); $count += preg_match_all('/(\s*)/u', $text, $m); $count += preg_match_all('/\(\s*\)/', $text, $m); return max(1, $count); }; // 计算步骤数量 $countSteps = function($text) { $matches = []; $cnt = preg_match_all('/第\s*\d+\s*步/u', $text ?? '', $matches); return max(1, $cnt); }; $renderBoxes = function($num) { // 判卷方框放大 1.2 倍,保持单行布局 if ($num == 2) { // 两个方框时,使用右对齐布局 return '