exam-paper.blade.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. .options-grid-4 {
  87. display: grid;
  88. grid-template-columns: repeat(4, 1fr);
  89. gap: 8px 12px;
  90. margin-left: 35px;
  91. margin-top: 10px;
  92. }
  93. .options-grid-2 {
  94. display: grid;
  95. grid-template-columns: 1fr 1fr;
  96. gap: 8px 20px;
  97. margin-left: 35px;
  98. margin-top: 10px;
  99. }
  100. .option {
  101. width: 100%;
  102. font-size: 14px;
  103. line-height: 1.5;
  104. word-wrap: break-word;
  105. display: flex;
  106. align-items: flex-start;
  107. }
  108. .option-inline {
  109. display: inline-flex;
  110. align-items: baseline;
  111. margin-right: 20px;
  112. }
  113. .option-compact {
  114. font-size: 13px;
  115. line-height: 1.4;
  116. }
  117. .fill-line {
  118. display: inline-block;
  119. border-bottom: 1px solid #000;
  120. width: 100px;
  121. text-align: center;
  122. }
  123. .answer-space {
  124. height: 150px; /* 解答题留白 */
  125. border: 1px dashed #eee;
  126. margin-top: 10px;
  127. }
  128. @media print {
  129. .no-print {
  130. display: none;
  131. }
  132. body {
  133. -webkit-print-color-adjust: exact;
  134. }
  135. }
  136. </style>
  137. </head>
  138. <body>
  139. <div class="seal-line">
  140. 装&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;题
  141. </div>
  142. <div class="header">
  143. <div class="school-name">数学智能测试卷</div>
  144. <div class="paper-title">{{ $paper->paper_name ?? '未命名试卷' }}</div>
  145. <div class="info-row">
  146. <span>老师:{{ $teacher['name'] ?? '________' }}</span>
  147. <span>年级:{{ $student['grade'] ?? '________' }}</span>
  148. <span>姓名:{{ $student['name'] ?? '________' }}</span>
  149. <span>得分:________</span>
  150. </div>
  151. </div>
  152. <!-- 一、选择题 -->
  153. <div class="section-title">一、选择题
  154. @if(count($questions['choice']) > 0)
  155. @php
  156. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['choice']));
  157. @endphp
  158. (本大题共 {{ count($questions['choice']) }} 小题,共 {{ $choiceTotal }} 分)
  159. @else
  160. (本大题共 0 小题,共 0 分)
  161. @endif
  162. </div>
  163. @if(count($questions['choice']) > 0)
  164. @foreach($questions['choice'] as $index => $q)
  165. @php
  166. $questionNumber = $index + 1; // 选择题从1开始编号
  167. // 清理和预处理题干内容:移除题号前缀
  168. $cleanContent = preg_replace('/^\d+[\.\、]\s*/', '', $q->content);
  169. $cleanContent = trim($cleanContent);
  170. // 优先使用控制器传递的选项
  171. $options = $q->options ?? [];
  172. // 如果没有从控制器获取到选项,尝试从内容提取
  173. if (empty($options)) {
  174. // 支持多种选项格式:A. / A、/ A:/ A.
  175. $pattern = '/([A-D])[\.、:.:]\s*(.+?)(?=\s*[A-D][\.、:.:]|$)/su';
  176. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  177. foreach ($matches as $match) {
  178. $optionText = trim($match[2]);
  179. if (!empty($optionText)) {
  180. $options[] = $optionText;
  181. }
  182. }
  183. }
  184. }
  185. // 提取纯题干(选项前的部分)
  186. $stemLine = $cleanContent;
  187. if (!empty($options)) {
  188. // 找到第一个选项标记的位置
  189. if (preg_match('/^(.+?)(?=[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  190. $stemLine = trim($stemMatch[1]);
  191. }
  192. }
  193. // 移除题干末尾可能的括号
  194. $stemLine = preg_replace('/()\s*$/', '', $stemLine);
  195. $stemLine = trim($stemLine);
  196. @endphp
  197. <div class="question">
  198. <div class="question-content">
  199. <span class="omr-marker"></span>
  200. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}.</span>
  201. <span style="margin-left: 4px;">({{ $q->score ?? 5 }}分) @math($stemLine)</span>
  202. </div>
  203. @if(!empty($options))
  204. @php
  205. // 确保有4个选项(A、B、C、D)
  206. $standardOptions = ['A', 'B', 'C', 'D'];
  207. $displayOptions = [];
  208. foreach ($standardOptions as $idx => $letter) {
  209. if (isset($options[$idx]) && !empty($options[$idx])) {
  210. $displayOptions[$letter] = $options[$idx];
  211. } else {
  212. // 补充缺失的选项
  213. $displayOptions[$letter] = '(待补充选项' . $letter . ')';
  214. }
  215. }
  216. // 计算选项长度,决定布局
  217. $maxOptionLength = 0;
  218. $totalOptionLength = 0;
  219. foreach ($displayOptions as $letter => $option) {
  220. $text = strip_tags($option);
  221. $length = mb_strlen($text);
  222. $maxOptionLength = max($maxOptionLength, $length);
  223. $totalOptionLength += $length;
  224. }
  225. // 三种布局选择:
  226. // 1. 一行4列:极短选项(单个选项≤10字符)
  227. // 2. 一行2列:短选项(单个选项≤25字符)
  228. // 3. 垂直布局:长选项(单个选项>25字符)
  229. if ($maxOptionLength <= 10) {
  230. $layoutType = 'inline-4';
  231. $maxOptionLength = 12; // 给一点余量
  232. } elseif ($maxOptionLength <= 25) {
  233. $layoutType = 'grid-2';
  234. $maxOptionLength = 30; // 给一点余量
  235. } else {
  236. $layoutType = 'vertical';
  237. }
  238. @endphp
  239. @if($layoutType === 'inline-4')
  240. {{-- 极短选项:一行4列布局 --}}
  241. <div style="margin-left: 35px; margin-top: 10px;">
  242. <span style="font-size: 14px;">
  243. @foreach($displayOptions as $letter => $option)
  244. <span class="option-inline option-compact">
  245. <span style="font-weight: bold; margin-right: 4px;">{{ $letter }}.</span>
  246. <span>@math($option)</span>
  247. </span>
  248. @endforeach
  249. </span>
  250. </div>
  251. @elseif($layoutType === 'grid-2')
  252. {{-- 短选项:一行2列布局 --}}
  253. <div class="options-grid-2">
  254. @foreach($displayOptions as $letter => $option)
  255. <div class="option">
  256. <span style="font-weight: bold; margin-right: 8px;">{{ $letter }}.</span>
  257. <span>@math($option)</span>
  258. </div>
  259. @endforeach
  260. </div>
  261. @else
  262. {{-- 长选项:垂直布局 --}}
  263. <div class="options">
  264. @foreach($displayOptions as $letter => $option)
  265. <div class="option">
  266. <span style="font-weight: bold; margin-right: 8px;">{{ $letter }}.</span>
  267. <span>@math($option)</span>
  268. </div>
  269. @endforeach
  270. </div>
  271. @endif
  272. @else
  273. {{-- 如果没有任何选项,显示占位符 --}}
  274. <div class="options-grid-2">
  275. <div class="option" style="font-style: italic; color: #999;">
  276. <span style="font-weight: bold; margin-right: 8px;">A.</span>
  277. <span>(待补充选项A)</span>
  278. </div>
  279. <div class="option" style="font-style: italic; color: #999;">
  280. <span style="font-weight: bold; margin-right: 8px;">B.</span>
  281. <span>(待补充选项B)</span>
  282. </div>
  283. <div class="option" style="font-style: italic; color: #999;">
  284. <span style="font-weight: bold; margin-right: 8px;">C.</span>
  285. <span>(待补充选项C)</span>
  286. </div>
  287. <div class="option" style="font-style: italic; color: #999;">
  288. <span style="font-weight: bold; margin-right: 8px;">D.</span>
  289. <span>(待补充选项D)</span>
  290. </div>
  291. </div>
  292. @endif
  293. </div>
  294. @endforeach
  295. @else
  296. <div class="question">
  297. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  298. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  299. </div>
  300. </div>
  301. @endif
  302. <!-- 二、填空题 -->
  303. <div class="section-title">二、填空题
  304. @if(count($questions['fill']) > 0)
  305. @php
  306. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['fill']));
  307. @endphp
  308. (本大题共 {{ count($questions['fill']) }} 小题,共 {{ $fillTotal }} 分)
  309. @else
  310. (本大题共 0 小题,共 0 分)
  311. @endif
  312. </div>
  313. @if(count($questions['fill']) > 0)
  314. @foreach($questions['fill'] as $index => $q)
  315. @php
  316. $questionNumber = count($questions['choice']) + $index + 1; // 填空题接着选择题编号
  317. @endphp
  318. <div class="question">
  319. <div class="question-content">
  320. <span class="omr-marker"></span>
  321. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}.</span>
  322. <span style="margin-left: 4px;">({{ $q->score ?? 5 }}分) @math(str_replace('__________', '<span class="fill-line"></span>', $q->content))</span>
  323. </div>
  324. </div>
  325. @endforeach
  326. @else
  327. <div class="question">
  328. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  329. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  330. </div>
  331. </div>
  332. @endif
  333. <!-- 三、解答题 -->
  334. <div class="section-title">三、解答题
  335. @if(count($questions['answer']) > 0)
  336. (本大题共 {{ count($questions['answer']) }} 小题,共 {{ array_sum(array_column($questions['answer'], 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  337. @else
  338. (本大题共 0 小题,共 0 分)
  339. @endif
  340. </div>
  341. @if(count($questions['answer']) > 0)
  342. @foreach($questions['answer'] as $index => $q)
  343. @php
  344. $questionNumber = count($questions['choice']) + count($questions['fill']) + $index + 1; // 解答题接着前面所有题型编号
  345. @endphp
  346. <div class="question">
  347. <div class="question-content">
  348. <span class="omr-marker"></span>
  349. <span class="font-bold" style="margin-right: 8px;">{{ $questionNumber }}.</span>
  350. <span style="margin-left: 4px;">({{ $q->score ?? 10 }}分) @math($q->content)</span>
  351. </div>
  352. <div class="answer-space"></div>
  353. </div>
  354. @endforeach
  355. @else
  356. <div class="question">
  357. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  358. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  359. </div>
  360. </div>
  361. @endif
  362. <!-- 试卷总分统计 -->
  363. @php
  364. $totalChoiceScore = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['choice']));
  365. $totalFillScore = array_sum(array_map(fn($q) => $q->score ?? 5, $questions['fill']));
  366. $totalAnswerScore = array_sum(array_map(fn($q) => $q->score ?? 10, $questions['answer']));
  367. $grandTotal = $totalChoiceScore + $totalFillScore + $totalAnswerScore;
  368. @endphp
  369. <div style="margin-top: 30px; padding: 15px; border-top: 2px solid #000; text-align: right; font-size: 14px;">
  370. <strong>试卷总分:{{ $grandTotal }} 分</strong>
  371. (选择题 {{ $totalChoiceScore }} 分 + 填空题 {{ $totalFillScore }} 分 + 解答题 {{ $totalAnswerScore }} 分)
  372. </div>
  373. {{-- 参考答案(仅在开启时显示) --}}
  374. @if($includeAnswer)
  375. <div style="page-break-before: always; margin-top: 40px;">
  376. <div style="text-align: center; font-size: 22px; font-weight: bold; margin-bottom: 30px; border-bottom: 2px solid #000; padding-bottom: 10px;">
  377. 参考答案
  378. </div>
  379. <div style="font-size: 14px; margin-bottom: 20px; text-align: center; color: #666;">
  380. {{ $paper->paper_name ?? '未命名试卷' }}
  381. </div>
  382. {{-- 选择题答案 --}}
  383. @if(count($questions['choice']) > 0)
  384. <div style="margin-bottom: 20px;">
  385. <div style="font-weight: bold; font-size: 16px; margin-bottom: 10px;">一、选择题</div>
  386. <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; font-size: 14px;">
  387. @foreach($questions['choice'] as $index => $q)
  388. <div style="padding: 8px; background: #f5f5f5; border-radius: 4px;">
  389. <span style="font-weight: bold;">{{ $index + 1 }}.</span>
  390. @if(!empty($q->answer))
  391. <span style="margin-left: 8px; font-weight: bold; color: #d32f2f;">
  392. @php
  393. // 提取答案中的选项字母
  394. $answerText = $q->answer;
  395. $letter = '';
  396. if (preg_match('/([A-D])/i', $answerText, $match)) {
  397. $letter = strtoupper($match[1]);
  398. } elseif (preg_match('/答案[::]\s*([A-D])/i', $answerText, $match)) {
  399. $letter = strtoupper($match[1]);
  400. }
  401. echo $letter ?: '(待补充)';
  402. @endphp
  403. </span>
  404. @else
  405. <span style="margin-left: 8px; color: #999; font-style: italic;">(待补充)</span>
  406. @endif
  407. </div>
  408. @endforeach
  409. </div>
  410. </div>
  411. @endif
  412. {{-- 填空题答案 --}}
  413. @if(count($questions['fill']) > 0)
  414. <div style="margin-bottom: 20px;">
  415. <div style="font-weight: bold; font-size: 16px; margin-bottom: 10px;">二、填空题</div>
  416. <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; font-size: 14px;">
  417. @foreach($questions['fill'] as $index => $q)
  418. <div style="padding: 8px; background: #f5f5f5; border-radius: 4px;">
  419. <span style="font-weight: bold;">{{ count($questions['choice']) + $index + 1 }}.</span>
  420. <span style="margin-left: 8px;">
  421. @if(!empty($q->answer))
  422. @math($q->answer)
  423. @else
  424. <span style="color: #999; font-style: italic;">(待补充)</span>
  425. @endif
  426. </span>
  427. </div>
  428. @endforeach
  429. </div>
  430. </div>
  431. @endif
  432. {{-- 解答题答案 --}}
  433. @if(count($questions['answer']) > 0)
  434. <div style="margin-bottom: 20px;">
  435. <div style="font-weight: bold; font-size: 16px; margin-bottom: 15px;">三、解答题</div>
  436. <div style="space-y: 15px;">
  437. @foreach($questions['answer'] as $index => $q)
  438. @php
  439. $questionNumber = count($questions['choice']) + count($questions['fill']) + $index + 1;
  440. @endphp
  441. <div style="margin-bottom: 20px; padding: 15px; background: #f9f9f9; border-radius: 4px; border-left: 4px solid #4163ff;">
  442. <div style="font-weight: bold; font-size: 15px; margin-bottom: 10px;">
  443. {{ $questionNumber }}. ({{ $q->score ?? 10 }}分)
  444. </div>
  445. @if(!empty($q->answer))
  446. <div style="font-size: 14px; line-height: 1.8;">
  447. @math($q->answer)
  448. </div>
  449. @else
  450. <div style="font-size: 14px; color: #999; font-style: italic;">
  451. (答案待补充或请参考标准答案)
  452. </div>
  453. @endif
  454. @if(!empty($q->solution))
  455. <div style="margin-top: 10px; font-size: 13px; color: #666; padding-top: 10px; border-top: 1px dashed #ddd;">
  456. <strong>解析:</strong> @math($q->solution)
  457. </div>
  458. @endif
  459. </div>
  460. @endforeach
  461. </div>
  462. </div>
  463. @endif
  464. </div>
  465. @endif
  466. <div class="no-print" style="position: fixed; bottom: 20px; right: 20px;">
  467. <button onclick="window.print()" style="padding: 10px 20px; background: #4163ff; color: white; border: none; border-radius: 5px; cursor: pointer;">打印试卷</button>
  468. </div>
  469. <!-- KaTeX JavaScript 库 -->
  470. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
  471. <script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"></script>
  472. <script>
  473. document.addEventListener('DOMContentLoaded', function() {
  474. console.log('试卷公式渲染器启动');
  475. // 配置 KaTeX 自动渲染
  476. function renderMath() {
  477. try {
  478. renderMathInElement(document.body, {
  479. delimiters: [
  480. {left: '$$', right: '$$', display: true},
  481. {left: '$', right: '$', display: false},
  482. {left: '\\(', right: '\\)', display: false},
  483. {left: '\\[', right: '\\]', display: true}
  484. ],
  485. throwOnError: false,
  486. strict: false,
  487. trust: true,
  488. macros: {
  489. "\\f": "#1f(#2)"
  490. }
  491. });
  492. console.log('数学公式渲染完成');
  493. } catch (e) {
  494. console.warn('公式渲染警告:', e);
  495. }
  496. }
  497. // 页面加载后渲染
  498. renderMath();
  499. // 如果页面是动态加载的,等待一段时间后再次渲染
  500. setTimeout(renderMath, 500);
  501. setTimeout(renderMath, 1000);
  502. // 添加到全局,必要时手动调用
  503. window.renderExamMath = renderMath;
  504. });
  505. </script>
  506. </body>
  507. </html>