exam-paper.blade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. @php
  133. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['choice']));
  134. @endphp
  135. (本大题共 {{ count($questions['choice']) }} 小题,共 {{ $choiceTotal }} 分)
  136. @else
  137. (本大题共 0 小题,共 0 分)
  138. @endif
  139. </div>
  140. @if(count($questions['choice']) > 0)
  141. @foreach($questions['choice'] as $index => $q)
  142. @php
  143. $questionNumber = $index + 1; // 选择题从1开始编号
  144. // 清理和预处理题干内容:移除题号前缀
  145. $cleanContent = preg_replace('/^\d+[\.\、]\s*/', '', $q->content);
  146. $cleanContent = trim($cleanContent);
  147. // 优先使用控制器传递的选项
  148. $options = $q->options ?? [];
  149. // 如果没有从控制器获取到选项,尝试从内容提取
  150. if (empty($options)) {
  151. // 支持多种选项格式:A. / A、/ A:/ A.
  152. $pattern = '/([A-D])[\.、:.:]\s*(.+?)(?=\s*[A-D][\.、:.:]|$)/su';
  153. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  154. foreach ($matches as $match) {
  155. $optionText = trim($match[2]);
  156. if (!empty($optionText)) {
  157. $options[] = $optionText;
  158. }
  159. }
  160. }
  161. }
  162. // 提取纯题干(选项前的部分)
  163. $stemLine = $cleanContent;
  164. if (!empty($options)) {
  165. // 找到第一个选项标记的位置
  166. if (preg_match('/^(.+?)(?=[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  167. $stemLine = trim($stemMatch[1]);
  168. }
  169. }
  170. // 移除题干末尾可能的括号
  171. $stemLine = preg_replace('/()\s*$/', '', $stemLine);
  172. $stemLine = trim($stemLine);
  173. @endphp
  174. <div class="question">
  175. <div class="question-content">
  176. <span class="omr-marker"></span>
  177. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}.</span>
  178. <span style="margin-left: 4px;">({{ $q->score ?? 5 }}分) @math($stemLine)</span>
  179. </div>
  180. @if(!empty($options))
  181. <div class="options">
  182. @foreach($options as $optIndex => $option)
  183. <div class="option">
  184. {{ chr(65 + $optIndex) }}. @math($option)
  185. </div>
  186. @endforeach
  187. </div>
  188. @endif
  189. </div>
  190. @endforeach
  191. @else
  192. <div class="question">
  193. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  194. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  195. </div>
  196. </div>
  197. @endif
  198. <!-- 二、填空题 -->
  199. <div class="section-title">二、填空题
  200. @if(count($questions['fill']) > 0)
  201. @php
  202. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['fill']));
  203. @endphp
  204. (本大题共 {{ count($questions['fill']) }} 小题,共 {{ $fillTotal }} 分)
  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 style="margin-left: 4px;">({{ $q->score ?? 5 }}分) @math(str_replace('__________', '<span class="fill-line"></span>', $q->content))</span>
  219. </div>
  220. </div>
  221. @endforeach
  222. @else
  223. <div class="question">
  224. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  225. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  226. </div>
  227. </div>
  228. @endif
  229. <!-- 三、解答题 -->
  230. <div class="section-title">三、解答题
  231. @if(count($questions['answer']) > 0)
  232. (本大题共 {{ count($questions['answer']) }} 小题,共 {{ array_sum(array_column($questions['answer'], 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  233. @else
  234. (本大题共 0 小题,共 0 分)
  235. @endif
  236. </div>
  237. @if(count($questions['answer']) > 0)
  238. @foreach($questions['answer'] as $index => $q)
  239. @php
  240. $questionNumber = count($questions['choice']) + count($questions['fill']) + $index + 1; // 解答题接着前面所有题型编号
  241. @endphp
  242. <div class="question">
  243. <div class="question-content">
  244. <span class="omr-marker"></span>
  245. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}.</span>
  246. <span style="margin-left: 4px;">({{ $q->score ?? 10 }}分) @math($q->content)</span>
  247. </div>
  248. <div class="answer-space"></div>
  249. </div>
  250. @endforeach
  251. @else
  252. <div class="question">
  253. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  254. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  255. </div>
  256. </div>
  257. @endif
  258. <!-- 试卷总分统计 -->
  259. @php
  260. $totalChoiceScore = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['choice']));
  261. $totalFillScore = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['fill']));
  262. $totalAnswerScore = array_sum(array_map(fn($q) => $q->score ?? 10, $questions['answer']));
  263. $grandTotal = $totalChoiceScore + $totalFillScore + $totalAnswerScore;
  264. @endphp
  265. <div style="margin-top: 30px; padding: 15px; border-top: 2px solid #000; text-align: right; font-size: 14px;">
  266. <strong>试卷总分:{{ $grandTotal }} 分</strong>
  267. (选择题 {{ $totalChoiceScore }} 分 + 填空题 {{ $totalFillScore }} 分 + 解答题 {{ $totalAnswerScore }} 分)
  268. </div>
  269. <div class="no-print" style="position: fixed; bottom: 20px; right: 20px;">
  270. <button onclick="window.print()" style="padding: 10px 20px; background: #4163ff; color: white; border: none; border-radius: 5px; cursor: pointer;">打印试卷</button>
  271. </div>
  272. <!-- KaTeX JavaScript 库 -->
  273. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
  274. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"></script>
  275. <script>
  276. document.addEventListener('DOMContentLoaded', function() {
  277. console.log('试卷公式渲染器启动');
  278. // 配置 KaTeX 自动渲染
  279. function renderMath() {
  280. try {
  281. renderMathInElement(document.body, {
  282. delimiters: [
  283. {left: '$$', right: '$$', display: true},
  284. {left: '$', right: '$', display: false},
  285. {left: '\\(', right: '\\)', display: false},
  286. {left: '\\[', right: '\\]', display: true}
  287. ],
  288. throwOnError: false,
  289. strict: false,
  290. trust: true,
  291. macros: {
  292. "\\f": "#1f(#2)"
  293. }
  294. });
  295. console.log('数学公式渲染完成');
  296. } catch (e) {
  297. console.warn('公式渲染警告:', e);
  298. }
  299. }
  300. // 页面加载后渲染
  301. renderMath();
  302. // 如果页面是动态加载的,等待一段时间后再次渲染
  303. setTimeout(renderMath, 500);
  304. setTimeout(renderMath, 1000);
  305. // 添加到全局,必要时手动调用
  306. window.renderExamMath = renderMath;
  307. });
  308. </script>
  309. </body>
  310. </html>