ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitPositionGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
30{
32
33 public const SUBTAB_SETTINGS = 'settings';
34 public const SUBTAB_PERMISSIONS = 'obj_orgunit_positions';
35 public const CMD_CONFIRM_DELETION = 'confirmDeletion';
36 public const CMD_ASSIGN = 'assign';
37
39 protected \ILIAS\UI\Component\Link\Factory $link_factory;
40 protected \ilOrgUnitPositionDBRepository $positionRepo;
41 protected \ilOrgUnitUserAssignmentDBRepository $assignmentRepo;
42 protected \ilOrgUnitPermissionDBRepository $permissionRepo;
43 protected \ilObjectDefinition $objectDefinition;
45
46
47 public function __construct()
48 {
50 $this->positionRepo = $dic["repo.Positions"];
51 $this->assignmentRepo = $dic["repo.UserAssignments"];
52 $this->permissionRepo = $dic["repo.Permissions"];
53
54 $to_int = $dic['refinery']->kindlyTo()->int();
55 $ref_id = $dic['query']->retrieve('ref_id', $to_int);
56 $this->link_factory = $dic['ui.factory']->link();
57
59
60 global $DIC;
61 $this->toolbar = $DIC->toolbar();
62 $this->objectDefinition = $DIC["objDefinition"];
63
64 $this->initRequest(
65 $DIC->http(),
66 $dic['refinery']
67 );
68
70 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
71 $this->ctrl->redirectByClass(ilObjOrgUnitGUI::class);
72 }
73
74 $this->post = $DIC->http()->wrapper()->post();
75 }
76
77 protected function getPossibleNextClasses(): array
78 {
79 return array(
80 ilOrgUnitUserAssignmentGUI::class,
81 );
82 }
83
84 protected function getActiveTabId(): string
85 {
87 }
88
89 protected function index(): void
90 {
91 $url = $this->getSinglePosLinkTarget(self::CMD_ADD, 0);
92 $link = $this->link_factory->standard(
93 $this->lng->txt('add_position'),
94 $url
95 );
96 $this->toolbar->addComponent($link);
97
98 $table = $this->getTable()->withRequest($this->request);
99 $this->tpl->setContent($this->ui_renderer->render($table));
100 }
101
102 protected function add(): void
103 {
104 $position = $this->positionRepo->create();
105 $form = new ilOrgUnitPositionFormGUI($this, $position);
106 $this->tpl->setContent($form->getHTML());
107 }
108
109 protected function create(): void
110 {
111 $this->redirectIfCancelled();
112 $form = new ilOrgUnitPositionFormGUI($this, $this->positionRepo->create());
113 if ($form->saveObject() === true) {
114 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_position_created'), true);
115 $this->ctrl->redirect($this, self::CMD_INDEX);
116 }
117
118 $this->tpl->setContent($form->getHTML());
119 }
120
121 protected function edit(): void
122 {
123 $this->addSubTabs();
124 $this->activeSubTab(self::SUBTAB_SETTINGS);
125 $position = $this->getPositionFromRequest();
126 $form = new ilOrgUnitPositionFormGUI($this, $position);
127 $form->fillForm();
128 $this->tpl->setContent($form->getHTML());
129 }
130
131 protected function update(): void
132 {
133 $this->redirectIfCancelled();
134 $position = $this->getPositionFromRequest();
135 $form = new ilOrgUnitPositionFormGUI($this, $position);
136 $form->setValuesByPost();
137 if ($form->saveObject() === true) {
138 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_position_updated'), true);
139 $this->ctrl->redirect($this, self::CMD_INDEX);
140 }
141
142 $this->tpl->setContent($form->getHTML());
143 }
144
145 protected function defaultPermissions(): void
146 {
147 $this->addSubTabs();
148 $this->activeSubTab(self::SUBTAB_PERMISSIONS);
149 $form = $this->getDefaultPermissionsForm($this->getRowIdFromQuery());
150 $this->tpl->setContent($this->ui_renderer->render($form));
151 }
152
153 protected function updateDefaultPermissions(): void
154 {
155 $form = $this->getDefaultPermissionsForm($this->getRowIdFromQuery())
156 ->withRequest($this->request);
157 if ($form->getData()) {
158 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_permission_saved'), true);
159 $this->defaultPermissions();
160 } else {
161 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_success_permission_not_saved'), true);
162 $this->tpl->setContent($this->ui_renderer->render($form));
163 }
164 }
165
166 protected function getDefaultPermissionsForm(int $position_id): StandardForm
167 {
168 $sections = [];
169 $sections[] = $this->ui_factory->input()->field()->section(
170 [],
171 $this->lng->txt("form_title_org_default_permissions_update")
172 );
173
174 $form_action = $this->getSinglePosLinkTarget('updateDefaultPermissions', $position_id);
175 $permissions = $this->permissionRepo->getDefaultsForActiveContexts($position_id);
176 $permission_repo = $this->permissionRepo;
177 foreach ($permissions as $perm) {
178 $fields = [];
179 $operations = $perm->getPossibleOperations();
180 foreach ($operations as $operation) {
181 $fields[$operation->getOperationId()] = $this->ui_factory->input()->field()
182 ->checkbox($this->lng->txt("org_op_{$operation->getOperationString()}"))
183 ->withValue(
184 $perm->isOperationIdSelected($operation->getOperationId())
185 );
186 }
187
188 $context = $perm->getContext()->getContext();
189 $sections[$perm->getId()] = $this->ui_factory->input()->field()
190 ->section($fields, $this->getTitleForFormHeaderByContext($context))
191 ->withAdditionalTransformation(
192 $this->refinery->custom()->transformation(
193 function ($v) use ($operations, $perm, $permission_repo) {
194 $v = array_filter($v);
195 $nu_ops = array_filter($operations, fn($o) => array_key_exists($o->getOperationId(), $v));
196 $protected = $perm->isProtected();
197 //$perm = $permission_repo->update($perm);
198 $perm = $perm->withOperations($nu_ops)->withProtected(false);
199 $permission_repo->store($perm);
200 //$perm=$perm->withProtected($protected);
201 //$permission_repo->store($perm);
202 return true;
203 }
204 )
205 );
206 }
207
208 return $this->ui_factory->input()->container()->form()->standard($form_action, $sections);
209 }
210
211 protected function getTitleForFormHeaderByContext(string $context)
212 {
213 $lang_code = "obj_{$context}";
214 if ($this->objectDefinition->isPlugin($context)) {
215 return ilObjectPlugin::lookupTxtById($context, $lang_code);
216 }
217 return $this->lng->txt($lang_code);
218 }
219
220 protected function redirectIfCancelled()
221 {
222 if ($this->post->has('cmd')) {
223 $cmd = $this->post->retrieve(
224 'cmd',
225 $this->refinery->custom()->transformation(
226 fn($v) => array_key_first($v)
227 )
228 );
229 if ($cmd === self::CMD_CANCEL) {
230 $url = $this->url_builder
231 ->withParameter($this->action_token, self::CMD_INDEX)
232 ->buildURI()
233 ->__toString();
234 $this->ctrl->redirectToURL($url);
235 }
236 }
237 }
238
239 protected function assign(int $position_id): void
240 {
241 $position = $this->positionRepo->getSingle($position_id, 'id');
242 if ($position->isCorePosition()) {
243 $this->cancel();
244 }
245
246 $employee_position = $this->positionRepo->getSingle(ilOrgUnitPosition::CORE_POSITION_EMPLOYEE, 'core_identifier');
247 $assignments = $this->assignmentRepo->getByPosition($position->getId());
248 foreach ($assignments as $assignment) {
249 $this->assignmentRepo->store($assignment->withPositionId($employee_position->getId()));
250 }
251
252 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_assignment_to_employee_done'), true);
253 }
254
255 protected function confirmDeletion(): void
256 {
257 $position = $this->getPositionFromRequest();
258 if ($position->isCorePosition()) {
259 $this->cancel();
260 }
261 $this->lng->loadLanguageModule('orgu');
262 $position_string = $this->lng->txt("position") . ": ";
263 $authority_string = $this->lng->txt("authorities") . ": ";
264 $user_string = $this->lng->txt("user_assignments") . ": ";
265
266 $confirmation = new ilConfirmationGUI();
267 $confirmation->setFormAction($this->ctrl->getFormAction($this));
268 $confirmation->setCancel($this->lng->txt(self::CMD_CANCEL), self::CMD_CANCEL);
269 $confirmation->setConfirm($this->lng->txt(self::CMD_DELETE), self::CMD_DELETE);
270 $confirmation->setHeaderText($this->lng->txt('msg_confirm_deletion'));
271 $confirmation->addItem(self::AR_ID, (string) $position->getId(), $position_string
272 . $position->getTitle());
273 // Authorities
274 $authority_string .= implode(", ", $this->getAuthorityDescription($position->getAuthorities()));
275 $confirmation->addItem('authorities', '', $authority_string);
276
277 // Amount uf user-assignments
278 $userIdsOfPosition = $this->assignmentRepo->getUsersByPosition($position->getId());
279 $ilOrgUnitUserQueries = new ilOrgUnitUserQueries();
280 $usersOfPosition = $ilOrgUnitUserQueries->findAllUsersByUserIds($userIdsOfPosition);
281 $userNames = $ilOrgUnitUserQueries->getAllUserNames($usersOfPosition);
282
283 $confirmation->addItem('users', '', $user_string . implode(', ', $userNames));
284
285 $checkbox_assign_users = new ilCheckboxInputGUI('', 'assign_users');
286 $checkbox_assign_users->setChecked(true);
287 $checkbox_assign_users->setValue('1');
288 $checkbox_assign_users->setOptionTitle('Assign affected users to employee role');
289 $confirmation->addItem('assign_users', '', $checkbox_assign_users->render());
290
291 $this->tpl->setContent($confirmation->getHTML());
292 }
293
294 protected function delete(): void
295 {
296 $position_id = $this->post->retrieve(
297 self::AR_ID,
298 $this->refinery->byTrying([
299 $this->refinery->kindlyTo()->int(),
300 $this->refinery->always(null)
301 ])
302 );
303
304 if ($position_id === null) {
305 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_position_delete_fail'), true);
306 $this->ctrl->redirect($this, self::CMD_INDEX);
307 }
308
309 if ($this->post->has('assign_users')
310 && $this->post->retrieve(
311 'assign_users',
312 $this->refinery->byTrying([
313 $this->refinery->kindlyTo()->bool(),
314 $this->refinery->always(false)
315 ])
316 )
317 ) {
318 $this->assign($position_id);
319 }
320 $this->positionRepo->delete($position_id);
321 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_deleted'), true);
322 $this->ctrl->redirect($this, self::CMD_INDEX);
323 }
324
325 protected function cancel(): void
326 {
327 $this->ctrl->redirect($this, self::CMD_INDEX);
328 }
329
331 {
332 $id = $this->getRowIdFromQuery();
333 return $this->positionRepo->getSingle($id, 'id');
334 }
335
336 public function getSinglePosLinkTarget(string $action, ?int $pos_id = null): string
337 {
338 $target_id = $pos_id !== null ? [$pos_id] : [$this->getRowIdFromQuery()];
339 return $this->url_builder
340 ->withParameter($this->row_id_token, $target_id)
341 ->withParameter($this->action_token, $action)
342 ->buildURI()->__toString();
343 }
344
345 public function addSubTabs(): void
346 {
347 $this->pushSubTab(
348 self::SUBTAB_SETTINGS,
349 $this->getSinglePosLinkTarget(self::CMD_EDIT)
350 );
351 $this->pushSubTab(
352 self::SUBTAB_PERMISSIONS,
353 $this->getSinglePosLinkTarget(self::CMD_DEFAULT_PERMISSIONS)
354 );
355 }
356
357 protected function getTable(): Table\Data
358 {
359 $columns = [
360 'title' => $this->ui_factory->table()->column()->text($this->lng->txt("title")),
361 'description' => $this->ui_factory->table()->column()->text($this->lng->txt("description")),
362 'authorities' => $this->ui_factory->table()->column()->status($this->lng->txt("authorities"))
363 ->withIsSortable(false),
364 ];
365
366 $actions = [
367 'edit' => $this->ui_factory->table()->action()->single(
368 $this->lng->txt('edit'),
369 $this->url_builder->withParameter($this->action_token, "edit"),
370 $this->row_id_token
371 ),
372 'delete' => $this->ui_factory->table()->action()->single(
373 $this->lng->txt('delete'),
374 $this->url_builder->withParameter($this->action_token, "confirmDeletion"),
375 $this->row_id_token
376 ),
377 ];
378
379 return $this->ui_factory->table()
380 ->data($this->positionRepo, '', $columns)
381 ->withId('orgu_positions')
382 ->withActions($actions);
383 }
384
390 private function getAuthorityDescription(array $authorities): array
391 {
393 $lang->loadLanguageModule('orgu');
394 $lang_keys = array(
395 'in',
396 'over',
400 );
401 $t = [];
402 foreach ($lang_keys as $key) {
403 $t[$key] = $lang->txt($key);
404 }
405
406 $authority_description = [];
407 foreach ($authorities as $authority) {
408 switch ($authority->getOver()) {
410 $over_txt = $t["over_" . $authority->getOver()];
411 break;
412 default:
413 $over_txt = $this->positionRepo
414 ->getSingle($authority->getOver(), 'id')
415 ->getTitle();
416 break;
417 }
418
419 $authority_description[] = " " . $t["over"] . " " . $over_txt . " " . $t["in"] . " " . $t["scope_" . $authority->getScope()];
420 }
421
422 return $authority_description;
423 }
424}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
trait BaseGUIRequest
Base gui request wrapper.
pushSubTab(string $subtab_id, string $url)
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _checkAccessPositions(int $ref_id)
static lookupTxtById(string $plugin_id, string $lang_var)
Class ilOrgUnitPositionFormGUI.
Class ilOrgUnitPositionGUI.
getDefaultPermissionsForm(int $position_id)
getAuthorityDescription(array $authorities)
Returns descriptions for authorities as an array of strings.
getSinglePosLinkTarget(string $action, ?int $pos_id=null)
ilOrgUnitPermissionDBRepository $permissionRepo
ilOrgUnitPositionDBRepository $positionRepo
getTitleForFormHeaderByContext(string $context)
ILIAS HTTP Wrapper ArrayBasedRequestWrapper $post
ILIAS UI Component Link Factory $link_factory
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
ilObjectDefinition $objectDefinition
Class ilOrgUnitPosition.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes a standard form.
Definition: Standard.php:29
This describes a Data Table.
Definition: Data.php:31
withId(string $id)
The DataTable comes with a storage to keep e.g.
$ref_id
Definition: ltiauth.php:66
$dic
Definition: ltiresult.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
initRequest(HTTP\Services $http, Refinery\Factory $refinery, ?array $passed_query_params=null, ?array $passed_post_data=null)
Query params and post data parameters are used for testing.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68
$context
Definition: webdav.php:31
$lang
Definition: xapiexit.php:25