-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpar-reports.php
More file actions
224 lines (209 loc) · 12.1 KB
/
Copy pathpar-reports.php
File metadata and controls
224 lines (209 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* NewUI v4.0 — PAR Reports (Phase 16-followup, 2026-06-11)
*
* Two reports:
* 1. Per-incident timeline — every cycle for a specific ticket
* 2. Per-agency compliance — counts + ack rates over a date window
*
* Read-only; gated to anyone who can view incidents (RBAC).
*/
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/inc/i18n.php';
require_once __DIR__ . '/inc/rbac.php';
// 2026-07-04 (GH #13) — pick the session profile matching the
// client's cookie (TCADMOBILE vs PHPSESSID). Without this, a
// browser holding a mobile cookie opens an empty desktop session
// here and bounces to login -> redirect loop.
require_once __DIR__ . '/inc/session-bootstrap.php';
sess_bootstrap_auto();
session_start();
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once __DIR__ . '/inc/force-pw-change.php';
force_pw_change_redirect();
// Allow anyone who can manage PAR OR is an admin. (action.view_incident
// dropped 2026-08-15, tools/rbac_permission_audit.php -- never seeded.)
if (!rbac_can('action.manage_par') && !is_admin()) {
header('Location: index.php?err=forbidden');
exit;
}
$user = e($_SESSION['user']);
$level = current_role_name();
$theme = $_SESSION['day_night'] ?? 'Day';
$bs_theme = ($theme === 'Night') ? 'dark' : 'light';
$csrf = csrf_token();
$active_page = 'par-reports';
?>
<!DOCTYPE html>
<html lang="<?php echo e(i18n_lang()); ?>" data-bs-theme="<?php echo $bs_theme; ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="<?php echo e($csrf); ?>">
<title>PAR Reports — <?php echo e(t('login.title', 'Tickets NewUI')); ?> <?php echo newui_version(); ?></title>
<link rel="stylesheet" href="assets/vendor/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="assets/vendor/bootstrap/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/dashboard.css?v=<?php echo asset_v('assets/css/dashboard.css'); ?>">
</head>
<body>
<?php include_once NEWUI_ROOT . '/inc/navbar.php'; ?>
</header>
<div class="container-fluid p-3">
<div class="d-flex align-items-center justify-content-between mb-3">
<h5 class="mb-0">
<i class="bi bi-shield-check text-primary me-2"></i>PAR Reports
</h5>
<a href="index.php" class="btn btn-sm btn-outline-secondary">
<i class="bi bi-arrow-left me-1"></i>Dashboard
</a>
</div>
<ul class="nav nav-tabs mb-3" id="parReportTabs" role="tablist">
<li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tab-compliance" type="button">Agency Compliance</button></li>
<li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-timeline" type="button">Incident Timeline</button></li>
</ul>
<div class="tab-content">
<!-- ── Per-agency compliance ───────────────────────────────────── -->
<div class="tab-pane fade show active" id="tab-compliance">
<div class="card">
<div class="card-body">
<div class="row g-2 align-items-end mb-3">
<div class="col-md-3">
<label class="form-label form-label-sm" for="compFrom">From</label>
<input type="date" class="form-control form-control-sm" id="compFrom">
</div>
<div class="col-md-3">
<label class="form-label form-label-sm" for="compTo">To</label>
<input type="date" class="form-control form-control-sm" id="compTo">
</div>
<div class="col-md-2">
<button type="button" class="btn btn-sm btn-primary" id="btnRunCompliance">
<i class="bi bi-play-fill me-1"></i>Run
</button>
</div>
<div class="col-md-4 text-end">
<button type="button" class="btn btn-sm btn-outline-secondary" id="btnExportCompliance" disabled>
<i class="bi bi-download me-1"></i>CSV
</button>
</div>
</div>
<div id="complianceResults" class="small text-body-secondary">
Pick a date range and click Run.
</div>
</div>
</div>
</div>
<!-- ── Per-incident timeline ───────────────────────────────────── -->
<div class="tab-pane fade" id="tab-timeline">
<div class="card">
<div class="card-body">
<div class="row g-2 align-items-end mb-3">
<div class="col-md-3">
<label class="form-label form-label-sm" for="tlTicket">Incident #</label>
<input type="text" class="form-control form-control-sm" id="tlTicket"
placeholder="e.g. 26-0091"
title="Enter the incident number as shown on the incident (not the internal database id).">
</div>
<div class="col-md-2">
<button type="button" class="btn btn-sm btn-primary" id="btnRunTimeline">
<i class="bi bi-play-fill me-1"></i>Run
</button>
</div>
</div>
<div id="timelineResults" class="small text-body-secondary">
Enter an incident # and click Run.
</div>
</div>
</div>
</div>
</div>
</div>
<input type="hidden" id="csrfToken" value="<?php echo e($csrf); ?>">
<script src="assets/vendor/bootstrap/bootstrap.bundle.min.js"></script>
<script>
(function () {
'use strict';
function esc(s) {
if (s === null || s === undefined) return '';
var d = document.createElement('div');
d.textContent = String(s);
return d.innerHTML;
}
// Default to last 30 days
var today = new Date();
var from = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 30);
function ymd(d) { return d.getFullYear() + '-' + ('0'+(d.getMonth()+1)).slice(-2) + '-' + ('0'+d.getDate()).slice(-2); }
document.getElementById('compFrom').value = ymd(from);
document.getElementById('compTo').value = ymd(today);
document.getElementById('btnRunCompliance').addEventListener('click', function () {
var f = document.getElementById('compFrom').value;
var t = document.getElementById('compTo').value;
var results = document.getElementById('complianceResults');
results.innerHTML = '<div class="text-center py-3"><div class="spinner-border spinner-border-sm"></div></div>';
fetch('api/par.php?action=report_compliance&from=' + encodeURIComponent(f) + '&to=' + encodeURIComponent(t),
{ credentials: 'same-origin' })
.then(function (r) { return r.json(); }).then(function (data) {
if (data.error) { results.innerHTML = '<div class="alert alert-danger small">' + esc(data.error) + '</div>'; return; }
var s = data.summary || {};
var html = '<div class="row g-2 mb-3">' +
'<div class="col-md-3"><div class="card border-info"><div class="card-body py-2"><div class="small text-body-secondary">Cycles</div><div class="display-6">' + (s.total_cycles || 0) + '</div></div></div></div>' +
'<div class="col-md-3"><div class="card border-success"><div class="card-body py-2"><div class="small text-body-secondary">Unit acks</div><div class="display-6 text-success">' + (s.acks || 0) + '</div></div></div></div>' +
'<div class="col-md-3"><div class="card border-danger"><div class="card-body py-2"><div class="small text-body-secondary">Missed</div><div class="display-6 text-danger">' + (s.missed || 0) + '</div></div></div></div>' +
'<div class="col-md-3"><div class="card border-warning"><div class="card-body py-2"><div class="small text-body-secondary">Ack rate</div><div class="display-6">' + (s.ack_rate_pct || 0) + '%</div></div></div></div>' +
'</div>';
var rows = data.by_kind || [];
html += '<h6 class="small fw-semibold">By cycle kind</h6>';
html += '<table class="table table-sm"><thead><tr><th>Kind</th><th class="text-end">Cycles</th><th class="text-end">Acked</th><th class="text-end">Missed</th><th class="text-end">Ack %</th></tr></thead><tbody>';
for (var i = 0; i < rows.length; i++) {
var r = rows[i];
html += '<tr><td>' + esc(r.kind) + '</td>' +
'<td class="text-end">' + r.cycles + '</td>' +
'<td class="text-end text-success">' + r.acked + '</td>' +
'<td class="text-end text-danger">' + r.missed + '</td>' +
'<td class="text-end">' + r.ack_rate_pct + '%</td></tr>';
}
html += '</tbody></table>';
results.innerHTML = html;
document.getElementById('btnExportCompliance').disabled = false;
document.getElementById('btnExportCompliance').onclick = function () {
window.location.href = 'api/par.php?action=report_compliance&format=csv&from=' + encodeURIComponent(f) + '&to=' + encodeURIComponent(t);
};
});
});
document.getElementById('btnRunTimeline').addEventListener('click', function () {
// GH#51 — send the dispatcher's own case number as typed; the
// server resolves it to the internal ticket id.
var tid = (document.getElementById('tlTicket').value || '').trim();
var results = document.getElementById('timelineResults');
if (!tid) { results.innerHTML = '<div class="alert alert-warning small">Enter an incident number.</div>'; return; }
results.innerHTML = '<div class="text-center py-3"><div class="spinner-border spinner-border-sm"></div></div>';
fetch('api/par.php?action=history&ticket=' + encodeURIComponent(tid), { credentials: 'same-origin' })
.then(function (r) { return r.json(); }).then(function (data) {
if (data.error) { results.innerHTML = '<div class="alert alert-danger small">' + esc(data.error) + '</div>'; return; }
var cycles = data.history || [];
var displayNum = data.incident_number || tid;
if (cycles.length === 0) {
results.innerHTML = '<div class="alert alert-info small">No PAR cycles for incident #' + esc(displayNum) + '.</div>';
return;
}
var html = '<h6 class="small fw-semibold mb-2">Incident #' + esc(displayNum) + ' — ' + cycles.length + ' cycle(s)</h6>';
html += '<table class="table table-sm"><thead><tr><th>Started</th><th>Kind</th><th>Status</th><th class="text-end">Units</th><th class="text-end">Acked</th><th class="text-end">Missed</th></tr></thead><tbody>';
for (var i = 0; i < cycles.length; i++) {
var c = cycles[i];
html += '<tr><td>' + esc(c.initiated_at) + '</td>' +
'<td>' + esc(c.initiated_kind) + '</td>' +
'<td>' + esc(c.status) + '</td>' +
'<td class="text-end">' + c.units + '</td>' +
'<td class="text-end text-success">' + c.acked + '</td>' +
'<td class="text-end ' + (c.missed > 0 ? 'text-danger' : '') + '">' + c.missed + '</td></tr>';
}
html += '</tbody></table>';
results.innerHTML = html;
});
});
})();
</script>
</body>
</html>