Test Setup Failed
Pull Request — master (#461)
by Mohamed
09:15 queued 02:13
created

CleanExamData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 3
Metric Value
eloc 21
c 7
b 0
f 3
dl 0
loc 50
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 19 3
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
            ->orWhere('su.updated_from','doe')
52
            ->groupBy('is.student_id')
53
            ->orderBy('is.student_id')
54
            ->chunk($this->argument('limit'), function ($Students) use ($output) {
55
                $output->writeln('###########################################------Start cleanning exam records------###########################################');
56
                foreach ($Students as $Student) {
57
                    $exist = Examination_student::where('nsid', $Student->openemis_no)->exist();
58
                    if (!$exist) {
59
                        Institution_student::where('student_id', $Student->student_id)->delete();
60
                        Institution_class_student::where('student_id', $Student->student_id)->delete();
61
                        Institution_student_admission::where('student_id', $Student->student_id)->delete();
62
                        Security_user::where('id', $Student->student_id)->delete();
63
                        $output->writeln($Student->openemis_no.' Delete from SIS');
64
                    }
65
                }
66
            });
67
    }
68
}
69