! New maintenance tests: check first/last message ids are correct and validate open tickets all have the correct status
gruffen

gruffen commited on 2011-03-23 16:36:03
Showing 4 changed files, with 203 additions and 3 deletions.

... ...
@@ -401,10 +401,13 @@ $txt['shd_admin_maint_findrepair'] = 'Find and Repair Errors';
401 401
 $txt['shd_admin_maint_findrepair_desc'] = 'Sometimes, however unlikely, things get a little out of step inside the database. This operation performs an integrity check on the database and attempts to repair any errors it encounters.';
402 402
 
403 403
 $txt['shd_admin_maint_findrepair_status'] = 'Recalculating ticket counts...';
404
+$txt['shd_admin_maint_findrepair_firstlast'] = 'Recalculating ticket first/last associations...';
404 405
 
405 406
 $txt['shd_maint_zero_tickets'] = '%1$d ticket(s) were found with invalid ids, they have all been given new ids, the next available id numbers.';
406 407
 $txt['shd_maint_zero_msgs'] = '%1$d ticket posts(s) were found with invalid ids, they have all been given new ids, the next available id numbers.';
407 408
 $txt['shd_maint_deleted'] = '%1$d ticket(s) had incorrect counts of the number of posts and/or deleted posts. All have been recalculated.';
409
+$txt['shd_maint_first_last'] = '%1$d tickets had incorrect messages flagged for the ticket content, or its last reply. All have been rectified.';
410
+$txt['shd_maint_status'] = '%1$d tickets had the wrong status set for them. All have been rectified.';
408 411
 //@}
409 412
 
410 413
 /**
... ...
@@ -78,6 +78,14 @@ function shd_admin_maint_findrepair()
78 78
 			'name' => 'deleted',
79 79
 			'pc' => 10,
80 80
 		),
81
+		array(
82
+			'name' => 'first_last',
83
+			'pc' => 10,
84
+		),
85
+		array(
86
+			'name' => 'status',
87
+			'pc' => 10,
88
+		),
81 89
 		array(
82 90
 			'name' => 'clean_cache',
83 91
 			'pc' => 5,
... ...
@@ -109,6 +117,7 @@ function shd_admin_maint_findrepair()
109 117
 	$function();
110 118
 }
111 119
 
120
+// Validate that all tickets and messages have a valid id number
112 121
 function shd_maint_zero_entries()
113 122
 {
114 123
 	global $context, $smcFunc;
... ...
@@ -147,6 +156,7 @@ function shd_maint_zero_entries()
147 156
 	$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '" />';
148 157
 }
149 158
 
159
+// Ensure that the count of number of replies/deleted replies/whether ticket contains deleted replies are all correct.
150 160
 function shd_maint_deleted()
151 161
 {
152 162
 	global $context, $smcFunc;
... ...
@@ -256,11 +266,188 @@ function shd_maint_deleted()
256 266
 	}
257 267
 }
258 268
 
259
-function shd_maint_clean_cache()
269
+// Make sure the first and last posters on a ticket are correct.
270
+function shd_maint_first_last()
260 271
 {
261
-	global $context;
272
+	global $context, $smcFunc;
273
+
274
+	// First we need the number of tickets
275
+	$query = $smcFunc['db_query']('', '
276
+		SELECT COUNT(*)
277
+		FROM {db_prefix}helpdesk_tickets');
278
+	list($ticket_count) = $smcFunc['db_fetch_row']($query);
279
+	$smcFunc['db_free_result']($query);
280
+
281
+	$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
282
+
283
+	$step_size = 150;
284
+	$tickets = array();
285
+	$tickets_modify = array();
286
+	$query = $smcFunc['db_query']('', '
287
+		SELECT id_ticket, id_first_msg, id_last_msg
288
+		FROM {db_prefix}helpdesk_tickets
289
+		ORDER BY id_ticket ASC
290
+		LIMIT {int:start}, {int:limit}',
291
+		array(
292
+			'start' => $_REQUEST['start'],
293
+			'limit' => $step_size,
294
+		)
295
+	);
296
+	while ($row = $smcFunc['db_fetch_assoc']($query))
297
+		$tickets[$row['id_ticket']] = $row;
298
+	$smcFunc['db_free_result']($query);
299
+
300
+	if (!empty($tickets))
301
+	{
302
+		// Firstly, let's get the first/last messages from the messages table.
303
+		$query = $smcFunc['db_query']('', '
304
+			SELECT id_ticket, MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg
305
+			FROM {db_prefix}helpdesk_ticket_replies
306
+			WHERE id_ticket IN ({array_int:tickets})
307
+				AND message_status = ({int:normal})
308
+			GROUP BY id_ticket',
309
+			array(
310
+				'tickets' => array_keys($tickets),
311
+				'normal' => MSG_STATUS_NORMAL,
312
+			)
313
+		);
314
+		$ticket_cache = array();
315
+		while ($row = $smcFunc['db_fetch_assoc']($query))
316
+			$ticket_cache[$row['id_ticket']] = $row;
317
+		$smcFunc['db_free_result']($query);
318
+
319
+		// OK so we now have the message ids for first/last message. Are they right?
320
+		foreach ($ticket_cache as $id_ticket => $ticket_details)
321
+			if ($ticket_cache[$id_ticket]['id_first_msg'] != $tickets[$id_ticket]['id_first_msg'] || $ticket_cache[$id_ticket]['id_last_msg'] != $tickets[$id_ticket]['id_last_msg'])
322
+				$tickets_modify[$id_ticket] = $ticket_cache[$id_ticket];
323
+
324
+		// Any to update?
325
+		if (!empty($tickets_modify))
326
+		{
327
+			// Oh crap.
328
+			foreach ($tickets_modify as $id_ticket => $columns)
329
+			{
330
+				$smcFunc['db_query']('', '
331
+					UPDATE {db_prefix}helpdesk_tickets
332
+					SET id_first_msg = {int:id_first_msg},
333
+						id_last_msg = {int:id_last_msg}
334
+					WHERE id_ticket = {int:id_ticket}',
335
+					$columns
336
+				);
337
+			}
338
+			$_SESSION['shd_maint']['first_last'] = count($tickets_modify);
339
+		}
340
+	}
341
+
342
+	// Another round?
343
+	$_REQUEST['start'] += $step_size;
344
+	if ($_REQUEST['start'] > $ticket_count)
345
+	{
346
+		// All done
347
+		$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '" />';
348
+	}
349
+	else
350
+	{
351
+		// More to do, call back - and provide the subtitle
352
+		$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '" />
353
+		<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
354
+		$context['substep_enabled'] = true;
355
+		$context['substep_title'] = $txt['shd_admin_maint_findrepair_firstlast'];
356
+		$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
357
+	}
358
+}
359
+
360
+// Make sure all open tickets have the right statuses.
361
+function shd_maint_status()
362
+{
363
+	global $context, $smcFunc;
364
+
365
+	$open = array(TICKET_STATUS_NEW, TICKET_STATUS_PENDING_STAFF, TICKET_STATUS_PENDING_USER);
366
+
367
+	// First we need the number of tickets
368
+	$query = $smcFunc['db_query']('', '
369
+		SELECT COUNT(*)
370
+		FROM {db_prefix}helpdesk_tickets
371
+		WHERE status IN ({array_int:open})',
372
+		array(
373
+			'open' => $open,
374
+		)
375
+	);
376
+	list($ticket_count) = $smcFunc['db_fetch_row']($query);
377
+	$smcFunc['db_free_result']($query);
378
+
379
+	$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
380
+
381
+	$step_size = 100;
382
+	$tickets = array();
383
+	$tickets_modify = array();
384
+	$query = $smcFunc['db_query']('', '
385
+		SELECT id_ticket, num_replies, id_member_started, id_member_updated, status
386
+		FROM {db_prefix}helpdesk_tickets
387
+		WHERE status IN ({array_int:open})
388
+		ORDER BY id_ticket ASC
389
+		LIMIT {int:start}, {int:limit}',
390
+		array(
391
+			'open' => $open,
392
+			'start' => $_REQUEST['start'],
393
+			'limit' => $step_size,
394
+		)
395
+	);
396
+	while ($row = $smcFunc['db_fetch_assoc']($query))
397
+		$tickets[$row['id_ticket']] = $row;
398
+	$smcFunc['db_free_result']($query);
399
+
400
+	if (!empty($tickets))
401
+	{
402
+		foreach ($tickets as $ticket)
403
+		{
404
+			$new_status = shd_determine_status('reply', $ticket['id_member_started'], $ticket['id_member_updated'], $ticket['num_replies']);
405
+			if ($ticket['status'] != $new_status)
406
+				$tickets_modify[$ticket['id_ticket']] = $new_status;
407
+		}
408
+
409
+		// Any to update?
410
+		if (!empty($tickets_modify))
411
+		{
412
+			// Oh crap.
413
+			foreach ($tickets_modify as $id_ticket => $status)
414
+			{
415
+				$smcFunc['db_query']('', '
416
+					UPDATE {db_prefix}helpdesk_tickets
417
+					SET status = {int:status}
418
+					WHERE id_ticket = {int:id_ticket}',
419
+					array(
420
+						'id_ticket' => $id_ticket,
421
+						'status' => $status,
422
+					)
423
+				);
424
+			}
425
+			$_SESSION['shd_maint']['status'] = count($tickets_modify);
426
+		}
427
+	}
428
+
429
+	// Another round?
430
+	$_REQUEST['start'] += $step_size;
431
+	if ($_REQUEST['start'] > $ticket_count)
432
+	{
433
+		// All done
434
+		$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . ($context['step'] + 1) . '" />';
435
+	}
436
+	else
437
+	{
438
+		// More to do, call back - and provide the subtitle
439
+		$context['continue_post_data'] .= '<input type="hidden" name="step" value="' . $context['step'] . '" />
440
+		<input type="hidden" name="start" value="' . $_REQUEST['start'] . '">';
441
+		$context['substep_enabled'] = true;
442
+		$context['substep_title'] = $txt['shd_admin_maint_findrepair_firstlast'];
443
+		$context['substep_continue_percent'] = round(100 * $_REQUEST['start'] / $ticket_count);
444
+	}
445
+}
262 446
 
263 447
 // Make sure all SimpleDesk cache items are forcibly flushed.
448
+function shd_maint_clean_cache()
449
+{
450
+	global $context;
264 451
 	clean_cache('shd');
265 452
 
266 453
 	// Normally, we'd update $context['continue_post_data'] to indicate our next port of call. But here, we don't have to.
... ...
@@ -799,6 +799,11 @@ function shd_image_url($filename)
799 799
 */
800 800
 function shd_determine_status($action, $starter_id = 0, $replier_id = 0, $replies = -1)
801 801
 {
802
+	static $staff = null;
803
+
804
+	if ($staff === null)
805
+		$staff = shd_members_allowed_to('shd_staff');
806
+
802 807
 	$known_states = array(
803 808
 		'new',
804 809
 		'resolve',
... ...
@@ -834,7 +839,6 @@ function shd_determine_status($action, $starter_id = 0, $replier_id = 0, $replie
834 839
 				return TICKET_STATUS_NEW;
835 840
 			else
836 841
 			{
837
-				$staff = shd_members_allowed_to('shd_staff');
838 842
 				if (in_array($replier_id, $staff))
839 843
 					$new_status = $starter_id == $replier_id ? TICKET_STATUS_PENDING_STAFF : TICKET_STATUS_PENDING_USER; // i.e. if they're staff but it's their own ticket they're replying to, it's not with user.
840 844
 				else
... ...
@@ -108,6 +108,12 @@ function template_shd_admin_maint_findrepairdone()
108 108
 		if (!empty($context['maintenance_result']['deleted']))
109 109
 			echo '
110 110
 				<p class="padding">', sprintf($txt['shd_maint_deleted'], $context['maintenance_result']['deleted']), '</p>';
111
+		if (!empty($context['maintenance_result']['first_last']))
112
+			echo '
113
+				<p class="padding">', sprintf($txt['shd_maint_first_last'], $context['maintenance_result']['first_last']), '</p>';
114
+		if (!empty($context['maintenance_result']['status']))
115
+			echo '
116
+				<p class="padding">', sprintf($txt['shd_maint_status'], $context['maintenance_result']['status']), '</p>';
111 117
 
112 118
 		echo '
113 119
 				<p class="padding">
114 120