exam-paper.blade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{{ $paper->paper_name ?? '试卷预览' }}</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
  7. <style>
  8. @page {
  9. size: A4;
  10. margin: 2cm;
  11. }
  12. body {
  13. font-family: "SimSun", "Songti SC", serif; /* 宋体,适合试卷 */
  14. line-height: 1.6;
  15. color: #000;
  16. background: #fff;
  17. }
  18. .header {
  19. text-align: center;
  20. margin-bottom: 2rem;
  21. border-bottom: 2px solid #000;
  22. padding-bottom: 1rem;
  23. }
  24. .school-name {
  25. font-size: 24px;
  26. font-weight: bold;
  27. margin-bottom: 10px;
  28. }
  29. .paper-title {
  30. font-size: 20px;
  31. font-weight: bold;
  32. margin-bottom: 15px;
  33. }
  34. .info-row {
  35. display: flex;
  36. justify-content: space-between;
  37. font-size: 14px;
  38. margin-bottom: 5px;
  39. }
  40. .seal-line {
  41. position: absolute;
  42. left: -1.5cm;
  43. top: 0;
  44. bottom: 0;
  45. width: 1cm;
  46. border-right: 1px dashed #999;
  47. writing-mode: vertical-rl;
  48. text-align: center;
  49. font-size: 12px;
  50. color: #666;
  51. display: flex;
  52. align-items: center;
  53. justify-content: center;
  54. }
  55. .section-title {
  56. font-size: 16px;
  57. font-weight: bold;
  58. margin-top: 20px;
  59. margin-bottom: 10px;
  60. }
  61. .question {
  62. margin-bottom: 15px;
  63. page-break-inside: avoid;
  64. }
  65. .question-content {
  66. font-size: 14px;
  67. margin-bottom: 8px;
  68. display: flex;
  69. align-items: baseline;
  70. }
  71. .omr-marker {
  72. display: inline-block;
  73. width: 20px;
  74. height: 20px;
  75. border: 1px solid #000;
  76. border-radius: 50%;
  77. margin-right: 10px;
  78. position: relative;
  79. top: 4px;
  80. }
  81. .options {
  82. display: block;
  83. margin-left: 35px; /* 对齐题目内容 */
  84. margin-top: 10px;
  85. }
  86. .option {
  87. width: 100%;
  88. font-size: 14px;
  89. margin-bottom: 8px;
  90. padding-left: 10px;
  91. line-height: 1.5;
  92. word-wrap: break-word;
  93. }
  94. .fill-line {
  95. display: inline-block;
  96. border-bottom: 1px solid #000;
  97. width: 100px;
  98. text-align: center;
  99. }
  100. .answer-space {
  101. height: 150px; /* 解答题留白 */
  102. border: 1px dashed #eee;
  103. margin-top: 10px;
  104. }
  105. @media print {
  106. .no-print {
  107. display: none;
  108. }
  109. body {
  110. -webkit-print-color-adjust: exact;
  111. }
  112. }
  113. </style>
  114. </head>
  115. <body>
  116. <div class="seal-line">
  117. 装&nbsp;&nbsp;&nbsp;&nbsp;订&nbsp;&nbsp;&nbsp;&nbsp;线&nbsp;&nbsp;&nbsp;&nbsp;内&nbsp;&nbsp;&nbsp;&nbsp;不&nbsp;&nbsp;&nbsp;&nbsp;要&nbsp;&nbsp;&nbsp;&nbsp;答&nbsp;&nbsp;&nbsp;&nbsp;题
  118. </div>
  119. <div class="header">
  120. <div class="school-name">数学智能测试卷</div>
  121. <div class="paper-title">{{ $paper->paper_name ?? '未命名试卷' }}</div>
  122. <div class="info-row">
  123. <span>老师:{{ $teacher['name'] ?? '________' }}</span>
  124. <span>年级:{{ $student['grade'] ?? '________' }}</span>
  125. <span>姓名:{{ $student['name'] ?? '________' }}</span>
  126. <span>得分:________</span>
  127. </div>
  128. </div>
  129. <!-- 一、选择题 -->
  130. <div class="section-title">一、选择题
  131. @if(count($questions['choice']) > 0)
  132. (本大题共 {{ count($questions['choice']) }} 小题,每小题 {{ $questions['choice'][0]->score ?? 5 }} 分,共 {{ count($questions['choice']) * ($questions['choice'][0]->score ?? 5) }} 分)
  133. @else
  134. (本大题共 0 小题,共 0 分)
  135. @endif
  136. </div>
  137. @if(count($questions['choice']) > 0)
  138. @foreach($questions['choice'] as $index => $q)
  139. @php
  140. $questionNumber = $index + 1; // 选择题从1开始编号
  141. @endphp
  142. @php
  143. // 清理和预处理题干内容:移除题号前缀
  144. $cleanContent = preg_replace('/^\d+\.\s*/', '', $q->content);
  145. $cleanContent = trim($cleanContent);
  146. // 检查题干是否包含选项(以换行符分隔)
  147. $contentLines = explode("\n", $cleanContent);
  148. $stemLine = $contentLines[0] ?? $cleanContent;
  149. $stemLine = trim($stemLine);
  150. // 检测是否包含A. B. C. D.格式的选项(在同一行或换行)
  151. $hasOptionsInline = preg_match('/[A-D]\.\s*.{1,50}[A-D]\.\s*.{1,50}[A-D]\.\s*/', $cleanContent);
  152. $hasOptionsMultiLine = count($contentLines) > 1 && preg_match('/^[A-D]\.\s+/', $contentLines[1] ?? '');
  153. $options = [];
  154. if ($hasOptionsInline) {
  155. // 从同一行提取选项
  156. if (preg_match_all('/([A-D])\.\s*([^A-D]*?)(?=\s*[A-D]\.|$)/s', $cleanContent, $matches, PREG_SET_ORDER)) {
  157. foreach ($matches as $match) {
  158. $optionText = trim($match[2]);
  159. if (!empty($optionText)) {
  160. $options[] = $optionText;
  161. }
  162. }
  163. }
  164. // 提取纯题干(选项前的部分)
  165. $stemLine = preg_replace('/[A-D]\.\s*.+?(?=[A-D]\.|$)/s', '', $cleanContent);
  166. $stemLine = trim($stemLine);
  167. } elseif ($hasOptionsMultiLine) {
  168. // 从多行提取选项
  169. foreach (array_slice($contentLines, 1) as $line) {
  170. if (preg_match('/^([A-D])\.\s+(.+)$/', trim($line), $matches)) {
  171. $options[] = trim($matches[2]);
  172. }
  173. }
  174. }
  175. @endphp
  176. <div class="question">
  177. <div class="question-content">
  178. <span class="omr-marker"></span>
  179. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}</span>
  180. <span>.</span>
  181. <span style="margin-left: 4px;">@math($stemLine)</span>
  182. </div>
  183. @if(!empty($options))
  184. <div class="options">
  185. @foreach($options as $optIndex => $option)
  186. <div class="option">
  187. {{ chr(65 + $optIndex) }}. {{ $option }}
  188. </div>
  189. @endforeach
  190. </div>
  191. @endif
  192. </div>
  193. @endforeach
  194. @else
  195. <div class="question">
  196. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  197. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  198. </div>
  199. </div>
  200. @endif
  201. <!-- 二、填空题 -->
  202. <div class="section-title">二、填空题
  203. @if(count($questions['fill']) > 0)
  204. (本大题共 {{ count($questions['fill']) }} 小题,每小题 {{ $questions['fill'][0]->score ?? 5 }} 分,共 {{ count($questions['fill']) * ($questions['fill'][0]->score ?? 5) }} 分)
  205. @else
  206. (本大题共 0 小题,共 0 分)
  207. @endif
  208. </div>
  209. @if(count($questions['fill']) > 0)
  210. @foreach($questions['fill'] as $index => $q)
  211. @php
  212. $questionNumber = count($questions['choice']) + $index + 1; // 填空题接着选择题编号
  213. @endphp
  214. <div class="question">
  215. <div class="question-content">
  216. <span class="omr-marker"></span>
  217. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}</span>
  218. <span>.</span>
  219. <span style="margin-left: 4px;">@math(str_replace('__________', '<span class="fill-line"></span>', $q->content))</span>
  220. </div>
  221. </div>
  222. @endforeach
  223. @else
  224. <div class="question">
  225. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  226. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  227. </div>
  228. </div>
  229. @endif
  230. <!-- 三、解答题 -->
  231. <div class="section-title">三、解答题
  232. @if(count($questions['answer']) > 0)
  233. (本大题共 {{ count($questions['answer']) }} 小题,共 {{ array_sum(array_column($questions['answer'], 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  234. @else
  235. (本大题共 0 小题,共 0 分)
  236. @endif
  237. </div>
  238. @if(count($questions['answer']) > 0)
  239. @foreach($questions['answer'] as $index => $q)
  240. @php
  241. $questionNumber = count($questions['choice']) + count($questions['fill']) + $index + 1; // 解答题接着前面所有题型编号
  242. @endphp
  243. <div class="question">
  244. <div class="question-content">
  245. <span class="omr-marker"></span>
  246. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}</span>
  247. <span>.</span>
  248. <span style="margin-left: 4px;">({{$q->score}}分) @math($q->content)</span>
  249. </div>
  250. <div class="answer-space"></div>
  251. </div>
  252. @endforeach
  253. @else
  254. <div class="question">
  255. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  256. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  257. </div>
  258. </div>
  259. @endif
  260. <div class="no-print" style="position: fixed; bottom: 20px; right: 20px;">
  261. <button onclick="window.print()" style="padding: 10px 20px; background: #4163ff; color: white; border: none; border-radius: 5px; cursor: pointer;">打印试卷</button>
  262. </div>
  263. <!-- KaTeX JavaScript 库 -->
  264. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
  265. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"></script>
  266. <script>
  267. document.addEventListener('DOMContentLoaded', function() {
  268. console.log('试卷公式渲染器启动');
  269. // 配置 KaTeX 自动渲染
  270. function renderMath() {
  271. try {
  272. renderMathInElement(document.body, {
  273. delimiters: [
  274. {left: '$$', right: '$$', display: true},
  275. {left: '$', right: '$', display: false},
  276. {left: '\\(', right: '\\)', display: false},
  277. {left: '\\[', right: '\\]', display: true}
  278. ],
  279. throwOnError: false,
  280. strict: false,
  281. trust: true,
  282. macros: {
  283. "\\f": "#1f(#2)"
  284. }
  285. });
  286. console.log('数学公式渲染完成');
  287. } catch (e) {
  288. console.warn('公式渲染警告:', e);
  289. }
  290. }
  291. // 页面加载后渲染
  292. renderMath();
  293. // 如果页面是动态加载的,等待一段时间后再次渲染
  294. setTimeout(renderMath, 500);
  295. setTimeout(renderMath, 1000);
  296. // 添加到全局,必要时手动调用
  297. window.renderExamMath = renderMath;
  298. });
  299. </script>
  300. </body>
  301. </html>