Test Setup Failed
Pull Request — master (#566)
by Mohamed
11:56
created

UpdateClassEntriyWithZeroId::updateClassCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Institution;
6
use Illuminate\Console\Command;
7
use App\Models\Institution_class;
8
use Illuminate\Support\Facades\DB;
9
use App\Models\Institution_student;
10
use App\Models\Institution_class_student;
11
use App\Models\Institution_student_admission;
12
use App\Models\Security_user;
13
use Carbon\Carbon;
14
15
class UpdateClassEntriyWithZeroId extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'update:zero_id_class {from}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'update student class reference';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        DB::statement("SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
49
        $students = Institution_student::withTrashed()->where('updated_from',$this->argument('from'))
50
        ->get()->toArray();
51
        if(count($students)>0){
52
            array_walk($students,array($this,'process'));
53
        }else{
54
            echo "all are updated \r\n";
55
        }
56
    }
57
58
    public function process($student){
59
        $institution_class = Institution_class::select('id')->where('institution_id',$student['institution_id'])->get()->toArray();
60
        $wrongStudentsClass = Institution_class_student::whereNotIn('institution_class_id',$institution_class)
61
        ->orWhere('institution_class_id',0)
62
        ->where('student_id',$student['student_id'])
63
        ->get()->toArray();
64
        
65
        if(count($wrongStudentsClass)>0){
66
            Institution_class_student::where('student_id',$student['student_id'])->forceDelete();
67
            Institution_student_admission::where('student_id',$student['student_id'])->forceDelete();
68
            Institution_student::where('student_id',$student['student_id'])->forceDelete();
69
70
            array_walk($wrongStudentsClass,array($this,'updateClassCount'));
71
            
72
            echo "deleted wrong class reference:" .$student['student_id']; 
73
74
            $institutionClass =  Institution_class::getGradeClasses($student['education_grade_id'],$student['institution_id']);
75
76
            if(count($institutionClass) == 1){
77
                $start_date = new Carbon($student['start_date']);
78
                $end_date = new Carbon($student['end_date']);
79
                Institution_student_admission::create(
80
                    [
81
                        'student_id'=>$student['student_id'],
82
                        'institution_class_id'=>  $institutionClass[0]['id'],
83
                        'start_date' => $student['start_date'],
84
                        'start_year' => $start_date->format('Y'),
85
                        'end_date' => $student['start_date'],
86
                        'end_year' => $end_date->format('Y'),
87
                        'education_grade_id' => $student['education_grade_id'],
88
                        'institution_id' => $student['institution_id'],
89
                        'status_id' => 124,
90
                        'academic_period_id' => $student['academic_period_id'],
91
                        'created_user_id' => $student['created_user_id'],
92
                        'updated_from' => $student['updated_from']
93
                    ]
94
                );
95
                $institutionClassStudent = [
96
                    'student_id'=>$student['student_id'],
97
                    'institution_class_id'=>  $institutionClass[0]['id'],
98
                    'education_grade_id' => $student['education_grade_id'],
99
                    'institution_id' => $student['institution_id'],
100
                    'status_id' => 124,
101
                    'academic_period_id' => $student['academic_period_id'],
102
                    'student_status_id' => 1,
103
                    'created_user_id' => $student['created_user_id'],
104
                    'updated_from' => $student['updated_from']
105
                ];
106
                Institution_class_student::create($institutionClassStudent);
107
                Institution_student::create([
108
                    'student_id'=>$student['student_id'],
109
                    'student_status_id' => 1,
110
                    'education_grade_id' => $student['education_grade_id'],
111
                    'institution_id' => $student['institution_id'],
112
                    'academic_period_id' => $student['academic_period_id'],
113
                    'created_user_id' => $student['created_user_id'],
114
                    'start_date' => $student['start_date'],
115
                    'start_year' => $start_date->format('Y'),
116
                    'end_date' => $student['start_date'],
117
                    'end_year' => $end_date->format('Y'),
118
                    'taking_g5_exam' => $student['updated_from'] == 'doe' ? true: false,
119
                    'income_at_g5' =>  $student['income_at_g5'],
120
                    'updated_from' => $student['updated_from'],
121
                    'exam_center_for_special_education_g5' =>  $student['exam_center_for_special_education_g5'],
122
                    'modified_user_id' =>  $student['modified_user_id'],
123
                ]);
124
                echo "updated:" .$student['student_id']; 
125
                array_walk([$institutionClassStudent],array($this,'updateClassCount'));
0 ignored issues
show
Bug introduced by
array($institutionClassStudent) cannot be passed to array_walk() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                array_walk(/** @scrutinizer ignore-type */ [$institutionClassStudent],array($this,'updateClassCount'));
Loading history...
126
            }
127
128
        }
129
    }
130
131
    public function updateClassCount($institutionClass){
132
        $studentCount = Institution_class_student::getStudentsCount($institutionClass['institution_class_id']);
133
        Institution_class::where(['id' => $institutionClass['institution_class_id']])
134
        ->update([
135
            'total_male_students' => $studentCount['total_male_students'],
136
            'total_female_students' => $studentCount['total_female_students']
137
        ]);
138
    }
139
}
140