Time and calendar: a stopwatch timer started before there is a ticket that goes to a new ticket through Quick ticket or onto an open one, the time you logged today and this week in the timers panel, reminders for calendar events and for work booked on your tickets by notification and optional email (db 4.0.6, a new Reminders cron job), and a Timeline tab on projects with bookings in time, now, start and due, clashes and the open tickets not booked yet

This commit is contained in:
johnnyq
2026-11-19 21:03:37 -05:00
parent c98fa4bc8a
commit 5af76f5d8e
26 changed files with 1132 additions and 33 deletions

View File

@@ -656,3 +656,121 @@ function projectNotifyManager($row, $manager_id) {
$action = escapeSql('/agent/' . projectUrl($row['project_id'], $client_id));
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Project', notification = '$text', notification_action = '$action', notification_client_id = $client_id, notification_user_id = $manager_id");
}
/*
* The project's bookings laid out in time, for its Timeline tab: each booked
* ticket a bar from its booking's start to its end, on a scale from the
* project's start (or its first booking) to its due date (or its last one) -
* by the day up to three weeks, by the week (starting on $first_day, 0 Sunday
* or 1 Monday) up to twenty, else by the month. An open booking that overlaps
* another open one of the same agent's on this project lists it in 'clashes'.
* Times are on the viewer's clock (functions/user_timezone.php). Open tickets
* with no booking come back in 'unbooked'. Positions are percentages of the scale.
*/
function projectTimeline($project, array $tickets, $first_day = 0) {
$shifted = timezoneShifted();
$view = function ($value) use ($shifted) {
return strtotime($shifted ? timezoneToView($value) : $value);
};
$rows = [];
$unbooked = [];
foreach ($tickets as $ticket) {
if (empty($ticket['ticket_schedule'])) {
if (ticketIsOpen($ticket)) {
$unbooked[] = $ticket;
}
continue;
}
$start = $view($ticket['ticket_schedule']);
$end = max($view(ticketScheduleEnd($ticket['ticket_schedule'], $ticket['ticket_schedule_end'] ?? null)), $start + 60);
$rows[] = ['ticket' => $ticket, 'start' => $start, 'end' => $end, 'clashes' => []];
}
$out = ['rows' => [], 'unbooked' => $unbooked, 'ticks' => [], 'markers' => [], 'scale' => '', 'clashes' => 0];
if (!$rows) {
return $out;
}
usort($rows, function ($a, $b) {
return ($a['start'] <=> $b['start']) ?: (intval($a['ticket']['ticket_number']) <=> intval($b['ticket']['ticket_number']));
});
// The same agent booked twice at once
foreach ($rows as $i => $a) {
$agent = intval($a['ticket']['ticket_assigned_to']);
if (!$agent || !ticketIsOpen($a['ticket'])) {
continue;
}
foreach ($rows as $j => $b) {
if ($j <= $i || intval($b['ticket']['ticket_assigned_to']) !== $agent || !ticketIsOpen($b['ticket'])) {
continue;
}
if ($a['start'] < $b['end'] && $b['start'] < $a['end']) {
$rows[$i]['clashes'][] = ticketReference($b['ticket']);
$rows[$j]['clashes'][] = ticketReference($a['ticket']);
}
}
}
// The scale - whole days, from the earliest of the project's start and its bookings to the latest of its due date and theirs
$now = $view(date('Y-m-d H:i:s'));
$points = array_merge(array_column($rows, 'start'), array_column($rows, 'end'));
$project_start = !empty($project['project_start']) ? strtotime($project['project_start']) : null;
$project_due = !empty($project['project_due']) ? strtotime($project['project_due']) : null;
foreach ([$project_start, $project_due] as $point) {
if ($point) {
$points[] = $point;
}
}
$from = strtotime(date('Y-m-d', min($points)));
$to = strtotime(date('Y-m-d', max($points)) . ' +1 day');
$days = max(1, intval(round(($to - $from) / 86400)));
if ($days <= 21) {
$scale = 'day';
} elseif ($days <= 140) {
$scale = 'week';
$from = strtotime(date('Y-m-d', $from) . ' -' . ((intval(date('w', $from)) - intval($first_day) + 7) % 7) . ' days');
$to = strtotime(date('Y-m-d', $to) . ' +' . ((intval($first_day) - intval(date('w', $to)) + 7) % 7) . ' days');
} else {
$scale = 'month';
$from = strtotime(date('Y-m-01', $from));
$to = strtotime(date('Y-m-01', $to) . (date('j', $to) === '1' ? '' : ' +1 month'));
}
$span = max(1, $to - $from);
$pct = function ($ts) use ($from, $span) {
return round(max(0, min(100, ($ts - $from) / $span * 100)), 3);
};
$step = ['day' => '+1 day', 'week' => '+1 week', 'month' => '+1 month'][$scale];
$format = ['day' => 'D j', 'week' => 'M j', 'month' => 'M Y'][$scale];
for ($tick = $from; $tick < $to; $tick = strtotime(date('Y-m-d', $tick) . " $step")) {
$out['ticks'][] = ['at' => $pct($tick), 'label' => date($format, $tick), 'weekend' => $scale === 'day' && in_array(date('w', $tick), ['0', '6'], true)];
}
// Each tick's room to the next - a label is cut short there rather than running into the next; past a dozen, every other one is labelled
$many = count($out['ticks']) > 12;
foreach ($out['ticks'] as $i => $tick) {
$out['ticks'][$i]['minor'] = $many && $i % 2 === 1;
$next = $out['ticks'][$i + ($many && $i % 2 === 0 ? 2 : 1)]['at'] ?? 100;
$out['ticks'][$i]['room'] = round(($out['ticks'][$i + 1]['at'] ?? 100) - $tick['at'], 3);
$out['ticks'][$i]['label_room'] = round($next - $tick['at'], 3);
}
if ($now >= $from && $now < $to) {
$out['markers'][] = ['at' => $pct($now), 'kind' => 'today', 'label' => 'Now'];
}
if ($project_start) {
$out['markers'][] = ['at' => $pct($project_start), 'kind' => 'start', 'label' => 'Start ' . date('M j', $project_start)];
}
if ($project_due) {
$out['markers'][] = ['at' => $pct(strtotime(date('Y-m-d', $project_due) . ' +1 day')), 'kind' => 'due', 'label' => 'Due ' . date('M j', $project_due)];
}
foreach ($rows as $row) {
$open = ticketIsOpen($row['ticket']);
$row['left'] = $pct($row['start']);
$row['width'] = max(0, $pct($row['end']) - $row['left']);
$row['state'] = !$open ? 'done' : ($row['end'] < $now ? 'late' : 'open');
$out['clashes'] += $row['clashes'] ? 1 : 0;
$out['rows'][] = $row;
}
$out['scale'] = $scale;
return $out;
}