@foreach($generatedQuestions as $index => $question)
@php
$questionType = $question['question_type'] ?? ($question['type'] ?? '解答题');
$isChoice = ($questionType === 'choice' || $questionType === '选择题');
$options = $question['options'] ?? [];
// 如果选项不在单独的字段中,尝试从题干中解析选项
if ($isChoice && empty($options)) {
$stem = $question['stem'] ?? '';
preg_match_all('/([A-D])[\.\、\:]\s*(.+?)(?=[A-D][\.\、\:]|$)/', $stem, $matches);
if (!empty($matches[1]) && !empty($matches[2])) {
$parsedOptions = [];
for ($i = 0; $i < count($matches[1]); $i++) {
$parsedOptions[$matches[1][$i]] = trim($matches[2][$i]);
}
$options = $parsedOptions;
}
}
// 确保有4个选项(必须显示A、B、C、D)
if ($isChoice) {
$standardOptions = ['A', 'B', 'C', 'D'];
$fullOptions = [];
$optionIndex = 0;
foreach ($standardOptions as $key) {
// 检查键值形式 (A, B, C, D)
if (isset($options[$key]) && !empty($options[$key])) {
$fullOptions[$key] = $options[$key];
$optionIndex++;
}
// 检查数组索引形式 (0, 1, 2, 3)
elseif (is_array($options) && isset($options[$optionIndex]) && !empty($options[$optionIndex])) {
$fullOptions[$key] = $options[$optionIndex];
$optionIndex++;
}
// 如果没有值,补充占位符
else {
// 根据选项字母生成占位符文本
$placeholders = [
'A' => '(待补充选项A)',
'B' => '(待补充选项B)',
'C' => '(待补充选项C)',
'D' => '(待补充选项D)'
];
$fullOptions[$key] = $placeholders[$key];
}
}
$options = $fullOptions;
}
@endphp
{{ $index + 1 }}
{{ $question['kp_code'] }}
{{ $question['question_type'] ?? '解答题' }}
@if(isset($question['difficulty']))
{{ $question['difficulty'] <= 0.3 ? '基础' : ($question['difficulty'] <= 0.7 ? '中等' : '拔高') }}
@endif
{!! $question['stem'] !!}
{{-- 选择题选项展示 --}}
@if($isChoice && !empty($options))
@php
// 判断选项长度,决定布局
$maxOptionLength = 0;
foreach ($options as $key => $option) {
$text = is_string($option) ? strip_tags($option) : (string)$option;
$length = mb_strlen($text);
$maxOptionLength = max($maxOptionLength, $length);
}
// 如果最长选项不超过20个字符,可以考虑一行显示
$shouldDisplayInline = $maxOptionLength <= 20;
@endphp
@if($shouldDisplayInline)
{{-- 短选项:一行显示 --}}
@foreach($options as $key => $option)
{{ $key }}
{!! $option !!}
@endforeach
@else
{{-- 长选项:单独显示 --}}
@foreach($options as $key => $option)
{{ $key }}
{!! $option !!}
@endforeach
@endif
@endif
@endforeach