paper-body.blade.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. @php
  2. $choiceQuestions = $questions['choice'] ?? [];
  3. $fillQuestions = $questions['fill'] ?? [];
  4. $answerQuestions = $questions['answer'] ?? [];
  5. $gradingMode = $grading ?? false;
  6. // 是否在题干后显示题目ID (Q000123)
  7. $showQuestionId = config('exam.show_question_id_in_pdf', false);
  8. // 是否在题号后显示题目难度(用于校验排序)
  9. $showQuestionDifficulty = config('exam.show_question_difficulty_in_pdf', false);
  10. // 判卷模式是否显示题目题干与选项,默认显示;可通过 EXAM_PDF_GRADING_SHOW_STEM 关闭
  11. $showGradingStem = config('exam.pdf_grading_show_stem', true);
  12. // 格式化题目ID为6位补0格式
  13. $formatQuestionId = function($id) {
  14. if (empty($id)) return '';
  15. return '(Q' . str_pad($id, 6, '0', STR_PAD_LEFT) . ')';
  16. };
  17. $formatDifficulty = function($difficulty) {
  18. if ($difficulty === null || $difficulty === '') return '';
  19. $value = (float) $difficulty;
  20. return sprintf('[%.2f]', $value);
  21. };
  22. // 【新增】动态计算大题号 - 根据有题目的题型分配序号
  23. $sectionNumbers = [
  24. 'choice' => null,
  25. 'fill' => null,
  26. 'answer' => null
  27. ];
  28. $currentSectionNumber = 1;
  29. // 只给有题目的题型分配序号
  30. if (!empty($choiceQuestions)) {
  31. $sectionNumbers['choice'] = $currentSectionNumber++;
  32. }
  33. if (!empty($fillQuestions)) {
  34. $sectionNumbers['fill'] = $currentSectionNumber++;
  35. }
  36. if (!empty($answerQuestions)) {
  37. $sectionNumbers['answer'] = $currentSectionNumber++;
  38. }
  39. // 获取题型名称的辅助函数
  40. $getSectionTitle = function($type, $sectionNumber) {
  41. $typeNames = [
  42. 'choice' => '选择题',
  43. 'fill' => '填空题',
  44. 'answer' => '解答题'
  45. ];
  46. // 将数字转换为中文数字
  47. $chineseNumbers = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  48. $chineseNumber = $chineseNumbers[$sectionNumber] ?? (string)$sectionNumber;
  49. return $chineseNumber . '、' . $typeNames[$type];
  50. };
  51. // 检查是否有数学公式处理标记,避免重复处理
  52. $mathProcessed = false;
  53. // 检查所有题型中是否有任何题目包含 math_processed 标记
  54. foreach ([$choiceQuestions, $fillQuestions, $answerQuestions] as $questionType) {
  55. foreach ($questionType as $q) {
  56. if (isset($q->math_processed) && $q->math_processed) {
  57. $mathProcessed = true;
  58. break 2; // 找到标记就退出两层循环
  59. }
  60. }
  61. }
  62. $boxCounter = app(\App\Support\GradingMarkBoxCounter::class);
  63. $layoutDeciderService = app(\App\Support\OptionLayoutDecider::class);
  64. // 与判题卡共用同一计数规则,避免方框数量不一致
  65. $countBlanks = fn($text) => $boxCounter->countFillBlanks($text);
  66. $renderBoxes = function($num) {
  67. // 判卷方框放大 1.2 倍,保持单行布局
  68. if ($num == 2) {
  69. // 两个方框时,使用右对齐布局
  70. return '<div style="display:flex;justify-content:flex-end;gap:4px;">' .
  71. str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;"></span>', $num) .
  72. '</div>';
  73. }
  74. return str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;margin-right:4px;vertical-align:middle;"></span>', $num);
  75. };
  76. @endphp
  77. {{-- 【新增】步骤方框CSS样式 --}}
  78. <style>
  79. .solution-step {
  80. display: block;
  81. margin: 8px 0;
  82. padding: 4px 0;
  83. }
  84. .step-box {
  85. display: inline-block;
  86. margin-right: 8px;
  87. vertical-align: middle;
  88. }
  89. .step-label {
  90. white-space: normal;
  91. vertical-align: middle;
  92. }
  93. .solution-section {
  94. margin: 10px 0;
  95. padding: 8px;
  96. background-color: #f9f9f9;
  97. }
  98. </style>
  99. <!-- 动态大题号 - 选择题 -->
  100. @if($sectionNumbers['choice'] !== null)
  101. <div class="section-title">{{ $getSectionTitle('choice', $sectionNumbers['choice']) }}
  102. @if(count($choiceQuestions) > 0)
  103. @php
  104. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $choiceQuestions));
  105. @endphp
  106. (本大题共 {{ count($choiceQuestions) }} 小题,共 {{ $choiceTotal }} 分)
  107. @else
  108. (本大题共 0 小题,共 0 分)
  109. @endif
  110. </div>
  111. @if(count($choiceQuestions) > 0)
  112. @foreach($choiceQuestions as $index => $q)
  113. @php
  114. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  115. $questionNumber = $q->question_number ?? ($index + 1);
  116. $cleanContent = preg_replace('/^\d+[\.、]\s*/', '', $q->content);
  117. $cleanContent = trim($cleanContent);
  118. $options = $q->options ?? [];
  119. if (empty($options)) {
  120. // 【修复】选项标记必须在行首或空白后,避免误匹配 SVG 注释中的 BD:DC 等内容
  121. $pattern = '/(?:^|\s)([A-D])[\.、:.:]\s*(.+?)(?=(?:^|\s)[A-D][\.、:.:]|$)/su';
  122. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  123. foreach ($matches as $match) {
  124. $optionText = trim($match[2]);
  125. if (!empty($optionText)) {
  126. $options[] = $optionText;
  127. }
  128. }
  129. }
  130. }
  131. $stemLine = $cleanContent;
  132. if (!empty($options)) {
  133. // 【修复】只匹配行首或空白后的选项标记,避免误匹配 SVG 注释中的 BD:DC 等内容
  134. if (preg_match('/^(.+?)(?=(?:^|\s)[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  135. $stemLine = trim($stemMatch[1]);
  136. }
  137. }
  138. // 将题干中的空括号/下划线替换为短波浪线;如无占位符,则在末尾追加短波浪线
  139. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  140. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  141. $renderedStem = $stemLine;
  142. // 先处理LaTeX格式的underline命令
  143. $renderedStem = preg_replace('/\\\underline\{[^}]*\}/', $blankSpan, $renderedStem);
  144. $renderedStem = preg_replace('/\\\qquad+/', $blankSpan, $renderedStem);
  145. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  146. // 先标记LaTeX公式区域
  147. $latexPlaceholders = [];
  148. $counter = 0;
  149. $renderedStem = preg_replace_callback('/\$[^$]+\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  150. $placeholder = '<<<LATEX_' . $counter . '>>>';
  151. $latexPlaceholders[$placeholder] = $matches[0];
  152. $counter++;
  153. return $placeholder;
  154. }, $renderedStem);
  155. // 现在处理普通占位符(不会破坏LaTeX公式)
  156. $renderedStem = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedStem);
  157. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  158. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  159. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  160. $renderedStem = str_replace($placeholder, $encodedLatex, $renderedStem);
  161. }
  162. // 如果没有占位符,在末尾添加
  163. if ($renderedStem === $stemLine) {
  164. $renderedStem .= ' ' . $blankSpan;
  165. }
  166. $renderedStem = $mathProcessed ? $renderedStem : \App\Services\MathFormulaProcessor::processFormulas($renderedStem);
  167. @endphp
  168. <div class="question">
  169. <div class="question-grid">
  170. <div class="question-lead">
  171. @if($gradingMode)
  172. <span class="grading-boxes">{!! $renderBoxes(1) !!}</span>
  173. @endif
  174. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  175. </div>
  176. @if(!$gradingMode || $showGradingStem)
  177. <div class="question-main">
  178. <span class="question-stem">{!! $renderedStem !!}</span>
  179. @if($showQuestionId && !empty($q->id))
  180. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  181. @if($showQuestionDifficulty)
  182. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  183. @endif
  184. @endif
  185. </div>
  186. @endif
  187. @if((!$gradingMode || $showGradingStem) && !empty($options))
  188. @php
  189. $layoutMeta = $layoutDeciderService->decide(
  190. $options,
  191. $gradingMode ? 'grading' : 'exam'
  192. );
  193. $optionsClass = $layoutMeta['class'];
  194. $layoutDesc = $layoutMeta['layout'];
  195. \Illuminate\Support\Facades\Log::debug('选择题布局决策', [
  196. 'question_number' => $questionNumber,
  197. 'context' => $gradingMode ? 'grading' : 'exam',
  198. 'opt_count' => $layoutMeta['opt_count'],
  199. 'max_length' => $layoutMeta['max_length'],
  200. 'has_complex_formula' => $layoutMeta['has_complex_formula'],
  201. 'selected_class' => $optionsClass,
  202. 'layout' => $layoutDesc
  203. ]);
  204. @endphp
  205. <div class="question-lead spacer"></div>
  206. <div class="{{ $optionsClass }}">
  207. @foreach($options as $optIndex => $opt)
  208. @php
  209. // 兼容两种格式:数字索引 (0,1,2,3) 或字母键 (A,B,C,D)
  210. if (is_numeric($optIndex)) {
  211. $label = chr(65 + (int)$optIndex);
  212. } else {
  213. $label = strtoupper($optIndex);
  214. }
  215. // 【修复】根据是否已预处理决定处理方式
  216. $normalizedOpt = (string) $opt;
  217. // 选项内优先使用行内分式,避免 \dfrac 导致单个选项视觉突兀
  218. $normalizedOpt = str_replace('\\dfrac', '\\frac', $normalizedOpt);
  219. $normalizedOpt = str_replace('\\displaystyle', '', $normalizedOpt);
  220. $normalizedOpt = $layoutDeciderService->normalizeCompactMathForDisplay($normalizedOpt);
  221. // 清理来源HTML里可能携带的超大字号,避免单题选项异常放大
  222. $normalizedOpt = preg_replace('/font-size\s*:[^;"]+;?/iu', '', $normalizedOpt);
  223. $normalizedOpt = preg_replace('/line-height\s*:[^;"]+;?/iu', '', $normalizedOpt);
  224. $normalizedOpt = preg_replace('/style\s*=\s*([\'"])\s*\1/iu', '', $normalizedOpt);
  225. if ($mathProcessed) {
  226. // 已预处理:数据已包含处理好的 <img> 和公式,直接使用
  227. $renderedOpt = $normalizedOpt;
  228. } else {
  229. // 未预处理:先转义保护,processFormulas() 内部会解码并处理
  230. $encodedOpt = htmlspecialchars($normalizedOpt, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  231. $renderedOpt = \App\Services\MathFormulaProcessor::processFormulas($encodedOpt);
  232. }
  233. // 细粒度控制:短选项(如 1/2、-1/3、x、-x)尽量单行展示,长选项允许换行
  234. $rawOptText = html_entity_decode(strip_tags((string) $opt), ENT_QUOTES | ENT_HTML5, 'UTF-8');
  235. $rawOptText = preg_replace('/\s+/u', '', $rawOptText ?? '');
  236. $rawOptLen = mb_strlen((string) $rawOptText, 'UTF-8');
  237. $isShortOption = $rawOptLen <= 8;
  238. @endphp
  239. <div class="option option-compact">
  240. <strong>{{ $label }}.</strong>
  241. <span class="option-value {{ $isShortOption ? 'option-short' : 'option-long' }}">{!! $renderedOpt !!}</span>
  242. </div>
  243. @endforeach
  244. </div>
  245. @endif
  246. @if($gradingMode)
  247. @php
  248. $solutionText = trim($q->solution ?? '');
  249. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  250. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  251. $solutionHtml = $solutionText === ''
  252. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  253. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  254. @endphp
  255. @if($showGradingStem)
  256. <div class="question-lead spacer"></div>
  257. @endif
  258. <div class="answer-meta">
  259. @php
  260. $choiceAnswerRaw = $layoutDeciderService->normalizeCompactMathForDisplay((string) ($q->answer ?? ''));
  261. @endphp
  262. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? $choiceAnswerRaw : \App\Services\MathFormulaProcessor::processFormulas($choiceAnswerRaw) !!}</span></div>
  263. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  264. </div>
  265. @endif
  266. </div>
  267. </div>
  268. @endforeach
  269. @else
  270. <div class="question">
  271. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  272. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  273. </div>
  274. </div>
  275. @endif
  276. @endif
  277. <!-- 动态大题号 - 填空题 -->
  278. @if($sectionNumbers['fill'] !== null)
  279. <div class="section-title">{{ $getSectionTitle('fill', $sectionNumbers['fill']) }}
  280. @if(count($fillQuestions) > 0)
  281. @php
  282. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  283. @endphp
  284. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  285. @else
  286. (本大题共 0 小题,共 0 分)
  287. @endif
  288. </div>
  289. @if(count($fillQuestions) > 0)
  290. @foreach($fillQuestions as $index => $q)
  291. @php
  292. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  293. $questionNumber = $q->question_number ?? (count($choiceQuestions) + $index + 1);
  294. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  295. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  296. $renderedContent = $q->content;
  297. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  298. // 先标记LaTeX公式区域(支持包含反斜杠和花括号的LaTeX命令)
  299. $latexPlaceholders = [];
  300. $counter = 0;
  301. $renderedContent = preg_replace_callback('/\$(?:[^\$]|\\.)*\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  302. $placeholder = '<<<LATEX_FILL_' . $counter . '>>>';
  303. $latexPlaceholders[$placeholder] = $matches[0];
  304. $counter++;
  305. return $placeholder;
  306. }, $renderedContent);
  307. // 现在处理普通占位符(不会破坏LaTeX公式)
  308. $renderedContent = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedContent);
  309. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  310. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  311. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  312. $renderedContent = str_replace($placeholder, $encodedLatex, $renderedContent);
  313. }
  314. // 如果没有占位符且内容没有变化,在末尾添加
  315. // 但要检查是否已经有填空占位符(如\underline{\qquad})
  316. if ($renderedContent === $q->content && !preg_match('/\\\\underline|\\\\qquad|(\s*)|\(\s*\)/', $renderedContent)) {
  317. $renderedContent .= ' ' . $blankSpan;
  318. }
  319. $renderedContent = $mathProcessed ? $renderedContent : \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  320. @endphp
  321. <div class="question">
  322. <div class="question-grid">
  323. <div class="question-lead">
  324. @if($gradingMode)
  325. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  326. @endif
  327. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  328. </div>
  329. @if(!$gradingMode || $showGradingStem)
  330. <div class="question-main">
  331. <span class="question-stem">{!! $renderedContent !!}</span>
  332. @if($showQuestionId && !empty($q->id))
  333. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  334. @if($showQuestionDifficulty)
  335. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  336. @endif
  337. @endif
  338. </div>
  339. @endif
  340. @if($gradingMode)
  341. @php
  342. $solutionText = trim($q->solution ?? '');
  343. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  344. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  345. $solutionHtml = $solutionText === ''
  346. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  347. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  348. @endphp
  349. @if($showGradingStem)
  350. <div class="question-lead spacer"></div>
  351. @endif
  352. <div class="answer-meta">
  353. @php
  354. $fillAnswerRaw = $layoutDeciderService->normalizeCompactMathForDisplay((string) ($q->answer ?? ''));
  355. @endphp
  356. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? $fillAnswerRaw : \App\Services\MathFormulaProcessor::processFormulas($fillAnswerRaw) !!}</span></div>
  357. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  358. </div>
  359. @endif
  360. </div>
  361. </div>
  362. @endforeach
  363. @else
  364. <div class="question">
  365. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  366. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  367. </div>
  368. </div>
  369. @endif
  370. @endif
  371. <!-- 动态大题号 - 解答题 -->
  372. @if($sectionNumbers['answer'] !== null)
  373. <div class="section-title">{{ $getSectionTitle('answer', $sectionNumbers['answer']) }}
  374. @if(count($answerQuestions) > 0)
  375. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  376. @else
  377. (本大题共 0 小题,共 0 分)
  378. @endif
  379. </div>
  380. @if(count($answerQuestions) > 0)
  381. @foreach($answerQuestions as $index => $q)
  382. @php
  383. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  384. $questionNumber = $q->question_number ?? (count($choiceQuestions) + count($fillQuestions) + $index + 1);
  385. // 解答题小题排版优化(仅在小题编号语境下换行,避免误伤 f(1) 这类函数表达)
  386. $answerStem = (string) ($q->content ?? '');
  387. preg_match_all('/[((][1-9][0-9]*[))]/u', $answerStem, $subQuestionMatches);
  388. $subQuestionCount = count($subQuestionMatches[0] ?? []);
  389. // 前提:只有出现“至少两个小题编号(形成系列)”才做自动换行
  390. if ($subQuestionCount >= 2) {
  391. // 开头的 (1)/(2) 不额外插入换行,只做标准化空格
  392. $answerStem = preg_replace('/^\s*([((][1-9][0-9]*[))])\s*/u', '$1 ', $answerStem) ?? $answerStem;
  393. // 句读后接小题编号:断行
  394. $answerStem = preg_replace('/([。;;!?!?::.])\s*([((][1-9][0-9]*[))])\s*/u', '$1<br>$2 ', $answerStem) ?? $answerStem;
  395. // 换行簇(实际换行、字面量\\n、已有<br>)后接小题编号:统一压成单个 <br>
  396. $answerStem = preg_replace('/(?:(?:\\\\r\\\\n|\\\\n)|(?:\r?\n)|(?:<br\s*\/?>)|\s)+\s*([((][1-9][0-9]*[))])\s*/u', '<br>$1 ', $answerStem) ?? $answerStem;
  397. // 关键词引导的小题也换行,如 “求(1)…(2)… / 写出(1)…”
  398. $answerStem = preg_replace('/(求出|求解|求|写出|计算|证明|判断|化简)\s*([((][1-9][0-9]*[))])\s*/u', '$1<br>$2 ', $answerStem) ?? $answerStem;
  399. }
  400. $answerStemRendered = $mathProcessed
  401. ? $answerStem
  402. : \App\Services\MathFormulaProcessor::processFormulas($answerStem);
  403. @endphp
  404. <div class="question">
  405. <div class="question-grid">
  406. <div class="question-lead">
  407. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  408. </div>
  409. @if(!$gradingMode || $showGradingStem)
  410. <div class="question-main">
  411. @unless($gradingMode)
  412. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)
  413. @if($showQuestionId && !empty($q->id))
  414. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  415. @if($showQuestionDifficulty)
  416. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  417. @endif
  418. @endif
  419. </span>
  420. @endunless
  421. <span class="question-stem">{!! $answerStemRendered !!}</span>
  422. </div>
  423. @endif
  424. @unless($gradingMode)
  425. <div class="question-lead spacer"></div>
  426. <div class="answer-area boxy">
  427. <span class="answer-label">作答</span>
  428. </div>
  429. @endunless
  430. @if($gradingMode)
  431. @php
  432. $solutionRaw = $q->solution ?? '';
  433. $solutionProcessed = $mathProcessed ? $solutionRaw : \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  434. // 去掉分步得分等分值标记
  435. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  436. // 【修复】优化解析分段格式 - 支持两种格式:
  437. // 1. 【解题思路】格式
  438. // 2. 解题过程:格式
  439. // 先处理【】格式
  440. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', "\n\n===SECTION_START===\n【$1】\n===SECTION_END===\n\n", $solutionProcessed);
  441. // 【扩展】处理多种"解题过程"格式,包括带括号的内容
  442. $solutionProcessed = preg_replace('/(解题过程\s*[^:\n]*:)/u', "\n\n===SECTION_START===\n【解题过程】\n===SECTION_END===\n\n", $solutionProcessed);
  443. // 按section分割内容
  444. $sections = explode('===SECTION_START===', $solutionProcessed);
  445. $processedSections = [];
  446. $stepPattern = '(步骤\s*[0-9一二三四五六七八九十百零两]+\s*[::]?|第\s*[0-9一二三四五六七八九十百零两]+\s*步\s*[::]?)';
  447. foreach ($sections as $section) {
  448. if (empty(trim($section))) continue;
  449. // 去掉结尾标记
  450. $section = str_replace('===SECTION_END===', '', $section);
  451. // 检查是否是解题相关部分
  452. if (preg_match('/【(解题思路|详细解答|最终答案|解题过程)】/u', $section, $matches)) {
  453. $sectionTitle = $matches[0];
  454. $sectionContent = preg_replace('/【(解题思路|详细解答|最终答案|解题过程)】/u', '', $section);
  455. // 【修复】处理步骤 - 在每个"步骤N"或"第N步"前添加方框
  456. // 【优化】使用split分割步骤,为所有步骤添加方框(包括第一个)
  457. if (preg_match('/' . $stepPattern . '/u', $sectionContent)) {
  458. // 使用前瞻断言分割,保留分隔符
  459. $allSteps = preg_split('/(?=' . $stepPattern . ')/u', $sectionContent, -1, PREG_SPLIT_NO_EMPTY);
  460. if (count($allSteps) > 0) {
  461. $processed = '';
  462. // 与判题卡计数规则一致:仅显式步骤前加方框;前置引导语不加方框
  463. for ($i = 0; $i < count($allSteps); $i++) {
  464. $stepText = trim($allSteps[$i]);
  465. if (!empty($stepText)) {
  466. $prefix = ($i > 0) ? '<br>' : '';
  467. $isStep = preg_match('/^' . $stepPattern . '/u', $stepText);
  468. if ($isStep) {
  469. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  470. } else {
  471. $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
  472. }
  473. }
  474. }
  475. $sectionContent = $processed;
  476. }
  477. } else {
  478. // 没有明确步骤:在标题后添加一个方框作为开始
  479. $sectionContent = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . trim($sectionContent);
  480. }
  481. // 包装section
  482. $processedSections[] = '<div class="solution-section"><strong>' . $sectionTitle . '</strong><br>' . $sectionContent . '</div>';
  483. } else {
  484. // 非解题部分直接保留
  485. $processedSections[] = $section;
  486. }
  487. }
  488. // 重新组合所有部分
  489. $solutionProcessed = implode('', $processedSections);
  490. // 【新增】如果没有匹配到任何section标记,整个solution都没有方框,则默认加一个方框
  491. if (empty($processedSections) || (count($processedSections) === 1 && !str_contains($processedSections[0] ?? '', 'step-box'))) {
  492. // 检查是否有步骤关键词
  493. if (preg_match('/' . $stepPattern . '/u', $solutionProcessed)) {
  494. // 有步骤关键词:为每个步骤添加方框
  495. $allSteps = preg_split('/(?=' . $stepPattern . ')/u', $solutionProcessed, -1, PREG_SPLIT_NO_EMPTY);
  496. if (count($allSteps) > 0) {
  497. $processed = '';
  498. for ($i = 0; $i < count($allSteps); $i++) {
  499. $stepText = trim($allSteps[$i]);
  500. if (!empty($stepText)) {
  501. // 只有真正以"步骤"或"第X步"开头的部分才加方框
  502. // 第一个部分如果不是步骤开头(如【分析】),则不加方框
  503. $isStep = preg_match('/^' . $stepPattern . '/u', $stepText);
  504. $prefix = ($i > 0) ? '<br>' : '';
  505. if ($isStep) {
  506. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  507. } else {
  508. // 非步骤的前缀文本,直接输出不加方框
  509. $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
  510. }
  511. }
  512. }
  513. $solutionProcessed = $processed;
  514. }
  515. } else {
  516. // 没有步骤关键词:默认在开头添加一个方框
  517. $solutionProcessed = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . trim($solutionProcessed) . '</span></span>';
  518. }
  519. }
  520. // 将多余的换行转换为<br>,但保留合理的段落间距
  521. $solutionProcessed = preg_replace('/\n{3,}/u', "\n\n", $solutionProcessed);
  522. $solutionProcessed = nl2br($solutionProcessed);
  523. @endphp
  524. @if($showGradingStem)
  525. <div class="question-lead spacer"></div>
  526. @endif
  527. <div class="answer-meta">
  528. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  529. <div class="answer-line solution-parsed">{!! $solutionProcessed !!}</div>
  530. </div>
  531. @endif
  532. </div>
  533. </div>
  534. @endforeach
  535. @else
  536. <div class="question">
  537. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  538. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  539. </div>
  540. </div>
  541. @endif
  542. @endif