From 664c4aa07be52dd64c753036515599a38e848b42 Mon Sep 17 00:00:00 2001 From: Matthew Hilton Date: Tue, 22 Oct 2024 15:16:25 +1000 Subject: [PATCH] refactor: get object tag sync status count details separately --- classes/check/tagging_sync_status.php | 28 +++++++-------------------- classes/local/tag/tag_manager.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/classes/check/tagging_sync_status.php b/classes/check/tagging_sync_status.php index ba4d9302..c3b23720 100644 --- a/classes/check/tagging_sync_status.php +++ b/classes/check/tagging_sync_status.php @@ -18,9 +18,8 @@ use core\check\check; use core\check\result; -use html_table; -use html_writer; use tool_objectfs\local\tag\tag_manager; +use tool_objectfs\local\tag_sync_count_result; /** * Tagging sync status check @@ -47,28 +46,15 @@ public function get_action_link(): ?\action_link { */ public function get_result(): result { if (!tag_manager::is_tagging_enabled_and_supported()) { - return new result(result::NA, get_string('check:tagging:na', 'tool_objectfs')); + return new tag_sync_count_result(result::WARNING, get_string('check:tagging:na', 'tool_objectfs')); } - $statuses = tag_manager::get_tag_sync_status_summary(); - $table = new html_table(); - $table->head = [ - get_string('table:status', 'tool_objectfs'), - get_string('table:objectcount', 'tool_objectfs'), - ]; - - foreach (tag_manager::SYNC_STATUSES as $status) { - // If no objects have a status, they won't appear in the SQL above. - // In this case, just show zero (so the use knows it exists, but is zero). - $count = isset($statuses[$status]->statuscount) ? $statuses[$status]->statuscount : 0; - $table->data[$status] = [tag_manager::get_sync_status_string($status), $count]; - } - $table = html_writer::table($table); - - if (!empty($statuses[tag_manager::SYNC_STATUS_ERROR])) { - return new result(result::WARNING, get_string('check:tagging:syncerror', 'tool_objectfs'), $table); + // We only do a lightweight check here, the get_details is overwritten in tag_sync_status_result + // to provide more information that is more computationally expensive to calculate. + if (tag_manager::tag_sync_errors_exist()) { + return new tag_sync_count_result(result::WARNING, get_string('check:tagging:syncerror', 'tool_objectfs')); } - return new result(result::OK, get_string('check:tagging:syncok', 'tool_objectfs'), $table); + return new tag_sync_count_result(result::OK, get_string('check:tagging:syncok', 'tool_objectfs')); } } diff --git a/classes/local/tag/tag_manager.php b/classes/local/tag/tag_manager.php index 058b82f6..603d04a4 100644 --- a/classes/local/tag/tag_manager.php +++ b/classes/local/tag/tag_manager.php @@ -222,6 +222,7 @@ public static function get_sync_status_string(int $tagsyncstatus): string { /** * Returns a summary of the object tag sync statuses. + * Note on larger sites, this can be quite computationally difficult and should be used carefully. * @return array */ public static function get_tag_sync_status_summary(): array { @@ -230,4 +231,13 @@ public static function get_tag_sync_status_summary(): array { FROM {tool_objectfs_objects} GROUP BY tagsyncstatus"); } + + /** + * This is a lightweight check to just check if any objects are reporting tag sync errors. + * @return bool + */ + public static function tag_sync_errors_exist(): bool { + global $DB; + return $DB->record_exists('tool_objectfs_objects', ['tagsyncstatus' => self::SYNC_STATUS_ERROR]); + } }