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

Institution_class_student::getStudentNewClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
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
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
class Institution_class_student extends Model
12
{
13
14
    use SoftDeletes;
15
    
16
    /**
17
     * The database table used by the model.
18
     *
19
     * @var string
20
     */
21
    protected $table = 'institution_class_students';
22
23
    protected $softDelete = true;
24
25
    /**
26
     * Attributes that should be mass-assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = ['student_id', 'institution_class_id', 'education_grade_id', 'academic_period_id', 'institution_id', 'student_status_id', 'modified_user_id', 'modified', 'created_user_id', 'created'];
31
32
    /**
33
     * The attributes excluded from the model's JSON form.
34
     *
35
     * @var array
36
     */
37
    protected $hidden = [];
38
39
    /**
40
     * The attributes that should be casted to native types.
41
     *
42
     * @var array
43
     */
44
    protected $casts = [];
45
46
    /**
47
     * The attributes that should be mutated to dates.
48
     *
49
     * @var array
50
     */
51
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created', 'modified', 'created'];
52
53
54
55
    public $timestamps = false;
56
57
58
    public static function boot()
59
    {
60
        parent::boot();
61
        self::creating(function ($model) {
62
            $model->id = (string) Uuid::generate(4);
63
            $model->created = now();
64
        });
65
    }
66
67
    public function student()
68
    {
69
        return $this->belongsTo('App\Models\Security_user', 'student_id');
70
    }
71
72
    public  static function getStudentsCount($institution_class_id)
73
    {
74
        $total_male_students = self::with(['student' => function ($query) {
75
            $query->where('student.gender_id', '=', 1);
76
        }])->whereHas('student', function ($query) {
77
            $query->where('gender_id', '=', 1);
78
        })->where('institution_class_id', '=', $institution_class_id)->count();
79
80
        $total_female_students = self::with(['student' => function ($query) {
81
            $query->where('student.gender_id', '=', 2);
82
        }])->whereHas('student', function ($query) {
83
            $query->where('gender_id', '=', 2);
84
        })->where('institution_class_id', '=', $institution_class_id)->count();
85
86
        $totalStudents = $total_female_students + $total_male_students;
87
88
89
        return [
90
            'total' => $totalStudents,
91
            'total_female_students' => $total_female_students,
92
            'total_male_students' => $total_male_students
93
        ];
94
    }
95
96
    public static function  isDuplicated($inputs)
97
    {
98
99
        $exists = self::where('student_id', '=', $inputs['student_id'])
100
            ->where('institution_class_id', $inputs['institution_class_id'])
101
            ->count();
102
103
        return $exists;
104
    }
105
106
    public function getStudentNewClass($student)
107
    {
108
        return self::query()
109
            ->where('student_id', $student['student_id'])
110
            ->join('institution_classes', 'institution_class_students.institution_class_id', '=', 'institution_classes.id')
111
            ->where('institution_class_students.student_id', $student['student_id'])
112
            ->where('institution_class_students.academic_period_id',$student['academic_period_id'])
113
            ->where('institution_class_students.institution_id',$student['institution_id'])
114
            ->whereNull('institution_class_students.deleted_at')
115
            ->get()->first();
116
    }
117
118
119
    /**
120
     * Create new Institution class entry for student from examination data
121
     *
122
     * @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...
123
     * @param [type] $admissionInfo
124
     * @return void
125
     */
126
    public static function createExaminationData($student, $admissionInfo)
127
    {
128
        try {
129
            self::updateOrcreate([
130
                'student_id' => $student['id'],
131
                'institution_class_id' => $admissionInfo['instituion_class']['id'],
132
                'education_grade_id' => $admissionInfo['education_grade']->id,
133
                'academic_period_id' => $admissionInfo['academic_period']->id,
134
                'institution_id' => $admissionInfo['instituion']->id,
135
                'student_status_id' => 1
136
            ]);
137
        } catch (\Throwable $th) {
138
            Log::error($th);
139
        }
140
    }
141
142
    public static function createOrUpdate($studentId,$params,$file){
143
       return  self::create([
144
            'student_id' => $studentId,
145
            'institution_class_id' => $params['institution_class']->id,
146
            'education_grade_id' => $params['institution_grade']->education_grade_id,
147
            'academic_period_id' => $params['academic_period']->id,
148
            'institution_id' => $params['institution'],
149
            'student_status_id' => 1,
150
            'created_user_id' => $file['security_user_id']
151
        ]);
152
    }
153
}
154