Test Setup Failed
Pull Request — master (#461)
by Mohamed
07:12 queued 46s
created

CleanExamData::handle()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
cc 3
eloc 16
c 4
b 0
f 3
nc 1
nop 0
dl 0
loc 18
rs 9.7333
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use App\Models\Examination_student;
8
use App\Models\Institution_class_student;
9
use App\Models\Institution_student;
10
use App\Models\Institution_student_admission;
11
use App\Models\Security_user;
12
use Illuminate\Support\Facades\Artisan;
13
14
class CleanExamData extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'examination:clean {limit}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Clean SIS data duplication after Exam import';
29
30
    /**
31
     * Create a new command instance.
32
     *
33
     * @return void
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $output = new \Symfony\Component\Console\Output\ConsoleOutput();
48
        DB::table('institution_students as is')
49
            ->join('security_users as su', 'su.id', 'is.student_id')
50
            ->where('is.updated_from', 'doe')
51
            ->groupBy('is.student_id')
52
            ->orderBy('is.student_id')
53
            ->chunk($this->argument('limit'), function ($Students) use ($output) {
54
                $output->writeln('###########################################------Start cleanning exam records------###########################################');
55
                foreach ($Students as $Student) {
56
                    $exist = Examination_student::where('nsid', $Student->openemis_no)->exist();
57
                    if (!$exist) {
58
                        Institution_student::where('student_id', $Student->student_id)->delete();
59
                        Institution_class_student::where('student_id', $Student->student_id)->delete();
60
                        Institution_student_admission::where('student_id', $Student->student_id)->delete();
61
                        Security_user::where('id', $Student->student_id)->delete();
62
                        $output->writeln($Student->openemis_no.' Delete from SIS');
63
                    }
64
                }
65
            });
66
    }
67
}
68