paper-body.blade.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. @php
  2. $choiceQuestions = $questions['choice'] ?? [];
  3. $fillQuestions = $questions['fill'] ?? [];
  4. $answerQuestions = $questions['answer'] ?? [];
  5. $gradingMode = $grading ?? false;
  6. // 计算填空空格数量
  7. $countBlanks = function($text) {
  8. $count = 0;
  9. $count += preg_match_all('/_{2,}/u', $text, $m);
  10. $count += preg_match_all('/(\s*)/u', $text, $m);
  11. $count += preg_match_all('/\(\s*\)/', $text, $m);
  12. return max(1, $count);
  13. };
  14. // 计算步骤数量
  15. $countSteps = function($text) {
  16. $matches = [];
  17. $cnt = preg_match_all('/第\s*\d+\s*步/u', $text ?? '', $matches);
  18. return max(1, $cnt);
  19. };
  20. $renderBoxes = function($num) {
  21. return str_repeat('<span style="display:inline-block;width:14px;height:14px;line-height:14px;border:1px solid #333;margin-right:4px;vertical-align:middle;"></span>', $num);
  22. };
  23. @endphp
  24. <!-- 一、选择题 -->
  25. <div class="section-title">一、选择题
  26. @if(count($choiceQuestions) > 0)
  27. @php
  28. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $choiceQuestions));
  29. @endphp
  30. (本大题共 {{ count($choiceQuestions) }} 小题,共 {{ $choiceTotal }} 分)
  31. @else
  32. (本大题共 0 小题,共 0 分)
  33. @endif
  34. </div>
  35. @if(count($choiceQuestions) > 0)
  36. @foreach($choiceQuestions as $index => $q)
  37. @php
  38. $questionNumber = $index + 1;
  39. $cleanContent = preg_replace('/^\d+[\.、]\s*/', '', $q->content);
  40. $cleanContent = trim($cleanContent);
  41. $options = $q->options ?? [];
  42. if (empty($options)) {
  43. $pattern = '/([A-D])[\.、:.:]\s*(.+?)(?=\s*[A-D][\.、:.:]|$)/su';
  44. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  45. foreach ($matches as $match) {
  46. $optionText = trim($match[2]);
  47. if (!empty($optionText)) {
  48. $options[] = $optionText;
  49. }
  50. }
  51. }
  52. }
  53. $stemLine = $cleanContent;
  54. if (!empty($options)) {
  55. if (preg_match('/^(.+?)(?=[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  56. $stemLine = trim($stemMatch[1]);
  57. }
  58. }
  59. // 将题干中的空括号/下划线替换为短波浪线;如无占位符,则在末尾追加短波浪线
  60. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  61. $renderedStem = preg_replace(['/((\s*))/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $stemLine);
  62. if ($renderedStem === $stemLine) {
  63. $renderedStem .= ' ' . $blankSpan;
  64. }
  65. $renderedStem = \App\Services\MathFormulaProcessor::processFormulas($renderedStem);
  66. @endphp
  67. <div class="question">
  68. <div class="question-grid">
  69. <div class="question-lead">
  70. @if($gradingMode)
  71. <span class="grading-boxes">{!! $renderBoxes(1) !!}</span>
  72. @endif
  73. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  74. </div>
  75. <div class="question-main">
  76. <span class="question-stem">{!! $renderedStem !!}</span>
  77. </div>
  78. @if(!empty($options))
  79. @php
  80. $optCount = count($options);
  81. $optionsClass = $optCount <= 4 ? 'options-grid-2' : 'options';
  82. @endphp
  83. <div class="question-lead spacer"></div>
  84. <div class="{{ $optionsClass }}">
  85. @foreach($options as $optIndex => $opt)
  86. @php $label = chr(65 + $optIndex); @endphp
  87. <div class="option option-compact">
  88. <strong>{{ $label }}.</strong>&nbsp;{!! \App\Services\MathFormulaProcessor::processFormulas($opt) !!}
  89. </div>
  90. @endforeach
  91. </div>
  92. @endif
  93. @if($gradingMode)
  94. @php
  95. $solutionText = trim($q->solution ?? '');
  96. // 去掉前置的“解题思路”标签,避免出现“解题思路:【解题思路】”重复
  97. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  98. $solutionHtml = $solutionText === ''
  99. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  100. : \App\Services\MathFormulaProcessor::processFormulas($solutionText);
  101. @endphp
  102. <div class="question-lead spacer"></div>
  103. <div class="answer-meta">
  104. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  105. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  106. </div>
  107. @endif
  108. </div>
  109. </div>
  110. @endforeach
  111. @else
  112. <div class="question">
  113. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  114. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  115. </div>
  116. </div>
  117. @endif
  118. <!-- 二、填空题 -->
  119. <div class="section-title">二、填空题
  120. @if(count($fillQuestions) > 0)
  121. @php
  122. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  123. @endphp
  124. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  125. @else
  126. (本大题共 0 小题,共 0 分)
  127. @endif
  128. </div>
  129. @if(count($fillQuestions) > 0)
  130. @foreach($fillQuestions as $index => $q)
  131. @php
  132. $questionNumber = count($choiceQuestions) + $index + 1;
  133. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  134. $renderedContent = preg_replace(['/((\s*))/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $q->content);
  135. if ($renderedContent === $q->content) {
  136. $renderedContent .= ' ' . $blankSpan;
  137. }
  138. $renderedContent = \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  139. @endphp
  140. <div class="question">
  141. <div class="question-grid">
  142. <div class="question-lead">
  143. @if($gradingMode)
  144. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  145. @endif
  146. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  147. </div>
  148. <div class="question-main">
  149. <span class="question-stem">{!! $renderedContent !!}</span>
  150. </div>
  151. @if($gradingMode)
  152. @php
  153. $solutionText = trim($q->solution ?? '');
  154. // 去掉前置的“解题思路”标签,避免出现“解题思路:【解题思路】”重复
  155. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  156. $solutionHtml = $solutionText === ''
  157. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  158. : \App\Services\MathFormulaProcessor::processFormulas($solutionText);
  159. @endphp
  160. <div class="question-lead spacer"></div>
  161. <div class="answer-meta">
  162. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  163. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  164. </div>
  165. @endif
  166. </div>
  167. </div>
  168. @endforeach
  169. @else
  170. <div class="question">
  171. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  172. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  173. </div>
  174. </div>
  175. @endif
  176. <!-- 三、解答题 -->
  177. <div class="section-title">三、解答题
  178. @if(count($answerQuestions) > 0)
  179. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  180. @else
  181. (本大题共 0 小题,共 0 分)
  182. @endif
  183. </div>
  184. @if(count($answerQuestions) > 0)
  185. @foreach($answerQuestions as $index => $q)
  186. @php
  187. $questionNumber = count($choiceQuestions) + count($fillQuestions) + $index + 1;
  188. @endphp
  189. <div class="question">
  190. <div class="question-grid">
  191. <div class="question-lead">
  192. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  193. </div>
  194. <div class="question-main">
  195. @unless($gradingMode)
  196. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)</span>
  197. @endunless
  198. <span class="question-stem">{!! \App\Services\MathFormulaProcessor::processFormulas($q->content) !!}</span>
  199. </div>
  200. @unless($gradingMode)
  201. <div class="question-lead spacer"></div>
  202. <div class="answer-area boxy">
  203. <span class="answer-label">作答</span>
  204. </div>
  205. @endunless
  206. @if($gradingMode)
  207. @php
  208. $solutionRaw = $q->solution ?? '';
  209. $solutionProcessed = \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  210. // 去掉分步得分等分值标记
  211. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  212. // 断行处理:在【解题思路】【详细解答】【最终答案】前后加换行
  213. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', '<br><strong class="solution-heading">【$1】</strong><br>', $solutionProcessed);
  214. // 为每个“第 N 步”前添加方框;若没有,则在【详细解答】段落开头添加一个方框
  215. if (preg_match('/第\s*\d+\s*步/u', $solutionProcessed)) {
  216. $solutionProcessed = preg_replace_callback('/第\s*\d+\s*步/u', function($m) use ($renderBoxes) {
  217. return '<br><span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $m[0] . '</span></span>';
  218. }, $solutionProcessed);
  219. } else {
  220. // 在【详细解答】标题后追加方框;若无标题则在正文最前补一个
  221. $injected = false;
  222. $count = 0;
  223. $solutionProcessed = preg_replace(
  224. '/(<strong class="solution-heading">【详细解答】<\/strong><br>)/u',
  225. '$1' . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ',
  226. $solutionProcessed,
  227. 1,
  228. $count
  229. );
  230. if (!empty($count)) {
  231. $injected = true;
  232. }
  233. if (!$injected) {
  234. $solutionProcessed = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . ltrim($solutionProcessed);
  235. }
  236. }
  237. // 最终统一换行渲染
  238. $solutionProcessed = nl2br($solutionProcessed);
  239. @endphp
  240. <div class="question-lead spacer"></div>
  241. <div class="answer-meta">
  242. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  243. <div class="answer-line"><span class="solution-content">{!! $solutionProcessed !!}</span></div>
  244. </div>
  245. @endif
  246. </div>
  247. </div>
  248. @endforeach
  249. @else
  250. <div class="question">
  251. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  252. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  253. </div>
  254. </div>
  255. @endif