ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMStListUsers.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
20 namespace ILIAS\MyStaff\ListUsers;
21 
24 
30 {
31  private Container $dic;
32 
37  public function __construct(Container $dic)
38  {
39  $this->dic = $dic;
40  }
41 
42  final public function getData(array $arr_usr_ids = array(), array $options = array()): ListFetcherResult
43  {
44  //Permissions
45  if (count($arr_usr_ids) == 0) {
46  return new ListFetcherResult([], 0);
47  }
48 
49  $_options = array(
50  'filters' => array(),
51  'sort' => array(),
52  'limit' => array(),
53  'count' => false,
54  );
55  $options = array_merge($_options, $options);
56 
57  $select = 'SELECT
58  usr_id,
59  login,
60  gender,
61  firstname,
62  lastname,
63  title,
64  institution,
65  department,
66  street,
67  zipcode,
68  city,
69  country,
70  sel_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  $select .= " ORDER BY " . $options['sort']['field'] . " " . $options['sort']['direction'];
85  }
86 
87  if (isset($options['limit']['start']) && isset($options['limit']['end'])) {
88  $select .= " LIMIT " . $options['limit']['start'] . "," . $options['limit']['end'];
89  }
90 
91  $result = $this->dic->database()->query($select);
92  $user_data = array();
93 
94  while ($user = $this->dic->database()->fetchAssoc($result)) {
95  $list_user = new ilMStListUser();
96  $list_user->setUsrId(intval($user['usr_id']));
97  $list_user->setGender($user['gender'] ?? "");
98  $list_user->setTitle($user['title'] ?? "");
99  $list_user->setInstitution($user['institution'] ?? "");
100  $list_user->setDepartment($user['department'] ?? "");
101  $list_user->setStreet($user['street'] ?? "");
102  $list_user->setZipcode($user['zipcode'] ?? "");
103  $list_user->setCity($user['city'] ?? "");
104  $list_user->setCountry($user['country'] ?? "");
105  $list_user->setSelCountry($user['sel_country'] ?? "");
106  $list_user->setHobby($user['hobby'] ?? "");
107  $list_user->setMatriculation($user['matriculation'] ?? "");
108  $list_user->setActive(intval($user['active']));
109  $list_user->setLogin($user['login']);
110  $list_user->setFirstname($user['firstname']);
111  $list_user->setLastname($user['lastname']);
112  $list_user->setEmail($user['email'] ?? "");
113  $list_user->setSecondEmail($user['second_email'] ?? "");
114 
115  $user_data[] = $list_user;
116  }
117 
118  return new ListFetcherResult($user_data, $numRows);
119  }
120 
124  private function createWhereStatement(array $arr_usr_ids, array $arr_filter): string
125  {
126  $where = array();
127 
128  $where[] = $this->dic->database()->in('usr_data.usr_id', $arr_usr_ids, false, 'integer');
129 
130  if (!empty($arr_filter['user'])) {
131  $where[] = "(" . $this->dic->database()
132  ->like(
133  "usr_data.login",
134  "text",
135  "%" . $arr_filter['user'] . "%"
136  ) . " " . "OR " . $this->dic->database()
137  ->like(
138  "usr_data.firstname",
139  "text",
140  "%" . $arr_filter['user'] . "%"
141  ) . " " . "OR " . $this->dic->database()
142  ->like(
143  "usr_data.lastname",
144  "text",
145  "%" . $arr_filter['user'] . "%"
146  ) . " " . "OR " . $this->dic->database()
147  ->like(
148  "usr_data.email",
149  "text",
150  "%" . $arr_filter['user'] . "%"
151  ) . " " . "OR " . $this->dic->database()
152  ->like(
153  "usr_data.second_email",
154  "text",
155  "%" . $arr_filter['user'] . "%"
156  ) . ") ";
157  }
158 
159  if (!empty($arr_filter['org_unit'])) {
160  $where[] = 'usr_data.usr_id IN (SELECT user_id FROM il_orgu_ua WHERE orgu_id = ' . $this->dic->database()
161  ->quote(
162  $arr_filter['org_unit'],
163  'integer'
164  ) . ')';
165  }
166 
167  if (!empty($arr_filter['lastname'])) {
168  $where[] = '(lastname LIKE ' . $this->dic->database()->quote('%' . str_replace(
169  '*',
170  '%',
171  $arr_filter['lastname']
172  ) . '%', 'text') . ')';
173  }
174 
175  if (!empty($arr_filter['firstname'])) {
176  $where[] = '(firstname LIKE ' . $this->dic->database()->quote('%' . str_replace(
177  '*',
178  '%',
179  $arr_filter['firstname']
180  ) . '%', 'text') . ')';
181  }
182 
183  if (!empty($arr_filter['email'])) {
184  $where[] = '(email LIKE ' . $this->dic->database()->quote('%' . str_replace(
185  '*',
186  '%',
187  $arr_filter['email']
188  ) . '%', 'text') . ')';
189  }
190 
191  if (!empty($arr_filter['second_email'])) {
192  $where[] = '(second_email LIKE ' . $this->dic->database()->quote('%' . str_replace(
193  '*',
194  '%',
195  $arr_filter['second_email']
196  ) . '%', 'text') . ')';
197  }
198 
199  if (!empty($arr_filter['title'])) {
200  $where[] = '(title LIKE ' . $this->dic->database()->quote('%' . str_replace(
201  '*',
202  '%',
203  $arr_filter['title']
204  ) . '%', 'text') . ')';
205  }
206 
207  if ($arr_filter['activation'] ?? false) {
208  if ($arr_filter['activation'] == 'active') {
209  $where[] = '(active = "1")';
210  }
211  if ($arr_filter['activation'] == 'inactive') {
212  $where[] = '(active = "0")';
213  }
214  }
215 
216  if (!empty($where)) {
217  return ' WHERE ' . implode(' AND ', $where) . ' ';
218  } else {
219  return '';
220  }
221  }
222 }
createWhereStatement(array $arr_usr_ids, array $arr_filter)
Returns the WHERE Part for the Queries using parameter $user_ids AND local variable $filters...
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
getData(array $arr_usr_ids=array(), array $options=array())
__construct(Container $dic)
ilMStListUsers constructor.