1 | <?php |
||||||||
2 | |||||||||
3 | namespace App\Providers; |
||||||||
4 | |||||||||
5 | namespace App\Providers; |
||||||||
6 | |||||||||
7 | use App\Models\Academic_period; |
||||||||
8 | use App\Models\Area_administrative; |
||||||||
9 | use App\Models\Security_user; |
||||||||
10 | use App\Models\Identity_type; |
||||||||
11 | use Illuminate\Support\Carbon; |
||||||||
12 | use Illuminate\Support\Facades\Date; |
||||||||
13 | use Illuminate\Support\Facades\Validator; |
||||||||
14 | use Illuminate\Validation\Validator as IlluminateValidator; |
||||||||
15 | use Illuminate\Support\Facades\Log; |
||||||||
16 | use App\Models\Education_grade; |
||||||||
17 | use App\Models\Institution_class; |
||||||||
18 | use App\Models\Institution_class_grade; |
||||||||
19 | use App\Models\Institution_class_student; |
||||||||
20 | |||||||||
21 | class ValidatorExtended extends IlluminateValidator |
||||||||
22 | { |
||||||||
23 | |||||||||
24 | private $_custom_messages = array( |
||||||||
25 | "admission_age" => "The age limit not match with admission age for this class", |
||||||||
26 | "birth_place" => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat', |
||||||||
27 | 'user_unique' => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat', |
||||||||
28 | "is_bc" => "The Birth Certificate number is not valid", |
||||||||
29 | "is_student_in_class" => "The Student ID is not belong to this class", |
||||||||
30 | "bmi" => "The record must have BMI information", |
||||||||
31 | "identity" => "Identity number format should match with Identity type", |
||||||||
32 | ); |
||||||||
33 | |||||||||
34 | public function __construct( |
||||||||
35 | $translator, |
||||||||
36 | $data, |
||||||||
37 | $rules, |
||||||||
38 | $messages = array(), |
||||||||
39 | $customAttributes = array() |
||||||||
40 | ) { |
||||||||
41 | parent::__construct( |
||||||||
42 | $translator, |
||||||||
43 | $data, |
||||||||
44 | $rules, |
||||||||
45 | $messages, |
||||||||
46 | $customAttributes |
||||||||
47 | ); |
||||||||
48 | $this->_set_custom_stuff(); |
||||||||
49 | } |
||||||||
50 | |||||||||
51 | protected function _set_custom_stuff() |
||||||||
52 | { |
||||||||
53 | //setup our custom error messages |
||||||||
54 | $this->setCustomMessages($this->_custom_messages); |
||||||||
55 | } |
||||||||
56 | |||||||||
57 | /** |
||||||||
58 | * this will validate admission age limit of the student |
||||||||
59 | * |
||||||||
60 | * admission age validation |
||||||||
61 | */ |
||||||||
62 | |||||||||
63 | protected function validateAdmissionAge($attribute, $value, $parameters, $validator) |
||||||||
0 ignored issues
–
show
|
|||||||||
64 | { |
||||||||
65 | $institutionClass = Institution_class::find($parameters[0]); |
||||||||
66 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $institutionClass->id)->first(); |
||||||||
67 | if (!empty($institutionClass)) { |
||||||||
68 | $gradeEntity = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||||||||
0 ignored issues
–
show
|
|||||||||
69 | $academicPeriod = Academic_period::find($institutionClass->academic_period_id); |
||||||||
0 ignored issues
–
show
|
|||||||||
70 | if (empty($value)) { |
||||||||
71 | return false; |
||||||||
72 | } elseif ($gradeEntity !== null) { |
||||||||
73 | $admissionAge = (($gradeEntity->admission_age) * 12) - 1; |
||||||||
0 ignored issues
–
show
|
|||||||||
74 | $to = $academicPeriod->start_date; |
||||||||
0 ignored issues
–
show
|
|||||||||
75 | $diff_in_months = $to->diffInMonths($value); |
||||||||
76 | $ageOfStudent = $diff_in_months; |
||||||||
77 | $enrolmentMaximumAge = $admissionAge + 120; |
||||||||
78 | return ($ageOfStudent <= $enrolmentMaximumAge) && ($ageOfStudent >= $admissionAge); |
||||||||
79 | } else { |
||||||||
80 | return false; |
||||||||
81 | } |
||||||||
82 | } else { |
||||||||
83 | $this->_custom_messages['admission_age'] = 'given' . $attribute . 'Not found'; |
||||||||
84 | $this->_set_custom_stuff(); |
||||||||
85 | return false; |
||||||||
86 | } |
||||||||
87 | } |
||||||||
88 | |||||||||
89 | protected function validateHW($attribute, $value) |
||||||||
90 | { |
||||||||
91 | |||||||||
92 | if (is_numeric($value)) { |
||||||||
93 | if ($value < 10) { |
||||||||
94 | $this->_custom_messages['bmi'] = $attribute . ' is must greater than 10'; |
||||||||
95 | $this->_set_custom_stuff(); |
||||||||
96 | return false; |
||||||||
97 | } elseif ($value > 250) { |
||||||||
98 | $this->_custom_messages['bmi'] = $attribute . ' is must smaller than 250'; |
||||||||
99 | $this->_set_custom_stuff(); |
||||||||
100 | return false; |
||||||||
101 | } |
||||||||
102 | } else { |
||||||||
103 | $this->_custom_messages['bmi'] = $attribute . ' is must a valid numeric'; |
||||||||
104 | $this->_set_custom_stuff(); |
||||||||
105 | return false; |
||||||||
106 | } |
||||||||
107 | return true; |
||||||||
108 | } |
||||||||
109 | |||||||||
110 | protected function validateBmi($attribute, $value, $parameters) |
||||||||
111 | { |
||||||||
112 | $bmiGrades = ['G1', 'G4', 'G7', 'G10']; |
||||||||
113 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $parameters[0]) |
||||||||
114 | ->join('education_grades', 'institution_class_grades.education_grade_id', 'education_grades.id') |
||||||||
115 | ->first(); |
||||||||
116 | $educationGrade = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||||||||
0 ignored issues
–
show
|
|||||||||
117 | if (in_array($institutionGrade->code, $bmiGrades)) { |
||||||||
0 ignored issues
–
show
|
|||||||||
118 | if (!empty($value)) { |
||||||||
119 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||||||||
120 | return $this->validateHW($attribute, $value); |
||||||||
121 | } |
||||||||
122 | } else { |
||||||||
123 | $this->_custom_messages['bmi'] = $attribute . ' is required for ' . $educationGrade->name; |
||||||||
0 ignored issues
–
show
|
|||||||||
124 | $this->_set_custom_stuff(); |
||||||||
125 | return false; |
||||||||
126 | } |
||||||||
127 | } elseif (!empty($value)) { |
||||||||
128 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||||||||
129 | return $this->validateHW($attribute, $value); |
||||||||
130 | } |
||||||||
131 | } else { |
||||||||
132 | return true; |
||||||||
133 | } |
||||||||
134 | } |
||||||||
135 | |||||||||
136 | protected function validateBirthPlace($attribute, $value, $perameters, $validator) |
||||||||
0 ignored issues
–
show
The parameter
$attribute 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. ![]() The parameter
$perameters 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. ![]() |
|||||||||
137 | { |
||||||||
138 | foreach ($validator->getData() as $data) { |
||||||||
139 | if ($data['identity_type'] == 'BC' && key_exists('birth_divisional_secretariat', $data)) { |
||||||||
140 | $BirthDivision = Area_administrative::where('name', '=', '%' . $data['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5); // |
||||||||
141 | if ($BirthDivision->count() > 0) { |
||||||||
142 | $BirthArea = Area_administrative::where('name', '=', '%' . $value . '%') //$data['birth_registrar_office_as_in_birth_certificate'] |
||||||||
143 | ->where('parent_id', '=', $BirthDivision->first()->id)->count(); |
||||||||
144 | return $BirthArea > 0; |
||||||||
145 | } elseif (key_exists('birth_divisional_secretariat', $data) && (!key_exists('birth_registrar_office_as_in_birth_certificate', $data))) { |
||||||||
146 | $this->_custom_messages['birth_place'] = 'birth_registrar_office_as_in_birth_certificate required with BC'; |
||||||||
147 | $this->_set_custom_stuff(); |
||||||||
148 | return false; |
||||||||
149 | } else { |
||||||||
150 | return true; |
||||||||
151 | } |
||||||||
152 | } else { |
||||||||
153 | return true; |
||||||||
154 | } |
||||||||
155 | } |
||||||||
156 | } |
||||||||
157 | |||||||||
158 | protected function validateIsStudentInClass($attribute, $value, $perameters, $validator) |
||||||||
0 ignored issues
–
show
The parameter
$attribute 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. ![]() The parameter
$validator 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. ![]() |
|||||||||
159 | { |
||||||||
160 | $student = Security_user::where('openemis_no', '=', $value); |
||||||||
161 | if ($student->count() > 0) { |
||||||||
162 | $student = $student->first()->toArray(); |
||||||||
163 | $check = Institution_class_student::where('student_id', '=', $student['id'])->where('institution_class_id', '=', $perameters[0])->count(); |
||||||||
164 | if ($check == 1) { |
||||||||
165 | return true; |
||||||||
166 | } else { |
||||||||
167 | return false; |
||||||||
168 | } |
||||||||
169 | } else { |
||||||||
170 | return false; |
||||||||
171 | } |
||||||||
172 | } |
||||||||
173 | |||||||||
174 | |||||||||
175 | |||||||||
176 | protected function validateIdentity($attribute, $value, $perameters, $validator) |
||||||||
177 | { |
||||||||
178 | $valid = true; |
||||||||
179 | foreach ($validator->getData() as $data) { |
||||||||
180 | switch($data[$perameters[0]]){ |
||||||||
181 | case 'BC': |
||||||||
182 | $valid = preg_match('/^([0-9]{3,5})/i', $value); |
||||||||
183 | break; |
||||||||
184 | case 'NIC': |
||||||||
185 | $valid = preg_match('/^([0-9]{9}[VX]|([0-9]{12}))/i', $value); |
||||||||
186 | break; |
||||||||
187 | default: |
||||||||
188 | $valid = true; |
||||||||
189 | } |
||||||||
190 | } |
||||||||
191 | |||||||||
192 | if ($valid == 0) { |
||||||||
193 | $this->_custom_messages['identity'] = $attribute . ' is not valid Please check the NIC number'; |
||||||||
194 | $this->_set_custom_stuff(); |
||||||||
195 | return false; |
||||||||
196 | } else { |
||||||||
197 | return true; |
||||||||
198 | } |
||||||||
199 | } |
||||||||
200 | |||||||||
201 | protected function validateUserUnique($attribute, $value, $perameters, $validator) |
||||||||
0 ignored issues
–
show
The parameter
$perameters 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. ![]() The parameter
$attribute 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. ![]() |
|||||||||
202 | { |
||||||||
203 | foreach ($validator->getData() as $data) { |
||||||||
204 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||||||||
205 | if ($identityType !== null && ($value !== null)) { |
||||||||
206 | if ($identityType->national_code === 'BC') { |
||||||||
0 ignored issues
–
show
|
|||||||||
207 | return $this->checkUnique($value, $data, $identityType); |
||||||||
208 | } elseif ($identityType->national_code === 'NIC') { |
||||||||
209 | return $this->checkUnique($value, $data, $identityType); |
||||||||
210 | } |
||||||||
211 | } elseif (($value == null) || $value == "") { |
||||||||
212 | return true; |
||||||||
213 | } |
||||||||
214 | } |
||||||||
215 | } |
||||||||
216 | |||||||||
217 | protected function validateIsBc($attribute, $value, $perameters, $validator) |
||||||||
0 ignored issues
–
show
The parameter
$perameters 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. ![]() The parameter
$value 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. ![]() The parameter
$attribute 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. ![]() |
|||||||||
218 | { |
||||||||
219 | foreach ($validator->getData() as $data) { |
||||||||
220 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||||||||
221 | if (($identityType !== null) && ($identityType !== "")) { |
||||||||
222 | if (($identityType->national_code) === 'BC') { |
||||||||
0 ignored issues
–
show
|
|||||||||
223 | return (strlen((string) $data['identity_number']) < 7); |
||||||||
224 | } else { |
||||||||
225 | return true; |
||||||||
226 | } |
||||||||
227 | } else { |
||||||||
228 | return true; |
||||||||
229 | } |
||||||||
230 | } |
||||||||
231 | } |
||||||||
232 | |||||||||
233 | protected function checkUnique($value, $data, $identityType) |
||||||||
0 ignored issues
–
show
The parameter
$data 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. ![]() |
|||||||||
234 | { |
||||||||
235 | $isUnique = Security_user::where('identity_number', '=', $value)->where('identity_type_id', '=', $identityType->id); |
||||||||
236 | if ($isUnique->count() > 0) { |
||||||||
237 | $this->_custom_messages['user_unique'] = 'The identity number already in use. User ID is : ' . $isUnique->first()->openemis_no; |
||||||||
0 ignored issues
–
show
|
|||||||||
238 | $this->_set_custom_stuff(); |
||||||||
239 | return false; |
||||||||
240 | } else { |
||||||||
241 | return true; |
||||||||
242 | } |
||||||||
243 | } |
||||||||
244 | |||||||||
245 | protected function IsBc($data, $value) |
||||||||
246 | { |
||||||||
247 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||||||||
248 | if ($identityType !== null) { |
||||||||
249 | if (($identityType->national_code) === 'BC' && strlen((string) $value) < 8) { |
||||||||
0 ignored issues
–
show
|
|||||||||
250 | return false; |
||||||||
251 | } else { |
||||||||
252 | return true; |
||||||||
253 | } |
||||||||
254 | } else { |
||||||||
255 | return true; |
||||||||
256 | } |
||||||||
257 | } |
||||||||
258 | } |
||||||||
259 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.