Skip to content

Instantly share code, notes, and snippets.

@philharmonie
Last active December 28, 2022 12:34
Show Gist options
  • Save philharmonie/a86d58f3fd7f24fc0e238998a2c83702 to your computer and use it in GitHub Desktop.
Save philharmonie/a86d58f3fd7f24fc0e238998a2c83702 to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits;
use App\Models\Client;
use App\Models\Event;
use App\Models\Holiday;
use App\Models\Meeting;
use App\Models\MeetingType;
use App\Models\Presence;
use Carbon\Carbon;
use Illuminate\Support\Benchmark;
use RRule\RSet;
trait MeetingsTrait
{
public function getPresencesEvents($presences, $count = false, $queryPeriodStart, $queryPeriodEnd)
{
$benchmarks = [];
$events = null;
$benchmarks['fetchVacations'] = Benchmark::measure(function () use (&$events, $presences, $count, $queryPeriodStart, $queryPeriodEnd) {
$events = [];
$vacations = Presence::vacations()->approved()->dateRange($queryPeriodStart, $queryPeriodEnd)->get()->groupBy('user_id');
$meetings = Meeting::dateRange($queryPeriodStart, $queryPeriodEnd)->get()->groupBy('user_id');
$holidays = Holiday::dateRange($queryPeriodStart, $queryPeriodEnd)->get()->groupBy('user_id');
foreach ($presences as $i => $presence) {
$rset = new RSet();
$rset->addRRule($presence->toRrule());
// Remove Vacations
$userVacations = $vacations[$presence->user->id] ?? [];
foreach ($userVacations as $vacation) {
$rset->addExRule($vacation->toExDate($vacation['start'], $vacation['end']->addDay()));
}
// Remove Meetings
$userMeetings = $meetings[$presence->user->id] ?? [];
foreach ($userMeetings as $meeting) {
$rset->addExDate($meeting->start->addHour());
}
// Remove Holidays
$userHolidays = $holidays[$presence->user->id] ?? [];
foreach ($userHolidays as $holiday) {
$rset->addExRule($holiday->toExDate(Carbon::parse($holiday->date), Carbon::parse($holiday->date)->addDay()));
};
$occurences = $rset
->getOccurrencesBetween(
$queryPeriodStart,
$queryPeriodEnd
);
// Create events
foreach ($occurences as $r) {
$event = [
'model_id' => $presence->id,
'title' => $presence->user->name . ($presence->location ? ' - ' . $presence->location->name : ''),
'start' => $r->format('Y-m-d H:i:s'),
'end' => Carbon::parse($r)->addHour(),
'user_id' => $presence->user_id,
'location_id' => $presence->location_id,
'online' => $presence->online,
'allDay' => $presence->allDay,
'classNames' => 'presence',
];
$events[] = $event;
}
}
if ($count) {
$events = $this->countHours($events);
}
});
info($benchmarks);
return $events;
}
public function countHours($events)
{
// create a map of dates to event counts
$eventCountsByDate = [];
foreach ($events as $event) {
$date = date('Y-m-d', strtotime($event['start']));
if (!isset($eventCountsByDate[$date])) {
$eventCountsByDate[$date] = 0;
}
$eventCountsByDate[$date]++;
}
// create a collection of counted events
$countedEvents = [];
foreach ($eventCountsByDate as $date => $eventCount) {
$countedEvent = [
'title' => $eventCount,
'start' => $date,
'end' => $date,
'allDay' => true,
];
$countedEvents[] = $countedEvent;
}
return $countedEvents;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment