$dir = storage_path('app'); $applied=0; $skip=0; $bad=0; $bk=[]; foreach (glob($dir.'/_fib2mcq_out_*.json') as $f) { $arr = json_decode(file_get_contents($f), true); if (!is_array($arr)) continue; foreach ($arr as $c) { if (($c['action'] ?? '') !== 'convert') { $skip++; continue; } $id = $c['id'] ?? null; if (!$id) { $bad++; continue; } if (!isset($c['answer0'],$c['answer1'],$c['answer2'],$c['answer3'],$c['correct_answer'])) { $bad++; continue; } $ca = (int)$c['correct_answer']; if ($ca < 0 || $ca > 3) { $bad++; continue; } $opts = [$c['answer0'],$c['answer1'],$c['answer2'],$c['answer3']]; if (count(array_unique($opts)) < 4) { $bad++; continue; } // options must be distinct $row = DB::table('questions')->where('id',$id)->first(); if (!$row) { $bad++; continue; } $bk[] = ['id'=>$row->id,'type_id'=>$row->type_id,'question'=>$row->question,'answer0'=>$row->answer0,'answer1'=>$row->answer1,'answer2'=>$row->answer2,'answer3'=>$row->answer3,'correct_answer'=>$row->correct_answer]; DB::table('questions')->where('id',$id)->update([ 'type_id'=>1, 'question'=>$c['question'] ?? $row->question, 'answer0'=>$opts[0],'answer1'=>$opts[1],'answer2'=>$opts[2],'answer3'=>$opts[3], 'correct_answer'=>$ca, 'updated_at'=>now(), ]); $applied++; } } file_put_contents($dir.'/fib2mcq_backup_'.date('Ymd_His').'.json', json_encode($bk, JSON_UNESCAPED_UNICODE)); echo "applied=$applied skip_entries=$skip rejected=$bad backup_rows=".count($bk)."\n";