$days The shown days, Y-m-d, consecutive. * @param list $people Rows in order; id null is the "no assignee" row. * @param list $tasks * @param array> $minutesByUser user id => Y-m-d => booked minutes. * @return list */ public static function build(array $days, array $people, array $tasks, array $minutesByUser): array { $rows = []; foreach ($people as $person) { $bars = []; foreach ($tasks as $task) { $bar = self::bar($task, $person['id'], $days); if ($bar !== null) { $bars[] = $bar; } } [$bars, $lanes] = self::stack($bars); $minutes = []; foreach ($days as $day) { $minutes[$day] = $person['id'] === null ? 0 : ($minutesByUser[$person['id']][$day] ?? 0); } $rows[] = new CalendarRow($person['id'], $person['name'], $minutes, $bars, $lanes); } return $rows; } /** * @param list $days */ private static function bar(CalendarTask $task, ?int $userId, array $days): ?CalendarBar { if ($task->plannedFrom === null || $task->plannedTo === null || $days === []) { return null; } if ($userId === null) { if ($task->assignments !== []) { return null; } $from = $task->plannedFrom; $to = $task->plannedTo; $minutes = 0; } else { $assignment = $task->assignmentOf($userId); if ($assignment === null) { return null; } $from = $task->plannedFrom; $to = max($from, $assignment->to ?? $task->plannedTo); $minutes = $assignment->minutes; } $first = $days[0]; $last = $days[count($days) - 1]; if ($to < $first || $from > $last) { return null; } $start = $from < $first ? 0 : (int) array_search($from, $days, true); $end = $to > $last ? count($days) - 1 : (int) array_search($to, $days, true); $late = $task->status !== TaskStatus::Done && $task->dueOn !== null && $to > $task->dueOn; return new CalendarBar($task, $userId, $from, $to, $minutes, $start, $end - $start + 1, 0, $late); } /** * Puts each bar on the first lane where it does not overlap another. * * @param list $bars * @return array{0: list, 1: int} */ private static function stack(array $bars): array { usort($bars, fn (CalendarBar $a, CalendarBar $b): int => [$a->start, -$a->span, $a->task->taskId] <=> [$b->start, -$b->span, $b->task->taskId]); $laneEnds = []; $stacked = []; foreach ($bars as $bar) { $lane = 0; while (isset($laneEnds[$lane]) && $laneEnds[$lane] >= $bar->start) { $lane++; } $laneEnds[$lane] = $bar->start + $bar->span - 1; $stacked[] = new CalendarBar($bar->task, $bar->userId, $bar->from, $bar->to, $bar->minutes, $bar->start, $bar->span, $lane, $bar->late); } return [$stacked, max(1, count($laneEnds))]; } }