ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilOrgUnitUserRepository.php
Go to the documentation of this file.
1<?php
2
3namespace OrgUnit\User;
4
7
14{
15
19 protected $dic;
23 protected static $instance;
27 protected $orgu_users;
31 protected $with_superiors = false;
35 protected $with_positions = false;
36
37
42 public function __construct()
43 {
44 global $DIC;
45 $this->dic = $DIC;
46 }
47
48
53 {
54 $this->with_superiors = true;
55
56 return $this;
57 }
58
59
64 {
65 $this->with_positions = true;
66
67 return $this;
68 }
69
70
76 public function getOrgUnitUsers(array $arr_user_id) : array
77 {
78 $this->orgu_users = $this->loadUsersByUserIds($arr_user_id);
79
80 if ($this->with_superiors === true) {
81 $this->loadSuperiors($arr_user_id);
82 }
83
84 if ($this->with_positions === true) {
85 $this->loadPositions($arr_user_id);
86 }
87
88 return $this->orgu_users;
89 }
90
91
97 public function getOrgUnitUser(int $user_id) : ?ilOrgUnitUser
98 {
99 $this->orgu_users = $this->loadUsersByUserIds([$user_id]);
100
101 if (count($this->orgu_users) == 0) {
102 return null;
103 }
104
105 if ($this->with_superiors === true) {
106 $this->loadSuperiors([$user_id]);
107 }
108
109 return $this->orgu_users[0];
110 }
111
112
116 public function loadSuperiors(array $user_ids) : void
117 {
118 global $DIC;
119
120 $st = $DIC->database()->query($this->getSuperiorsSql($user_ids));
121
122 $empl_id_sup_ids = [];
123 while ($data = $DIC->database()->fetchAssoc($st)) {
124 $org_unit_user = ilOrgUnitUser::getInstanceById($data['empl_usr_id']);
125 $superior = ilOrgUnitUser::getInstance($data['sup_usr_id'], (string)$data['sup_login'], (string)$data['sup_email'], (string)$data['sup_second_email']);
126 $org_unit_user->addSuperior($superior);
127 }
128 }
129
130
136 public function getEmailAdressesOfSuperiors(array $user_ids) : array
137 {
138 global $DIC;
139
140 $st = $DIC->database()->query($this->getSuperiorsSql($user_ids));
141
142 $arr_email_sup = [];
143 while ($data = $DIC->database()->fetchAssoc($st)) {
144 $arr_email_sup[] = $data['sup_email'];
145 }
146
147 return $arr_email_sup;
148 }
149
150
156 protected function getSuperiorsSql(array $user_ids) : string
157 {
158 global $DIC;
159
160 $sql = "SELECT
161 orgu_ua.orgu_id AS orgu_id,
162 orgu_ua.user_id AS empl_usr_id,
163 orgu_ua2.user_id as sup_usr_id,
164 superior.email as sup_email,
165 superior.second_email as sup_second_email,
166 superior.login as sup_login
167 FROM
168 il_orgu_ua as orgu_ua,
169 il_orgu_ua as orgu_ua2
170 inner join usr_data as superior on superior.usr_id = orgu_ua2.user_id
171 WHERE
172 orgu_ua.orgu_id = orgu_ua2.orgu_id
173 and orgu_ua.user_id <> orgu_ua2.user_id
174 and orgu_ua.position_id = " . \ilOrgUnitPosition::CORE_POSITION_EMPLOYEE . "
175 and orgu_ua2.position_id = " . \ilOrgUnitPosition::CORE_POSITION_SUPERIOR . "
176 AND " . $DIC->database()->in('orgu_ua.user_id', $user_ids, false, 'integer');
177
178 return $sql;
179 }
180
181
187 public function loadPositions(array $user_ids) : array
188 {
192 $positions = [];
193
194 $assignments = ilOrgUnitUserAssignment::where(['user_id' => $user_ids])->get();
195 if (count($assignments) > 0) {
196 foreach ($assignments as $assignment) {
197 $org_unit_user = ilOrgUnitUser::getInstanceById($assignment->getUserId());
198 $org_unit_user->addPositions(ilOrgUnitPosition::find($assignment->getPositionId()));
199 }
200 }
201
202 return $positions;
203 }
204
205
211 private function loadUsersByUserIds(array $user_ids) : array
212 {
213 $users = array();
214
215 $q = "SELECT * FROM usr_data WHERE " . $this->dic->database()->in('usr_id', $user_ids, false, 'int');
216
217 $set = $this->dic->database()->query($q);
218
219 while ($row = $this->dic->database()->fetchAssoc($set)) {
220 $users[] = ilOrgUnitUser::getInstance($row['usr_id'], (string)$row['login'], (string)$row['email'], (string)$row['second_email']);
221 }
222
223 return $users;
224 }
225}
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
__construct()
ilOrgUnitUserRepository constructor.
static getInstanceById(int $user_id)
static getInstance(int $user_id, string $login, string $email, string $second_email)
Class ilOrgUnitPosition.
Class ilOrgUnitUserAssignment.
global $DIC
Definition: goto.php:24
$data
Definition: storeScorm.php:23