From 1d48e606437e5f935f417755a3c9e52e79bd8d46 Mon Sep 17 00:00:00 2001 From: Elmar Ludwig Date: Tue, 18 Oct 2022 15:19:01 +0000 Subject: count in SQL, not in PHP, fixes #1677 Closes #1677 Merge request studip/studip!1080 --- app/controllers/admin/domain.php | 2 +- app/views/admin/domain/index.php | 6 +++--- lib/models/UserDomain.php | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/domain.php b/app/controllers/admin/domain.php index facd370..e4bb9c8 100644 --- a/app/controllers/admin/domain.php +++ b/app/controllers/admin/domain.php @@ -138,7 +138,7 @@ class Admin_DomainController extends AuthenticatedController CSRFProtection::verifyUnsafeRequest(); $domain = new UserDomain($id); - if (count($domain->users) == 0) { + if ($domain->countUsers() === 0) { $domain->delete(); } else { PageLayout::postError(_('Domänen, denen noch Personen zugewiesen sind, können nicht gelöscht werden.')); diff --git a/app/views/admin/domain/index.php b/app/views/admin/domain/index.php index 9b37517..1d935eb 100644 --- a/app/views/admin/domain/index.php +++ b/app/views/admin/domain/index.php @@ -28,13 +28,13 @@ name) ?> id) ?> - users) ?> - courses) ?> + countUsers() ?> + countCourses() ?> id}") ?>" data-dialog="size=auto"> asImg(tooltip2(_('bearbeiten'))) ?> - users) === 0): ?> + countUsers() === 0): ?> asInput(tooltip2(_('löschen')) + [ 'class' => 'text-top', 'formaction' => $controller->url_for("admin/domain/delete/{$domain->id}"), diff --git a/lib/models/UserDomain.php b/lib/models/UserDomain.php index 44befce..8179082 100644 --- a/lib/models/UserDomain.php +++ b/lib/models/UserDomain.php @@ -49,6 +49,30 @@ class UserDomain extends SimpleORMap } /** + * Count the number of courses associated with this user domain. + * + * @return int number of courses + */ + public function countCourses(): int + { + $query = 'SELECT COUNT(*) FROM seminar_userdomains WHERE userdomain_id = ?'; + + return (int) DBManager::get()->fetchColumn($query, [$this->id]); + } + + /** + * Count the number of courses associated with this user domain. + * + * @return int number of users + */ + public function countUsers(): int + { + $query = 'SELECT COUNT(*) FROM user_userdomains WHERE userdomain_id = ?'; + + return (int) DBManager::get()->fetchColumn($query, [$this->id]); + } + + /** * Add a user to this user domain. */ public function addUser ($user_id) -- cgit v1.0