From 6fcc63d1ccbc8ef1911333b145ace0feadc9c170 Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Tue, 15 Nov 2022 13:38:16 +0000
Subject: [PATCH] fix sorting, closes #1781

Closes #1781

Merge request studip/studip!1163
---
 app/controllers/admin/user.php |  3 ++
 lib/models/User.class.php      | 52 ++++++++++++++++++----------------
 2 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/app/controllers/admin/user.php b/app/controllers/admin/user.php
index 043673b71d8..26563cf8f53 100644
--- a/app/controllers/admin/user.php
+++ b/app/controllers/admin/user.php
@@ -122,6 +122,9 @@ class Admin_UserController extends AuthenticatedController
                 $this->order = $this->order == 'desc' ? 'asc' : 'desc';
             }
 
+            $request['sortby'] = $this->sortby;
+            $request['order'] = $this->order;
+
             $empty_search          = $request['perm'] === 'alle';
 
             $values = [
diff --git a/lib/models/User.class.php b/lib/models/User.class.php
index b48db772cf6..7c63cf3b403 100644
--- a/lib/models/User.class.php
+++ b/lib/models/User.class.php
@@ -474,31 +474,33 @@ class User extends AuthUserMd5 implements Range, PrivacyObject
         $query .= implode(' AND ', $where);
         $query .= " GROUP BY au.`user_id` ";
 
-        //sortieren
-        switch ($attributes['sort']) {
-            case "perms":
-                $query .= "ORDER BY au.`perms` {$attributes['order']}, au.`username`";
-                break;
-            case "Vorname":
-                $query .= "ORDER BY au.`Vorname` {$attributes['order']}, au.`Nachname`";
-                break;
-            case "Nachname":
-                $query .= "ORDER BY au.`Nachname` {$attributes['order']}, au.`Vorname`";
-                break;
-            case "Email":
-                $query .= "ORDER BY au.`Email` {$attributes['order']}, au.`username`";
-                break;
-            case "changed":
-                $query .= "ORDER BY uo.`last_lifesign` {$attributes['order']}, au.`username`";
-                break;
-            case "mkdate":
-                $query .= "ORDER BY ui.`mkdate` {$attributes['order']}, au.`username`";
-                break;
-            case "auth_plugin":
-                $query .= "ORDER BY `auth_plugin` {$attributes['order']}, au.`username`";
-                break;
-            default:
-                $query .= " ORDER BY au.`username` {$attributes['order']}";
+        if (!empty($attributes['sortby'])) {
+            //sortieren
+            switch ($attributes['sortby']) {
+                case "perms":
+                    $query .= "ORDER BY au.`perms` {$attributes['order']}, au.`username`";
+                    break;
+                case "Vorname":
+                    $query .= "ORDER BY au.`Vorname` {$attributes['order']}, au.`Nachname`";
+                    break;
+                case "Nachname":
+                    $query .= "ORDER BY au.`Nachname` {$attributes['order']}, au.`Vorname`";
+                    break;
+                case "Email":
+                    $query .= "ORDER BY au.`Email` {$attributes['order']}, au.`username`";
+                    break;
+                case "changed":
+                    $query .= "ORDER BY uo.`last_lifesign` {$attributes['order']}, au.`username`";
+                    break;
+                case "mkdate":
+                    $query .= "ORDER BY ui.`mkdate` {$attributes['order']}, au.`username`";
+                    break;
+                case "auth_plugin":
+                    $query .= "ORDER BY `auth_plugin` {$attributes['order']}, au.`username`";
+                    break;
+                default:
+                    $query .= " ORDER BY au.`username` {$attributes['order']}";
+            }
         }
 
         return DBManager::get()->fetchAll($query, $params, __CLASS__ . '::buildExisting');
-- 
GitLab