Issues (367)

app/Imports/StudentUpdate.php (6 issues)

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
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
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
                $studentInfo['student_id'] = $studentInfo->id;
107
                Institution_student::updateStudentArea($studentInfo->toArray());
108
                $this->insertOrUpdateSubjects($row,$student,$institution);
109
                $totalStudents = Institution_class_student::getStudentsCount($this->file['institution_class_id']);
110
                Institution_class::where('id', '=', $institutionClass->id)
111
                        ->update([
112
                            'total_male_students' => $totalStudents['total_male_students'],
113
                            'total_female_students' => $totalStudents['total_female_students']]);
114
            }
115
        } catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
116
            $error = \Illuminate\Validation\ValidationException::withMessages([]);
117
            $failures = $e->failures();
118
            throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures);
119
            Log::info('email-sent', [$e]);
0 ignored issues
show
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...
120
        }
121
        unset($row);
122
    }
123
124
    public function getStudentSubjects($student) {
125
        return Institution_subject_student::where('student_id', '=', $student->student_id)
126
                        ->where('institution_class_id', '=', $student->institution_class_id)->get()->toArray();
127
    }
128
129
130
    public function rules(): array {
131
132
        return [
133
            '*.student_id' => 'required|exists:security_users,openemis_no|is_student_in_class:'.$this->file['institution_class_id'],
134
            '*.full_name' => 'nullable|regex:/^[a-zA-Z .]*$/u|max:256',
135
            '*.preferred_name' => 'nullable|regex:/^[a-zA-Z .]*$/u|max:90',
136
            '*.gender_mf' => 'nullable|in:M,F',
137
            '*.date_of_birth_yyyy_mm_dd' => 'date|nullable',
138
            '*.address' => 'nullable',
139
            '*.birth_registrar_office_as_in_birth_certificate' => 'nullable|exists:area_administratives,name|required_if:identity_type,BC|birth_place',
140
            '*.birth_divisional_secretariat' => 'nullable|exists:area_administratives,name|required_with:birth_registrar_office_as_in_birth_certificate',
141
            '*.nationality' => 'nullable',
142
            '*.identity_type' => 'nullable|required_with:*.identity_number',
143
            '*.identity_number' => 'nullable|identity:identity_type|required_with:*.identity_type',
144
            '*.education_grade' => 'nullable|exists:education_grades,code',
145
            '*.option_*' => 'nullable|exists:education_subjects,name',
146
            '*.bmi_height' => 'required_with:*.bmi_weight|nullable|numeric|max:200|min:60',
147
            '*.bmi_weight' => 'required_with:*.bmi_height|nullable|numeric|max:200|min:10',
148
            '*.bmi_date_yyyy_mm_dd' => 'required_with:*.bmi_height|nullable|date',
149
            '*.bmi_academic_period' => 'required_with:*.bmi_weight|nullable|exists:academic_periods,name',
150
            '*.admission_no' => 'nullable|min:4|max:12|regex:/^[A-Za-z0-9\/]+$/',
151
            '*.start_date_yyyy_mm_dd' => 'nullable|date',
152
            '*.special_need_type' => 'nullable',
153
            '*.special_need' => 'nullable|exists:special_need_difficulties,name|required_if:special_need_type,Differantly Able',
154
            '*.fathers_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
155
            '*.fathers_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.fathers_full_name',
156
            '*.fathers_address' => 'required_with:*.fathers_full_name',
157
            '*.fathers_address_area' => 'required_with:*.fathers_full_name|nullable|exists:area_administratives,name',
158
            '*.fathers_nationality' => 'required_with:*.fathers_full_name',
159
            '*.fathers_identity_type' => 'nullable|required_with:*.fathers_identity_number|in:NIC,BC',
160
            '*.fathers_identity_number' => 'nullable|required_with:*.fathers_identity_type|identity:fathers_identity_type',
161
            '*.mothers_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
162
            '*.mothers_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.mothers_full_name',
163
            '*.mothers_address' => 'required_with:*.mothers_full_name',
164
            '*.mothers_address_area' => 'required_with:*.mothers_full_name|nullable|exists:area_administratives,name',
165
            '*.mothers_nationality' => "required_with:*.mothers_full_name",
166
            '*.mothers_identity_type' => "nullable|required_with:*.mothers_identity_number|in:NIC,BC",
167
            '*.mothers_identity_number' => 'nullable|identity:mothers_identity_type',
168
            '*.guardians_full_name' => 'nullable|regex:/^[\pL\s\-]+$/u',
169
            '*.guardians_gender_mf' => 'required_with:*.guardians_full_name',
170
            '*.guardians_date_of_birth_yyyy_mm_dd' => 'nullable|required_with:*.guardians_full_name',
171
            '*.guardians_address' => 'required_with:*.guardians_full_name',
172
            '*.guardians_address_area' => 'required_with:*.guardians_full_name|nullable|exists:area_administratives,name',
173
            '*.guardians_nationality' => 'required_with:*.guardians_full_name',
174
            '*.guardians_identity_type' => 'nullable|required_with:*.guardians_identity_number|in:NIC,BC',
175
            '*.guardians_identity_number' => 'nullable|identity:guardians_identity_type',
176
        ];
177
    }
178
179
}
180