From 1d48e606437e5f935f417755a3c9e52e79bd8d46 Mon Sep 17 00:00:00 2001 From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de> Date: Tue, 18 Oct 2022 15:19:01 +0000 Subject: [PATCH] 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 facd370aa53..e4bb9c8695e 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 9b37517f2f3..1d935eb1759 100644 --- a/app/views/admin/domain/index.php +++ b/app/views/admin/domain/index.php @@ -28,13 +28,13 @@ <tr> <td><?= htmlReady($domain->name) ?></td> <td><?= htmlReady($domain->id) ?></td> - <td><?= count($domain->users) ?></td> - <td><?= count($domain->courses) ?></td> + <td><?= $domain->countUsers() ?></td> + <td><?= $domain->countCourses() ?></td> <td class="actions"> <a href="<?= $controller->link_for("admin/domain/edit/{$domain->id}") ?>" data-dialog="size=auto"> <?= Icon::create('edit')->asImg(tooltip2(_('bearbeiten'))) ?> </a> - <? if (count($domain->users) === 0): ?> + <? if ($domain->countUsers() === 0): ?> <?= Icon::create('trash')->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 44befce2c51..8179082a031 100644 --- a/lib/models/UserDomain.php +++ b/lib/models/UserDomain.php @@ -48,6 +48,30 @@ class UserDomain extends SimpleORMap return self::findBySQL('1 ORDER BY name'); } + /** + * 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. */ -- GitLab