2026-05-30 22:15:16 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use App\Models\QuestionBank;
|
2026-06-04 08:36:18 +07:00
|
|
|
use App\Models\Subject;
|
2026-05-30 22:15:16 +07:00
|
|
|
|
|
|
|
|
class MigrateOldQuestions extends Command
|
|
|
|
|
{
|
2026-06-04 08:36:18 +07:00
|
|
|
// Ini adalah 'kunci' agar command muncul di php artisan list
|
2026-05-30 22:15:16 +07:00
|
|
|
protected $signature = 'lms:migrate-questions';
|
2026-06-04 08:36:18 +07:00
|
|
|
protected $description = 'Menyalin data soal & subject dari lmsv2-old ke lmsv2 secara presisi';
|
2026-05-30 22:15:16 +07:00
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2026-06-04 08:36:18 +07:00
|
|
|
$this->info('Memulai Sinkronisasi Master Subject & Bank Soal...');
|
2026-05-30 22:15:16 +07:00
|
|
|
|
2026-06-04 08:36:18 +07:00
|
|
|
// --- 1. SINKRONISASI MASTER SUBJECT ---
|
|
|
|
|
$this->info('Menarik data Subject...');
|
|
|
|
|
$oldSubjects = DB::table('lmsv2-old.subjects')->get();
|
|
|
|
|
foreach ($oldSubjects as $os) {
|
|
|
|
|
Subject::updateOrCreate(
|
|
|
|
|
['id' => $os->id],
|
|
|
|
|
['name' => $os->name ?? $os->title ?? 'Materi ' . $os->id]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- 2. SINKRONISASI BANK SOAL ---
|
|
|
|
|
$this->info('Menarik data Bank Soal...');
|
2026-05-30 22:15:16 +07:00
|
|
|
$oldQuestions = DB::table('lmsv2-old.questions')->get();
|
|
|
|
|
|
|
|
|
|
if ($oldQuestions->isEmpty()) {
|
|
|
|
|
$this->warn('Tidak ada data di tabel lmsv2-old.questions!');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar = $this->output->createProgressBar(count($oldQuestions));
|
|
|
|
|
$bar->start();
|
|
|
|
|
|
|
|
|
|
$successCount = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($oldQuestions as $old) {
|
|
|
|
|
|
2026-06-04 08:36:18 +07:00
|
|
|
// Pemetaan Tipe & Level
|
2026-05-30 22:15:16 +07:00
|
|
|
$type = 'single';
|
|
|
|
|
$oldType = strtolower($old->question_type ?? '');
|
|
|
|
|
if (str_contains($oldType, 'multiple')) { $type = 'multiple'; }
|
|
|
|
|
elseif (str_contains($oldType, 'true') || str_contains($oldType, 'boolean')) { $type = 'true_false'; }
|
|
|
|
|
elseif (str_contains($oldType, 'desc') || str_contains($oldType, 'essay')) { $type = 'descriptive'; }
|
|
|
|
|
|
|
|
|
|
$level = 'sedang';
|
|
|
|
|
$oldLevel = strtolower($old->level ?? '');
|
|
|
|
|
if (str_contains($oldLevel, 'mudah') || str_contains($oldLevel, 'easy')) { $level = 'mudah'; }
|
|
|
|
|
elseif (str_contains($oldLevel, 'sulit') || str_contains($oldLevel, 'hard')) { $level = 'sulit'; }
|
|
|
|
|
|
2026-06-04 08:36:18 +07:00
|
|
|
// Pemetaan ID (Lama ke Baru)
|
|
|
|
|
$deptId = (!empty($old->section_id) && $old->section_id != 0) ? $old->section_id : null;
|
|
|
|
|
$posId = (!empty($old->class_id) && $old->class_id != 0) ? $old->class_id : null;
|
|
|
|
|
$subjId = (!empty($old->subject_id) && $old->subject_id != 0) ? $old->subject_id : null;
|
|
|
|
|
$creatorId = (!empty($old->staff_id) && $old->staff_id != 0) ? $old->staff_id : null;
|
|
|
|
|
|
|
|
|
|
$correctAnswer = !empty($old->correct) ? [$old->correct] : null;
|
2026-05-30 22:15:16 +07:00
|
|
|
|
|
|
|
|
QuestionBank::updateOrCreate(
|
2026-06-04 08:36:18 +07:00
|
|
|
['old_id' => $old->id],
|
2026-05-30 22:15:16 +07:00
|
|
|
[
|
2026-06-04 08:36:18 +07:00
|
|
|
'department_id' => $deptId,
|
|
|
|
|
'position_id' => $posId,
|
|
|
|
|
'subject_id' => $subjId,
|
2026-05-30 22:15:16 +07:00
|
|
|
'question_type' => $type,
|
|
|
|
|
'question_level' => $level,
|
|
|
|
|
'question_text' => $old->question,
|
|
|
|
|
'option_a' => $old->opt_a,
|
|
|
|
|
'option_b' => $old->opt_b,
|
|
|
|
|
'option_c' => $old->opt_c,
|
|
|
|
|
'option_d' => $old->opt_d,
|
|
|
|
|
'option_e' => $old->opt_e,
|
2026-06-04 08:36:18 +07:00
|
|
|
'correct_answer' => $correctAnswer,
|
2026-05-30 22:15:16 +07:00
|
|
|
'created_by' => $creatorId,
|
|
|
|
|
'created_at' => $old->created_at ?? now(),
|
|
|
|
|
'updated_at' => $old->updated_at ?? now(),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$successCount++;
|
|
|
|
|
$bar->advance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar->finish();
|
|
|
|
|
$this->newLine();
|
2026-06-04 08:36:18 +07:00
|
|
|
$this->info("Sinkronisasi Selesai! Berhasil memperbarui {$successCount} Soal.");
|
2026-05-30 22:15:16 +07:00
|
|
|
}
|
|
|
|
|
}
|