Test Setup Failed
Pull Request — master (#478)
by Mohamed
13:16
created

Institution_subject::getStudentsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.7666
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Institution_subject extends Base_Model  {
8
9
    /**
10
     * The database table used by the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'institution_subjects';
15
16
    /**
17
     * Attributes that should be mass-assignable.
18
     *
19
     * @var array
20
     */
21
    protected $fillable = ['name', 'no_of_seats', 'total_male_students', 'total_female_students', 'institution_id', 'education_grade_id', 'education_subject_id', 'academic_period_id', 'modified_user_id', 'modified', 'created_user_id', 'created'];
22
23
    /**
24
     * The attributes excluded from the model's JSON form.
25
     *
26
     * @var array
27
     */
28
    protected $hidden = [];
29
30
    /**
31
     * The attributes that should be casted to native types.
32
     *
33
     * @var array
34
     */
35
    protected $casts = [];
36
37
    /**
38
     * The attributes that should be mutated to dates.
39
     *
40
     * @var array
41
     */
42
    protected $dates = ['modified', 'created', 'modified', 'created'];
43
44
45
    public  function institutionGradeSubject(){
46
        return $this->belongsTo('App\Models\Education_grades_subject','education_subject_id','education_subject_id');
47
    }
48
49
    public  function institutionOptionalGradeSubject(){
50
        return $this->belongsTo('App\Models\Education_grades_subject','education_grade_id','education_grade_id');
51
    }
52
53
    public  function institutionMandatoryGradeSubject(){
54
        return $this->belongsTo('App\Models\Education_grades_subject','education_grade_id','education_grade_id');
55
    }
56
57
58
    public  function institutionClassSubject(){
59
        return $this->hasMany('App\Models\Institution_class_subject','institution_class_id','id');
60
    }
61
62
63
64
    public function getInstitutionSubjects($institution_id,$academic_period_id){
65
        return self::query()->where('institution_id',$institution_id)
66
            ->where('academic_period_id',$academic_period_id)
67
            ->get()->toArray();
68
    }
69
70
    public  static function getStudentsCount($institution_subject_id)
71
    {
72
        $total_male_students = self::with(['student' => function ($query) {
73
            $query->where('student.gender_id', '=', 1);
74
        }])->whereHas('student', function ($query) {
75
            $query->where('gender_id', '=', 1);
76
        })->where('institution_subject_id', '=', $institution_subject_id)->count();
77
78
        $total_female_students = self::with(['student' => function ($query) {
79
            $query->where('student.gender_id', '=', 2);
80
        }])->whereHas('student', function ($query) {
81
            $query->where('gender_id', '=', 2);
82
        })->where('institution_subject_id', '=', $institution_subject_id)->count();
83
84
        $totalStudents = $total_female_students + $total_male_students;
85
86
87
        return [
88
            'total' => $totalStudents,
89
            'total_female_students' => $total_female_students,
90
            'total_male_students' => $total_male_students
91
        ];
92
    }
93
94
95
}
96