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; |
|
|
|
|
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
|
|
|
|