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