Passed
Push — master ( ef8d4f...ddeb28 )
by Thomas
12:09
created

Attendance::event()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use App\Jobs\WatchAttendance;
6
use App\Mail\PendingAttendanceReminder;
7
use Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Mail;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
13
/**
14
 * App\Models\Attendance
15
 *
16
 * @property int $id
17
 * @property int $student_id
18
 * @property int $event_id
19
 * @property int $attendance_type_id
20
 * @property \Illuminate\Support\Carbon|null $created_at
21
 * @property \Illuminate\Support\Carbon|null $updated_at
22
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Activitylog\Models\Activity[] $activities
23
 * @property-read int|null $activities_count
24
 * @property-read \App\Models\AttendanceType $attendance_type
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Contact[] $contacts
26
 * @property-read int|null $contacts_count
27
 * @property-read \App\Models\Event $event
28
 * @property-read \App\Models\Student $student
29
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance newModelQuery()
30
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance newQuery()
31
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance query()
32
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereAttendanceTypeId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereCreatedAt($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereEventId($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereId($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereStudentId($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|Attendance whereUpdatedAt($value)
38
 * @mixin \Eloquent
39
 */
40
class Attendance extends Model
41
{
42
    use LogsActivity;
43
44
    protected $guarded = ['id'];
45
    protected $with = ['attendance_type'];
46
    protected static $logUnguarded = true;
47
48
    protected static function boot()
49
    {
50
        parent::boot();
51
52
        // when an attendance record is added, we check if this is an absence
53
        static::saved(function (self $attendance) {
54
            if ($attendance->attendance_type_id == 4) { // todo move to configurable settings
55
                // Log::info('Absence marked for student '.$attendance->student->name);
56
                // will check the record again and send a notification if it hasn't changed
57
                WatchAttendance::dispatch($attendance)
58
                ->delay(now()); // todo move to configurable settings
59
            }
60
        });
61
    }
62
63
    /** RELATIONS */
64
    public function student()
65
    {
66
        return $this->belongsTo(Student::class);
67
    }
68
69
    /** Additional data = contact information associated to the student */
70
    public function contacts()
71
    {
72
        return $this->hasMany(Contact::class, 'student_id', 'id');
73
    }
74
75
    /** event = one instance of the course */
76
    public function event()
77
    {
78
        return $this->belongsTo(Event::class);
79
    }
80
81
    public function attendance_type()
82
    {
83
        return $this->belongsTo(AttendanceType::class);
84
    }
85
86
    /** METHODS */
87
88
    /**
89
     * get absences count per student
90
     * this is useful for monitoring the absences.
91
     */
92
    public function get_absence_count_per_student(Period $period)
93
    {
94
        // return attendance records for period
95
        $coursesIds = $period->courses->pluck('id');
96
        $eventsIds = Event::whereIn('course_id', $coursesIds)->pluck('id');
97
98
        return self::with('event.course')->with('student')->whereIn('event_id', $eventsIds)->whereIn('attendance_type_id', [3, 4])->get()->groupBy('student_id');
99
    }
100
101
    public function getStudentNameAttribute() : string
102
    {
103
        return $this->student->name ?? '';
104
    }
105
}
106