ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitUserAssignmentGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
32{
33 public const SUBTAB_ASSIGNMENTS = 'user_assignments';
34 public const SUBTAB_ASSIGNMENTS_RECURSIVE = 'user_assignments_recursive';
35 public const CMD_ASSIGNMENTS_RECURSIVE = 'assignmentsRecursive';
36 public const CMD_REMOVE_CONFIRM = 'confirmRemove';
37 public const CMD_REMOVE = 'remove';
38 public const CMD_REMOVE_RECURSIVELY_CONFIRM = 'confirmRemoveRecursively';
39 public const CMD_REMOVE_RECURSIVE = "removeRecursive";
40 public const CMD_SHOW_LP = 'showLearningProgress';
41
43 private \ilOrgUnitPositionDBRepository $positionRepo;
44 private \ilOrgUnitUserAssignmentDBRepository $assignmentRepo;
45
46 public function __construct()
47 {
48 parent::__construct(['orgu', 'staff']);
49
50 global $DIC;
51 $this->toolbar = $DIC->toolbar();
52
54 $this->positionRepo = $dic["repo.Positions"];
55 $this->assignmentRepo = $dic["repo.UserAssignments"];
56 }
57
58 public function executeCommand(): void
59 {
60 if (
62 (int) filter_input(INPUT_GET, "ref_id", FILTER_SANITIZE_NUMBER_INT)
63 )
64 &&
66 (int) filter_input(INPUT_GET, "ref_id", FILTER_SANITIZE_NUMBER_INT)
67 )
68 ) {
69 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
70 $this->ctrl->redirectByClass(ilObjOrgUnitGUI::class);
71 }
72
73 $r = $this->http->request();
74 switch ($this->ctrl->getNextClass()) {
75 case strtolower(ilRepositorySearchGUI::class):
76 switch ($this->ctrl->getCmd()) {
77 case 'addUserFromAutoComplete':
78 if ($r->getQueryParams()['addusertype'] == "staff") {
79 $this->addStaff();
80 }
81 break;
82 default:
83 $repo = new ilRepositorySearchGUI();
84 $repo->setCallback($this, 'addStaffFromSearch');
85 $this->ctrl->forwardCommand($repo);
86 break;
87 }
88 break;
89
90 default:
91 parent::executeCommand();
92 break;
93 }
94 }
95
96 protected function index(): void
97 {
98 $this->addSubTabs();
99 $this->activeSubTab(self::SUBTAB_ASSIGNMENTS);
100
101 $types = $this->positionRepo->getArray('id', 'title');
102 $this->ctrl->setParameterByClass(ilRepositorySearchGUI::class, 'addusertype', 'staff');
103
104 if ($this->access->checkAccess("write", "", $this->getParentRefId())) {
106 'auto_complete_name' => $this->lng->txt('user'),
107 'user_type' => $types,
108 'submit_name' => $this->lng->txt('add'),
109 ));
110 }
111
112 $tables = [];
113 foreach ($this->positionRepo->getPositionsForOrgUnit($this->getParentRefId()) as $ilOrgUnitPosition) {
114 $tables[] = $this->getStaffTable($ilOrgUnitPosition, [$this->getParentRefId()], false);
115 }
116 $this->setContent($this->ui_renderer->render($tables));
117 }
118
119 protected function assignmentsRecursive(): void
120 {
121 $this->addSubTabs();
122 $this->activeSubTab(self::SUBTAB_ASSIGNMENTS_RECURSIVE);
123
124 $orgu_ref_id = $this->getParentRefId();
125 $orgu_tree = ilObjOrgUnitTree::_getInstance();
126 $permission_access_staff_recursive = [];
127 // maybe any parent gives us recursive permission
128 (int) $root = (int) ilObjOrgUnit::getRootOrgRefId();
129 $parent = (int) $orgu_tree->getParent($orgu_ref_id);
130
131 while ($parent !== $root) {
133 array_merge(
134 $permission_access_staff_recursive = $permission_access_staff_recursive,
135 $orgu_tree->getAllChildren($parent)
136 );
137 }
138 $parent = (int) $orgu_tree->getParent($parent);
139 }
140
141 foreach ($orgu_tree->getAllChildren($orgu_ref_id) as $ref_id) {
142 $recursive = in_array($ref_id, $permission_access_staff_recursive);
143 if (!$recursive) {
144 // ok, so no permission from above, lets check local permissions
146 // update recursive permissions
147 $permission_access_staff_recursive = array_merge(
148 $permission_access_staff_recursive,
149 $orgu_tree->getAllChildren($ref_id)
150 );
152 // skip orgus in which one may not view the staff
153 continue;
154 }
155 }
156 }
157
158 $tables = [];
159 foreach ($this->positionRepo->getPositionsForOrgUnit($this->getParentRefId()) as $ilOrgUnitPosition) {
160 $tables[] = $this->getStaffTable($ilOrgUnitPosition, $permission_access_staff_recursive, true);
161 }
162 $this->setContent($this->ui_renderer->render($tables));
163 }
164
165 protected function confirmRemove(bool $recursive = false): void
166 {
167 list($position_id, $usr_id) = $this->getPositionAndUserIdFromTableQuery();
168
169 $id = implode('_', [(string) $position_id, (string) $usr_id]);
170 $usr_name = ilObjUser::_lookupLogin($usr_id);
171 $pos_name = $this->positionRepo->getSingle($position_id, 'id')->getTitle();
172
173 $item = $this->ui_factory->modal()->interruptiveItem()
174 ->keyValue($id, $usr_name, $pos_name);
175
176 $del_command = $recursive ? self::CMD_REMOVE_RECURSIVE : self::CMD_REMOVE;
177 $action = $this->url_builder
178 ->withParameter($this->row_id_token, $id)
179 ->withParameter($this->action_token, $del_command)
180 ->buildURI()->__toString();
181
182 echo($this->ui_renderer->renderAsync([
183 $this->ui_factory->modal()->interruptive(
184 $this->lng->txt('remove_user'),
185 sprintf($this->lng->txt('msg_confirm_remove_user'), $pos_name),
186 $action
187 )->withAffectedItems([$item])
188 ]));
189 exit();
190 }
191
192 protected function confirmRemoveRecursively(): void
193 {
194 $this->confirmRemove(true);
195 }
196
197 protected function remove(): void
198 {
199 list($position_id, $usr_id) = $this->getPositionAndUserIdFromTableQuery();
200
201 $assignment = $this->assignmentRepo->find(
202 $usr_id,
203 $position_id,
204 $this->getParentRefId()
205 );
206 if (!$assignment) {
207 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("user_not_found_to_delete"), true);
208 $this->ctrl->redirect($this, self::CMD_INDEX);
209 }
210 $this->assignmentRepo->delete($assignment);
211
212 $this->tpl->setOnScreenMessage('success', $this->lng->txt('remove_successful'), true);
213 $this->cancel();
214 }
215
216 protected function removeRecursive(): void
217 {
218 list($position_id, $usr_id) = $this->getPositionAndUserIdFromTableQuery();
219 $assignments = $this->assignmentRepo->getByUserAndPosition($usr_id, $position_id);
220 foreach ($assignments as $assignment) {
221 $this->assignmentRepo->delete($assignment);
222 }
223 $this->tpl->setOnScreenMessage('success', $this->lng->txt('remove_successful'), true);
224 $this->assignmentsRecursive();
225 }
226
227 protected function showLearningProgress(): void
228 {
229 list($position_id, $usr_id) = $this->getPositionAndUserIdFromTableQuery();
230 $this->ctrl->setParameterByClass(ilLearningProgressGUI::class, 'obj_id', $usr_id);
231 $target = $this->ctrl->getLinkTargetByClass(ilLearningProgressGUI::class, "");
232 $this->ctrl->redirectToURL($target);
233 }
234
235 protected function getStaffTable(
236 ilOrgUnitPosition $position,
237 array $orgu_ids,
238 bool $recursive = false
239 ): Table\Data {
240 $columns = [
241 'login' => $this->ui_factory->table()->column()->text($this->lng->txt("login")),
242 'firstname' => $this->ui_factory->table()->column()->text($this->lng->txt("firstname")),
243 'lastname' => $this->ui_factory->table()->column()->text($this->lng->txt("lastname")),
244 'active' => $this->ui_factory->table()->column()->boolean(
245 $this->lng->txt("active"),
246 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small'),
247 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small')
248 )->withIsOptional(true, false),
249 ];
250
251 $remove_cmd = self::CMD_REMOVE_CONFIRM;
252 if ($recursive) {
254 $columns['orgu_title'] = $this->ui_factory->table()->column()->text($this->lng->txt("obj_orgu"));
255 }
256
257 $actions = [
258 'remove' => $this->ui_factory->table()->action()->single(
259 $this->lng->txt('remove'),
260 $this->url_builder->withParameter($this->action_token, $remove_cmd),
261 $this->row_id_token
262 )->withAsync(),
263
264 'show_learning_progress' => $this->ui_factory->table()->action()->single(
265 $this->lng->txt('show_learning_progress'),
266 $this->url_builder->withParameter($this->action_token, self::CMD_SHOW_LP),
267 $this->row_id_token
268 ),
269 ];
270
271 $lp_visible = array_filter(
272 $orgu_ids,
273 fn($id) => $this->access->checkAccess("view_learning_progress", "", $id)
274 );
275
276 return $this->ui_factory->table()
277 ->data($this->assignmentRepo, $position->getTitle(), $columns)
278 ->withId(implode('.', ['orgustaff',$this->getParentRefId(),$position->getId()]))
279 ->withActions($actions)
280 ->withAdditionalParameters([
281 'position_id' => $position->getId(),
282 'orgu_ids' => $orgu_ids,
283 'lp_visible_ref_ids' => $lp_visible,
284 'write_access' => $this->access->checkAccess("write", "", $this->getParentRefId())
285 ])
286 ->withRequest($this->request);
287 }
288
292 protected function getPositionAndUserIdFromTableQuery(): array
293 {
294 if ($this->query->has($this->row_id_token->getName())) {
295 return $this->query->retrieve(
296 $this->row_id_token->getName(),
297 $this->refinery->custom()->transformation(
298 function ($v) {
299 $id = is_array($v) ? array_shift($v) : $v;
300 return array_map('intval', explode('_', $id));
301 }
302 )
303 );
304 }
305 throw new \Exception('no position/user id in query');
306 }
307
308 protected function cancel(): void
309 {
310 $this->ctrl->redirect($this, self::CMD_INDEX);
311 }
312
313 public function addStaff(): void
314 {
315 if (!$this->access->checkAccess("write", "", $this->getParentRefId())) {
316 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
317 $this->ctrl->redirect($this, self::CMD_INDEX);
318 }
319
320 $users = explode(',', $_POST['user_login']);
321 $user_ids = array();
322 foreach ($users as $user) {
324 if ($user_id) {
325 $user_ids[] = $user_id;
326 }
327 }
328
329 if (!count($user_ids)) {
330 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("user_not_found"), true);
331 $this->ctrl->redirect($this, self::CMD_INDEX);
332 }
333
334 $position_id = (int) ($_POST['user_type'] ?? ilOrgUnitPosition::CORE_POSITION_EMPLOYEE);
335
336 if ($position_id === 0 || !$this->positionRepo->getSingle($position_id, 'id')) {
337 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("user_not_found"), true);
338 $this->ctrl->redirect($this, self::CMD_INDEX);
339 }
340 foreach ($user_ids as $user_id) {
341 $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getParentRefId());
342 }
343
344 $this->tpl->setOnScreenMessage('success', $this->lng->txt("users_successfuly_added"), true);
345 $this->ctrl->redirect($this, self::CMD_INDEX);
346 }
347
351 public function addStaffFromSearch(array $user_ids, ?string $user_type = null): void
352 {
353 if (!$this->access->checkAccess("write", "", $this->getParentRefId())) {
354 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
355 $this->ctrl->redirect($this, self::CMD_INDEX);
356 }
357
358 if (!count($user_ids)) {
359 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("user_not_found"), true);
360 $this->ctrl->redirect($this, self::CMD_INDEX);
361 }
362
363 $position_id = (int) ($user_type ?? ilOrgUnitPosition::CORE_POSITION_EMPLOYEE);
364
365 if ($position_id === 0 || !$this->positionRepo->getSingle($position_id, 'id')) {
366 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("user_not_found"), true);
367 $this->ctrl->redirect($this, self::CMD_INDEX);
368 }
369 foreach ($user_ids as $user_id) {
370 $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getParentRefId());
371 }
372
373 $this->tpl->setOnScreenMessage('success', $this->lng->txt("users_successfuly_added"), true);
374 $this->ctrl->redirect($this, self::CMD_INDEX);
375 }
376
377 public function addSubTabs(): void
378 {
379 $this->pushSubTab(self::SUBTAB_ASSIGNMENTS, $this->ctrl
380 ->getLinkTarget($this, self::CMD_INDEX));
381 $this->pushSubTab(self::SUBTAB_ASSIGNMENTS_RECURSIVE, $this->ctrl
382 ->getLinkTarget(
383 $this,
384 self::CMD_ASSIGNMENTS_RECURSIVE
385 ));
386 }
387}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
return true
static _checkAccessStaffRec(int $ref_id)
static _checkAccessPositions(int $ref_id)
static _checkAccessStaff(int $ref_id)
static getRootOrgRefId()
static _lookupId(string|array $a_user_str)
static _lookupLogin(int $a_user_id)
Class ilOrgUnitPosition.
Class ilOrgUnitUserAssignmentGUI.
getStaffTable(ilOrgUnitPosition $position, array $orgu_ids, bool $recursive=false)
addStaffFromSearch(array $user_ids, ?string $user_type=null)
ilOrgUnitPositionDBRepository $positionRepo
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
static fillAutoCompleteToolbar(object $parent_object, ?ilToolbarGUI $toolbar=null, array $a_options=[], bool $a_sticky=false)
array( auto_complete_name = $lng->txt('user'), auto_complete_size = 15, user_type = array(ilCoursePar...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
exit
This describes a Data Table.
Definition: Data.php:31
$_POST['cmd']
Definition: lti.php:27
$ref_id
Definition: ltiauth.php:66
$dic
Definition: ltiresult.php:33
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26