paper-body.blade.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. @php
  2. $choiceQuestions = $questions['choice'] ?? [];
  3. $fillQuestions = $questions['fill'] ?? [];
  4. $answerQuestions = $questions['answer'] ?? [];
  5. $gradingMode = $grading ?? false;
  6. // 【新增】动态计算大题号 - 根据有题目的题型分配序号
  7. $sectionNumbers = [
  8. 'choice' => null,
  9. 'fill' => null,
  10. 'answer' => null
  11. ];
  12. $currentSectionNumber = 1;
  13. // 只给有题目的题型分配序号
  14. if (!empty($choiceQuestions)) {
  15. $sectionNumbers['choice'] = $currentSectionNumber++;
  16. }
  17. if (!empty($fillQuestions)) {
  18. $sectionNumbers['fill'] = $currentSectionNumber++;
  19. }
  20. if (!empty($answerQuestions)) {
  21. $sectionNumbers['answer'] = $currentSectionNumber++;
  22. }
  23. // 获取题型名称的辅助函数
  24. $getSectionTitle = function($type, $sectionNumber) {
  25. $typeNames = [
  26. 'choice' => '选择题',
  27. 'fill' => '填空题',
  28. 'answer' => '解答题'
  29. ];
  30. // 将数字转换为中文数字
  31. $chineseNumbers = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  32. $chineseNumber = $chineseNumbers[$sectionNumber] ?? (string)$sectionNumber;
  33. return $chineseNumber . '、' . $typeNames[$type];
  34. };
  35. // 检查是否有数学公式处理标记,避免重复处理
  36. $mathProcessed = false;
  37. // 检查所有题型中是否有任何题目包含 math_processed 标记
  38. foreach ([$choiceQuestions, $fillQuestions, $answerQuestions] as $questionType) {
  39. foreach ($questionType as $q) {
  40. if (isset($q->math_processed) && $q->math_processed) {
  41. $mathProcessed = true;
  42. break 2; // 找到标记就退出两层循环
  43. }
  44. }
  45. }
  46. // 计算填空空格数量
  47. $countBlanks = function($text) {
  48. $count = 0;
  49. $count += preg_match_all('/_{2,}/u', $text, $m);
  50. $count += preg_match_all('/(\s*)/u', $text, $m);
  51. $count += preg_match_all('/\(\s*\)/', $text, $m);
  52. return max(1, $count);
  53. };
  54. // 计算步骤数量
  55. $countSteps = function($text) {
  56. $matches = [];
  57. $cnt = preg_match_all('/第\s*\d+\s*步/u', $text ?? '', $matches);
  58. return max(1, $cnt);
  59. };
  60. $renderBoxes = function($num) {
  61. // 判卷方框放大 1.2 倍,保持单行布局
  62. if ($num == 2) {
  63. // 两个方框时,使用右对齐布局
  64. return '<div style="display:flex;justify-content:flex-end;gap:4px;">' .
  65. str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;"></span>', $num) .
  66. '</div>';
  67. }
  68. 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);
  69. };
  70. @endphp
  71. {{-- 【新增】步骤方框CSS样式 --}}
  72. <style>
  73. .solution-step {
  74. display: block;
  75. margin: 8px 0;
  76. padding: 4px 0;
  77. }
  78. .step-box {
  79. display: inline-block;
  80. margin-right: 8px;
  81. vertical-align: middle;
  82. }
  83. .step-label {
  84. white-space: normal;
  85. vertical-align: middle;
  86. }
  87. .solution-section {
  88. margin: 10px 0;
  89. padding: 8px;
  90. background-color: #f9f9f9;
  91. }
  92. </style>
  93. <!-- 动态大题号 - 选择题 -->
  94. @if($sectionNumbers['choice'] !== null)
  95. <div class="section-title">{{ $getSectionTitle('choice', $sectionNumbers['choice']) }}
  96. @if(count($choiceQuestions) > 0)
  97. @php
  98. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $choiceQuestions));
  99. @endphp
  100. (本大题共 {{ count($choiceQuestions) }} 小题,共 {{ $choiceTotal }} 分)
  101. @else
  102. (本大题共 0 小题,共 0 分)
  103. @endif
  104. </div>
  105. @if(count($choiceQuestions) > 0)
  106. @foreach($choiceQuestions as $index => $q)
  107. @php
  108. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  109. $questionNumber = $q->question_number ?? ($index + 1);
  110. $cleanContent = preg_replace('/^\d+[\.、]\s*/', '', $q->content);
  111. $cleanContent = trim($cleanContent);
  112. $options = $q->options ?? [];
  113. if (empty($options)) {
  114. // 【修复】选项标记必须在行首或空白后,避免误匹配 SVG 注释中的 BD:DC 等内容
  115. $pattern = '/(?:^|\s)([A-D])[\.、:.:]\s*(.+?)(?=(?:^|\s)[A-D][\.、:.:]|$)/su';
  116. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  117. foreach ($matches as $match) {
  118. $optionText = trim($match[2]);
  119. if (!empty($optionText)) {
  120. $options[] = $optionText;
  121. }
  122. }
  123. }
  124. }
  125. $stemLine = $cleanContent;
  126. if (!empty($options)) {
  127. // 【修复】只匹配行首或空白后的选项标记,避免误匹配 SVG 注释中的 BD:DC 等内容
  128. if (preg_match('/^(.+?)(?=(?:^|\s)[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  129. $stemLine = trim($stemMatch[1]);
  130. }
  131. }
  132. // 将题干中的空括号/下划线替换为短波浪线;如无占位符,则在末尾追加短波浪线
  133. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  134. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  135. $renderedStem = $stemLine;
  136. // 先处理LaTeX格式的underline命令
  137. $renderedStem = preg_replace('/\\\underline\{[^}]*\}/', $blankSpan, $renderedStem);
  138. $renderedStem = preg_replace('/\\\qquad+/', $blankSpan, $renderedStem);
  139. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  140. // 先标记LaTeX公式区域
  141. $latexPlaceholders = [];
  142. $counter = 0;
  143. $renderedStem = preg_replace_callback('/\$[^$]+\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  144. $placeholder = '<<<LATEX_' . $counter . '>>>';
  145. $latexPlaceholders[$placeholder] = $matches[0];
  146. $counter++;
  147. return $placeholder;
  148. }, $renderedStem);
  149. // 现在处理普通占位符(不会破坏LaTeX公式)
  150. $renderedStem = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedStem);
  151. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  152. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  153. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  154. $renderedStem = str_replace($placeholder, $encodedLatex, $renderedStem);
  155. }
  156. // 如果没有占位符,在末尾添加
  157. if ($renderedStem === $stemLine) {
  158. $renderedStem .= ' ' . $blankSpan;
  159. }
  160. $renderedStem = $mathProcessed ? $renderedStem : \App\Services\MathFormulaProcessor::processFormulas($renderedStem);
  161. @endphp
  162. <div class="question">
  163. <div class="question-grid">
  164. <div class="question-lead">
  165. @if($gradingMode)
  166. <span class="grading-boxes">{!! $renderBoxes(1) !!}</span>
  167. @endif
  168. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  169. </div>
  170. <div class="question-main">
  171. <span class="question-stem">{!! $renderedStem !!}</span>
  172. </div>
  173. @if(!empty($options))
  174. @php
  175. // 计算选项长度并动态选择布局
  176. $optCount = count($options);
  177. $maxOptionLength = 0;
  178. foreach ($options as $opt) {
  179. $optText = strip_tags(\App\Services\MathFormulaProcessor::processFormulas($opt));
  180. $maxOptionLength = max($maxOptionLength, mb_strlen($optText, 'UTF-8'));
  181. }
  182. // 根据最长选项长度和选项数量动态选择布局
  183. // 短选项(≤15字符)且选项数≤4:4列布局
  184. // 中等选项(16-30字符)或选项数>4:2列布局
  185. // 长选项(>30字符):1列布局
  186. if ($maxOptionLength <= 15 && $optCount <= 4) {
  187. $optionsClass = 'options-grid-4';
  188. $layoutDesc = '4列布局';
  189. } elseif ($maxOptionLength <= 30) {
  190. $optionsClass = 'options-grid-2';
  191. $layoutDesc = '2列布局';
  192. } else {
  193. $optionsClass = 'options-grid-1';
  194. $layoutDesc = '1列布局';
  195. }
  196. \Illuminate\Support\Facades\Log::debug('选择题布局决策', [
  197. 'question_number' => $questionNumber,
  198. 'opt_count' => $optCount,
  199. 'max_length' => $maxOptionLength,
  200. 'selected_class' => $optionsClass,
  201. 'layout' => $layoutDesc
  202. ]);
  203. @endphp
  204. <div class="question-lead spacer"></div>
  205. <div class="{{ $optionsClass }}">
  206. @foreach($options as $optIndex => $opt)
  207. @php
  208. $label = chr(65 + (int)$optIndex);
  209. // 对LaTeX公式中的特殊字符进行HTML实体编码,防止被浏览器当作HTML标签处理
  210. $encodedOpt = htmlspecialchars($opt, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  211. $renderedOpt = $mathProcessed ? $encodedOpt : \App\Services\MathFormulaProcessor::processFormulas($encodedOpt);
  212. @endphp
  213. <div class="option option-compact">
  214. <strong>{{ $label }}.</strong>&nbsp;{!! $renderedOpt !!}
  215. </div>
  216. @endforeach
  217. </div>
  218. @endif
  219. @if($gradingMode)
  220. @php
  221. $solutionText = trim($q->solution ?? '');
  222. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  223. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  224. $solutionHtml = $solutionText === ''
  225. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  226. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  227. @endphp
  228. <div class="question-lead spacer"></div>
  229. <div class="answer-meta">
  230. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  231. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  232. </div>
  233. @endif
  234. </div>
  235. </div>
  236. @endforeach
  237. @else
  238. <div class="question">
  239. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  240. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  241. </div>
  242. </div>
  243. @endif
  244. @endif
  245. <!-- 动态大题号 - 填空题 -->
  246. @if($sectionNumbers['fill'] !== null)
  247. <div class="section-title">{{ $getSectionTitle('fill', $sectionNumbers['fill']) }}
  248. @if(count($fillQuestions) > 0)
  249. @php
  250. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  251. @endphp
  252. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  253. @else
  254. (本大题共 0 小题,共 0 分)
  255. @endif
  256. </div>
  257. @if(count($fillQuestions) > 0)
  258. @foreach($fillQuestions as $index => $q)
  259. @php
  260. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  261. $questionNumber = $q->question_number ?? (count($choiceQuestions) + $index + 1);
  262. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  263. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  264. $renderedContent = $q->content;
  265. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  266. // 先标记LaTeX公式区域(支持包含反斜杠和花括号的LaTeX命令)
  267. $latexPlaceholders = [];
  268. $counter = 0;
  269. $renderedContent = preg_replace_callback('/\$(?:[^\$]|\\.)*\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  270. $placeholder = '<<<LATEX_FILL_' . $counter . '>>>';
  271. $latexPlaceholders[$placeholder] = $matches[0];
  272. $counter++;
  273. return $placeholder;
  274. }, $renderedContent);
  275. // 现在处理普通占位符(不会破坏LaTeX公式)
  276. $renderedContent = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedContent);
  277. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  278. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  279. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  280. $renderedContent = str_replace($placeholder, $encodedLatex, $renderedContent);
  281. }
  282. // 如果没有占位符且内容没有变化,在末尾添加
  283. // 但要检查是否已经有填空占位符(如\underline{\qquad})
  284. if ($renderedContent === $q->content && !preg_match('/\\\\underline|\\\\qquad|(\s*)|\(\s*\)/', $renderedContent)) {
  285. $renderedContent .= ' ' . $blankSpan;
  286. }
  287. $renderedContent = $mathProcessed ? $renderedContent : \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  288. @endphp
  289. <div class="question">
  290. <div class="question-grid">
  291. <div class="question-lead">
  292. @if($gradingMode)
  293. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  294. @endif
  295. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  296. </div>
  297. <div class="question-main">
  298. <span class="question-stem">{!! $renderedContent !!}</span>
  299. </div>
  300. @if($gradingMode)
  301. @php
  302. $solutionText = trim($q->solution ?? '');
  303. // 去掉前置的“解题思路”标签,避免出现“解题思路:【解题思路】”重复
  304. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  305. $solutionHtml = $solutionText === ''
  306. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  307. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  308. @endphp
  309. <div class="question-lead spacer"></div>
  310. <div class="answer-meta">
  311. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  312. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  313. </div>
  314. @endif
  315. </div>
  316. </div>
  317. @endforeach
  318. @else
  319. <div class="question">
  320. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  321. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  322. </div>
  323. </div>
  324. @endif
  325. @endif
  326. <!-- 动态大题号 - 解答题 -->
  327. @if($sectionNumbers['answer'] !== null)
  328. <div class="section-title">{{ $getSectionTitle('answer', $sectionNumbers['answer']) }}
  329. @if(count($answerQuestions) > 0)
  330. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  331. @else
  332. (本大题共 0 小题,共 0 分)
  333. @endif
  334. </div>
  335. @if(count($answerQuestions) > 0)
  336. @foreach($answerQuestions as $index => $q)
  337. @php
  338. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  339. $questionNumber = $q->question_number ?? (count($choiceQuestions) + count($fillQuestions) + $index + 1);
  340. @endphp
  341. <div class="question">
  342. <div class="question-grid">
  343. <div class="question-lead">
  344. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  345. </div>
  346. <div class="question-main">
  347. @unless($gradingMode)
  348. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)</span>
  349. @endunless
  350. <span class="question-stem">{!! $mathProcessed ? $q->content : \App\Services\MathFormulaProcessor::processFormulas($q->content) !!}</span>
  351. </div>
  352. @unless($gradingMode)
  353. <div class="question-lead spacer"></div>
  354. <div class="answer-area boxy">
  355. <span class="answer-label">作答</span>
  356. </div>
  357. @endunless
  358. @if($gradingMode)
  359. @php
  360. $solutionRaw = $q->solution ?? '';
  361. $solutionProcessed = $mathProcessed ? $solutionRaw : \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  362. // 去掉分步得分等分值标记
  363. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  364. // 【修复】优化解析分段格式 - 支持两种格式:
  365. // 1. 【解题思路】格式
  366. // 2. 解题过程:格式
  367. // 先处理【】格式
  368. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', "\n\n===SECTION_START===\n【$1】\n===SECTION_END===\n\n", $solutionProcessed);
  369. // 【扩展】处理多种"解题过程"格式,包括带括号的内容
  370. $solutionProcessed = preg_replace('/(解题过程\s*[^:\n]*:)/u', "\n\n===SECTION_START===\n【解题过程】\n===SECTION_END===\n\n", $solutionProcessed);
  371. // 按section分割内容
  372. $sections = explode('===SECTION_START===', $solutionProcessed);
  373. $processedSections = [];
  374. foreach ($sections as $section) {
  375. if (empty(trim($section))) continue;
  376. // 去掉结尾标记
  377. $section = str_replace('===SECTION_END===', '', $section);
  378. // 检查是否是解题相关部分
  379. if (preg_match('/【(解题思路|详细解答|最终答案|解题过程)】/u', $section, $matches)) {
  380. $sectionTitle = $matches[0];
  381. $sectionContent = preg_replace('/【(解题思路|详细解答|最终答案|解题过程)】/u', '', $section);
  382. // 【修复】处理步骤 - 在每个"步骤N"或"第N步"前添加方框
  383. // 【优化】使用split分割步骤,为所有步骤添加方框(包括第一个)
  384. if (preg_match('/(步骤\s*\d+|第\s*\d+\s*步)/u', $sectionContent)) {
  385. // 使用前瞻断言分割,保留分隔符
  386. $allSteps = preg_split('/(?=步骤\s*\d+|第\s*\d+\s*步)/u', $sectionContent, -1, PREG_SPLIT_NO_EMPTY);
  387. if (count($allSteps) > 0) {
  388. $processed = '';
  389. // 为每个步骤添加方框(包括第一个)
  390. for ($i = 0; $i < count($allSteps); $i++) {
  391. $stepText = trim($allSteps[$i]);
  392. if (!empty($stepText)) {
  393. // 为每个步骤添加方框和换行(第一个步骤前面不加<br>)
  394. $prefix = ($i > 0) ? '<br>' : '';
  395. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  396. }
  397. }
  398. $sectionContent = $processed;
  399. }
  400. } else {
  401. // 没有明确步骤:在标题后添加一个方框作为开始
  402. $sectionContent = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . trim($sectionContent);
  403. }
  404. // 包装section
  405. $processedSections[] = '<div class="solution-section"><strong>' . $sectionTitle . '</strong><br>' . $sectionContent . '</div>';
  406. } else {
  407. // 非解题部分直接保留
  408. $processedSections[] = $section;
  409. }
  410. }
  411. // 重新组合所有部分
  412. $solutionProcessed = implode('', $processedSections);
  413. // 将多余的换行转换为<br>,但保留合理的段落间距
  414. $solutionProcessed = preg_replace('/\n{3,}/u', "\n\n", $solutionProcessed);
  415. $solutionProcessed = nl2br($solutionProcessed);
  416. @endphp
  417. <div class="question-lead spacer"></div>
  418. <div class="answer-meta">
  419. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  420. <div class="answer-line solution-parsed">{!! $solutionProcessed !!}</div>
  421. </div>
  422. @endif
  423. </div>
  424. </div>
  425. @endforeach
  426. @else
  427. <div class="question">
  428. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  429. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  430. </div>
  431. </div>
  432. @endif
  433. @endif