Test Setup Failed
Push — master ( ad3b88...b2f2cc )
by Mohamed
15:01 queued 07:46
created

StudentUpdate::model()   A

Complexity

Conditions 4
Paths 27

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
eloc 29
c 4
b 0
f 0
nc 27
nop 1
dl 0
loc 36
rs 9.456
1
<?php
2
3
namespace App\Imports;
4
5
use App\Models\User;
6
use function foo\func;
7
use App\Models\Nationality;
8
use App\Rules\admissionAge;
9
use App\Models\User_contact;
10
use App\Models\Identity_type;
11
use App\Models\Security_user;
12
use App\Models\User_identity;
13
use App\Models\Import_mapping;
14
use App\Models\Security_group;
15
use App\Models\User_body_mass;
16
use App\Models\Academic_period;
17
use App\Models\Student_guardian;
18
use App\Models\User_nationality;
19
use App\Models\Institution_class;
20
use App\Models\User_special_need;
21
use App\Mail\StudentCountExceeded;
22
use App\Mail\StudentImportSuccess;
23
use Illuminate\Support\Facades\DB;
24
use App\Models\Area_administrative;
25
use App\Models\Institution_student;
26
use App\Models\Institution_subject;
27
use App\Models\Workflow_transition;
28
use Illuminate\Support\Facades\Log;
29
use Illuminate\Support\Facades\Mail;
30
use App\Models\Institution_class_grade;
31
use App\Models\Special_need_difficulty;
32
use Illuminate\Support\Facades\Request;
33
use Maatwebsite\Excel\Concerns\ToModel;
34
use App\Models\Education_grades_subject;
35
use App\Models\Institution_class_student;
36
use App\Models\Institution_class_subject;
37
use Illuminate\Support\Facades\Validator;
38
use Maatwebsite\Excel\Concerns\WithLimit;
39
use Maatwebsite\Excel\Events\BeforeSheet;
40
use Maatwebsite\Excel\Validators\Failure;
41
use Maatwebsite\Excel\Concerns\Importable;
42
use Maatwebsite\Excel\Concerns\WithEvents;
43
use Maatwebsite\Excel\Events\BeforeImport;
44
use App\Models\Institution_subject_student;
45
use Maatwebsite\Excel\Concerns\SkipsErrors;
46
use Maatwebsite\Excel\Concerns\WithMapping;
47
use Maatwebsite\Excel\Concerns\SkipsOnError;
48
use Maatwebsite\Excel\Concerns\WithStartRow;
49
use App\Models\Institution_student_admission;
50
use Maatwebsite\Excel\Concerns\SkipsFailures;
51
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
52
use Maatwebsite\Excel\Concerns\WithHeadingRow;
53
use Maatwebsite\Excel\Concerns\WithValidation;
54
use Maatwebsite\Excel\Concerns\WithBatchInserts;
55
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
56
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
57
58
class StudentUpdate extends Import implements  ToModel, WithStartRow, WithHeadingRow, WithMultipleSheets, WithEvents, WithMapping, WithLimit, WithBatchInserts, WithValidation , SkipsOnFailure , SkipsOnError{
59
60
    use Importable,
0 ignored issues
show
introduced by
The trait Maatwebsite\Excel\Concerns\Importable requires some properties which are not provided by App\Imports\StudentUpdate: $disk, $readerType, $filePath
Loading history...
61
        RegistersEventListeners,
62
        SkipsFailures,
63
        SkipsErrors;
64
65
66
    public function sheets(): array {
67
        return [
68
            'Update Students' => $this
69
        ];
70
    }
71
72
    public function registerEvents(): array {
73
        // TODO: Implement registerEvents() method.
74
        return [
75
            BeforeSheet::class => function(BeforeSheet $event) {
76
                $this->sheetNames[] = $event->getSheet()->getTitle();
0 ignored issues
show
Bug Best Practice introduced by
The property sheetNames does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
                $this->worksheet = $event->getSheet();
0 ignored issues
show
Bug Best Practice introduced by
The property worksheet does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
                $worksheet = $event->getSheet();
79
                $this->highestRow = $worksheet->getHighestDataRow('B');
0 ignored issues
show
Bug Best Practice introduced by
The property highestRow does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
            }
81
        ];
82
    }
83
84
85
    public function model(array $row) {
86
87
        try {
88
            $institutionClass = Institution_class::find($this->file['institution_class_id']);
89
            $institution = $institutionClass->institution_id;
0 ignored issues
show
Bug introduced by
The property institution_id does not exist on App\Models\Institution_class. Did you mean institution?
Loading history...
90
            if (!empty($institutionClass)) {
91
                $this->setGender($row);
92
                $studentInfo = Security_user::createOrUpdateStudentProfile($row,'update',$this->file);
93
                $student = Institution_class_student::where('student_id', '=', $studentInfo->id)->first();
94
                if(!empty($row['admission_no'])){
95
                    Institution_student::where('student_id','=',$studentInfo->id)
96
                    ->where('institution_id','=', $institution)
97
                    ->update(['admission_id'=> $row['admission_no']]);
98
                }
99
                User_special_need::createOrUpdate($studentInfo->id,$row,$this->file);
100
                User_body_mass::createOrUpdate($studentInfo->id,$row,$this->file);
101
102
                $this->createOrUpdateGuardian($row,$student,'father');
103
                $this->createOrUpdateGuardian($row,$student,'mother');
104
                $this->createOrUpdateGuardian($row,$student,'guardian');
105
106
                Institution_student::updateStudentArea($student->toArray());
107
                $this->insertOrUpdateSubjects($row,$student,$institution);
108
                $totalStudents = Institution_class_student::getStudentsCount($this->file['institution_class_id']);
109
                Institution_class::where('id', '=', $institutionClass->id)
110
                        ->update([
111
                            'total_male_students' => $totalStudents['total_male_students'],
112
                            'total_female_students' => $totalStudents['total_female_students']]);
113
            }
114
        } catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
115
            $error = \Illuminate\Validation\ValidationException::withMessages([]);
116
            $failures = $e->failures();
117
            throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures);
118
            Log::info('email-sent', [$e]);
0 ignored issues
show
Unused Code introduced by
Illuminate\Support\Facad...email-sent', array($e)) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
119
        }
120
        unset($row);
121
    }
122
123
    public function getStudentSubjects($student) {
124
        return Institution_subject_student::where('student_id', '=', $student->student_id)
125
                        ->where('institution_class_id', '=', $student->institution_class_id)->get()->toArray();
126
    }
127
128
129
    public function rules(): array {
130
131
        return [
132
            '*.student_id' => 'required|exists:security_users,openemis_no|is_student_in_class:'.$this->file['institution_class_id'],
133
            '*.full_name' => 'nullable|regex:/^[a-zA-Z .]*$/u|max:256',
134
            '*.preferred_name' => 'nullable|regex:/^[a-zA-Z .]*$/u|max:90',
135
            '*.gender_mf' => 'nullable|in:M,F',
136
            '*.date_of_birth_yyyy_mm_dd' => 'date|nullable',
137
            '*.address' => 'nullable',
138
            '*.birth_registrar_office_as_in_birth_certificate' => 'nullable|exists:area_administratives,name|required_if:identity_type,BC|birth_place',
139
            '*.birth_divisional_secretariat' => 'nullable|exists:area_administratives,name|required_with:birth_registrar_office_as_in_birth_certificate',
140
            '*.nationality' => 'nullable',
141
            '*.identity_type' => 'nullable|required_with:*.identity_number',
142
            '*.identity_number' => 'nullable|identity:identity_type|required_with:*.identity_type',
143
            '*.education_grade' => 'nullable|exists:education_grades,code',
144
            '*.option_*' => 'nullable|exists:education_subjects,name',
145
            '*.bmi_height' => 'required_with:*.bmi_weight|nullable|numeric|max:200|min:60',
146
            '*.bmi_weight' => 'required_with:*.bmi_height|nullable|numeric|max:200|min:10',
147
            '*.bmi_date_yyyy_mm_dd' => 'required_with:*.bmi_height|nullable|date',
148
            '*.bmi_academic_period' => 'required_with:*.bmi_weight|nullable|exists:academic_periods,name',
149
            '*.admission_no' => 'nullable|min:4|max:12|regex:/^[A-Za-z0-9\/]+$/',
150
            '*.start_date_yyyy_mm_dd' => 'nullable|date',
151
            '*.special_need_type' => 'nullable',
152
            '*.special_need' => 'nullable|exists:special_need_difficulties,name|required_if:special_need_type,Differantly Able',
153
            '*.fathers_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
154
            '*.fathers_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.fathers_full_name',
155
            '*.fathers_address' => 'required_with:*.fathers_full_name',
156
            '*.fathers_address_area' => 'required_with:*.fathers_full_name|nullable|exists:area_administratives,name',
157
            '*.fathers_nationality' => 'required_with:*.fathers_full_name',
158
            '*.fathers_identity_type' => 'required_with:*.fathers_identity_number',
159
            '*.fathers_identity_number' => 'nullable|required_with:*.fathers_identity_type|nic:fathers_identity_number',
160
            '*.mothers_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
161
            '*.mothers_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.mothers_full_name',
162
            '*.mothers_address' => 'required_with:*.mothers_full_name',
163
            '*.mothers_address_area' => 'required_with:*.mothers_full_name|nullable|exists:area_administratives,name',
164
            '*.mothers_nationality' => "required_with:*.mothers_full_name",
165
            '*.mothers_identity_type' => "required_with:*.mothers_identity_number",
166
            '*.mothers_identity_number' => 'nullable|required_with:*.mothers_identity_type|nic:mothers_identity_number',
167
            '*.guardians_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
168
            '*.guardians_gender_mf' => 'required_with:*.guardians_full_name',
169
            '*.guardians_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.guardians_full_name',
170
            '*.guardians_address' => 'required_with:*.guardians_full_name',
171
            '*.guardians_address_area' => 'required_with:*.guardians_full_name|nullable|exists:area_administratives,name',
172
            '*.guardians_nationality' => 'required_with:*.guardians_full_name',
173
            '*.guardians_identity_type' => 'required_with:*.guardians_identity_number',
174
            '*.guardians_identity_number' => 'nullable|required_with:*.guardians_identity_type|nic:guardians_identity_number',
175
        ];
176
    }
177
178
}
179