Test Setup Failed
Pull Request — master (#610)
by Mohamed
14:20
created

CloneController::updateInstitutionClasses()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
c 0
b 0
f 0
nc 7
nop 3
dl 0
loc 25
rs 9.7333
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Models\Academic_period;
7
use App\Models\Institution_class;
8
use App\Models\Institution_shift;
9
use Illuminate\Support\Facades\DB;
10
use App\Models\Institution_subject;
11
use Illuminate\Support\Facades\Log;
12
use App\Http\Controllers\Controller;
13
use App\Models\Institution_class_grade;
14
use App\Models\Education_grades_subject;
15
use App\Models\Institution_class_student;
16
use App\Models\Institution_class_subject;
17
use App\Models\Institution_student;
18
19
class CloneController extends Controller
20
{
21
    /**
22
     * Create a new command instance.
23
     *
24
     * @return void
25
     */
26
    public function __construct()
27
28
    {
29
        $this->shifts = new Institution_shift();
0 ignored issues
show
Bug Best Practice introduced by
The property shifts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->academic_period = new Academic_period();
0 ignored issues
show
Bug Best Practice introduced by
The property academic_period does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->institution_classes = new Institution_class();
0 ignored issues
show
Bug Best Practice introduced by
The property institution_classes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->institution_class_subjects = new Institution_class_subject();
0 ignored issues
show
Bug Best Practice introduced by
The property institution_class_subjects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->institution_subjects =  new Institution_subject();
0 ignored issues
show
Bug Best Practice introduced by
The property institution_subjects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->education_grade_subjects =  new Education_grades_subject();
0 ignored issues
show
Bug Best Practice introduced by
The property education_grade_subjects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
        $this->output = new \Symfony\Component\Console\Output\ConsoleOutput();
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
    }
37
38
39
    public function array_walk($shift, $count, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

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

39
    public function array_walk($shift, /** @scrutinizer ignore-unused */ $count, $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        array_walk($shift, array($this, 'process'), $params);
42
    }
43
44
    public function cleanConfig($params)
45
    {
46
        $academicPeriod = $params['academic_period'];
47
        $this->shifts->where(['academic_period_id' => $academicPeriod->id])->delete();
48
        $this->output->writeln('cleaned shifts');
49
50
        $this->shifts->where(['cloned' => $academicPeriod->code])->update(['cloned' => $params['previous_academic_period']['code']]);
51
        $this->output->writeln('updated shifts');
52
53
        $classIds =  $this->institution_classes->select('id')->where(['academic_period_id' => $academicPeriod->id])->get()->toArray();
54
        $this->institution_classes->where(['academic_period_id' => $academicPeriod->id])->delete();
55
        $this->output->writeln('cleaned classes');
56
57
        $this->institution_class_subjects->whereNotIn('institution_class_id', $classIds)->delete();
58
        $this->output->writeln('cleaned subjects');
59
60
        do {
61
            $deleted =  $this->institution_subjects->where('academic_period_id', $academicPeriod->id)->limit(100000)->delete();
62
            $this->output->writeln('100000 institutions cleaned shifts');
63
        } while ($deleted > 0);
64
    }
65
66
    public function process($shift, $count, $params)
67
    {
68
        echo ('[' . getmypid() . ']This Process executed at' . date("F d, Y h:i:s A") . "\n");
69
        $year = $params['year'];
70
        $academicPeriod = $params['academic_period'];
71
        $previousAcademicPeriod = $params['previous_academic_period'];
72
        $mode = $params['mode'] == 'AL' ? true : false;
73
74
        $data = $this->updateShifts($year, $shift);
75
        $shiftId = $data['shift_id'];
76
77
        $params = [
78
            'previous_academic_period_id' => $previousAcademicPeriod->id,
79
            'academic_period_id' => $academicPeriod->id,
80
            'shift_id' => $data['shift_id'],
81
            'mode' => $mode
82
        ];
83
84
85
        if ($mode) {
86
            $institutionClasses = $this->institution_classes->getShiftClasses($shift,  $mode);
87
            try {
88
                array_walk($institutionClasses, array($this, 'updateInstitutionClasses'), $params);
89
                $this->output->writeln('updating from ' . $shift['institution_id']);
90
            } catch (\Exception $e) {
91
                Log::error($e->getMessage(), [$e]);
92
            }
93
        } else {
94
            $institutionSubjects = $this->institution_subjects->getInstitutionSubjects($shift['institution_id'], $previousAcademicPeriod->id, $mode);
95
            try {
96
                if ($data['created']) {
97
                    $institutionClasses = $this->institution_classes->getShiftClasses($shift, $mode);
98
                    array_walk($institutionSubjects, array($this, 'insertInstitutionSubjects'), $academicPeriod);
99
                    if (!empty($institutionClasses) && !is_null($shiftId) && !is_null($academicPeriod)) {
100
                        $newInstitutionClasses = $this->generateNewClass($institutionClasses, $shiftId, $academicPeriod->id);
101
                        try {
102
                            array_walk($newInstitutionClasses, array($this, 'insertInstitutionClasses'), $params);
103
                            $this->output->writeln('##########################################################################################################################');
104
                            $this->output->writeln('updating from ' . $shift['institution_id']);
105
                        } catch (\Exception $e) {
106
                            Log::error($e->getMessage(), [$e]);
107
                        }
108
                    } else {
109
                        $this->output->writeln('no classes found ' . $shift['institution_id']);
110
                    }
111
                } else {
112
                    try {
113
                        $shift['id'] = $shiftId;
114
                        $institutionClasses = $this->institution_classes->getShiftClasses($shift, $mode);
115
                        array_walk($institutionClasses, array($this, 'updateInstitutionClasses'), $params);
116
                        $this->output->writeln('##########################################################################################################################');
117
                        $this->output->writeln('updating from ' . $shift['institution_id']);
118
                    } catch (\Exception $e) {
119
                        Log::error($e->getMessage(), [$e]);
120
                    }
121
                }
122
            } catch (\Exception $e) {
123
                Log::error($e->getMessage(), [$e]);
124
            }
125
        }
126
    }
127
128
129
    /**
130
     * @param $subjects
131
     * @param $count
132
     * @param $academicPeriod
133
     */
134
    public function insertInstitutionSubjects($subjects, $count, $academicPeriod)
135
    {
136
        try {
137
            $subjects['academic_period_id'] = $academicPeriod->id;
138
            $subjects['created'] = now();
139
            unset($subjects['total_male_students']);
140
            unset($subjects['total_female_students']);
141
            unset($subjects['id']);
142
            $this->institution_subjects->create($subjects);
143
        } catch (\Exception $e) {
144
            Log::error($e->getMessage(), [$e]);
145
        }
146
    }
147
148
    public function updateInstitutionClasses($class, $count, $params)
149
    {
150
        try {
151
            if ($params['mode']) {
152
                Institution_class::where('id', $class['id'])
153
                    ->update([
154
                        'institution_shift_id' => $params['shift_id'],
155
                        'academic_period_id' => $params['academic_period_id']
156
                    ]);
157
158
                Institution_class_student::where('institution_class_id', $class['id'])
159
                    ->update([
160
                        'academic_period_id' => $params['academic_period_id'],
161
                        'modified' => now()
162
                    ]);
163
164
                $educationGrade = Institution_class_grade::select('education_grade_id')->where('institution_class_id', $class['id'])->get()->toArray();
165
166
                Institution_student::whereIn('education_grade_id', $educationGrade)
167
                    ->update([
168
                        'academic_period_id' => $params['academic_period_id']
169
                    ]);
170
            }
171
        } catch (\Exception $e) {
172
            Log::error($e->getMessage(), [$e]);
173
        }
174
    }
175
176
    public function  insertInstitutionClasses($class, $count, $param)
177
    {
178
        try {
179
            $academicPeriod = $param['academic_period_id'];
180
            $educationGrdae = $class['education_grade_id'];
181
182
            $classId = $class['id'];
183
            unset($class['id']);
184
            $institutionSubjects = Institution_subject::query()->where('education_grade_id', $class['education_grade_id'])
185
                ->where('institution_id', $class['institution_id'])
186
                ->where('academic_period_id', $academicPeriod)->get()->toArray();
187
            $params = [
188
                'class' => $class,
189
                'subjects' => $institutionSubjects,
190
                'academic_period_id' => $academicPeriod,
191
                'classId' => $classId
192
            ];
193
            unset($class['education_grade_id']);
194
            $noOfStudents = $class['no_of_students'] == 0 ? 40 : $class['no_of_students'];
195
            $class['academic_period_id'] = $academicPeriod;
196
            $class['no_of_students'] = $noOfStudents;
197
            $class['created'] = now();
198
            $class['institution_shift_id'] = $param['shift_id'];
199
            // $class['created_user_id'] = 
200
            $this->output->writeln('Create class:' . $class['name']);
201
            $class = Institution_class::create($class);
202
            $institutionClassGrdaeObj['institution_class_id'] = $class->id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$institutionClassGrdaeObj was never initialized. Although not strictly required by PHP, it is generally a good practice to add $institutionClassGrdaeObj = array(); before regardless.
Loading history...
203
            $institutionClassGrdaeObj['education_grade_id'] = $educationGrdae;
204
            $institutionClassGrdaeObj['created_user_id'] = $class['created_user_id'];
205
            Institution_class_grade::create($institutionClassGrdaeObj);
206
            $institutionSubjects = Institution_subject::query()->where('education_grade_id', $educationGrdae)
207
                ->where('institution_id', $class->institution_id)
0 ignored issues
show
Bug introduced by
The property institution_id does not exist on App\Models\Institution_class. Did you mean institution?
Loading history...
208
                ->where('academic_period_id', $academicPeriod)
209
                ->groupBy('education_subject_id')
210
                ->get()
211
                ->toArray();
212
            $params['class'] = $class;
213
            $this->insertInstitutionClassSubjects($institutionSubjects, $class);
214
            //                array_walk($classSubjects,array($this,'insertInstitutionClassSubjects'),$params);
215
        } catch (\Exception $e) {
216
            Log::error($e->getMessage(), [$e]);
217
        }
218
    }
219
220
    public function insertInstitutionClassSubjects($subjects, $class)
221
    {
222
        if (!empty($subjects)) {
223
            try {
224
                array_walk($subjects, array($this, 'insertClassSubjects'), $class);
225
                $this->output->writeln('updating subjects ' . $class->name);
226
            } catch (\Exception $e) {
227
                Log::error($e->getMessage(), [$e]);
228
            }
229
        };
230
    }
231
232
233
    public function insertClassSubjects($subject, $count, $newClassId)
234
    {
235
        try {
236
            $subjectobj['status'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$subjectobj was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subjectobj = array(); before regardless.
Loading history...
237
            $subjectobj['created_user_id'] = 1;
238
            $subjectobj['created'] = now();
239
240
            $subjectobj['institution_class_id'] = $newClassId->id;
241
            $subjectobj['institution_subject_id'] = $subject['id'];
242
243
            if (!$this->institution_class_subjects->isDuplicated($subjectobj)) {
244
                $this->institution_class_subjects->create($subjectobj);
245
            }
246
        } catch (\Exception $e) {
247
            Log::error($e->getMessage(), [$e]);
248
        }
249
    }
250
251
    /**
252
     * generate new class object for new academic year
253
     *
254
     * @param $classes
255
     * @param $shiftId
256
     * @param $academicPeriod
257
     * @return array
258
     */
259
    public function generateNewClass($classes, $shiftId, $academicPeriod)
260
    {
261
        $newClasses = [];
262
        foreach ($classes as $class) {
263
            $noOfStudents = $class['no_of_students'] == 0 ? 40 : $class['no_of_students'];
264
            $class['academic_period_id'] = $academicPeriod;
265
            $class['no_of_students'] = $noOfStudents;
266
            $class['created'] = now();
267
            $class['institution_shift_id'] = $shiftId;
268
            array_push($newClasses, $class);
269
        }
270
        return $newClasses;
271
    }
272
273
    /**
274
     * update shifts
275
     * @param $year
276
     * @param $shift
277
     * @return mixed
278
     */
279
    public function updateShifts($year, $shift)
280
    {
281
        try {
282
            $academicPeriod = $this->academic_period->getAcademicPeriod($year);
283
            $this->shifts->where(['id' =>  $shift['id']])->update(['cloned' => $year]);
284
            $shift['academic_period_id'] = $academicPeriod->id;
285
            $exist = $this->shifts->getShift($shift);
286
            $data = array();
287
288
            if (is_null($exist)) {
289
                $shift['cloned'] = $year;
290
                unset($shift['id']);
291
                unset($shift['created']);
292
                unset($shift['modified']);
293
                $shift = $this->shifts->create((array)$shift);
294
                $data = [
295
                    'shift_id' => $shift->id,
296
                    'created' => true
297
                ];
298
            } else {
299
                $data = [
300
                    'shift_id' => $exist->id,
301
                    'created' => false
302
                ];
303
            };
304
            return $data;
305
        } catch (\Exception $e) {
306
            dd($e);
307
        }
308
    }
309
}
310