CourseTime::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Activitylog\Traits\LogsActivity;
8
9
/**
10
 * @mixin IdeHelperCourseTime
11
 */
12
class CourseTime extends Model
13
{
14
    use LogsActivity;
15
16
    protected static function boot()
17
    {
18
        parent::boot();
19
20
        // when a coursetime is added, we should create corresponding events
21
        static::created(function ($coursetime) {
22
            $coursetime->create_events();
23
        });
24
25
        // when a coursetime is deleted, we should delete all associated future events
26
        static::deleted(function ($coursetime) {
27
            $coursetime->events()->delete();
28
            // todo delete only future events
29
        });
30
    }
31
32
    public $timestamps = false;
33
34
    protected $guarded = ['id'];
35
36
    protected static bool $logUnguarded = true;
37
38
    public function identifiableAttribute()
39
    {
40
        return $this->day;
41
    }
42
43
    /*
44
    |--------------------------------------------------------------------------
45
    | FUNCTIONS
46
    |--------------------------------------------------------------------------
47
    */
48
49
    public function create_events()
50
    {
51
        $today = Carbon::parse($this->course->start_date)->startOfDay();
52
        $end = Carbon::parse($this->course->end_date)->endOfDay();
53
54
        // for each day in the course period span
55
        while ($today <= $end) {
56
57
                // if today is a day of class, create the event
58
            if ($this->day == $today->format('w')) {
59
                Event::create([
60
                    'course_id' => $this->course->id,
61
                    'teacher_id' => $this->course->teacher_id,
62
                    'room_id' => $this->course->room_id,
63
                    'start' => $today->setTimeFromTimeString($this->start)->toDateTimeString(),
64
                    'end' => $today->setTimeFromTimeString($this->end)->toDateTimeString(),
65
                    'name' => $this->course->name,
66
                    'course_time_id' => $this->id,
67
                    'exempt_attendance' => $this->course->exempt_attendance,
68
                ]);
69
            }
70
            $today->addDay();
71
        }
72
    }
73
74
    /*
75
    |--------------------------------------------------------------------------
76
    | RELATIONS
77
    |--------------------------------------------------------------------------
78
    */
79
80
    /** events = class sessions.
81
     * An Event is related to the CourseTime that generated it. This is needed to update related events when updating the course schedule.
82
     */
83
    public function events()
84
    {
85
        return $this->hasMany(Event::class);
86
    }
87
88
    public function course()
89
    {
90
        return $this->belongsTo(Course::class);
91
    }
92
93
    /*
94
    |--------------------------------------------------------------------------
95
    | SCOPES
96
    |--------------------------------------------------------------------------
97
    */
98
99
    /*
100
    |--------------------------------------------------------------------------
101
    | ACCESORS
102
    |--------------------------------------------------------------------------
103
    */
104
105
    /*
106
    |--------------------------------------------------------------------------
107
    | MUTATORS
108
    |--------------------------------------------------------------------------
109
    */
110
}
111