// Render/format validation for P5 ai-generated questions.
$rows = DB::table('questions')->where('qa_status','ai_generated')
->where('source','like','% P5 %')->get();
$n=$rows->count();
$badParen=[]; $badDD=[]; $fibFrac=[]; $fibNoInput=[]; $mcqBad=[]; $rawBlank=[]; $rawSlashParen=[];
foreach ($rows as $r) {
$q=(string)$r->question;
if (substr_count($q,'\\(') !== substr_count($q,'\\)')) $badParen[]=$r->id;
if (substr_count($q,'$$') % 2 !== 0) $badDD[]=$r->id;
if (strpos($q,'[?]')!==false) $rawBlank[]=$r->id; // unconverted blank
if (preg_match('/\\\\[a-zA-Z]/',$q) && strpos($q,'\\(')===false && strpos($q,'$')===false) $rawSlashParen[]=$r->id; // bare LaTeX cmd outside delimiters
if ((int)$r->type_id === 2) {
foreach (['answer0','answer1','answer2','answer3'] as $c) {
$v=trim((string)$r->$c);
if ($v!=='' && !preg_match('/^-?\d+(\.\d+)?$/',$v)) { $fibFrac[]=$r->id.':'.$c.'='.$v; break; }
}
if (strpos($q,'id;
}
if ((int)$r->type_id === 1) {
$opts=array_filter([$r->answer0,$r->answer1,$r->answer2,$r->answer3], fn($x)=>$x!==null && $x!=='');
if (count($opts)<2 || $r->correct_answer===null) $mcqBad[]=$r->id;
}
}
$show=fn($a)=>count($a)?implode(',',array_slice($a,0,12)):'-';
echo "P5 ai_generated questions: $n\n";
echo "unbalanced \\(..\\): ".count($badParen)." [".$show($badParen)."]\n";
echo "odd \$\$ count: ".count($badDD)." [".$show($badDD)."]\n";
echo "raw [?] left: ".count($rawBlank)." [".$show($rawBlank)."]\n";
echo "bare LaTeX cmd no delim: ".count($rawSlashParen)." [".$show($rawSlashParen)."]\n";
echo "FIB non-numeric answer: ".count($fibFrac)." [".$show($fibFrac)."]\n";
echo "FIB without : ".count($fibNoInput)." [".$show($fibNoInput)."]\n";
echo "MCQ missing opts/answer: ".count($mcqBad)." [".$show($mcqBad)."]\n";
echo "--- samples (math-bearing) ---\n";
$s=0;
foreach ($rows as $r) { if ($s>=6) break; if (strpos((string)$r->question,'\\(')!==false || strpos((string)$r->question,'$')!==false) { echo "#".$r->id." t".$r->type_id.": ".substr(preg_replace('/\s+/',' ',$r->question),0,140)."\n"; $s++; } }