Institution_student::updateStudentArea()   D
last analyzed

Complexity

Conditions 23
Paths 9

Size

Total Lines 40
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 23
eloc 36
nc 9
nop 1
dl 0
loc 40
rs 4.1666
c 1
b 1
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Models;
4
5
use Webpatser\Uuid\Uuid;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
12
class Institution_student extends Base_Model
13
{
14
15
    use SoftDeletes;
16
17
    public const CREATED_AT = 'created';
18
    public const UPDATED_AT = 'modified';
19
20
    /**
21
     * The database table used by the model.
22
     *
23
     * @var string
24
     */
25
    protected $table = 'institution_students';
26
27
    protected $softDelete = true;
28
29
30
    /**
31
     * @var bool
32
     */
33
    public $timestamps = true;
34
35
    /**
36
     * Attributes that should be mass-assignable.
37
     *
38
     * @var array
39
     */
40
    protected $fillable = ['student_status_id', 'student_id', 'education_grade_id', 'academic_period_id', 'start_date', 'start_year', 'end_date', 'end_year', 'institution_id', 'previous_institution_student_id', 'modified_user_id', 'modified', 'created_user_id', 'created', 'area_administrative_id', 'admission_id'];
41
42
    /**
43
     * The attributes excluded from the model's JSON form.
44
     *
45
     * @var array
46
     */
47
    protected $hidden = [];
48
49
    /**
50
     * The attributes that should be casted to native types.
51
     *
52
     * @var array
53
     */
54
    protected $casts = [];
55
56
    protected $maps = [
57
        'uuid' => 'id'
58
    ];
59
60
    /**
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
62
     */
63
    public function institutionStudents()
64
    {
65
        return $this->belongsTo('App\Security_user', 'student_id');
66
    }
67
68
    /**
69
     *
70
     */
71
    public static function boot()
72
    {
73
        parent::boot();
74
        self::creating(function ($model) {
75
            $model->id = (string) Uuid::generate(4);
76
            $model->created = now();
77
        });
78
    }
79
80
81
    /**
82
     * @var string
83
     */
84
    protected $primaryKey = 'id';
85
    protected $keyType = 'string';
86
87
    /**
88
     * @param $inputs
89
     * @return bool
90
     *
91
     *
92
     */
93
    public static function  isDuplicated($inputs)
94
    {
95
96
        $exists = self::where('student_id', '=', $inputs['student_id'])->count();
97
98
99
        return $exists;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $exists also could return the type Illuminate\Database\Eloquent\Builder|integer which is incompatible with the documented return type boolean.
Loading history...
100
    }
101
102
103
    /**
104
     * The attributes that should be mutated to dates.
105
     *
106
     * @var array
107
     */
108
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created'];
109
110
    /**
111
     * get list of students which are going to be promoted
112
     *
113
     * @param $institutionGrade
114
     * @param $academicPeriod
115
     * @return array
116
     */
117
    public function getStudentListToPromote($institutionGrade, $academicPeriod)
118
    {
119
        return $this
120
            ->select(
121
                'institution_students.id',
122
                'institution_students.student_id',
123
                'institution_students.student_status_id',
124
                'institution_students.education_grade_id',
125
                'institution_students.education_grade_id',
126
                'institution_students.academic_period_id',
127
                'institution_students.institution_id',
128
                'institution_students.created_user_id',
129
                'institution_students.admission_id'
130
            )
131
            ->where('institution_students.institution_id', $institutionGrade['institution_id'])
132
            ->where('institution_students.student_status_id',1)
133
            ->where('institution_students.education_grade_id', $institutionGrade['education_grade_id'])
134
            ->where('institution_students.deleted_at',null)
135
            ->where('institution_students.academic_period_id', $academicPeriod->id)->get()->toArray();
136
    }
137
138
    /**
139
     * Create new Institution student from examination data
140
     *
141
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
142
     * @param [type] $admissionInfo
143
     * @return void
144
     */
145
    public static function createExaminationData($student, $admissionInfo)
146
    {
147
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
148
        try {
149
            self::create([
150
                'id' => (string) Uuid::generate(4),
151
                'student_status_id' => 1,
152
                'student_id' => $student['id'],
153
                'taking_g5_exam' => $student['taking_g5_exam'],
154
                'taking_ol_exam' => $student['taking_ol_exam'],
155
                'taking_al_exam' => $student['taking_al_exam'],
156
                'taking_git_exam' => $student['taking_git_exam'],
157
                // Set special examination center
158
                'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
159
                'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
160
                'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
161
                'exam_center_for_special_education_git' =>   $student['taking_git_exam'] ? $student['sp_center'] : 0,
162
                'income_at_g5' => $student['a_income'],
163
                'education_grade_id' => $admissionInfo['education_grade']->id,
164
                'academic_period_id' => $admissionInfo['academic_period']->id,
165
                'start_date' => $admissionInfo['academic_period']->start_date,
166
                'start_year' => $admissionInfo['academic_period']->start_year,
167
                'end_date' => $admissionInfo['academic_period']->end_date,
168
                'end_year' => $admissionInfo['academic_period']->end_year,
169
                'institution_id' => $admissionInfo['instituion']->id,
170
                'updated_from' => 'doe',
171
                'created' => now(),
172
                'created_user_id' => 1
173
            ]);
174
        } catch (\Throwable $th) {
175
            Log::error($th);
176
        }
177
    }
178
179
    /**
180
     * Update new Institution student from examination data
181
     *
182
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
183
     * @param [type] $admissionInfo
184
     * @return void
185
     */
186
    public static function updateExaminationData($student, $admissionInfo)
187
    {
188
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
189
        try {
190
            self::where([
191
                'student_id' => $student['student_id'],
192
                'education_grade_id' => $admissionInfo['education_grade']->id,
193
                'academic_period_id' => $admissionInfo['academic_period']->id,
194
            ])->update(
195
                [
196
                    'taking_g5_exam' => $student['taking_g5_exam'],
197
                    'taking_ol_exam' => $student['taking_ol_exam'],
198
                    'taking_al_exam' => $student['taking_al_exam'],
199
                    // Set special examination center
200
                    'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
201
                    'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
202
                    'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
203
                    'updated_from' => 'doe',
204
                    'income_at_g5' => $student['a_income'],
205
                    'modified' => now(),
206
                    'modified_user_id' => 1
207
                ]
208
            );
209
        } catch (\Throwable $th) {
210
            Log::error($th);
211
        }
212
    }
213
214
    public static function updateStudentArea(array $student){
215
        $father = Student_guardian::where('student_id',$student['student_id'])
216
        ->join('security_users as sg','guardian_id', 'sg.id')
217
        ->where('guardian_relation_id',1)
218
        ->get()->first();
219
220
        $mother = Student_guardian::where('student_id',$student['student_id'])
221
        ->join('security_users as sg','guardian_id', 'sg.id')
222
        ->where('guardian_relation_id',2)
223
        ->get()->first();
224
225
        $guardian = Student_guardian::where('student_id',$student['student_id'])
226
        ->join('security_users as sg','guardian_id', 'sg.id')
227
        ->where('guardian_relation_id',3)
228
        ->get()->first();
229
    
230
        if(!is_null($father) && is_null($mother) && is_null($guardian)){
231
            Security_user::where('id',$student['student_id'])
232
            ->update(['address_area_id' => $father->address_area_id]);
0 ignored issues
show
Bug introduced by
The property address_area_id does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
233
        }elseif(!is_null($mother)  && (is_null($father) && is_null($guardian))){
234
            Security_user::where('id',$student['student_id'])
235
            ->update(['address_area_id' => $mother->address_area_id]);
236
        }elseif(!is_null($guardian) && is_null($father) && is_null($mother)){
237
            Security_user::where('id',$student['student_id'])
238
            ->update(['address_area_id' => $guardian->address_area_id]);
239
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id ==  $mother->address_area_id)){
240
            Security_user::where('id',$student['student_id'])
241
            ->update(['address_area_id' => $mother->address_area_id]);
242
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id !==  $mother->address_area_id) && !is_null($guardian)){
243
            Security_user::where('id',$student['student_id'])
244
            ->update(['address_area_id' => $guardian->address_area_id]);
245
        }elseif(!is_null($father) && $father->address == $student['address']){
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
246
            Security_user::where('id',$student['student_id'])
247
            ->update(['address_area_id' => $father->address_area_id]);
248
        }elseif(!is_null($mother) && $mother->address == $student['address']){
249
            Security_user::where('id',$student['student_id'])
250
            ->update(['address_area_id' => $mother->address_area_id]);
251
        }elseif(!is_null($guardian) && $guardian->address == $student['address']){
252
            Security_user::where('id',$student['student_id'])
253
            ->update(['address_area_id' => $guardian->address_area_id]);
254
        }
255
    }
256
257
    public static function createOrUpdate($studentId,$row,$params,$file){
258
        self::create([
259
            'student_status_id' => 1,
260
            'student_id' => $studentId,
261
            'education_grade_id' => $params['institution_grade']->education_grade_id,
262
            'academic_period_id' => $params['academic_period']->id,
263
            'start_date' => $row['start_date_yyyy_mm_dd'],
264
            'start_year' => $row['start_date_yyyy_mm_dd']->format('Y'),
265
            'end_date' => $params['academic_period']->end_date,
266
            'end_year' => $params['academic_period']->end_year,
267
            'institution_id' => $params['institution'],
268
            'admission_id' => $row['admission_no'],
269
            'created_user_id' => $file['security_user_id']
270
        ]);
271
    }
272
}
273