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_grade; |
||||
18 | use App\Models\Institution_student; |
||||
19 | |||||
20 | class CloneController extends Controller |
||||
21 | { |
||||
22 | /** |
||||
23 | * Create a new command instance. |
||||
24 | * |
||||
25 | * @return void |
||||
26 | */ |
||||
27 | public function __construct() |
||||
28 | |||||
29 | { |
||||
30 | $this->shifts = new Institution_shift(); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
31 | $this->academic_period = new Academic_period(); |
||||
0 ignored issues
–
show
|
|||||
32 | $this->institution_classes = new Institution_class(); |
||||
0 ignored issues
–
show
|
|||||
33 | $this->institution_class_subjects = new Institution_class_subject(); |
||||
0 ignored issues
–
show
|
|||||
34 | $this->institution_subjects = new Institution_subject(); |
||||
0 ignored issues
–
show
|
|||||
35 | $this->institution_grades = new Institution_grade(); |
||||
0 ignored issues
–
show
|
|||||
36 | $this->education_grade_subjects = new Education_grades_subject(); |
||||
0 ignored issues
–
show
|
|||||
37 | $this->output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||||
0 ignored issues
–
show
|
|||||
38 | } |
||||
39 | |||||
40 | |||||
41 | public function array_walk($shift, $count, $params) |
||||
0 ignored issues
–
show
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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
42 | { |
||||
43 | try{ |
||||
44 | DB::beginTransaction(); |
||||
45 | array_walk($shift, array($this, 'process'), $params); |
||||
46 | DB::commit(); |
||||
47 | }catch(\Exception $e){ |
||||
48 | $this->output->writeln('Terminating ' . $shift['institution_id']); |
||||
49 | DB::rollBack(); |
||||
50 | } |
||||
51 | } |
||||
52 | |||||
53 | public function cleanConfig($params) |
||||
54 | { |
||||
55 | $academicPeriod = $params['academic_period']; |
||||
56 | $this->shifts->where(['academic_period_id' => $academicPeriod->id])->delete(); |
||||
57 | $this->output->writeln('cleaned shifts'); |
||||
58 | |||||
59 | $this->shifts->where(['cloned' => $academicPeriod->code])->update(['cloned' => $params['previous_academic_period']['code']]); |
||||
60 | $this->output->writeln('updated shifts'); |
||||
61 | |||||
62 | $classIds = $this->institution_classes->select('id')->where(['academic_period_id' => $academicPeriod->id])->get()->toArray(); |
||||
0 ignored issues
–
show
|
|||||
63 | $this->institution_classes->where(['academic_period_id' => $academicPeriod->id])->delete(); |
||||
64 | $this->output->writeln('cleaned classes'); |
||||
65 | |||||
66 | do { |
||||
67 | $deleted = $this->institution_class_subjects->whereRaw("institution_class_id not in (select id from institution_classes where academic_period_id =".$academicPeriod->id." )")->limit(10000)->delete(); |
||||
68 | $this->output->writeln('cleaned subjects'); |
||||
69 | }while($deleted > 0); |
||||
70 | |||||
71 | do { |
||||
72 | $deleted = $this->institution_subjects->where('academic_period_id', $academicPeriod->id)->limit(10000)->delete(); |
||||
73 | $this->output->writeln('10000 institutions cleaned subjects'); |
||||
74 | } while ($deleted > 0); |
||||
75 | } |
||||
76 | |||||
77 | public function process($shift, $count, $params) |
||||
78 | { |
||||
79 | echo ('[' . getmypid() . ']This Process executed at' . date("F d, Y h:i:s A") . "\n"); |
||||
80 | $year = $params['year']; |
||||
81 | $academicPeriod = $params['academic_period']; |
||||
82 | $previousAcademicPeriod = $params['previous_academic_period']; |
||||
83 | $mode = $params['mode'] == 'AL' ? true : false; |
||||
84 | |||||
85 | $data = $this->updateShifts($year, $shift); |
||||
86 | $shiftId = $data['shift_id']; |
||||
87 | |||||
88 | $params = [ |
||||
89 | 'previous_academic_period_id' => $previousAcademicPeriod->id, |
||||
90 | 'academic_period_id' => $academicPeriod->id, |
||||
91 | 'shift_id' => $data['shift_id'], |
||||
92 | 'mode' => $mode |
||||
93 | ]; |
||||
94 | |||||
95 | |||||
96 | if ($mode) { |
||||
97 | $institutionClasses = $this->institution_classes->getShiftClasses($shift, $mode, $params); |
||||
98 | try { |
||||
99 | array_walk($institutionClasses, array($this, 'updateInstitutionClasses'), $params); |
||||
100 | $this->output->writeln('updating from ' . $shift['institution_id']); |
||||
101 | } catch (\Exception $e) { |
||||
102 | Log::error($e->getMessage(), [$e]); |
||||
103 | } |
||||
104 | } else { |
||||
105 | $institutionSubjects = $this->institution_grades->getGradeSubjects($shift['institution_id']); |
||||
106 | try { |
||||
107 | if ($data['created']) { |
||||
108 | $institutionClasses = $this->institution_classes->getShiftClasses($shift, $mode); |
||||
109 | array_walk($institutionSubjects, array($this, 'insertInstitutionSubjects'), $academicPeriod); |
||||
110 | if (!empty($institutionClasses) && !is_null($shiftId) && !is_null($academicPeriod)) { |
||||
111 | $newInstitutionClasses = $this->generateNewClass($institutionClasses, $shiftId, $academicPeriod->id); |
||||
112 | try { |
||||
113 | array_walk($newInstitutionClasses, array($this, 'insertInstitutionClasses'), $params); |
||||
114 | $this->output->writeln('##########################################################################################################################'); |
||||
115 | $this->output->writeln('updating from = ' . $shift['institution_id']); |
||||
116 | } catch (\Exception $e) { |
||||
117 | $this->output->writeln('Terminating ' . $shift['institution_id']); |
||||
118 | DB::rollBack(); |
||||
119 | Log::error($e->getMessage(), [$e]); |
||||
120 | } |
||||
121 | } else { |
||||
122 | $this->output->writeln('no classes found ' . $shift['institution_id']); |
||||
123 | } |
||||
124 | } else { |
||||
125 | try { |
||||
126 | $shift['id'] = $shiftId; |
||||
127 | $institutionClasses = $this->institution_classes->getShiftClasses($shift, $mode); |
||||
128 | array_walk($institutionClasses, array($this, 'updateInstitutionClasses'), $params); |
||||
129 | $this->output->writeln('##########################################################################################################################'); |
||||
130 | $this->output->writeln('updating from ' . $shift['institution_id']); |
||||
131 | } catch (\Exception $e) { |
||||
132 | $this->output->writeln('Terminating ' . $shift['institution_id']); |
||||
133 | DB::rollBack(); |
||||
134 | Log::error($e->getMessage(), [$e]); |
||||
135 | } |
||||
136 | } |
||||
137 | DB::commit(); |
||||
138 | } catch (\Exception $e) { |
||||
139 | Log::error($e->getMessage(), [$e]); |
||||
140 | $this->output->writeln('Terminating ' . $shift['institution_id']); |
||||
141 | DB::rollBack(); |
||||
142 | } |
||||
143 | } |
||||
144 | } |
||||
145 | |||||
146 | |||||
147 | /** |
||||
148 | * @param $subjects |
||||
149 | * @param $count |
||||
150 | * @param $academicPeriod |
||||
151 | */ |
||||
152 | public function insertInstitutionSubjects($subject, $count, $academicPeriod) |
||||
153 | { |
||||
154 | try { |
||||
155 | $subject['academic_period_id'] = $academicPeriod->id; |
||||
156 | $subject['created'] = now(); |
||||
157 | $subject['created_user_id'] = 1; |
||||
158 | $this->institution_subjects->create($subject); |
||||
159 | } catch (\Exception $e) { |
||||
160 | DB::rollBack(); |
||||
161 | Log::error($e->getMessage(), [$e]); |
||||
162 | } |
||||
163 | } |
||||
164 | |||||
165 | public function updateInstitutionClasses($class, $count, $params) |
||||
166 | { |
||||
167 | try { |
||||
168 | if ($params['mode']) { |
||||
169 | Institution_class::where('id', $class['id']) |
||||
170 | ->where('academic_period_id',$params['previous_academic_period_id']) |
||||
171 | ->update([ |
||||
172 | 'institution_shift_id' => $params['shift_id'], |
||||
173 | 'academic_period_id' => $params['academic_period_id'] |
||||
174 | ]); |
||||
175 | |||||
176 | Institution_class_student::where('institution_class_id', $class['id']) |
||||
177 | ->where('academic_period_id',$params['previous_academic_period_id']) |
||||
178 | ->update([ |
||||
179 | 'academic_period_id' => $params['academic_period_id'], |
||||
180 | 'modified' => now() |
||||
181 | ]); |
||||
182 | |||||
183 | $educationGrade = Institution_class_grade::select('education_grade_id')->where('institution_class_id', $class['id'])->get()->toArray(); |
||||
184 | |||||
185 | Institution_student::whereIn('education_grade_id', $educationGrade) |
||||
186 | ->where('academic_period_id',$params['previous_academic_period_id']) |
||||
187 | ->update([ |
||||
188 | 'academic_period_id' => $params['academic_period_id'] |
||||
189 | ]); |
||||
190 | } |
||||
191 | } catch (\Exception $e) { |
||||
192 | DB::rollBack(); |
||||
193 | Log::error($e->getMessage(), [$e]); |
||||
194 | } |
||||
195 | } |
||||
196 | |||||
197 | public function insertInstitutionClasses($class, $count, $param) |
||||
198 | { |
||||
199 | try { |
||||
200 | $academicPeriod = $param['academic_period_id']; |
||||
201 | $educationGrdae = $class['education_grade_id']; |
||||
202 | |||||
203 | $classId = $class['id']; |
||||
204 | unset($class['id']); |
||||
205 | $institutionSubjects = Institution_subject::query()->where('education_grade_id', $class['education_grade_id']) |
||||
206 | ->where('institution_id', $class['institution_id']) |
||||
207 | ->where('academic_period_id', $academicPeriod)->get()->toArray(); |
||||
208 | $params = [ |
||||
209 | 'class' => $class, |
||||
210 | 'subjects' => $institutionSubjects, |
||||
211 | 'academic_period_id' => $academicPeriod, |
||||
212 | 'classId' => $classId |
||||
213 | ]; |
||||
214 | unset($class['education_grade_id']); |
||||
215 | $noOfStudents = $class['no_of_students'] == 0 ? 40 : $class['no_of_students']; |
||||
216 | $class['academic_period_id'] = $academicPeriod; |
||||
217 | $class['no_of_students'] = $noOfStudents; |
||||
218 | $class['created'] = now(); |
||||
219 | $class['institution_shift_id'] = $param['shift_id']; |
||||
220 | // $class['created_user_id'] = |
||||
221 | $this->output->writeln('Create class:' . $class['name']); |
||||
222 | $class = Institution_class::create($class); |
||||
223 | $institutionClassGrdaeObj['institution_class_id'] = $class->id; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
224 | $institutionClassGrdaeObj['education_grade_id'] = $educationGrdae; |
||||
225 | $institutionClassGrdaeObj['created_user_id'] = $class['created_user_id']; |
||||
226 | Institution_class_grade::create($institutionClassGrdaeObj); |
||||
227 | $institutionSubjects = Institution_subject::query()->where('education_grade_id', $educationGrdae) |
||||
228 | ->where('institution_id', $class->institution_id) |
||||
0 ignored issues
–
show
|
|||||
229 | ->where('academic_period_id', $academicPeriod) |
||||
230 | ->get() |
||||
231 | ->toArray(); |
||||
232 | $params['class'] = $class; |
||||
233 | $this->insertInstitutionClassSubjects($institutionSubjects, $class); |
||||
234 | } catch (\Exception $e) { |
||||
235 | DB::rollBack(); |
||||
236 | Log::error($e->getMessage(), [$e]); |
||||
237 | } |
||||
238 | } |
||||
239 | |||||
240 | public function insertInstitutionClassSubjects($subjects, $class) |
||||
241 | { |
||||
242 | if (!empty($subjects)) { |
||||
243 | try { |
||||
244 | array_walk($subjects, array($this, 'insertClassSubjects'), $class); |
||||
245 | $this->output->writeln('updating subjects ' . $class->name); |
||||
246 | } catch (\Exception $e) { |
||||
247 | DB::rollBack(); |
||||
248 | Log::error($e->getMessage(), [$e]); |
||||
249 | } |
||||
250 | }; |
||||
251 | } |
||||
252 | |||||
253 | |||||
254 | public function insertClassSubjects($subject, $count, $newClassId) |
||||
255 | { |
||||
256 | try { |
||||
257 | $subjectobj['status'] = 1; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
258 | $subjectobj['created_user_id'] = 1; |
||||
259 | $subjectobj['created'] = now(); |
||||
260 | |||||
261 | $subjectobj['institution_class_id'] = $newClassId->id; |
||||
262 | $subjectobj['institution_subject_id'] = $subject['id']; |
||||
263 | |||||
264 | if (!$this->institution_class_subjects->isDuplicated($subjectobj)) { |
||||
265 | $this->institution_class_subjects->create($subjectobj); |
||||
266 | } |
||||
267 | } catch (\Exception $e) { |
||||
268 | DB::rollBack(); |
||||
269 | Log::error($e->getMessage(), [$e]); |
||||
270 | } |
||||
271 | } |
||||
272 | |||||
273 | /** |
||||
274 | * generate new class object for new academic year |
||||
275 | * |
||||
276 | * @param $classes |
||||
277 | * @param $shiftId |
||||
278 | * @param $academicPeriod |
||||
279 | * @return array |
||||
280 | */ |
||||
281 | public function generateNewClass($classes, $shiftId, $academicPeriod) |
||||
282 | { |
||||
283 | $newClasses = []; |
||||
284 | foreach ($classes as $class) { |
||||
285 | $noOfStudents = $class['no_of_students'] == 0 ? 40 : $class['no_of_students']; |
||||
286 | $class['academic_period_id'] = $academicPeriod; |
||||
287 | $class['no_of_students'] = $noOfStudents; |
||||
288 | $class['created'] = now(); |
||||
289 | $class['institution_shift_id'] = $shiftId; |
||||
290 | array_push($newClasses, $class); |
||||
291 | } |
||||
292 | return $newClasses; |
||||
293 | } |
||||
294 | |||||
295 | /** |
||||
296 | * update shifts |
||||
297 | * @param $year |
||||
298 | * @param $shift |
||||
299 | * @return mixed |
||||
300 | */ |
||||
301 | public function updateShifts($year, $shift) |
||||
302 | { |
||||
303 | try { |
||||
304 | $academicPeriod = $this->academic_period->getAcademicPeriod($year); |
||||
305 | $this->shifts->where(['id' => $shift['id']])->update(['cloned' => $year]); |
||||
306 | $shift['academic_period_id'] = $academicPeriod->id; |
||||
307 | $exist = $this->shifts->getShift($shift); |
||||
308 | $data = array(); |
||||
309 | |||||
310 | if (is_null($exist)) { |
||||
311 | $shift['cloned'] = $academicPeriod->code; |
||||
0 ignored issues
–
show
|
|||||
312 | unset($shift['id']); |
||||
313 | unset($shift['created']); |
||||
314 | unset($shift['modified']); |
||||
315 | $shift = $this->shifts->create((array)$shift); |
||||
316 | $data = [ |
||||
317 | 'shift_id' => $shift->id, |
||||
318 | 'created' => true |
||||
319 | ]; |
||||
320 | } else { |
||||
321 | $data = [ |
||||
322 | 'shift_id' => $exist->id, |
||||
323 | 'created' => false |
||||
324 | ]; |
||||
325 | }; |
||||
326 | return $data; |
||||
327 | } catch (\Exception $e) { |
||||
328 | DB::rollBack(); |
||||
329 | } |
||||
330 | } |
||||
331 | } |
||||
332 |