Conditions | 51 |
Paths | > 20000 |
Total Lines | 303 |
Code Lines | 216 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 3 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function model(array $row) { |
||
86 | |||
87 | try { |
||
88 | $institutionClass = Institution_class::find($this->file['institution_class_id']); |
||
89 | $institution = $institutionClass->institution_id; |
||
90 | |||
91 | |||
92 | if (!array_filter($row)) { |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | if (!empty($institutionClass)) { |
||
97 | $mandatorySubject = Institution_class_subject::getMandetorySubjects($this->file['institution_class_id']); |
||
98 | $subjects = getMatchingKeys($row); |
||
99 | $genderId = null; |
||
100 | switch ($row['gender_mf']) { |
||
101 | case 'M': |
||
102 | $row['gender_mf'] = 1; |
||
103 | $this->maleStudentsCount += 1; |
||
104 | break; |
||
105 | case 'F': |
||
106 | $row['gender_mf'] = 2; |
||
107 | $this->femaleStudentsCount += 1; |
||
108 | break; |
||
109 | } |
||
110 | |||
111 | $BirthArea = Area_administrative::where('name', 'like', '%' . $row['birth_registrar_office_as_in_birth_certificate'] . '%')->first(); |
||
112 | $nationalityId = Nationality::where('name', 'like', '%' . $row['nationality'] . '%')->first(); |
||
113 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['identity_type'] . '%')->first(); |
||
114 | $academicPeriod = Academic_period::where('name', '=', $row['academic_period'])->first(); |
||
115 | |||
116 | |||
117 | $date = $row['date_of_birth_yyyy_mm_dd']; |
||
118 | |||
119 | $identityType = $identityType !== null ? $identityType->id : null; |
||
120 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
121 | |||
122 | $BirthArea = $BirthArea !== null ? $BirthArea->id : null; |
||
123 | $identityNUmber = $row['identity_number']; |
||
124 | |||
125 | //create students data |
||
126 | \Log::debug('Security_user'); |
||
127 | |||
128 | $studentInfo = Security_user::where('openemis_no', '=', $row['student_id'])->first(); |
||
129 | Security_user::where('openemis_no', $studentInfo['openemis_no']) |
||
130 | ->update([ |
||
131 | 'first_name' => $row['full_name'] ? $row['full_name'] : $studentInfo['first_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
132 | 'last_name' => $row['full_name'] ? genNameWithInitials($row['full_name']) : genNameWithInitials($studentInfo['first_name']), |
||
133 | 'gender_id' => $genderId ? $genderId : $studentInfo['gender_id'], |
||
134 | 'date_of_birth' => $date ? $date : $studentInfo['date_of_birth'], |
||
135 | 'address' => $row['address'] ? $row['address'] : $studentInfo['address'], |
||
136 | 'birthplace_area_id' => $row['birth_registrar_office_as_in_birth_certificate'] ? $BirthArea : $studentInfo['birthplace_area_id'], |
||
137 | 'nationality_id' => $row['nationality'] ? $nationalityId : $studentInfo['nationality_id'], |
||
138 | 'identity_type_id' => $row['identity_type'] ? $identityType : $studentInfo['identity_type_id'], |
||
139 | 'identity_number' => $row['identity_number'] ? $identityNUmber : $studentInfo['identity_number'], |
||
140 | 'is_student' => 1, |
||
141 | 'modified' => now(), |
||
142 | 'modified_user_id' => $this->file['security_user_id'] |
||
143 | ]); |
||
144 | |||
145 | $student = Institution_class_student::where('student_id', '=', $studentInfo->id)->first(); |
||
146 | |||
147 | if(!empty($row['admission_no']) && !empty($academicPeriod)){ |
||
148 | Institution_student::where('student_id','=',$studentInfo->id) |
||
149 | ->where('institution_id','=', $institution) |
||
150 | ->where('academic_period_id','=',$academicPeriod->id) |
||
151 | ->update(['admission_id'=> $row['admission_no']]); |
||
152 | } |
||
153 | |||
154 | if (!empty($row['special_need'])) { |
||
155 | |||
156 | $specialNeed = Special_need_difficulty::where('name', '=', $row['special_need'])->first(); |
||
157 | $data = [ |
||
158 | 'special_need_date' => now(), |
||
159 | 'security_user_id' => $student->student_id, |
||
160 | 'special_need_type_id' => 1, |
||
161 | 'special_need_difficulty_id' => $specialNeed->id, |
||
162 | 'created_user_id' => $this->file['security_user_id'] |
||
163 | ]; |
||
164 | |||
165 | $check = User_special_need::isDuplicated($data); |
||
166 | if ($check) { |
||
167 | User_special_need::create($data); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | |||
172 | |||
173 | if (!empty($row['bmi_height']) && (!empty($row['bmi_weight']))) { |
||
174 | |||
175 | // convert Meeter to CM |
||
176 | $hight = $row['bmi_height'] / 100; |
||
177 | |||
178 | //calculate BMI |
||
179 | $bodyMass = ($row['bmi_weight']) / pow($hight, 2); |
||
180 | |||
181 | $bmiAcademic = Academic_period::where('name', '=', $row['bmi_academic_period'])->first(); |
||
182 | $count = User_body_mass::where('academic_period_id' ,'=',$bmiAcademic->id ) |
||
183 | ->where('security_user_id','=',$student->student_id)->count(); |
||
184 | |||
185 | \Log::debug('User_body_mass'); |
||
186 | if(!($count > 0)){ |
||
187 | User_body_mass::create([ |
||
188 | 'height' => $row['bmi_height'], |
||
189 | 'weight' => $row['bmi_weight'], |
||
190 | 'date' => $row['bmi_date_yyyy_mm_dd'], |
||
191 | 'body_mass_index' => $bodyMass, |
||
192 | 'academic_period_id' => $bmiAcademic->id, |
||
193 | 'security_user_id' => $student->student_id, |
||
194 | 'created_user_id' => $this->file['security_user_id'] |
||
195 | ]); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | if (!empty($row['fathers_full_name']) && ($row['fathers_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
200 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['fathers_address_area'] . '%')->first(); |
||
201 | $nationalityId = Nationality::where('name', 'like', '%' . $row['fathers_nationality'] . '%')->first(); |
||
202 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['fathers_identity_type'] . '%')->first(); |
||
203 | $openemisFather = $this->uniqueUid::getUniqueAlphanumeric(); |
||
204 | |||
205 | $identityType = ($identityType !== null) ? $identityType->id : null; |
||
206 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
207 | |||
208 | $father = null; |
||
209 | if (!empty($row['fathers_identity_number'])) { |
||
210 | $father = Security_user::where('identity_type_id', '=', $nationalityId) |
||
211 | ->where('identity_number', '=', $row['fathers_identity_number'])->first(); |
||
212 | } |
||
213 | |||
214 | |||
215 | if ($father === null) { |
||
216 | $data = [ |
||
217 | 'username' => str_replace('-','',$openemisFather), |
||
218 | 'openemis_no' => $openemisFather, |
||
219 | 'first_name' => $row['fathers_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
220 | 'last_name' => genNameWithInitials($row['fathers_full_name']), |
||
221 | 'gender_id' => 1, |
||
222 | 'date_of_birth' => $row['fathers_date_of_birth_yyyy_mm_dd'], |
||
223 | 'address' => $row['fathers_address'], |
||
224 | 'address_area_id' => $AddressArea->id, |
||
225 | 'nationality_id' => $nationalityId, |
||
226 | 'identity_type_id' => $identityType, |
||
227 | 'identity_number' => $row['fathers_identity_number'], |
||
228 | 'is_guardian' => 1, |
||
229 | 'created_user_id' => $this->file['security_user_id'] |
||
230 | ]; |
||
231 | $father = Security_user::create($data); |
||
232 | |||
233 | $father['guardian_relation_id'] = 1; |
||
234 | if (array_key_exists('fathers_phone', $row)) { |
||
235 | $father['contact'] = $row['fathers_phone']; |
||
236 | User_contact::createOrUpdate($father,$this->file['security_user_id']); |
||
237 | } |
||
238 | Student_guardian::createStudentGuardian($student, $father, $this->file['security_user_id']); |
||
239 | } else { |
||
240 | Security_user::where('id', '=', $father->id) |
||
241 | ->update(['is_guardian' => 1]); |
||
242 | $father['guardian_relation_id'] = 1; |
||
243 | if (array_key_exists('fathers_phone', $row)) { |
||
244 | $father['contact'] = $row['fathers_phone']; |
||
245 | User_contact::createOrUpdate($father,$this->file['security_user_id']); |
||
246 | } |
||
247 | Student_guardian::createStudentGuardian($student, $father, $this->file['security_user_id']); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | if (!empty($row['mothers_full_name']) && ($row['mothers_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
252 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['mothers_address_area'] . '%')->first(); |
||
253 | $nationalityId = Nationality::where('name', 'like', '%' . $row['mothers_nationality'] . '%')->first(); |
||
254 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['mothers_identity_type'] . '%')->first(); |
||
255 | $openemisMother = $this->uniqueUid::getUniqueAlphanumeric(); |
||
256 | |||
257 | $identityType = $identityType !== null ? $identityType->id : null; |
||
258 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
259 | |||
260 | $mother = null; |
||
261 | |||
262 | if (!empty($row['mothers_identity_number'])) { |
||
263 | $mother = Security_user::where('identity_type_id', '=', $nationalityId) |
||
264 | ->where('identity_number', '=', $row['mothers_identity_number'])->first(); |
||
265 | } |
||
266 | |||
267 | if ($mother === null) { |
||
268 | $mother = Security_user::create([ |
||
269 | 'username' => str_replace('-','',$openemisMother), |
||
270 | 'openemis_no' => $openemisMother, |
||
271 | 'first_name' => $row['mothers_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
272 | 'last_name' => genNameWithInitials($row['mothers_full_name']), |
||
273 | 'gender_id' => 2, |
||
274 | 'date_of_birth' => $row['mothers_date_of_birth_yyyy_mm_dd'], |
||
275 | 'address' => $row['mothers_address'], |
||
276 | 'address_area_id' => $AddressArea->id, |
||
277 | 'nationality_id' => $nationalityId, |
||
278 | 'identity_type_id' => $identityType, |
||
279 | 'identity_number' => $row['mothers_identity_number'], |
||
280 | 'is_guardian' => 1, |
||
281 | 'created_user_id' => $this->file['security_user_id'] |
||
282 | ]); |
||
283 | |||
284 | $mother['guardian_relation_id'] = 2; |
||
285 | if (array_key_exists('mothers_phone', $row)) { |
||
286 | $mother['contact'] = $row['mothers_phone']; |
||
287 | User_contact::createOrUpdate($mother,$this->file['security_user_id']); |
||
288 | } |
||
289 | Student_guardian::createStudentGuardian($student, $mother, $this->file['security_user_id']); |
||
290 | } else { |
||
291 | Security_user::where('id', '=', $mother->id) |
||
292 | ->update(['is_guardian' => 1]); |
||
293 | $mother['guardian_relation_id'] = 2; |
||
294 | if (array_key_exists('mothers_phone', $row)) { |
||
295 | $mother['contact'] = $row['mothers_phone']; |
||
296 | User_contact::createOrUpdate($mother,$this->file['security_user_id']); |
||
297 | } |
||
298 | Student_guardian::createStudentGuardian($student, $mother, $this->file['security_user_id']); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | |||
303 | if (!empty($row['guardians_full_name']) && ($row['guardians_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
304 | $genderId = $row['guardians_gender_mf'] == 'M' ? 1 : 2; |
||
305 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['guardians_address_area'] . '%')->first(); |
||
306 | $nationalityId = Nationality::where('name', 'like', '%' . $row['guardians_nationality'] . '%')->first(); |
||
307 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['guardians_identity_type'] . '%')->first(); |
||
308 | $openemisGuardian = $this->uniqueUid::getUniqueAlphanumeric(); |
||
309 | |||
310 | $identityType = $identityType !== null ? $identityType->id : null; |
||
311 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
312 | |||
313 | $guardian = null; |
||
314 | |||
315 | if (!empty($row['guardians_identity_number'])) { |
||
316 | $guardian = Security_user::where('identity_type_id', '=', $nationalityId) |
||
317 | ->where('identity_number', '=', $row['guardians_identity_number'])->first(); |
||
318 | } |
||
319 | |||
320 | if ($guardian === null) { |
||
321 | $guardian = Security_user::create([ |
||
322 | 'username' => str_replace('-','',$openemisGuardian), |
||
323 | 'openemis_no' => $openemisGuardian, |
||
324 | 'first_name' => $row['guardians_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
325 | 'last_name' => genNameWithInitials($row['guardians_full_name']), |
||
326 | 'gender_id' => $genderId, |
||
327 | 'date_of_birth' => $row['guardians_date_of_birth_yyyy_mm_dd'], |
||
328 | 'address' => $row['guardians_address'], |
||
329 | 'address_area_id' => $AddressArea->id, |
||
330 | // 'birthplace_area_id' => $BirthArea->id, |
||
331 | 'nationality_id' => $nationalityId, |
||
332 | 'identity_type_id' => $identityType, |
||
333 | 'identity_number' => $row['guardians_identity_number'], |
||
334 | 'is_guardian' => 1, |
||
335 | 'created_user_id' => $this->file['security_user_id'] |
||
336 | ]); |
||
337 | |||
338 | $guardian['guardian_relation_id'] = 3; |
||
339 | if (array_key_exists('guardians_phone', $row)) { |
||
340 | $guardian['contact'] = $row['guardians_phone']; |
||
341 | User_contact::createOrUpdate($guardian,$this->file['security_user_id']); |
||
342 | } |
||
343 | Student_guardian::createStudentGuardian($student, $guardian, $this->file['security_user_id']); |
||
344 | } else { |
||
345 | Security_user::where('id', '=', $guardian->id) |
||
346 | ->update(['is_guardian' => 1]); |
||
347 | $guardian['guardian_relation_id'] = 3; |
||
348 | if (array_key_exists('guardians_phone', $row)) { |
||
349 | $guardian['contact'] = $row['guardians_phone']; |
||
350 | User_contact::createOrUpdate($guardian,$this->file['security_user_id']); |
||
351 | } |
||
352 | Student_guardian::createStudentGuardian($student, $guardian, $this->file['security_user_id']); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | $optionalSubjects = Institution_class_subject::getStudentOptionalSubject($subjects, $student, $row, $institution); |
||
357 | |||
358 | $allSubjects = array_merge_recursive($optionalSubjects, $mandatorySubject); |
||
359 | // $stundetSubjects = $this->getStudentSubjects($student); |
||
360 | // $allSubjects = array_merge_recursive($newSubjects, $stundetSubjects); |
||
361 | |||
362 | if (!empty($allSubjects)) { |
||
363 | $allSubjects = unique_multidim_array($allSubjects, 'institution_subject_id'); |
||
364 | $this->student = $student; |
||
365 | $allSubjects = array_map(array($this,'setStudentSubjects'),$allSubjects); |
||
366 | // $allSubjects = array_unique($allSubjects,SORT_REGULAR); |
||
367 | $allSubjects = unique_multidim_array($allSubjects, 'education_subject_id'); |
||
368 | array_walk($allSubjects,array($this,'insertSubject')); |
||
369 | array_walk($allSubjects, array($this, 'updateSubjectCount')); |
||
370 | } |
||
371 | |||
372 | unset($allSubjects); |
||
373 | |||
374 | $totalStudents = Institution_class_student::getStudentsCount($this->file['institution_class_id']); |
||
375 | |||
376 | Institution_class::where('id', '=', $institutionClass->id) |
||
377 | ->update([ |
||
378 | 'total_male_students' => $totalStudents['total_male_students'], |
||
379 | 'total_female_students' => $totalStudents['total_female_students']]); |
||
380 | } |
||
381 | } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
||
382 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
383 | $failures = $e->failures(); |
||
384 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
385 | Log::info('email-sent', [$e]); |
||
386 | } |
||
387 | unset($row); |
||
388 | } |
||
446 |