| Total Complexity | 41 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 39 | ||
| Bugs | 15 | Features | 6 |
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() |
||
| 39 | { |
||
| 40 | return view('uploadcsv'); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function uploadFile(Request $request) |
||
| 44 | { |
||
| 45 | |||
| 46 | if ($request->input('submit') != null) { |
||
| 47 | |||
| 48 | $file = $request->file('file'); |
||
| 49 | |||
| 50 | // File Details |
||
| 51 | $filename = 'exams_students.csv'; |
||
| 52 | $extension = $file->getClientOriginalExtension(); |
||
| 53 | $fileSize = $file->getSize(); |
||
| 54 | |||
| 55 | // Valid File Extensions |
||
| 56 | $valid_extension = array("csv"); |
||
| 57 | |||
| 58 | // 20MB in Bytes |
||
| 59 | $maxFileSize = 30971520; |
||
| 60 | |||
| 61 | // Check file extension |
||
| 62 | if (in_array(strtolower($extension), $valid_extension)) { |
||
| 63 | |||
| 64 | // Check file size |
||
| 65 | if ($fileSize <= $maxFileSize) { |
||
| 66 | |||
| 67 | // File upload location |
||
| 68 | Storage::disk('local')->putFileAs( |
||
| 69 | 'examination/', |
||
| 70 | $file, |
||
| 71 | $filename |
||
| 72 | ); |
||
| 73 | Session::flash('message', 'File upload successfully!'); |
||
| 74 | // Redirect to index |
||
| 75 | } else { |
||
| 76 | Session::flash('message', 'File too large. File must be less than 20MB.'); |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | Session::flash('message', 'Invalid File Extension.'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | return redirect()->action('ExaminationStudentsController@index'); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Import students data to the Examinations table |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public static function callOnClick() |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Iterate over existing student's data |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function doMatch($offset, $limit, $mode) |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set Examination values |
||
| 185 | * |
||
| 186 | * @param array $students |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function setIsTakingExam($students) |
||
| 190 | { |
||
| 191 | $students['taking_g5_exam'] = false; |
||
| 192 | $students['taking_ol_exam'] = false; |
||
| 193 | $students['taking_al_exam'] = false; |
||
| 194 | switch ($this->education_grade->code) { |
||
| 195 | case 'G5': |
||
| 196 | $students['taking_g5_exam'] = true; |
||
| 197 | break; |
||
| 198 | case 'G4': |
||
| 199 | $students['taking_g5_exam'] = true; |
||
| 200 | break; |
||
| 201 | case 'G10': |
||
| 202 | $students['taking_ol_exam'] = true; |
||
| 203 | break; |
||
| 204 | case 'G11': |
||
| 205 | $students['taking_ol_exam'] = true; |
||
| 206 | break; |
||
| 207 | // case preg_match('13', $this->education_grade->code): |
||
| 208 | // $students['taking_al_exam'] = true; |
||
| 209 | // break; |
||
| 210 | } |
||
| 211 | return $students; |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Main function to merge the student's to SIS |
||
| 217 | * |
||
| 218 | * @param array $student |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | public function clone($student) |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * This function is implemented similar_text search algorithm |
||
| 286 | * to get the most matching name with the existing students |
||
| 287 | * data set |
||
| 288 | * |
||
| 289 | * @param array $student |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getMatchingStudents($student) |
||
| 293 | { |
||
| 294 | /** |
||
| 295 | * ->where('gender_id',$student['gender'] + 1) |
||
| 296 | ->where('institutions.code',$student['schoolid']) |
||
| 297 | ->where('date_of_birth',$student['b_date']) |
||
| 298 | */ |
||
| 299 | $sis_student = $this->student->getMatches($student); |
||
| 300 | $doe_students = Examination_student::where('gender',$student['gender']) |
||
| 301 | ->where('b_date',$student['b_date']) |
||
| 302 | ->where('schoolid',$student['schoolid']) |
||
| 303 | ->count(); |
||
| 304 | $count = $this->student->getStudentCount($student); |
||
| 305 | |||
| 306 | $studentData = []; |
||
| 307 | $sis_users = (array) json_decode(json_encode($sis_student), true); |
||
| 308 | // if the same gender same DOE has more than one |
||
| 309 | if($doe_students > 1){ |
||
| 310 | $studentData = $this->searchSimilarName($student, $sis_users,false); |
||
| 311 | }else{ |
||
| 312 | $studentData = $this->searchSimilarName($student, $sis_users); |
||
| 313 | } |
||
| 314 | return $studentData; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Search most matching name |
||
| 319 | * |
||
| 320 | * @param array $student |
||
| 321 | * @param array $sis_students |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function searchSimilarName($student, $sis_students,$surname_search = true) |
||
| 325 | { |
||
| 326 | $highest = []; |
||
| 327 | $minDistance = 0; |
||
| 328 | $matches = []; |
||
| 329 | $data = []; |
||
| 330 | foreach ($sis_students as $key => $value) { |
||
| 331 | similar_text(strtoupper($value['first_name']), (strtoupper($student['f_name'])), $percentage); |
||
| 332 | $distance = levenshtein(strtoupper($student['f_name']), strtoupper($value['first_name'])); |
||
| 333 | $value['rate'] = $percentage; |
||
| 334 | switch (true) { |
||
| 335 | case $value['rate'] == 100; |
||
| 336 | $highest = $value; |
||
| 337 | break; |
||
| 338 | case (($distance <= 2) && ($distance < $minDistance)); |
||
| 339 | $highest = $value; |
||
| 340 | $minDistance = $distance; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | if($surname_search){ |
||
| 345 | if (empty($highest)) { |
||
| 346 | foreach ($sis_students as $key => $value) { |
||
| 347 | //search name with last name |
||
| 348 | similar_text(strtoupper(get_l_name($student['f_name'])), strtoupper(get_l_name($value['first_name'])), $percentage); |
||
| 349 | $value['rate'] = $percentage; |
||
| 350 | switch (true) { |
||
| 351 | case ($value['rate'] == 100); |
||
| 352 | $highest = $value; |
||
| 353 | $matches[] = $value; |
||
| 354 | break; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | if(count($matches)>1){ |
||
| 361 | $highest = $this->searchSimilarName($student, $sis_students,false); |
||
| 362 | } |
||
| 363 | |||
| 364 | return $highest; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Generate new NSID for students |
||
| 369 | * |
||
| 370 | * @param array $student |
||
| 371 | * @param array $sis_student |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function updateStudentId($student, $sis_student) |
||
| 375 | { |
||
| 376 | try { |
||
| 377 | $student['nsid'] = $sis_student['openemis_no']; |
||
| 378 | // add new NSID to the examinations data set |
||
| 379 | unset($student['id']); |
||
| 380 | unset($student['taking_g5_exam']); |
||
| 381 | unset($student['taking_al_exam']); |
||
| 382 | unset($student['taking_ol_exam']); |
||
| 383 | unset($student['total']); |
||
| 384 | $this->examination_student->where('st_no', $student['st_no'])->update($student); |
||
| 385 | unset($student['st_no']); |
||
| 386 | $this->output->writeln('Updated to NSID' . $sis_student['openemis_no']); |
||
| 387 | } catch (\Exception $th) { |
||
| 388 | dd($th); |
||
| 389 | $this->output->writeln('error'); |
||
| 390 | Log::error($th); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * export the all data with NSID |
||
| 396 | * |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | public function export() |
||
| 410 | } |
||
| 411 | |||
| 412 | public function downloadErrors() |
||
| 413 | { |
||
| 417 | } |
||
| 418 | |||
| 419 | public function downloadProcessedFile() |
||
| 425 |