1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Listeners; |
4
|
|
|
|
5
|
|
|
use App\Events\CourseUpdated; |
6
|
|
|
use App\Models\Event; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
|
10
|
|
|
class UpdateCourseEvents |
11
|
|
|
{ |
12
|
|
|
public function handle(CourseUpdated $event) |
13
|
|
|
{ |
14
|
|
|
$course = $event->course; |
15
|
|
|
|
16
|
|
|
// if course dates have changed, sync all events |
17
|
|
|
if ($course->isDirty('start_date') || $course->isDirty('end_date') || $course->relationLoaded('times') && $course->times->isDirty()) { |
18
|
|
|
Log::info('cleaning the events after course date change'); |
19
|
|
|
|
20
|
|
|
if ($course->events->count() > 0) { |
21
|
|
|
// create events before first existing event and after course start |
22
|
|
|
$firstEvent = $course->events()->reorder('start')->first(); |
23
|
|
|
$firstEventDate = Carbon::parse($firstEvent->start)->startOfDay(); |
24
|
|
|
$lastEvent = $course->events()->reorder('start', 'desc')->first(); |
25
|
|
|
$lastEventDate = Carbon::parse($lastEvent->start)->endOfDay(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$courseStartDate = Carbon::parse($course->start_date)->startOfDay(); |
29
|
|
|
$courseEndDate = Carbon::parse($course->end_date)->startOfDay(); |
30
|
|
|
|
31
|
|
|
// delete events before course start date |
32
|
|
|
Event::where('course_id', $course->id)->where('start', '<', $courseStartDate->startOfDay())->delete(); |
33
|
|
|
// delete events after course end date |
34
|
|
|
Event::where('course_id', $course->id)->where('end', '>', $courseEndDate->endOfDay())->delete(); |
35
|
|
|
|
36
|
|
|
// for each day before the first event |
37
|
|
|
while ($courseStartDate < $courseEndDate) { |
38
|
|
|
// Event already exists |
39
|
|
|
if (isset($firstEventDate) && isset($lastEventDate) && ($firstEventDate <= $courseStartDate) && ($courseStartDate <= $lastEventDate)) { |
40
|
|
|
$courseStartDate->addDay(); |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// if there is a coursetime for today, create the event |
45
|
|
|
if ($course->times->contains('day', $courseStartDate->format('w'))) { |
46
|
|
|
Event::create([ |
47
|
|
|
'course_id' => $course->id, |
48
|
|
|
'teacher_id' => $course->teacher_id, |
49
|
|
|
'room_id' => $course->room_id, |
50
|
|
|
'start' => $courseStartDate->setTimeFromTimeString($course->times->where('day', $courseStartDate->format('w'))->first()->start)->toDateTimeString(), |
51
|
|
|
'end' => $courseStartDate->setTimeFromTimeString($course->times->where('day', $courseStartDate->format('w'))->first()->end)->toDateTimeString(), |
52
|
|
|
'name' => $course->name, |
53
|
|
|
'course_time_id' => $course->times->where('day', $courseStartDate->format('w'))->first()->id, |
54
|
|
|
'exempt_attendance' => $course->exempt_attendance, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
$courseStartDate->addDay(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// update course events with new room, teacher and name |
62
|
|
|
Event::where('course_id', $course->id)->update(['room_id' => $course->room_id]); |
63
|
|
|
Event::where('course_id', $course->id)->update(['teacher_id' => $course->teacher_id]); |
64
|
|
|
Event::where('course_id', $course->id)->update(['name' => $course->name]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|