ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitUserAssignmentGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
32 {
33  public const CMD_ASSIGNMENTS_RECURSIVE = 'assignmentsRecursive';
34  public const SUBTAB_ASSIGNMENTS = 'user_assignments';
35  public const SUBTAB_ASSIGNMENTS_RECURSIVE = 'user_assignments_recursive';
36  private \ilGlobalTemplateInterface $main_tpl;
37  private Services $http;
38  private ilCtrl $ctrl;
42  private \ilOrgUnitPositionDBRepository $positionRepo;
43  private \ilOrgUnitUserAssignmentDBRepository $assignmentRepo;
44 
45  public function __construct()
46  {
47  global $DIC;
48 
50 
51  $this->main_tpl = $DIC->ui()->mainTemplate();
52  $this->http = $DIC->http();
53  $this->ctrl = $DIC->ctrl();
54  $this->toolbar = $DIC->toolbar();
55  $this->access = $DIC->access();
56  $this->language = $DIC->language();
57 
59  $this->positionRepo = $dic["repo.Positions"];
60  $this->assignmentRepo = $dic["repo.UserAssignments"];
61  }
62 
63  public function executeCommand(): void
64  {
65  if (
67  (int) filter_input(INPUT_GET, "ref_id", FILTER_SANITIZE_NUMBER_INT)
68  )
69  &&
71  (int) filter_input(INPUT_GET, "ref_id", FILTER_SANITIZE_NUMBER_INT)
72  )
73  ) {
74  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("permission_denied"), true);
75  $this->ctrl->redirectByClass(ilObjOrgUnitGUI::class);
76  }
77 
78  $r = $this->http->request();
79  switch ($this->ctrl->getNextClass()) {
80  case strtolower(ilRepositorySearchGUI::class):
81  switch ($this->ctrl->getCmd()) {
82  case 'addUserFromAutoComplete':
83  if ($r->getQueryParams()['addusertype'] == "staff") {
84  $this->addStaff();
85  }
86  break;
87  default:
88  $repo = new ilRepositorySearchGUI();
89  $repo->setCallback($this, 'addStaffFromSearch');
90  $this->ctrl->forwardCommand($repo);
91  break;
92  }
93  break;
94 
95  default:
96  parent::executeCommand();
97  break;
98  }
99  }
100 
101  protected function index(): void
102  {
103  $this->addSubTabs();
104  $this->activeSubTab(self::SUBTAB_ASSIGNMENTS);
105 
106  // Header
107  $types = $this->positionRepo->getArray('id', 'title');
108 
109  $this->ctrl->setParameterByClass(ilRepositorySearchGUI::class, 'addusertype', 'staff');
111  'auto_complete_name' => $this->language->txt('user'),
112  'user_type' => $types,
113  'submit_name' => $this->language->txt('add'),
114  ));
115 
116  // Tables
117  $html = '';
118  foreach ($this->positionRepo->getPositionsForOrgUnit($this->getParentRefId()) as $ilOrgUnitPosition) {
119  $ilOrgUnitUserAssignmentTableGUI = new ilOrgUnitUserAssignmentTableGUI(
120  $this,
121  self::CMD_INDEX,
122  $ilOrgUnitPosition
123  );
124  $html .= $ilOrgUnitUserAssignmentTableGUI->getHTML();
125  }
126  $this->setContent($html);
127  }
128 
129  protected function assignmentsRecursive(): void
130  {
131  $this->addSubTabs();
132  $this->activeSubTab(self::SUBTAB_ASSIGNMENTS_RECURSIVE);
133  // Tables
134  $html = '';
135  foreach ($this->positionRepo->getPositionsForOrgUnit($this->getParentRefId()) as $ilOrgUnitPosition) {
136  $ilOrgUnitRecursiveUserAssignmentTableGUI =
138  $this,
139  self::CMD_ASSIGNMENTS_RECURSIVE,
140  $ilOrgUnitPosition
141  );
142  $html .= $ilOrgUnitRecursiveUserAssignmentTableGUI->getHTML();
143  }
144  $this->setContent($html);
145  }
146 
147  protected function confirm(): void
148  {
149  $confirmation = $this->getConfirmationGUI();
150  $confirmation->setConfirm($this->language->txt('remove_user'), self::CMD_DELETE);
151 
152  $this->setContent($confirmation->getHTML());
153  }
154 
155  protected function confirmRecursive(): void
156  {
157  $confirmation = $this->getConfirmationGUI();
158  $confirmation->setConfirm($this->language->txt('remove_user'), self::CMD_DELETE_RECURSIVE);
159 
160  $this->setContent($confirmation->getHTML());
161  }
162 
163  protected function getConfirmationGUI(): ilConfirmationGUI
164  {
165  $this->ctrl->saveParameter($this, 'position_id');
166  $confirmation = new ilConfirmationGUI();
167  $confirmation->setFormAction($this->ctrl->getFormAction($this));
168  $confirmation->setCancel($this->language->txt(self::CMD_CANCEL), self::CMD_CANCEL);
169 
170  $params = $this->http->request()->getQueryParams();
171  $usr_id = $params['usr_id'];
172  $position_id = $params['position_id'];
173 
174  $types = $this->positionRepo->getArray('id', 'title');
175  $position_title = $types[$position_id];
176 
177  $confirmation->setHeaderText(sprintf($this->language->txt('msg_confirm_remove_user'), $position_title));
178  $confirmation->addItem('usr_id', $usr_id, ilObjUser::_lookupLogin((int) $usr_id));
179 
180  return $confirmation;
181  }
182 
183  protected function delete(): void
184  {
185  $params = $this->http->request()->getQueryParams();
186  $usr_id = (int) $_POST['usr_id'];
187  $position_id = (int) $params['position_id'];
188 
189  $assignment = $this->assignmentRepo->find(
190  $usr_id,
191  $position_id,
192  $this->getParentRefId()
193  );
194  if (!$assignment) {
195  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("user_not_found_to_delete"), true);
196  $this->ctrl->redirect($this, self::CMD_INDEX);
197  }
198  $this->assignmentRepo->delete($assignment);
199 
200  $this->main_tpl->setOnScreenMessage('success', $this->language->txt('remove_successful'), true);
201  $this->cancel();
202  }
203 
204  protected function deleteRecursive(): void
205  {
206  $r = $this->http->request();
207  $assignments = $this->assignmentRepo
208  ->getByUserAndPosition((int) $_POST['usr_id'], (int) $r->getQueryParams()['position_id']);
209 
210  foreach ($assignments as $assignment) {
211  $this->assignmentRepo->delete($assignment);
212  }
213  $this->main_tpl->setOnScreenMessage('success', $this->language->txt('remove_successful'), true);
214  $this->cancel();
215  }
216 
217  protected function cancel(): void
218  {
219  $this->ctrl->redirect($this, self::CMD_INDEX);
220  }
221 
222  public function addStaff(): void
223  {
224  if (!$this->access->checkAccess("write", "", $this->getParentRefId())) {
225  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("permission_denied"), true);
226  $this->ctrl->redirect($this, self::CMD_INDEX);
227  }
228 
229  $users = explode(',', $_POST['user_login']);
230  $user_ids = array();
231  foreach ($users as $user) {
232  $user_id = ilObjUser::_lookupId($user);
233  if ($user_id) {
234  $user_ids[] = $user_id;
235  }
236  }
237 
238  if (!count($user_ids)) {
239  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("user_not_found"), true);
240  $this->ctrl->redirect($this, self::CMD_INDEX);
241  }
242 
243  $position_id = (int) ($_POST['user_type'] ?? ilOrgUnitPosition::CORE_POSITION_EMPLOYEE);
244 
245  if ($position_id === 0 || !$this->positionRepo->getSingle($position_id, 'id')) {
246  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("user_not_found"), true);
247  $this->ctrl->redirect($this, self::CMD_INDEX);
248  }
249  foreach ($user_ids as $user_id) {
250  $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getParentRefId());
251  }
252 
253  $this->main_tpl->setOnScreenMessage('success', $this->language->txt("users_successfuly_added"), true);
254  $this->ctrl->redirect($this, self::CMD_INDEX);
255  }
256 
260  public function addStaffFromSearch(array $user_ids, ?string $user_type = null): void
261  {
262  if (!$this->access->checkAccess("write", "", $this->getParentRefId())) {
263  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("permission_denied"), true);
264  $this->ctrl->redirect($this, self::CMD_INDEX);
265  }
266 
267  if (!count($user_ids)) {
268  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("user_not_found"), true);
269  $this->ctrl->redirect($this, self::CMD_INDEX);
270  }
271 
272  $position_id = (int) ($user_type ?? ilOrgUnitPosition::CORE_POSITION_EMPLOYEE);
273 
274  if ($position_id === 0 || !$this->positionRepo->getSingle($position_id, 'id')) {
275  $this->main_tpl->setOnScreenMessage('failure', $this->language->txt("user_not_found"), true);
276  $this->ctrl->redirect($this, self::CMD_INDEX);
277  }
278  foreach ($user_ids as $user_id) {
279  $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getParentRefId());
280  }
281 
282  $this->main_tpl->setOnScreenMessage('success', $this->language->txt("users_successfuly_added"), true);
283  $this->ctrl->redirect($this, self::CMD_INDEX);
284  }
285 
286  public function addSubTabs(): void
287  {
288  $this->pushSubTab(self::SUBTAB_ASSIGNMENTS, $this->ctrl
289  ->getLinkTarget($this, self::CMD_INDEX));
290  $this->pushSubTab(self::SUBTAB_ASSIGNMENTS_RECURSIVE, $this->ctrl
291  ->getLinkTarget(
292  $this,
293  self::CMD_ASSIGNMENTS_RECURSIVE
294  ));
295  }
296 }
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
Class ilOrgUnitUserAssignmentGUI.
static _lookupId($a_user_str)
global $DIC
Definition: feed.php:28
pushSubTab(string $subtab_id, string $url)
static http()
Fetches the global http state from ILIAS.
__construct(VocabulariesInterface $vocabularies)
ilOrgUnitPositionDBRepository $positionRepo
addStaffFromSearch(array $user_ids, ?string $user_type=null)
static fillAutoCompleteToolbar(object $parent_object, ilToolbarGUI $toolbar=null, array $a_options=[], bool $a_sticky=false)
array( auto_complete_name = $lng->txt(&#39;user&#39;), auto_complete_size = 15, user_type = array(ilCoursePar...
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
static _checkAccessStaff(int $ref_id)
static _checkAccessPositions(int $ref_id)
$dic
Definition: result.php:32
Class ilOrgUnitUserAssignmentTableGUI.
$r
static _lookupLogin(int $a_user_id)