Test Setup Failed
Pull Request — master (#304)
by Mohamed
07:04
created

Institution_student::createExaminationData()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 24
c 1
b 1
f 0
nc 4
nop 2
dl 0
loc 28
rs 8.9137
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
10
11
class Institution_student extends Base_Model
12
{
13
14
15
    public const CREATED_AT = 'created';
16
    public const UPDATED_AT = 'modified';
17
18
    /**
19
     * The database table used by the model.
20
     *
21
     * @var string
22
     */
23
    protected $table = 'institution_students';
24
25
26
    /**
27
     * @var bool
28
     */
29
    public $timestamps = true;
30
31
    /**
32
     * Attributes that should be mass-assignable.
33
     *
34
     * @var array
35
     */
36
    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'];
37
38
    /**
39
     * The attributes excluded from the model's JSON form.
40
     *
41
     * @var array
42
     */
43
    protected $hidden = [];
44
45
    /**
46
     * The attributes that should be casted to native types.
47
     *
48
     * @var array
49
     */
50
    protected $casts = [];
51
52
    /**
53
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
54
     */
55
    public function institutionStudents()
56
    {
57
        return $this->belongsTo('App\Security_user', 'student_id');
58
    }
59
60
    /**
61
     *
62
     */
63
    public static function boot()
64
    {
65
        parent::boot();
66
        self::creating(function ($model) {
67
            $model->id = (string) Uuid::generate(4);
68
            $model->created = now();
69
        });
70
    }
71
72
    /**
73
     * @var string
74
     */
75
    protected $primaryKey = 'uuid';
76
77
    /**
78
     * @param $inputs
79
     * @return bool
80
     *
81
     *
82
     */
83
    public static function  isDuplicated($inputs)
84
    {
85
86
        $exists = self::where('student_id', '=', $inputs['student_id'])->count();
87
88
89
        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...
90
    }
91
92
93
    /**
94
     * The attributes that should be mutated to dates.
95
     *
96
     * @var array
97
     */
98
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created'];
99
100
    /**
101
     * get list of students which are going to be promoted
102
     *
103
     * @param $institutionGrade
104
     * @param $academicPeriod
105
     * @return array
106
     */
107
    public function getStudentListToPromote($institutionGrade, $academicPeriod)
108
    {
109
        return self::query()
110
            ->select(
111
                'institution_students.id',
112
                'institution_students.student_id',
113
                'institution_students.student_status_id',
114
                'institution_students.education_grade_id',
115
                'institution_students.education_grade_id',
116
                'institution_students.academic_period_id',
117
                'institution_students.institution_id',
118
                'institution_students.created_user_id',
119
                'institution_students.admission_id'
120
            )
121
            ->where('institution_students.institution_id', $institutionGrade['institution_id'])
122
            ->where('institution_students.education_grade_id', $institutionGrade['education_grade_id'])
123
            ->where('institution_students.academic_period_id', $academicPeriod->id)->get()->toArray();
124
    }
125
126
    /**
127
     * Create new Institution student from examination data
128
     *
129
     * @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...
130
     * @param [type] $admissionInfo
131
     * @return void
132
     */
133
    public static function createExaminationData($student, $admissionInfo)
134
    {
135
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
136
        try {
137
            self::create([
138
                'id' => (string) Uuid::generate(4),
139
                'student_status_id' => 1,
140
                'student_id' => $student['id'],
141
                'taking_g5_exam' => $student['taking_g5_exam'],
142
                'taking_ol_exam' => $student['taking_ol_exam'],
143
                'taking_al_exam' => $student['taking_al_exam'],
144
                // Set special examination center
145
                'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
146
                'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
147
                'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
148
                'income_at_g5' => $student['a_income'],
149
                'education_grade_id' => $admissionInfo['education_grade']->id,
150
                'academic_period_id' => $admissionInfo['academic_period']->id,
151
                'start_date' => $admissionInfo['academic_period']->start_date,
152
                'start_year' => $admissionInfo['academic_period']->start_year,
153
                'end_date' => $admissionInfo['academic_period']->end_date,
154
                'end_year' => $admissionInfo['academic_period']->end_year,
155
                'institution_id' => $admissionInfo['instituion']->id,
156
                'created' => now(),
157
                'created_user_id' => 1
158
            ]);
159
        } catch (\Throwable $th) {
160
            Log::error($th);
161
        }
162
    }
163
164
    /**
165
     * Update new Institution student from examination data
166
     *
167
     * @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...
168
     * @param [type] $admissionInfo
169
     * @return void
170
     */
171
    public static function updateExaminationData($student, $admissionInfo)
172
    {
173
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
174
        try {
175
            self::where([
176
                'student_id' => $student['student_id'],
177
                'education_grade_id' => $admissionInfo['education_grade']->id,
178
                'academic_period_id' => $admissionInfo['academic_period']->id,
179
            ])->update(
180
                [
181
                    'taking_g5_exam' => $student['taking_g5_exam'],
182
                    'taking_ol_exam' => $student['taking_ol_exam'],
183
                    'taking_al_exam' => $student['taking_al_exam'],
184
                    // Set special examination center
185
                    'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
186
                    'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
187
                    'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
188
    
189
                    'income_at_g5' => $student['a_income'],
190
                    'modified' => now(),
191
                    'modified_user_id' => 1
192
                ]
193
            );
194
        } catch (\Throwable $th) {
195
            Log::error($th);
196
        }
197
    }
198
}
199