forked from SimpleMachines/SimpleDesk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSDPluginStats.php
More file actions
416 lines (351 loc) · 11 KB
/
SDPluginStats.php
File metadata and controls
416 lines (351 loc) · 11 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**************************************************************
* Simple Desk Project - www.simpledesk.net *
***************************************************************
* An advanced help desk modification built on SMF *
***************************************************************
* *
* * Copyright 2025 - SimpleDesk.net *
* *
* This file and its contents are subject to the license *
* included with this distribution, license.txt, which *
* states that this software is New BSD Licensed. *
* Any questions, please contact SimpleDesk.net *
* *
***************************************************************
* SimpleDesk Version: 2.1.0 *
* File Info: SDPluginStats.php *
**************************************************************/
/**
* This file handles the replacement front page.
*
* @package plugin-stats
* @since 2.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
/*
* Called from the SMF admin menu hook
*/
function shd_stats_adminmenu(&$admin_areas)
{
global $context, $modSettings, $txt;
if (allowedTo('admin_forum') && !empty($modSettings['shdp_enable_stats']))
$admin_areas['helpdesk_info']['areas']['helpdesk_info']['subsections']['stats'] = array($txt['shdp_stats']);
}
function shd_stats_hdadmininfo(&$subactions)
{
global $context, $modSettings, $txt;
if (!allowedTo('admin_forum') || empty($modSettings['shdp_enable_stats']))
return;
$subactions['stats'] = [
'function' => 'shd_stats_source',
'icon' => '../reports.png',
'title' => $txt['shdp_stats']
];
$context[$context['admin_menu_name']]['tab_data']['tabs']['stats'] = $subactions['stats'];
}
/*
* The options screen for the stats
*/
function shd_stats_admin(&$config_vars)
{
$config_vars[] = '';
$config_vars[] = ['check', 'shdp_enable_stats'];
}
/*
* The source file for the actual stats page.
*/
function shd_stats_source()
{
global $modSettings, $context;
// All possible stat info.
$stats = [
'status',
'today',
'most',
'average',
'totals',
'urgency',
'history',
];
// Loop out the stat info.
$context['shd_stats'] = [];
foreach ($stats as $function)
{
$func = 'shd_stats_' . $function;
$context['shd_stats'][$function] = $func();
}
loadTemplate('sd_plugins_template/SDPluginStats');
}
// This gets our ticket status info.
function shd_stats_status()
{
global $smcFunc;
$status = [
TICKET_STATUS_NEW => 0,
TICKET_STATUS_PENDING_STAFF => 0,
TICKET_STATUS_PENDING_USER => 0,
TICKET_STATUS_CLOSED => 0,
TICKET_STATUS_WITH_SUPERVISOR => 0,
TICKET_STATUS_ESCALATED => 0,
TICKET_STATUS_DELETED => 0
];
// Collect up some numbers.
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_ticket) AS count, status
FROM {db_prefix}helpdesk_tickets
WHERE status IN ({array_int:status})
GROUP BY status',
[
'status' => array_keys($status)
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$status[$row['status']] = $row['count'];
$smcFunc['db_free_result']($request);
$status['total_open'] = $status[TICKET_STATUS_NEW] + $status[TICKET_STATUS_PENDING_STAFF] + $status[TICKET_STATUS_PENDING_USER] + $status[TICKET_STATUS_WITH_SUPERVISOR] + $status[TICKET_STATUS_ESCALATED];
$status['total_closed'] = $status[TICKET_STATUS_CLOSED] + $status[TICKET_STATUS_DELETED];
$status['total_total'] = $status['total_open'] + $status['total_closed'];
// We are fast.
if ($status['total_open'] == $status['total_closed'])
$status['ratio'] = '1:1';
elseif ($status['total_open'] == 0)
$status['ratio'] = '0:' . $status['total_closed'];
elseif ($status['total_closed'] == 0)
$status['ratio'] = $status['total_open'] . ':0';
elseif ($status['total_open'] > $status['total_closed'])
$status['ratio'] = round($status['total_open'] / $status['total_closed']) . ':1';
elseif ($status['total_open'] < $status['total_closed'])
$status['ratio'] = '1:' . round($status['total_closed'] / $status['total_open']);
return $status;
}
// This gets our ticket open/closed info.
function shd_stats_today()
{
global $smcFunc;
$actions = [
// These are open tickets.
'newticket' => TICKET_STATUS_NEW,
'unresolve' => TICKET_STATUS_NEW,
// These are resolved tickets.
'resolve' => TICKET_STATUS_CLOSED,
];
$totals = [
TICKET_STATUS_NEW => 0,
TICKET_STATUS_CLOSED => 0,
];
$request = $smcFunc['db_query']('', '
SELECT COUNT(la.id_ticket) AS count, t.status
FROM {db_prefix}helpdesk_log_action AS la
INNER JOIN {db_prefix}helpdesk_tickets AS t ON (la.id_ticket = t.id_ticket)
WHERE la.action IN ({array_string:actions})
AND la.log_time > {int:today}
GROUP BY la.action, t.status',
[
'actions' => array_keys($actions),
// we could use strtotime from a date(n j Y), but this seems safer calculations
'today' => mktime(0, 0, 0, date('n'), date('j'), date('Y'))
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$totals[$row['status']] = $row['count'];
$smcFunc['db_free_result']($request);
return $totals;
}
// This gets our ticket open/closed info.
function shd_stats_most()
{
global $smcFunc;
$actions = [
TICKET_STATUS_NEW => ['newticket'],
TICKET_STATUS_CLOSED => ['resolve']
];
$most = [
TICKET_STATUS_NEW => [0, 0],
TICKET_STATUS_CLOSED => [0, 0],
];
foreach ($actions as $id_action => $action)
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(la.id_ticket) AS count, t.status, MAX(log_time) AS log_time
FROM {db_prefix}helpdesk_log_action AS la
INNER JOIN {db_prefix}helpdesk_tickets AS t ON (la.id_ticket = t.id_ticket)
WHERE
la.action IN ({array_string:actions})
AND t.status = {int:status}
GROUP BY
unix_timestamp() - log_time < {int:24hrs} AND unix_timestamp() - log_time + {int:24hrs} > 0
ORDER BY count DESC
LIMIT 1',
[
'actions' => $action,
'status' => $id_action,
'24hrs' => 86400,
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$most[$row['status']] = array($row['count'], $row['log_time']);
$smcFunc['db_free_result']($request);
}
return $most;
}
// This gets our ticket open/closed info.
function shd_stats_average()
{
global $smcFunc;
$actions = [
TICKET_STATUS_NEW => ['newticket', 'unresolve'],
TICKET_STATUS_CLOSED => ['resolve'],
TICKET_STATUS_PENDING_STAFF => ['assign'],
];
$average = [
TICKET_STATUS_NEW => 0,
TICKET_STATUS_CLOSED => 0,
TICKET_STATUS_PENDING_STAFF => 0,
];
foreach ($actions as $action)
{
$request = $smcFunc['db_query']('', '
SELECT AVG(la.id_ticket) AS count, t.status
FROM {db_prefix}helpdesk_log_action AS la
INNER JOIN {db_prefix}helpdesk_tickets AS t ON (la.id_ticket = t.id_ticket)
WHERE la.action IN ({array_string:actions})
GROUP BY t.status',
[
'actions' => $action,
'24hrs' => 86400,
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$average[$row['status']] = $row['count'];
$smcFunc['db_free_result']($request);
}
return $average;
}
// This gets our user info.
function shd_stats_totals()
{
global $smcFunc;
// Count Admins separately for now.!
$admins = [];
$request = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE id_group = {int:admin} OR FIND_IN_SET({int:admin}, additional_groups)',
[
'admin' => 1
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$admins[] = $row['id_member'];
$smcFunc['db_free_result']($request);
// @TODO: This most likely will filesort and be slow on large helpdesks.
$request = $smcFunc['db_query']('', '
SELECT COUNT(mem.id_member) AS count, hdr.template
FROM {db_prefix}members AS mem
INNER JOIN {db_prefix}helpdesk_role_groups AS hdrg ON (mem.id_group = hdrg.id_group OR FIND_IN_SET(hdrg.id_group, mem.additional_groups))
INNER JOIN {db_prefix}helpdesk_roles AS hdr ON (hdrg.id_role = hdr.id_role)
WHERE mem.id_member NOT IN ({array_int:admins})
GROUP BY hdr.template',
[
'admins' => $admins
]);
$totals = [
ROLE_USER => 0,
ROLE_STAFF => 0,
// ROLE_SUPERVISOR => 0,
ROLE_ADMIN => 0
];
while ($row = $smcFunc['db_fetch_assoc']($request))
$totals[$row['template']] = $row['count'];
$smcFunc['db_free_result']($request);
// Add in the admins.
if (empty($totals[ROLE_ADMIN]))
$totals[ROLE_ADMIN] += count($admins);
return $totals;
}
// This gets our urgency.
function shd_stats_urgency()
{
global $smcFunc;
$urgency = [
TICKET_URGENCY_LOW => 0,
TICKET_URGENCY_MEDIUM => 0,
TICKET_URGENCY_HIGH => 0,
TICKET_URGENCY_VHIGH => 0,
TICKET_URGENCY_SEVERE => 0,
TICKET_URGENCY_CRITICAL => 0,
];
// We reformat it now.
$urgency = [
'open' => $urgency,
'closed' => $urgency
];
// Open or closed is all we need to know.
$status = [
TICKET_STATUS_NEW => 'open',
TICKET_STATUS_PENDING_STAFF => 'open',
TICKET_STATUS_PENDING_USER => 'open',
TICKET_STATUS_CLOSED => 'closed',
TICKET_STATUS_WITH_SUPERVISOR => 'open',
TICKET_STATUS_ESCALATED => 'open',
TICKET_STATUS_DELETED => 'closed',
TICKET_STATUS_HOLD => 'hold'
];
// Collect up some numbers.
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_ticket) AS count, urgency, status
FROM {db_prefix}helpdesk_tickets
WHERE urgency IN ({array_string:urgency})
GROUP BY urgency, status',
[
'urgency' => array_keys($urgency)
]);
while ($row = $smcFunc['db_fetch_assoc']($request))
$urgency[$status[$row['status']]][$row['urgency']] = $row['count'];
$smcFunc['db_free_result']($request);
return $urgency;
}
// Time to become Archeologists.
function shd_stats_history()
{
global $smcFunc;
// What tasks we want to perform.
$tasks = [
'open' => 0,
'resolved' => 0,
'assigned' => 0,
'reopen' => 0,
'child' => [],
];
$conversion = [
'newticket' => 'open',
'assign' => 'assigned',
'resolve' => 'resolved',
];
// @TODO: In future, use ajax to support selecting years/months totals
$request = $smcFunc['db_query']('', '
SELECT log_time, action
FROM {db_prefix}helpdesk_log_action
WHERE
action IN ({array_string:valid_actions})',
[
'valid_actions' => ['newticket', 'assign', 'reopen', 'resolve', 'close']
]);
$history = [];
while ($row = $smcFunc['db_fetch_assoc']($request))
{
list($year, $month, $day) = explode(' ', date('Y n d', $row['log_time']));
// If only we had to do this once a year.
if (!isset($history[$year]))
$history[$year] = $tasks;
$history[$year][$conversion[$row['action']]] += 1;
// Now we do the monthly stats
if (!isset($history[$year]['child'][$month]))
$history[$year]['child'][$month] = $tasks;
$history[$year]['child'][$month][$conversion[$row['action']]] += 1;
// For day we just simply do it.
if (!isset($history[$year]['child'][$month]['child'][$day]))
$history[$year]['child'][$month]['child'][$day] = $tasks;
$history[$year]['child'][$month]['child'][$day][$conversion[$row['action']]] += 1;
}
$smcFunc['db_free_result']($request);
// We now return to the history channel.
return $history;
}