1 | <?php |
||
2 | |||
3 | namespace App\Console\Commands; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use App\Models\Institution_class_student; |
||
7 | use App\Models\Institution_class_subject; |
||
8 | use App\Models\Institution_student_admission; |
||
9 | use App\Models\Institution_student; |
||
10 | use App\Models\Institution; |
||
11 | use Illuminate\Support\Facades\DB; |
||
12 | use Illuminate\Support\Facades\Log; |
||
13 | |||
14 | class RunAddStudentsToInstitutions extends Command |
||
15 | { |
||
16 | /** |
||
17 | * The name and signature of the console command. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $signature = 'admission:students {institution}'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'Add approved students data to indtitution_student table'; |
||
29 | |||
30 | /** |
||
31 | * Create a new command instance. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function __construct() |
||
36 | { |
||
37 | $this->count = 0; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
38 | parent::__construct(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Execute the console command. |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function handle() |
||
47 | { |
||
48 | |||
49 | $institution = Institution::where([ |
||
50 | 'id' => $this->argument('institution') |
||
51 | ])->first(); |
||
52 | |||
53 | if(!is_null($institution)){ |
||
54 | DB::enableQueryLog(); |
||
55 | try { |
||
56 | $this->info('adding missing students to the institution ' . $institution->name); |
||
0 ignored issues
–
show
|
|||
57 | $approvedstudent = Institution_student_admission::select('institution_student_admission.*') |
||
58 | ->leftJoin('institution_students', 'institution_student_admission.student_id', '=', 'institution_students.student_id') |
||
59 | ->whereIn('status_id',[121,122,123,124]) |
||
60 | ->where('institution_student_admission.institution_id',$institution->id)->get()->toArray(); |
||
61 | |||
62 | $approvedstudent = array_chunk($approvedstudent, 50); |
||
63 | |||
64 | array_walk($approvedstudent, array($this, 'addStudents')); |
||
65 | |||
66 | }catch (\Exception $e) { |
||
67 | Log::error($e); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | protected function addStudents($approvedstudent){ |
||
73 | array_walk($approvedstudent,array($this,'addStudent')); |
||
74 | } |
||
75 | |||
76 | protected function addStudent($approvedstudent){ |
||
77 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
78 | Log::info($approvedstudent); |
||
79 | // sleep(1); |
||
80 | if((Institution_student::isDuplicated($approvedstudent) == 0)){ |
||
81 | dd($approvedstudent); |
||
82 | $this->count += 1; |
||
83 | $this->student = $approvedstudent ; |
||
84 | try{ |
||
85 | $output->writeln($approvedstudent['student_id']."Updated"); |
||
86 | Institution_student::create([ |
||
87 | 'student_status_id' => 1, |
||
88 | 'student_id' => $approvedstudent['student_id'], |
||
89 | 'education_grade_id' => $approvedstudent['education_grade_id'], |
||
90 | 'academic_period_id' => $approvedstudent['academic_period_id'], |
||
91 | 'start_date' => $approvedstudent['start_date'], |
||
92 | 'start_year' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['start_date'])->year , // $approvedstudent['start_date']->format('Y'), |
||
93 | 'end_date' => $approvedstudent['end_date'], |
||
94 | 'end_year' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['end_date'])->year , //$approvedstudent['end_date']->format('Y'), |
||
95 | 'institution_id' => $approvedstudent['institution_id'], |
||
96 | 'admission_id' => $approvedstudent['admission_id'], |
||
97 | 'created_user_id' => $approvedstudent['created_user_id'], |
||
98 | ]); |
||
99 | if(!is_null($approvedstudent['institution_class_id'])){ |
||
100 | Institution_class_student::create([ |
||
101 | 'student_id' => $approvedstudent['student_id'], |
||
102 | 'institution_class_id' => $approvedstudent['institution_class_id'], |
||
103 | 'education_grade_id' => $approvedstudent['education_grade_id'], |
||
104 | 'academic_period_id' => $approvedstudent['academic_period_id'], |
||
105 | 'institution_id' =>$approvedstudent['institution_id'], |
||
106 | 'student_status_id' => 1, |
||
107 | 'created_user_id' => $approvedstudent['created_user_id'], |
||
108 | ]); |
||
109 | } |
||
110 | }catch (\Exception $e){ |
||
111 | echo $e->getMessage(); |
||
112 | } |
||
113 | } |
||
114 | $output->writeln(' |
||
115 | #################################################### |
||
116 | Total number of students updated : '.$this->count.' |
||
117 | # # |
||
118 | ####################################################' ); |
||
119 | } |
||
120 | } |
||
121 | |||
122 |