moe-lk /
bulk-upload
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use Webpatser\Uuid\Uuid; |
||
| 6 | use App\Models\Institution; |
||
| 7 | use App\Models\Academic_period; |
||
| 8 | use App\Models\Education_grade; |
||
| 9 | use App\Models\Institution_class; |
||
| 10 | use Illuminate\Support\Facades\DB; |
||
| 11 | use App\Models\Institution_student; |
||
| 12 | use App\Models\Institution_subject; |
||
| 13 | use Illuminate\Support\Facades\Log; |
||
| 14 | use App\Models\Institution_class_student; |
||
| 15 | use App\Models\Institution_class_subject; |
||
| 16 | use App\Models\Institution_subject_student; |
||
| 17 | use App\Models\Institution_student_admission; |
||
| 18 | |||
| 19 | class BulkPromotion extends Controller |
||
| 20 | { |
||
| 21 | public function __construct() |
||
| 22 | { |
||
| 23 | $this->instituion_grade = new \App\Models\Institution_grade(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 24 | $this->education_grades = new Education_grade(); |
||
|
0 ignored issues
–
show
|
|||
| 25 | $this->academic_period = new Academic_period(); |
||
|
0 ignored issues
–
show
|
|||
| 26 | $this->institution_students = new Institution_student(); |
||
|
0 ignored issues
–
show
|
|||
| 27 | $this->institutions = new Institution(); |
||
|
0 ignored issues
–
show
|
|||
| 28 | $this->institution_class_students = new Institution_class_student(); |
||
|
0 ignored issues
–
show
|
|||
| 29 | $this->institution_classes = new Institution_class(); |
||
|
0 ignored issues
–
show
|
|||
| 30 | $this->institution_student_admission = new Institution_student_admission(); |
||
|
0 ignored issues
–
show
|
|||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Process Grade wise |
||
| 35 | * |
||
| 36 | * @param [type] $institutionGrade |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @param [type] $year |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | public function callback($institutionGrade, $params) |
||
| 41 | { |
||
| 42 | array_walk($institutionGrade, array($this, 'processGrades'), $params); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Process Cloning process based on institution grade |
||
| 47 | * |
||
| 48 | * @param [type] $institutionGrade |
||
|
0 ignored issues
–
show
|
|||
| 49 | * @param [type] $count |
||
| 50 | * @param [type] $year |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public function processGrades($institutionGrade, $count, $params) |
||
| 54 | { |
||
| 55 | try { |
||
| 56 | DB::beginTransaction(); |
||
| 57 | if (!empty($institutionGrade) && $this->institutions->isActive($institutionGrade['institution_id'])) { |
||
| 58 | $this->instituion_grade->updatePromoted($params['academicPeriod']->code, $institutionGrade['id']); |
||
| 59 | $isAvailableforPromotion = false; |
||
| 60 | $nextGrade = $this->education_grades->getNextGrade($institutionGrade['education_grade_id']); |
||
| 61 | if (!empty($nextGrade)) { |
||
| 62 | $isAvailableforPromotion = $this->instituion_grade->getInstitutionGrade($institutionGrade['institution_id'], $nextGrade->id); |
||
| 63 | } |
||
| 64 | if (!empty($isAvailableforPromotion)) { |
||
| 65 | $this->process($institutionGrade, $nextGrade, $params); |
||
| 66 | DB::commit(); |
||
| 67 | } else { |
||
| 68 | DB::rollBack(); |
||
| 69 | } |
||
| 70 | //leave school levers |
||
| 71 | // else { |
||
| 72 | // $this->process($institutionGrade, $nextGrade, $params); |
||
| 73 | // } |
||
| 74 | } |
||
| 75 | } catch (\Exception $e) { |
||
| 76 | DB::rollBack(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Promote students of grate to next grade |
||
| 83 | * |
||
| 84 | * @param $institutionGrade |
||
| 85 | * @param $nextGrade |
||
| 86 | * @param $academicPeriod |
||
| 87 | * @param $nextAcademicPeriod |
||
| 88 | * @param array $parallelClasses |
||
| 89 | * @param $status |
||
| 90 | */ |
||
| 91 | public function promotion($institutionGrade, $nextGrade, $academicPeriod, $nextAcademicPeriod, $parallelClasses = [], $status) |
||
| 92 | { |
||
| 93 | $institution = Institution::where('id', $institutionGrade['institution_id'])->get()->first(); |
||
| 94 | $studentListToPromote = $this->institution_students->getStudentListToPromote($institutionGrade, $academicPeriod); |
||
| 95 | |||
| 96 | $params = [ |
||
| 97 | $nextAcademicPeriod, |
||
| 98 | $institutionGrade, |
||
| 99 | $nextGrade, |
||
| 100 | $status |
||
| 101 | ]; |
||
| 102 | |||
| 103 | try { |
||
| 104 | array_walk($studentListToPromote, array($this, 'promote'), $params); |
||
| 105 | |||
| 106 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 107 | $output->writeln('##########################################################################################################################'); |
||
| 108 | $output->writeln('Promoting from ' . $institutionGrade['name'] . ' IN ' . $institution->name . ' No of Students: ' . count($studentListToPromote)); |
||
|
0 ignored issues
–
show
|
|||
| 109 | |||
| 110 | |||
| 111 | if (!empty($parallelClasses)) { |
||
| 112 | $params = [ |
||
| 113 | $nextAcademicPeriod, |
||
| 114 | $institutionGrade, |
||
| 115 | $nextGrade, |
||
| 116 | $parallelClasses, |
||
| 117 | $status |
||
| 118 | ]; |
||
| 119 | array_walk($studentListToPromote, array($this, 'assingeToClasses'), $params); |
||
| 120 | array_walk($parallelClasses, array($this, 'updateStudentCount')); |
||
| 121 | DB::commit(); |
||
| 122 | } |
||
| 123 | } catch (\Exception $e) { |
||
| 124 | DB::rollBack(); |
||
| 125 | Log::error($e->getMessage()); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * update students count on class rooms |
||
| 132 | * |
||
| 133 | * @param [type] $class |
||
|
0 ignored issues
–
show
|
|||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | public function updateStudentCount($class) |
||
| 137 | { |
||
| 138 | $studentCounts = Institution_class_student::getStudentsCount($class['id']); |
||
| 139 | unset($studentCounts['total']); |
||
| 140 | Institution_class::query()->where('id', $class['id'])->update($studentCounts); |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Process institution grade in to the define promotion senarios |
||
| 146 | * |
||
| 147 | * @param $institutionGrade |
||
| 148 | * @param $nextGrade |
||
| 149 | * @param $year |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function process($institutionGrade, $nextGrade, $params) |
||
| 153 | { |
||
| 154 | $academicPeriod = $params['academicPeriod']; |
||
| 155 | $previousAcademicPeriod = $params['previousAcademicPeriod']; |
||
| 156 | $nextGradeObj = null; |
||
| 157 | $currentGradeObj = $this->instituion_grade->getParallelClasses($institutionGrade['id'], $institutionGrade['institution_id'], $institutionGrade['education_grade_id'], $previousAcademicPeriod->id); |
||
| 158 | if ($nextGrade !== [] && !is_null($nextGrade)) { |
||
| 159 | $nextGradeObj = $this->instituion_grade->getParallelClasses($institutionGrade['id'], $institutionGrade['institution_id'], $nextGrade->id, $academicPeriod->id); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!is_null($nextGradeObj)) { |
||
| 163 | if ($nextGradeObj->count() == 1) { |
||
| 164 | // promote parallel classes |
||
| 165 | $this->promotion($institutionGrade, $nextGrade, $previousAcademicPeriod, $academicPeriod, $nextGradeObj->toArray(), 1); |
||
| 166 | return 1; |
||
| 167 | } elseif (($nextGradeObj->count() > 1) && ($nextGradeObj->count() !== $currentGradeObj->count())) { |
||
| 168 | // promote pool promotion |
||
| 169 | $this->promotion($institutionGrade, $nextGrade, $previousAcademicPeriod, $academicPeriod, [], 1); |
||
| 170 | return 2; |
||
| 171 | } elseif (($nextGradeObj->count() > 1) && $currentGradeObj->count() == $nextGradeObj->count()) { |
||
| 172 | // Promote matching class name with previous class |
||
| 173 | $this->promotion($institutionGrade, $nextGrade, $previousAcademicPeriod, $academicPeriod, $nextGradeObj->toArray(), 1); |
||
| 174 | return 1; |
||
| 175 | } else { |
||
| 176 | // default pool promotion |
||
| 177 | $this->promotion($institutionGrade, $nextGrade, $previousAcademicPeriod, $academicPeriod, [], 1); |
||
| 178 | return 2; |
||
| 179 | } |
||
| 180 | } else { |
||
| 181 | // default pool promotion |
||
| 182 | $this->promotion($institutionGrade, $nextGrade, $previousAcademicPeriod, $academicPeriod, [], 3); |
||
| 183 | return 2; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * update promoted student's data in to the DB |
||
| 190 | * |
||
| 191 | * @param $student |
||
| 192 | * @param $count |
||
| 193 | * @param $params |
||
| 194 | */ |
||
| 195 | public function promote($student, $count, $params) |
||
| 196 | { |
||
| 197 | |||
| 198 | $academicPeriod = $params[0]; |
||
| 199 | $nextGrade = $params[2]; |
||
| 200 | $status = $params[3]; |
||
| 201 | $studentData = [ |
||
| 202 | 'student_status_id' => $status, |
||
| 203 | 'education_grade_id' => $nextGrade !== null ? $nextGrade->id : $student['education_grade_id'], |
||
| 204 | 'academic_period_id' => $academicPeriod->id, |
||
| 205 | 'start_date' => $academicPeriod->start_date, |
||
| 206 | 'start_year' => $academicPeriod->start_year, |
||
| 207 | 'end_date' => $academicPeriod->end_date, |
||
| 208 | 'end_year' => $academicPeriod->end_year, |
||
| 209 | 'institution_id' => $student['institution_id'], |
||
| 210 | 'admission_id' => $student['admission_id'], |
||
| 211 | // 'student_id' => $student['id'], |
||
| 212 | 'created_user_id' => $student['created_user_id'] |
||
| 213 | ]; |
||
| 214 | |||
| 215 | try { |
||
| 216 | Institution_student::where('id', (string)$student['id'])->update($studentData); |
||
| 217 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 218 | $output->writeln('----------------- ' . $student['admission_id'] . ' to ' . $studentData['education_grade_id']); |
||
| 219 | } catch (\Exception $e) { |
||
| 220 | Log::error($e->getMessage()); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * get promoted new class of the students |
||
| 227 | * |
||
| 228 | * @param $student |
||
| 229 | * @param $educationGrade |
||
| 230 | * @param $nextGrade |
||
| 231 | * @param $classes |
||
| 232 | * @return false|int|string|null |
||
| 233 | */ |
||
| 234 | public function getStudentClass($student, $educationGrade, $nextGrade, $classes) |
||
| 235 | { |
||
| 236 | $studentClass = $this->institution_class_students->getStudentNewClass($student); |
||
| 237 | if (!is_null($studentClass)) { |
||
| 238 | $class = array_search(str_replace($educationGrade['name'], $nextGrade->name, $studentClass->name), array_column($classes, 'name')); |
||
|
0 ignored issues
–
show
|
|||
| 239 | if(!($class)){ |
||
| 240 | $nextGradeName = explode(" ",$nextGrade->name)[0]; |
||
| 241 | $educationGrade['name'] = explode(" ",$educationGrade['name'])[0]; |
||
| 242 | $class = array_search(str_replace($educationGrade['name'], $nextGradeName, $studentClass->name), array_column($classes, 'name')); |
||
| 243 | } |
||
| 244 | return $class; |
||
| 245 | } else { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Create class entry for promoted students |
||
| 252 | * |
||
| 253 | * @param $student |
||
| 254 | * @param $count |
||
| 255 | * @param $params |
||
| 256 | */ |
||
| 257 | public function assingeToClasses($student, $count, $params) |
||
| 258 | { |
||
| 259 | $academicPeriod = $params[0]; |
||
| 260 | $educationGrade = $params[1]; |
||
| 261 | $nextGrade = $params[2]; |
||
| 262 | $classes = $params[3]; |
||
| 263 | $status = $params[4]; |
||
| 264 | $class = null; |
||
| 265 | if (count($classes) == 1) { |
||
| 266 | $class = $classes[0]; |
||
| 267 | } else { |
||
| 268 | $class = $this->getStudentClass($student, $educationGrade, $nextGrade, $classes); |
||
| 269 | if (is_numeric($class)) { |
||
| 270 | $class = $classes[$class]; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | if (!is_null($class)) { |
||
| 275 | |||
| 276 | $studentObj = [ |
||
| 277 | 'student_id' => $student['student_id'], |
||
| 278 | 'institution_class_id' => $class['id'], |
||
| 279 | 'education_grade_id' => $nextGrade->id, |
||
| 280 | 'academic_period_id' => $academicPeriod->id, |
||
| 281 | 'institution_id' => $student['institution_id'], |
||
| 282 | 'student_status_id' => $status, |
||
| 283 | 'created_user_id' => $student['created_user_id'] |
||
| 284 | ]; |
||
| 285 | // $allInsSubjects = Institution_class_subject::getAllSubjects($class); |
||
| 286 | |||
| 287 | try { |
||
| 288 | if (!$this->institution_class_students->isDuplicated($studentObj) && !is_null($class['id'])) { |
||
| 289 | $this->institution_class_students->create($studentObj); |
||
| 290 | // if (!empty($allInsSubjects)) { |
||
| 291 | // $allSubjects = unique_multidim_array($allInsSubjects, 'institution_subject_id'); |
||
| 292 | // $this->student = $studentObj; |
||
| 293 | // $allSubjects = array_map(array($this, 'setStudentSubjects'), $allSubjects); |
||
| 294 | // $allSubjects = unique_multidim_array($allSubjects, 'education_subject_id'); |
||
| 295 | // array_walk($allSubjects, array($this, 'insertSubject')); |
||
| 296 | // array_walk($allInsSubjects,array($this,'updateSubjectCount')); |
||
| 297 | // } |
||
| 298 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 299 | $output->writeln('----------------- ' . $student['student_id'] . 'to ' . $class['name']); |
||
| 300 | } else { |
||
| 301 | $this->institution_class_students->where('id', (string)$student['id'])->update($studentObj); |
||
| 302 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 303 | $output->writeln('----------------- ' . $student['student_id'] . 'to ' . $class['name']); |
||
| 304 | } |
||
| 305 | } catch (\Exception $e) { |
||
| 306 | |||
| 307 | Log::error($e->getMessage()); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Update subject count |
||
| 314 | * |
||
| 315 | * @param [type] $subject |
||
|
0 ignored issues
–
show
|
|||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | protected function updateSubjectCount($subject) |
||
| 319 | { |
||
| 320 | $totalStudents = Institution_subject_student::getStudentsCount($subject['institution_subject_id']); |
||
| 321 | Institution_subject::where(['institution_subject_id' => $subject['institution_subject_id']]) |
||
| 322 | ->update([ |
||
| 323 | 'total_male_students' => $totalStudents['total_male_students'], |
||
| 324 | 'total_female_students' => $totalStudents['total_female_students'] |
||
| 325 | ]); |
||
| 326 | } |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Set student subjects |
||
| 331 | * |
||
| 332 | * @param [type] $subject |
||
|
0 ignored issues
–
show
|
|||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | protected function setStudentSubjects($subject) |
||
| 336 | { |
||
| 337 | return [ |
||
| 338 | 'id' => (string) Uuid::generate(4), |
||
| 339 | 'student_id' => $this->student['student_id'], |
||
|
0 ignored issues
–
show
|
|||
| 340 | 'institution_class_id' => $this->student['institution_class_id'], |
||
| 341 | 'institution_subject_id' => $subject['institution_subject_id'], |
||
| 342 | 'institution_id' => $this->student['institution_id'], |
||
| 343 | 'academic_period_id' => $this->student['academic_period_id'], |
||
| 344 | 'education_subject_id' => $subject['institution_subject']['education_subject_id'], |
||
| 345 | 'education_grade_id' => $this->student['education_grade_id'], |
||
| 346 | 'student_status_id' => 1, |
||
| 347 | 'created_user_id' => $this->student['created_user_id'], |
||
| 348 | 'created' => now() |
||
| 349 | ]; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Insert subjects |
||
| 354 | * |
||
| 355 | * @param [type] $subject |
||
|
0 ignored issues
–
show
|
|||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | protected function insertSubject($subject) |
||
| 359 | { |
||
| 360 | if (!Institution_subject_student::isDuplicated($subject)) { |
||
| 361 | Institution_subject_student::updateOrInsert($subject); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 |