Total Complexity | 51 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 6 | Features | 2 |
Complex classes like ExaminationStudentsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExaminationStudentsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ExaminationStudentsController extends Controller |
||
26 | { |
||
27 | public function __construct($year = 2019, $grade = 'G5') |
||
28 | { |
||
29 | $this->year = $year; |
||
|
|||
30 | $this->grade = $grade; |
||
31 | $this->student = new Security_user(); |
||
32 | $this->examination_student = new Examination_student(); |
||
33 | $this->academic_period = Academic_period::where('code', '=', $this->year)->first(); |
||
34 | $this->education_grade = Education_grade::where('code', '=', $this->grade)->first(); |
||
35 | $this->output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
36 | } |
||
37 | |||
38 | public function index() |
||
41 | } |
||
42 | |||
43 | public function uploadFile(Request $request) |
||
44 | { |
||
45 | if ($request->input('submit') != null) { |
||
46 | |||
47 | $file = $request->file('file'); |
||
48 | |||
49 | // File Details |
||
50 | $filename = 'exams_students.csv'; |
||
51 | $extension = $file->getClientOriginalExtension(); |
||
52 | $fileSize = $file->getSize(); |
||
53 | |||
54 | // Valid File Extensions |
||
55 | $valid_extension = array("csv"); |
||
56 | |||
57 | // 40MB in Bytes |
||
58 | $maxFileSize = 40971520; |
||
59 | |||
60 | // Check file extension |
||
61 | if (in_array(strtolower($extension), $valid_extension)) { |
||
62 | |||
63 | // Check file size |
||
64 | if ($fileSize <= $maxFileSize) { |
||
65 | |||
66 | // File upload location |
||
67 | Storage::disk('local')->putFileAs( |
||
68 | 'examination/', |
||
69 | $file, |
||
70 | $filename |
||
71 | ); |
||
72 | Session::flash('message', 'File upload successfully!'); |
||
73 | // Redirect to index |
||
74 | } else { |
||
75 | Session::flash('message', 'File too large. File must be less than 20MB.'); |
||
76 | } |
||
77 | } else { |
||
78 | Session::flash('message', 'Invalid File Extension.'); |
||
79 | } |
||
80 | } |
||
81 | return redirect()->action('ExaminationStudentsController@index'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Import students data to the Examinations table |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | public static function callOnClick($year, $grade) |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * updated wrong census |
||
129 | * |
||
130 | * @param [type] $data |
||
131 | * @return void |
||
132 | */ |
||
133 | public function updateCensusNo($data) |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Iterate over existing student's data |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function doMatch($offset, $limit, $mode) |
||
173 | { |
||
174 | $students = []; |
||
175 | switch ($mode) { |
||
176 | case 'duplicate': |
||
177 | $students = DB::table('examination_students as es') |
||
178 | ->select(DB::raw('count(*) as total'), 'e2.*') |
||
179 | ->where('grade', $this->grade) |
||
180 | ->where('year', $this->year) |
||
181 | ->join('examination_students as e2', 'es.nsid', 'e2.nsid') |
||
182 | ->having('total', '>', 1) |
||
183 | ->groupBy('e2.st_no') |
||
184 | ->orderBy('e2.st_no') |
||
185 | ->offset($offset) |
||
186 | ->limit($limit) |
||
187 | ->get()->toArray(); |
||
188 | $students = (array) json_decode(json_encode($students)); |
||
189 | $this->output->writeln(count($students) . 'students remaining duplicate'); |
||
190 | array_walk($students, array($this, 'clone')); |
||
191 | $this->output->writeln('All are generated'); |
||
192 | break; |
||
193 | case 'empty'; |
||
194 | $students = Examination_student:: |
||
195 | whereNull('nsid') |
||
196 | ->where('grade', $this->grade) |
||
197 | ->where('year', $this->year) |
||
198 | ->offset($offset) |
||
199 | ->limit($limit) |
||
200 | ->get()->toArray(); |
||
201 | $students = (array) json_decode(json_encode($students)); |
||
202 | $this->output->writeln(count($students) . 'students remaining empty'); |
||
203 | array_walk($students, array($this, 'clone')); |
||
204 | $this->output->writeln('All are generated'); |
||
205 | break; |
||
206 | case 'invalid'; |
||
207 | $students = Examination_student::whereRaw('CHAR_LENGTH(nsid) > 11') |
||
208 | ->where('grade', $this->grade) |
||
209 | ->where('year', $this->year) |
||
210 | ->get()->toArray(); |
||
211 | $students = (array) json_decode(json_encode($students)); |
||
212 | $this->output->writeln(count($students) . 'students remaining with wrong NSID'); |
||
213 | array_walk($students, array($this, 'clone')); |
||
214 | $this->output->writeln('All are generated'); |
||
215 | break; |
||
216 | case 'count': |
||
217 | $count = Examination_student::distinct('nsid') |
||
218 | ->where('grade', $this->grade) |
||
219 | ->where('year', $this->year) |
||
220 | ->count(); |
||
221 | $all = Examination_student::select('nsid') |
||
222 | ->count(); |
||
223 | $this->output->writeln($all . 'Total Unique nsid are: ' . $count); |
||
224 | break; |
||
225 | default: |
||
226 | $students = Examination_student::offset($offset) |
||
227 | ->where('grade', $this->grade) |
||
228 | ->where('year', $this->year) |
||
229 | ->limit($limit) |
||
230 | ->get()->toArray(); |
||
231 | $students = (array) json_decode(json_encode($students)); |
||
232 | $this->output->writeln(count($students) . 'students remaining empty'); |
||
233 | array_walk($students, array($this, 'clone')); |
||
234 | $this->output->writeln('All are generated'); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Set Examination values |
||
240 | * |
||
241 | * @param array $students |
||
242 | * @return array |
||
243 | */ |
||
244 | public function setIsTakingExam($students) |
||
245 | { |
||
246 | $students['taking_g5_exam'] = false; |
||
247 | $students['taking_ol_exam'] = false; |
||
248 | $students['taking_al_exam'] = false; |
||
249 | $students['taking_git_exam'] = false; |
||
250 | switch ($students['grade']) { |
||
251 | case 'G5': |
||
252 | $students['taking_g5_exam'] = true; |
||
253 | break; |
||
254 | case 'G4': |
||
255 | $students['taking_g5_exam'] = true; |
||
256 | break; |
||
257 | case 'G10': |
||
258 | $students['taking_ol_exam'] = true; |
||
259 | break; |
||
260 | case 'G11': |
||
261 | $students['taking_ol_exam'] = true; |
||
262 | break; |
||
263 | case 'GIT': |
||
264 | $students['taking_git_exam'] = true; |
||
265 | break; |
||
266 | } |
||
267 | |||
268 | return $students; |
||
269 | } |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Main function to merge the student's to SIS |
||
274 | * |
||
275 | * @param array $student |
||
276 | * @return void |
||
277 | */ |
||
278 | public function clone($student) |
||
279 | { |
||
280 | $student = (array)json_decode(json_encode($student)); |
||
281 | //get student matching with same dob and gender |
||
282 | |||
283 | $matchedStudent = $this->getMatchingStudents($student); |
||
284 | |||
285 | //add 0 to school id |
||
286 | $student['schoolid'] = str_pad($student['schoolid'], 5, '0', STR_PAD_LEFT); |
||
287 | |||
288 | // if the first match missing do complete insertion |
||
289 | $institution = Institution::where('code', '=', $student['schoolid'])->first(); |
||
290 | if (!is_null($institution)) { |
||
291 | |||
292 | // ge the class lists to belong the school |
||
293 | $institutionClass = Institution_class::where( |
||
294 | [ |
||
295 | 'institution_id' => $institution->id, |
||
296 | 'academic_period_id' => $this->academic_period->id |
||
297 | ] |
||
298 | ) |
||
299 | ->where( 'education_grade_id','in', [$this->student->education_garde_id]) |
||
300 | ->join('institution_class_grades', 'institution_classes.id', 'institution_class_grades.institution_class_id')->get()->toArray(); |
||
301 | |||
302 | |||
303 | if(!is_null($this->student->education_garde_id)){ |
||
304 | $this->education_grade = Education_grade::where('id', '=', $this->student->education_garde_id)->first(); |
||
305 | }else{ |
||
306 | $this->student->education_garde_id = $this->education_grade->id; |
||
307 | } |
||
308 | |||
309 | // set search variables |
||
310 | $admissionInfo = [ |
||
311 | 'instituion_class' => $institutionClass, |
||
312 | 'instituion' => $institution, |
||
313 | 'education_grade' => $this->education_grade, |
||
314 | 'academic_period' => $this->academic_period |
||
315 | ]; |
||
316 | |||
317 | // if no matching found |
||
318 | if (empty($matchedStudent)) { |
||
319 | $sis_student = $this->student->insertExaminationStudent($student); |
||
320 | |||
321 | //TODO implement insert student to admission table |
||
322 | $student['id'] = $sis_student['id']; |
||
323 | $sis_student['student_id'] = $student['id' ]; |
||
324 | |||
325 | $student = $this->setIsTakingExam($student); |
||
326 | if (count($institutionClass) == 1) { |
||
327 | $admissionInfo['instituion_class'] = $institutionClass[0]; |
||
328 | Institution_student::createExaminationData($student, $admissionInfo); |
||
329 | Institution_student_admission::createExaminationData($student, $admissionInfo); |
||
330 | Institution_class_student::createExaminationData($student, $admissionInfo); |
||
331 | } else { |
||
332 | Institution_student_admission::createExaminationData($student, $admissionInfo); |
||
333 | Institution_student::createExaminationData($student, $admissionInfo); |
||
334 | } |
||
335 | |||
336 | $this->updateStudentId($student, $sis_student); |
||
337 | // update the matched student's data |
||
338 | } else { |
||
339 | $student = $this->setIsTakingExam($student); |
||
340 | $studentData = $this->student->updateExaminationStudent($student, $matchedStudent); |
||
341 | $matchedStudent = array_merge((array) $student, $matchedStudent); |
||
342 | $studentData = array_merge((array) $matchedStudent, $studentData); |
||
343 | Institution_student::updateExaminationData($studentData, $admissionInfo); |
||
344 | $this->updateStudentId($student, $studentData); |
||
345 | } |
||
346 | } else { |
||
347 | |||
348 | $this->output->writeln('Student ' . $student['st_no'] . ' not imorted' . $student['f_name']); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * This function is implemented similar_text search algorithm |
||
354 | * to get the most matching name with the existing students |
||
355 | * data set |
||
356 | * |
||
357 | * @param array $student |
||
358 | * @return array |
||
359 | */ |
||
360 | public function getMatchingStudents($student) |
||
361 | { |
||
362 | /** |
||
363 | */ |
||
364 | $sis_student = $this->student->getMatches($student); |
||
365 | $doe_students = Examination_student::where('gender', $student['gender']) |
||
366 | ->where('b_date', $student['b_date']) |
||
367 | ->where('schoolid', $student['schoolid']) |
||
368 | ->where('year',$this->year) |
||
369 | ->where('grade',$this->grade) |
||
370 | ->count(); |
||
371 | $count = $this->student->getStudentCount($student); |
||
372 | |||
373 | $studentData = []; |
||
374 | $sis_users = (array) json_decode(json_encode($sis_student), true); |
||
375 | // if the same gender same DOE has more than one |
||
376 | if (($doe_students > 1) || ($count > 1)) { |
||
377 | $studentData = $this->searchSimilarName($student, $sis_users, false); |
||
378 | } else { |
||
379 | $studentData = $this->searchSimilarName($student, $sis_users); |
||
380 | } |
||
381 | return $studentData; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Search most matching name |
||
386 | * |
||
387 | * @param array $student |
||
388 | * @param array $sis_students |
||
389 | * @return array |
||
390 | */ |
||
391 | public function searchSimilarName($student, $sis_students, $surname_search = true) |
||
392 | { |
||
393 | $highest = []; |
||
394 | $minDistance = 0; |
||
395 | $matches = []; |
||
396 | $data = []; |
||
397 | foreach ($sis_students as $key => $value) { |
||
398 | similar_text(strtoupper($value['first_name']), (strtoupper($student['f_name'])), $percentage); |
||
399 | $distance = levenshtein(strtoupper($student['f_name']), strtoupper($value['first_name'])); |
||
400 | $value['rate'] = $percentage; |
||
401 | switch (true) { |
||
402 | case $value['rate'] == 100; |
||
403 | $highest = $value; |
||
404 | break; |
||
405 | case (($distance <= 2) && ($distance < $minDistance)); |
||
406 | $highest = $value; |
||
407 | $minDistance = $distance; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | if ($surname_search) { |
||
412 | if (empty($highest)) { |
||
413 | foreach ($sis_students as $key => $value) { |
||
414 | //search name with last name |
||
415 | similar_text(strtoupper(get_l_name($student['f_name'])), strtoupper(get_l_name($value['first_name'])), $percentage); |
||
416 | $value['rate'] = $percentage; |
||
417 | switch (true) { |
||
418 | case ($value['rate'] == 100); |
||
419 | $highest = $value; |
||
420 | $matches[] = $value; |
||
421 | break; |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 | } |
||
426 | |||
427 | if (count($matches) > 1) { |
||
428 | $highest = $this->searchSimilarName($student, $sis_students, false); |
||
429 | } |
||
430 | |||
431 | return $highest; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Generate new NSID for students |
||
436 | * |
||
437 | * @param array $student |
||
438 | * @param array $sis_student |
||
439 | * @return void |
||
440 | */ |
||
441 | public function updateStudentId($student, $sis_student) |
||
442 | { |
||
443 | try { |
||
444 | $student['nsid'] = $sis_student['openemis_no']; |
||
445 | // add new NSID to the examinations data set |
||
446 | unset($student['id']); |
||
447 | unset($student['taking_g5_exam']); |
||
448 | unset($student['taking_al_exam']); |
||
449 | unset($student['taking_ol_exam']); |
||
450 | unset($student['taking_git_exam']); |
||
451 | unset($student['total']); |
||
452 | $students['updated_at'] = now(); |
||
453 | $this->examination_student->where('st_no', $student['st_no'])->update($student); |
||
454 | unset($student['st_no']); |
||
455 | $this->output->writeln('Updated to NSID' . $sis_student['openemis_no']); |
||
456 | } catch (\Exception $th) { |
||
457 | $this->output->writeln('error'); |
||
458 | Log::error($th); |
||
459 | } |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * export the all data with NSID |
||
464 | * |
||
465 | * @return void |
||
466 | */ |
||
467 | public function export() |
||
477 | } |
||
478 | |||
479 | public function downloadErrors() |
||
480 | { |
||
481 | |||
482 | $file_path = storage_path() . '/app/examination/errors.csv'; |
||
484 | } |
||
485 | |||
486 | public function downloadProcessedFile() |
||
493 |