ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.ilMStListUsers.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
31{
32 private Container $dic;
33
38 public function __construct(Container $dic)
39 {
40 $this->dic = $dic;
41 }
42
43 final public function getData(array $arr_usr_ids = array(), array $options = array()): ListFetcherResult
44 {
45 //Permissions
46 if (count($arr_usr_ids) == 0) {
47 return new ListFetcherResult([], 0);
48 }
49
50 $_options = array(
51 'filters' => array(),
52 'sort' => array(),
53 'limit' => array(),
54 'count' => false,
55 );
56 $options = array_merge($_options, $options);
57
58 $select = 'SELECT
59 usr_id,
60 login,
61 gender,
62 firstname,
63 lastname,
64 title,
65 institution,
66 department,
67 street,
68 zipcode,
69 city,
70 country,
71 hobby,
72 email,
73 second_email,
74 matriculation,
75 active
76 FROM ' . $this->dic->database()->quoteIdentifier('usr_data') .
77
78 self::createWhereStatement($arr_usr_ids, $options['filters']);
79
80 $result = $this->dic->database()->query($select);
81 $numRows = $this->dic->database()->numRows($result);
82
83 if ($options['sort']) {
84 /*
85 * Sort field is validated in ilMStListUsersTableGUI::getSafeOrderField,
86 * the available columns depend on user administration settings.
87 */
88 $sort_field = (string) ($options['sort']['field'] ?? '');
89 $sort_direction = $this->getSafeSortDirection((string) ($options['sort']['direction'] ?? ''));
90 $select .= " ORDER BY " . $this->dic->database()->quoteIdentifier($sort_field) . " " . $sort_direction;
91 }
92
93 if (isset($options['limit']['start']) && isset($options['limit']['end'])) {
94 $select .= " LIMIT " . (int) $options['limit']['start'] . "," . (int) $options['limit']['end'];
95 }
96
97 $result = $this->dic->database()->query($select);
98 $user_data = array();
99
100 while ($user = $this->dic->database()->fetchAssoc($result)) {
101 $list_user = new ilMStListUser();
102 $list_user->setUsrId(intval($user['usr_id']));
103 $list_user->setGender($user['gender'] ?? "");
104 $list_user->setTitle($user['title'] ?? "");
105 $list_user->setInstitution($user['institution'] ?? "");
106 $list_user->setDepartment($user['department'] ?? "");
107 $list_user->setStreet($user['street'] ?? "");
108 $list_user->setZipcode($user['zipcode'] ?? "");
109 $list_user->setCity($user['city'] ?? "");
110 $list_user->setCountry($user['country'] ?? "");
111 $list_user->setHobby($user['hobby'] ?? "");
112 $list_user->setMatriculation($user['matriculation'] ?? "");
113 $list_user->setActive(intval($user['active']));
114 $list_user->setLogin($user['login']);
115 $list_user->setFirstname($user['firstname']);
116 $list_user->setLastname($user['lastname']);
117 $list_user->setEmail($user['email'] ?? "");
118 $list_user->setSecondEmail($user['second_email'] ?? "");
119
120 $user_data[] = $list_user;
121 }
122
123 return new ListFetcherResult($user_data, $numRows);
124 }
125
129 private function createWhereStatement(array $arr_usr_ids, array $arr_filter): string
130 {
131 $where = array();
132
133 $where[] = $this->dic->database()->in('usr_data.usr_id', $arr_usr_ids, false, 'integer');
134
135 if (!empty($arr_filter['user'])) {
136 $where[] = "(" . $this->dic->database()
137 ->like(
138 "usr_data.login",
139 "text",
140 "%" . $arr_filter['user'] . "%"
141 ) . " " . "OR " . $this->dic->database()
142 ->like(
143 "usr_data.firstname",
144 "text",
145 "%" . $arr_filter['user'] . "%"
146 ) . " " . "OR " . $this->dic->database()
147 ->like(
148 "usr_data.lastname",
149 "text",
150 "%" . $arr_filter['user'] . "%"
151 ) . " " . "OR " . $this->dic->database()
152 ->like(
153 "usr_data.email",
154 "text",
155 "%" . $arr_filter['user'] . "%"
156 ) . " " . "OR " . $this->dic->database()
157 ->like(
158 "usr_data.second_email",
159 "text",
160 "%" . $arr_filter['user'] . "%"
161 ) . ") ";
162 }
163
164 if (!empty($arr_filter['org_unit'])) {
165 $where[] = 'usr_data.usr_id IN (SELECT user_id FROM il_orgu_ua WHERE orgu_id = ' . $this->dic->database()
166 ->quote(
167 $arr_filter['org_unit'],
168 'integer'
169 ) . ')';
170 }
171
172 if (!empty($arr_filter['lastname'])) {
173 $where[] = '(lastname LIKE ' . $this->dic->database()->quote('%' . str_replace(
174 '*',
175 '%',
176 $arr_filter['lastname']
177 ) . '%', 'text') . ')';
178 }
179
180 if (!empty($arr_filter['firstname'])) {
181 $where[] = '(firstname LIKE ' . $this->dic->database()->quote('%' . str_replace(
182 '*',
183 '%',
184 $arr_filter['firstname']
185 ) . '%', 'text') . ')';
186 }
187
188 if (!empty($arr_filter['email'])) {
189 $where[] = '(email LIKE ' . $this->dic->database()->quote('%' . str_replace(
190 '*',
191 '%',
192 $arr_filter['email']
193 ) . '%', 'text') . ')';
194 }
195
196 if (!empty($arr_filter['second_email'])) {
197 $where[] = '(second_email LIKE ' . $this->dic->database()->quote('%' . str_replace(
198 '*',
199 '%',
200 $arr_filter['second_email']
201 ) . '%', 'text') . ')';
202 }
203
204 if (!empty($arr_filter['title'])) {
205 $where[] = '(title LIKE ' . $this->dic->database()->quote('%' . str_replace(
206 '*',
207 '%',
208 $arr_filter['title']
209 ) . '%', 'text') . ')';
210 }
211
212 if ($arr_filter['activation'] ?? false) {
213 if ($arr_filter['activation'] == 'active') {
214 $where[] = '(active = "1")';
215 }
216 if ($arr_filter['activation'] == 'inactive') {
217 $where[] = '(active = "0")';
218 }
219 }
220
221 if (!empty($where)) {
222 return ' WHERE ' . implode(' AND ', $where) . ' ';
223 } else {
224 return '';
225 }
226 }
227
228 private function getSafeSortDirection(string $sort_direction): string
229 {
230 return strtolower($sort_direction) === 'asc'
231 ? 'asc'
232 : 'desc';
233 }
234}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
createWhereStatement(array $arr_usr_ids, array $arr_filter)
Returns the WHERE Part for the Queries using parameter $user_ids AND local variable $filters.
getData(array $arr_usr_ids=array(), array $options=array())
__construct(Container $dic)
ilMStListUsers constructor.