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 | /** |
||
10 | * @mixin IdeHelperYear |
||
11 | */ |
||
12 | class Year extends Model |
||
13 | { |
||
14 | use CrudTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | |||
16 | public $timestamps = false; |
||
17 | |||
18 | protected $guarded = ['id']; |
||
19 | |||
20 | public function periods() |
||
21 | { |
||
22 | return $this->hasMany(Period::class); |
||
23 | } |
||
24 | |||
25 | public function getPartnershipsAttribute() |
||
26 | { |
||
27 | return Course::whereIn('period_id', $this->periods->pluck('id'))->pluck('partner_id')->unique()->count(); |
||
28 | } |
||
29 | |||
30 | public function studentCount($gender = null) |
||
31 | { |
||
32 | if (in_array($gender, [1, 2])) { |
||
33 | return DB::table('enrollments') |
||
34 | ->join('courses', 'enrollments.course_id', 'courses.id') |
||
35 | ->join('periods', 'courses.period_id', 'periods.id') |
||
36 | ->join('students', 'enrollments.student_id', 'students.id') |
||
37 | ->where('periods.year_id', $this->id) |
||
38 | ->whereIn('enrollments.status_id', ['1', '2']) // filter out cancelled enrollments, todo make this configurable. |
||
39 | ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null) |
||
40 | ->where('students.gender_id', $gender) |
||
41 | ->distinct('student_id')->count('enrollments.student_id'); |
||
42 | } |
||
43 | |||
44 | if ($gender === 0) { |
||
45 | return DB::table('enrollments') |
||
46 | ->join('courses', 'enrollments.course_id', 'courses.id') |
||
47 | ->join('periods', 'courses.period_id', 'periods.id') |
||
48 | ->join('students', 'enrollments.student_id', 'students.id') |
||
49 | ->where('periods.year_id', $this->id) |
||
50 | ->whereIn('enrollments.status_id', ['1', '2']) // filter out cancelled enrollments, todo make this configurable. |
||
51 | ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null) |
||
52 | ->where(function ($query) { |
||
53 | return $query->where('students.gender_id', 0)->orWhereNull('students.gender_id'); |
||
54 | }) |
||
55 | ->distinct('student_id')->count('enrollments.student_id'); |
||
56 | } |
||
57 | |||
58 | 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. |
||
59 | ->where('enrollments.parent_id', null)->where('enrollments.deleted_at', null)->distinct('student_id')->count('enrollments.student_id'); |
||
60 | } |
||
61 | } |
||
62 |