Test Setup Failed
Push — master ( 93675a...ba3342 )
by Mohamed
14:07 queued 12s
created

Institution_student::updateExaminationData()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 2
nop 2
dl 0
loc 24
rs 9.3888
c 0
b 0
f 0
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
        try {
136
            self::create([
137
                'student_status_id' => 1,
138
                'student_id' => $student['id'],
139
                'taking_g5_exam' => $student['taking_g5_exam'],
140
                'taking_ol_exam' =>$student['taking_ol_exam'],
141
                'taking_al_exam' => $student['taking_al_exam'],
142
                // Set special examination center
143
                'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : false,
144
                'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : false,
145
                'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : false,
146
                'income_at_g5' => $student['a_income'],
147
                'education_grade_id' => $admissionInfo['education_grade']->id,
148
                'academic_period_id' => $admissionInfo['academic_period']->id,
149
                'start_date' => $admissionInfo['academic_period']->start_date,
150
                'start_year' => $admissionInfo['academic_period']->start_year,
151
                'end_date' => $admissionInfo['academic_period']->end_date,
152
                'end_year' => $admissionInfo['academic_period']->end_year,
153
                'institution_id' => $admissionInfo['instituion']->id,
154
                'created' => now(),
155
                'created_user_id' => 1
156
            ]);
157
        } catch (\Throwable $th) {
158
            Log::error($th);
159
        }
160
    }
161
162
    /**
163
     * Update new Institution student from examination data
164
     *
165
     * @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...
166
     * @param [type] $admissionInfo
167
     * @return void
168
     */
169
    public static function updateExaminationData($student, $admissionInfo)
170
    {
171
        try {
172
            self::where([
173
                'student_id' => $student['student_id'],
174
                'education_grade_id' => $admissionInfo['education_grade']->id,
175
                'academic_period_id' => $admissionInfo['academic_period']->id,
176
            ])->update(
177
                [
178
                    'taking_g5_exam' => $student['taking_g5_exam'],
179
                    'taking_ol_exam' => $student['taking_ol_exam'],
180
                    'taking_al_exam' => $student['taking_al_exam'],
181
                    // Set special examination center
182
                    'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : false,
183
                    'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : false,
184
                    'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : false,
185
    
186
                    'income_at_g5' => $student['a_income'],
187
                    'modified' => now(),
188
                    'modified_user_id' => 1
189
                ]
190
            );
191
        } catch (\Throwable $th) {
192
            Log::error($th);
193
        }
194
    }
195
}
196