|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/liminfinity.edukrypt.in/app/Imports/ |
Upload File : |
<?php
namespace App\Imports;
use App\Models\Mcq_question;
use App\Models\Mcq_option;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\Importable;
class ImportQuestion implements ToModel, WithStartRow, WithValidation, WithHeadingRow, SkipsEmptyRows
{
use Importable;
public function startRow(): int
{
return 2;
}
public function rules(): array
{
return [
"tags" => "required",
"code" => "required|unique:mcq_questions,code",
"question" => "required",
"solution" => "required",
"mark" => "required|numeric",
"penalty" => "required|numeric",
"correct" => "required|numeric",
"option01" => "required",
"option02" => "required",
];
}
public function model($row)
{
// dd($row);
$question = Mcq_question::create([
'parent_id' => 0,
'admin_id' => 2,
'type' => 'single_mcq',
'question' => $row['question'],
'solution' => $row['solution'],
'code' => $row['code'],
'tags' => $row['tags'],
'mark' => $row['mark'],
'penalty' => $row['penalty'],
'created_at' => date('Y-m-d H:i:s'),
]);
if (!empty($question)) {
// Option 01
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 1,
'options' => $row['option01'],
'correct' => $row['correct'] == 1 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
// Option 02
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 1,
'options' => $row['option02'],
'correct' => $row['correct'] == 2 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
// Option 03
if (!empty($row['option03'])) {
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 1,
'options' => $row['option03'],
'correct' => $row['correct'] == 3 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
}
// Option 04
if (!empty($row['option04'])) {
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 1,
'options' => $row['option04'],
'correct' => $row['correct'] == 4 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
}
// Option 05
if (!empty($row['option05'])) {
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 5,
'options' => $row['option05'],
'correct' => $row['correct'] == 5 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
}
// Option 06
if (!empty($row['option06'])) {
Mcq_option::create([
'question_id' => $question->id,
'sort_order' => 6,
'options' => $row['option06'],
'correct' => $row['correct'] == 6 ? 1 : 0,
'created_at' => date('Y-m-d H:i:s'),
]);
}
}
}
}