Passed
Push — master ( 49a2c9...58d635 )
by Thomas
07:31
created

Year::studentCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 3
nop 1
dl 0
loc 30
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Backpack\CRUD\app\Models\Traits\CrudTrait;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
9
class Year extends Model
10
{
11
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Models\Traits\CrudTrait requires some properties which are not provided by App\Models\Year: $fakeColumns, $identifiableAttribute, $Type
Loading history...
12
13
    public $timestamps = false;
14
15
    protected $guarded = ['id'];
16
17
    public function periods()
18
    {
19
        return $this->hasMany(Period::class);
20
    }
21
22
    public function getPartnershipsAttribute()
23
    {
24
        return Course::whereIn('period_id', $this->periods->pluck('id'))->pluck('partner_id')->unique()->count();
25
    }
26
27
    public function studentCount($gender = null)
28
    {
29
        if (in_array($gender, [1,2])) {
30
            return DB::table('enrollments')
31
                ->join('courses', 'enrollments.course_id', 'courses.id')
32
                ->join('periods', 'courses.period_id', 'periods.id')
33
                ->join('students', 'enrollments.student_id', 'students.id')
34
                ->where('periods.year_id', $this->id)
35
                ->whereIn('enrollments.status_id', ['1', '2']) // filter out cancelled enrollments, todo make this configurable.
36
                ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null)
37
                ->where('students.gender_id', $gender)
38
                ->distinct('student_id')->count('enrollments.student_id');
39
        }
40
41
        if ($gender === 0) {
42
            return DB::table('enrollments')
43
                ->join('courses', 'enrollments.course_id', 'courses.id')
44
                ->join('periods', 'courses.period_id', 'periods.id')
45
                ->join('students', 'enrollments.student_id', 'students.id')
46
                ->where('periods.year_id', $this->id)
47
                ->whereIn('enrollments.status_id', ['1', '2']) // filter out cancelled enrollments, todo make this configurable.
48
                ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null)
49
                ->where(function($query) {
50
                    return $query->where('students.gender_id', 0)->orWhereNull('students.gender_id');
51
                })
52
                ->distinct('student_id')->count('enrollments.student_id');
53
        }
54
55
        return DB::table('enrollments')->join('courses', 'enrollments.course_id', 'courses.id')->join('periods', 'courses.period_id', 'periods.id')->where('periods.year_id', $this->id)->whereIn('enrollments.status_id', ['1', '2']) // filter out cancelled enrollments, todo make this configurable.
56
        ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null)->distinct('student_id')->count('enrollments.student_id');
57
    }
58
}
59