paper-body.blade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. <div class="question-lead spacer"></div>
  95. <div class="answer-meta">
  96. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  97. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->solution ?? '') !!}</span></div>
  98. </div>
  99. @endif
  100. </div>
  101. </div>
  102. @endforeach
  103. @else
  104. <div class="question">
  105. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  106. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  107. </div>
  108. </div>
  109. @endif
  110. <!-- 二、填空题 -->
  111. <div class="section-title">二、填空题
  112. @if(count($fillQuestions) > 0)
  113. @php
  114. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  115. @endphp
  116. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  117. @else
  118. (本大题共 0 小题,共 0 分)
  119. @endif
  120. </div>
  121. @if(count($fillQuestions) > 0)
  122. @foreach($fillQuestions as $index => $q)
  123. @php
  124. $questionNumber = count($choiceQuestions) + $index + 1;
  125. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  126. $renderedContent = preg_replace(['/((\s*))/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $q->content);
  127. if ($renderedContent === $q->content) {
  128. $renderedContent .= ' ' . $blankSpan;
  129. }
  130. $renderedContent = \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  131. @endphp
  132. <div class="question">
  133. <div class="question-grid">
  134. <div class="question-lead">
  135. @if($gradingMode)
  136. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  137. @endif
  138. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  139. </div>
  140. <div class="question-main">
  141. <span class="question-stem">{!! $renderedContent !!}</span>
  142. </div>
  143. @if($gradingMode)
  144. <div class="question-lead spacer"></div>
  145. <div class="answer-meta">
  146. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  147. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->solution ?? '') !!}</span></div>
  148. </div>
  149. @endif
  150. </div>
  151. </div>
  152. @endforeach
  153. @else
  154. <div class="question">
  155. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  156. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  157. </div>
  158. </div>
  159. @endif
  160. <!-- 三、解答题 -->
  161. <div class="section-title">三、解答题
  162. @if(count($answerQuestions) > 0)
  163. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  164. @else
  165. (本大题共 0 小题,共 0 分)
  166. @endif
  167. </div>
  168. @if(count($answerQuestions) > 0)
  169. @foreach($answerQuestions as $index => $q)
  170. @php
  171. $questionNumber = count($choiceQuestions) + count($fillQuestions) + $index + 1;
  172. @endphp
  173. <div class="question">
  174. <div class="question-grid">
  175. <div class="question-lead">
  176. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  177. </div>
  178. <div class="question-main">
  179. @unless($gradingMode)
  180. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)</span>
  181. @endunless
  182. <span class="question-stem">{!! \App\Services\MathFormulaProcessor::processFormulas($q->content) !!}</span>
  183. </div>
  184. @unless($gradingMode)
  185. <div class="question-lead spacer"></div>
  186. <div class="answer-area boxy">
  187. <span class="answer-label">作答</span>
  188. </div>
  189. @endunless
  190. @if($gradingMode)
  191. @php
  192. $solutionRaw = $q->solution ?? '';
  193. $solutionProcessed = \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  194. // 去掉分步得分等分值标记
  195. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  196. // 断行处理:在【解题思路】【详细解答】【最终答案】前后加换行
  197. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', '<br><strong class="solution-heading">【$1】</strong><br>', $solutionProcessed);
  198. // 为每个“第 N 步”前添加方框;若没有,则在【详细解答】段落开头添加一个方框
  199. if (preg_match('/第\s*\d+\s*步/u', $solutionProcessed)) {
  200. $solutionProcessed = preg_replace_callback('/第\s*\d+\s*步/u', function($m) use ($renderBoxes) {
  201. return '<br><span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $m[0] . '</span></span>';
  202. }, $solutionProcessed);
  203. } else {
  204. // 在【详细解答】标题后追加方框;若无标题则在正文最前补一个
  205. $injected = false;
  206. $count = 0;
  207. $solutionProcessed = preg_replace(
  208. '/(<strong class="solution-heading">【详细解答】<\/strong><br>)/u',
  209. '$1' . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ',
  210. $solutionProcessed,
  211. 1,
  212. $count
  213. );
  214. if (!empty($count)) {
  215. $injected = true;
  216. }
  217. if (!$injected) {
  218. $solutionProcessed = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . ltrim($solutionProcessed);
  219. }
  220. }
  221. // 最终统一换行渲染
  222. $solutionProcessed = nl2br($solutionProcessed);
  223. @endphp
  224. <div class="question-lead spacer"></div>
  225. <div class="answer-meta">
  226. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  227. <div class="answer-line"><span class="solution-content">{!! $solutionProcessed !!}</span></div>
  228. </div>
  229. @endif
  230. </div>
  231. </div>
  232. @endforeach
  233. @else
  234. <div class="question">
  235. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  236. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  237. </div>
  238. </div>
  239. @endif