ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilUserStartingPointGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
34 {
35  private ilLogger $log;
36  private Language $lng;
40  private ilTabsGUI $tabs;
41  private ilCtrl $ctrl;
42  private ilTree $tree;
43  private ilObjUser $user;
44  private ilDBInterface $db;
51 
52  private int $parent_ref_id;
53 
54  public function __construct(int $a_parent_ref_id)
55  {
57  global $DIC;
58 
59  $this->log = ilLoggerFactory::getLogger("user");
60  $this->lng = $DIC['lng'];
61  $this->settings = $DIC['ilSetting'];
62  $this->tpl = $DIC['tpl'];
63  $this->toolbar = $DIC['ilToolbar'];
64  $this->tabs = $DIC['ilTabs'];
65  $this->ctrl = $DIC['ilCtrl'];
66  $this->tree = $DIC['tree'];
67  $this->user = $DIC['ilUser'];
68  $this->db = $DIC['ilDB'];
69  $this->rbac_review = $DIC['rbacreview'];
70  $this->rbac_system = $DIC['rbacsystem'];
71  $this->ui_factory = $DIC['ui.factory'];
72  $this->ui_renderer = $DIC['ui.renderer'];
73  $this->user_request = new UserGUIRequest(
74  $DIC->http(),
75  $DIC->refinery()
76  );
77 
78  $this->starting_point_repository = new ilUserStartingPointRepository(
79  $this->user,
80  $this->db,
81  $DIC->logger(),
86  );
87 
88  $this->parent_ref_id = $a_parent_ref_id;
89 
90  $this->lng->loadLanguageModule("administration");
91  $this->lng->loadLanguageModule("user");
92  $this->lng->loadLanguageModule("dateplaner");
93  }
94 
95  public function executeCommand(): void
96  {
97  $cmd = $this->ctrl->getCmd();
98  if ($cmd == 'roleStartingPointform' || !$cmd) {
99  $cmd = 'initRoleStartingPointForm';
100  }
101 
102  $this->$cmd();
103  }
104 
108  public function startingPoints(): void
109  {
110  $this->toolbar->addComponent(
111  $this->ui_factory->link()->standard(
112  $this->lng->txt('create_starting_point'),
113  $this->ctrl->getLinkTarget($this, "roleStartingPointform")
114  )
115  );
116 
118  $this,
119  $this->starting_point_repository,
120  $this->rbac_review,
121  $this->ui_factory,
122  $this->ui_renderer,
123  );
124 
125  $this->tpl->setContent($tbl->getHTML());
126  }
127 
128  public function initUserStartingPointForm(?ilPropertyFormGUI $form = null): void
129  {
130  if (!($form instanceof ilPropertyFormGUI)) {
131  $form = $this->getUserStartingPointForm();
132  }
133  $this->tpl->setContent($form->getHTML());
134  }
135 
136  public function initRoleStartingPointForm(?ilPropertyFormGUI $form = null): void
137  {
138  if (!($form instanceof ilPropertyFormGUI)) {
139  $form = $this->getRoleStartingPointForm();
140  }
141  $this->tpl->setContent($form->getHTML());
142  }
143 
145  {
146  $form = new ilPropertyFormGUI();
147 
148  // starting point: personal
149  $startp = new ilCheckboxInputGUI($this->lng->txt('user_chooses_starting_page'), 'usr_start_pers');
150  $startp->setInfo($this->lng->txt('adm_user_starting_point_personal_info'));
151  $startp->setChecked($this->starting_point_repository->isPersonalStartingPointEnabled());
152 
153  $form->addItem($startp);
154 
155  $form->addCommandButton('saveUserStartingPoint', $this->lng->txt('save'));
156  $form->setFormAction($this->ctrl->getFormAction($this));
157 
158  return $form;
159  }
160 
165  {
166  if (!$this->rbac_system->checkAccess('write', $this->parent_ref_id)) {
167  $this->error->raiseError(
168  $this->lng->txt('msg_no_perm_read'),
169  $this->error->FATAL
170  );
171  }
172  $form = new ilPropertyFormGUI();
173  $this->ctrl->saveParameter($this, ['spid']);
174 
175  $starting_point_id = $this->user_request->getStartingPointId();
176  $starting_point = $this->getCurrentStartingPointOrNullForStartingPointForm($starting_point_id);
177  $starting_point_type = $this->getCurrentTypeForStartingPointForm($starting_point);
178  $req_role_id = $this->user_request->getRoleId();
179 
180  foreach ($this->getFormTypeSpecificStartingPointFormParts($starting_point_id, $req_role_id) as $input) {
181  $form->addItem(
182  $input
183  );
184  }
185 
186  $si = $this->getStartingPointSelectionInput($starting_point);
187  $si->setValue((string) $starting_point_type);
188  $form->addItem($si);
189 
190  // save and cancel commands
191  $form->addCommandButton('saveStartingPoint', $this->lng->txt('save'));
192  $form->addCommandButton('startingPoints', $this->lng->txt('cancel'));
193 
194  $form->setTitle($this->lng->txt('starting_point_settings'));
195  $form->setFormAction($this->ctrl->getFormAction($this));
196 
197  return $form;
198  }
199 
200  private function getCurrentStartingPointOrNullForStartingPointForm(?int $starting_point_id): ?ilUserStartingPoint
201  {
202  if ($starting_point_id === null) {
203  return null;
204  }
205 
206  return $this->starting_point_repository->getStartingPointById(
207  $starting_point_id
208  );
209  }
210 
211  private function getCurrentTypeForStartingPointForm(?ilUserStartingPoint $starting_point): ?int
212  {
213  if ($starting_point === null) {
214  return null;
215  }
216 
217  return $starting_point->getStartingPointType();
218  }
219 
220  private function getFormTypeSpecificStartingPointFormParts(?int $spoint_id, ?int $req_role_id): Generator
221  { //edit no default
222  if ($spoint_id === null) {
223  yield $this->getCreateFormSpecificInputs();
224  } else {
225  yield from $this->getEditFormSpecificInputs($spoint_id, $req_role_id);
226  }
227  }
228 
230  {
231  $roles = $this->starting_point_repository->getGlobalRolesWithoutStartingPoint();
232 
233 
234  // role type
235  $radg = new ilRadioGroupInputGUI($this->lng->txt('role'), 'role_type');
236  $radg->setValue('1');
237  if ($roles !== []) {
238  $radg->setValue('0');
239  $op1 = new ilRadioOption($this->lng->txt('user_global_role'), '0');
240  $radg->addOption($op1);
241 
242  $role_options = [];
243  foreach ($roles as $role) {
244  $role_options[$role['id']] = $role['title'];
245  }
246  $si_roles = new ilSelectInputGUI($this->lng->txt('roles_without_starting_point'), 'role');
247  $si_roles->setOptions($role_options);
248  $op1->addSubItem($si_roles);
249  }
250 
251  $op2 = new ilRadioOption($this->lng->txt('user_local_role'), '1');
252  $radg->addOption($op2);
253  $role_search = new ilRoleAutoCompleteInputGUI('', 'role_search', $this, 'addRoleAutoCompleteObject');
254  $role_search->setSize(40);
255  $op2->addSubItem($role_search);
256  return $radg;
257  }
258 
259  private function getEditFormSpecificInputs(int $spoint_id, int $req_role_id): array
260  {
261  $title = $this->lng->txt('default');
262  if ($spoint_id !== $this->starting_point_repository->getDefaultStartingPointID()) {
263  $role = new ilObjRole($req_role_id);
264  $title = $role->getTitle();
265  }
266 
267  $inputs = [];
268  $inputs[0] = new ilNonEditableValueGUI($this->lng->txt('editing_this_role'), 'role_disabled');
269  $inputs[0]->setValue($title);
270 
271  $inputs[1] = new ilHiddenInputGUI('role');
272  $inputs[1]->setValue((string) $req_role_id);
273 
274  $inputs[2] = new ilHiddenInputGUI('start_point_id');
275  $inputs[2]->setValue((string) $spoint_id);
276 
277  return $inputs;
278  }
279 
281  {
282  $si = new ilRadioGroupInputGUI($this->lng->txt('adm_user_starting_point'), 'start_point');
283  $si->setRequired(true);
284  $si->setInfo($this->lng->txt('adm_user_starting_point_info'));
285  $valid = array_keys($this->starting_point_repository->getPossibleStartingPoints());
286  foreach ($this->starting_point_repository->getPossibleStartingPoints(true) as $value => $caption) {
287  $si->addOption(
288  $this->getStartingPointSelectionOption($value, $caption, $st_point, $valid)
289  );
290  }
291 
292  return $si;
293  }
294 
296  int $value,
297  string $caption,
298  ?ilUserStartingPoint $st_point,
299  array $valid
300  ): ilRadioOption {
301  $opt = new ilRadioOption($this->lng->txt($caption), (string) $value);
302 
304  $opt->addSubItem(
305  $this->getCalenderSubInputs($st_point)
306  );
307  }
308 
310  $opt->addSubItem(
311  $this->getRepositoryObjectInput($st_point)
312  );
313  }
314 
315  if (!in_array($value, $valid)) {
316  $opt->setInfo($this->lng->txt('adm_user_starting_point_invalid_info'));
317  }
318 
319  return $opt;
320  }
321 
323  {
324  $default_cal_view = new ilRadioGroupInputGUI($this->lng->txt('cal_def_view'), 'user_calendar_view');
325  $default_cal_view->setRequired(true);
326 
327  $day = new ilRadioOption($this->lng->txt('day'), (string) ilCalendarSettings::DEFAULT_CAL_DAY);
328  $default_cal_view->addOption($day);
329  $week = new ilRadioOption($this->lng->txt('week'), (string) ilCalendarSettings::DEFAULT_CAL_WEEK);
330  $default_cal_view->addOption($week);
331  $month = new ilRadioOption($this->lng->txt('month'), (string) ilCalendarSettings::DEFAULT_CAL_MONTH);
332  $default_cal_view->addOption($month);
333 
334  $list = new ilRadioOption($this->lng->txt('cal_list'), (string) ilCalendarSettings::DEFAULT_CAL_LIST);
335 
336  $cal_periods = new ilSelectInputGUI($this->lng->txt('cal_list'), 'user_cal_period');
337  $cal_periods->setOptions([
338  ilCalendarAgendaListGUI::PERIOD_DAY => '1 ' . $this->lng->txt('day'),
339  ilCalendarAgendaListGUI::PERIOD_WEEK => '1 ' . $this->lng->txt('week'),
340  ilCalendarAgendaListGUI::PERIOD_MONTH => '1 ' . $this->lng->txt('month'),
341  ilCalendarAgendaListGUI::PERIOD_HALF_YEAR => '6 ' . $this->lng->txt('months')
342  ]);
343  $cal_periods->setRequired(true);
344 
345  if ($st_point !== null) {
346  $default_cal_view->setValue((string) $st_point->getCalendarView());
347  $cal_periods->setValue((string) $st_point->getCalendarPeriod());
348  }
349 
350  $list->addSubItem($cal_periods);
351  $default_cal_view->addOption($list);
352 
353  return $default_cal_view;
354  }
355 
357  {
358  $repobj_id = new ilTextInputGUI($this->lng->txt('adm_user_starting_point_ref_id'), 'start_object');
359  $repobj_id->setRequired(true);
360  $repobj_id->setSize(5);
361 
362  if ($st_point !== null) {
363  $start_ref_id = $st_point->getStartingObject();
364  $repobj_id->setValue($start_ref_id);
365  }
366 
367  if (isset($start_ref_id)) {
368  $start_obj_id = ilObject::_lookupObjId($start_ref_id);
369  if ($start_obj_id) {
370  $repobj_id->setInfo($this->lng->txt('obj_' . ilObject::_lookupType($start_obj_id)) .
371  ': ' . ilObject::_lookupTitle($start_obj_id));
372  }
373  }
374 
375  return $repobj_id;
376  }
377 
378  public function addRoleAutoCompleteObject(): void
379  {
381  }
382 
383  protected function saveUserStartingPoint(): void
384  {
385  if (!$this->rbac_system->checkAccess('write', $this->parent_ref_id)) {
386  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->FATAL);
387  }
388 
389  $form = $this->getUserStartingPointForm();
390 
391  if ($form->checkInput()) {
392  $this->starting_point_repository->togglePersonalStartingPointActivation((bool) $form->getInput('usr_start_pers'));
393  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
394  $this->ctrl->redirect($this, 'startingPoints');
395  }
396  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_error'), true);
397  $this->ctrl->redirect($this, 'startingPoints');
398  }
399 
403  protected function saveStartingPoint(): void
404  {
405  if (!$this->rbac_system->checkAccess('write', $this->parent_ref_id)) {
406  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->FATAL);
407  }
408 
409  $start_point_id = $this->user_request->getStartingPointId();
410 
411  //add from form
412  $form = $this->getRoleStartingPointForm();
413 
414  if (!$form->checkInput()) {
415  $form->setValuesByPost();
416  $this->tpl->setContent($form->getHTML());
417  return;
418  }
419 
420  $starting_point = $this->starting_point_repository->getStartingPointById(
421  $start_point_id
422  );
423 
424 
425  $role_id = $this->user_request->getRoleId();
426 
427  if ($form->getInput('role_type') === '1'
428  && ($role_id === null || $role_id < 1)) {
429  $parser = new ilQueryParser($form->getInput('role_search'));
430 
431  // TODO: Handle minWordLength
432  $parser->setMinWordLength(1);
433  $parser->setCombination(ilQueryParser::QP_COMBINATION_AND);
434  $parser->parse();
435 
436  $object_search = new ilLikeObjectSearch($parser);
437  $object_search->setFilter(['role']);
438  $res = $object_search->performSearch();
439 
440  $entries = $res->getEntries();
441 
442  if ($entries === []) {
443  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_corresponding_roles'), true);
444  $form->setValuesByPost();
445  $this->tpl->setContent($form->getHTML());
446  return;
447  }
448 
449  if (count($entries) > 1) {
450  $this->showRoleSelection(
451  $form->getInput('role'),
452  $form->getInput('role_search'),
453  $form->getInput('start_point'),
454  $form->getInput('start_object')
455  );
456  return;
457  }
458 
459  if (count($entries) === 1) {
460  $role = current($entries);
461  $role_id = $role['obj_id'];
462  }
463  }
464 
465  if ($role_id === 0) {
466  $role_id = $form->getInput('role');
467  }
468 
469  if ($role_id !== 0) {
470  $starting_point->setRuleTypeRoleBased();
471  $rules = ['role_id' => $role_id];
472  $starting_point->setRuleOptions(serialize($rules));
473  }
474 
475  $starting_point->setStartingPointType((int) $form->getInput('start_point'));
476 
477  $obj_id = (int) $form->getInput('start_object');
478  $cal_view = (int) $form->getInput('user_calendar_view');
479  $cal_period = (int) $form->getInput('user_cal_period');
480 
481 
482  if ($starting_point->getStartingPointType() === ilUserStartingPointRepository::START_REPOSITORY_OBJ
483  && (ilObject::_lookupObjId($obj_id) === 0 || $this->tree->isDeleted($obj_id))) {
484  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('obj_ref_id_not_exist'), true);
485  $form->setValuesByPost();
486  $this->tpl->setContent($form->getHTML());
487  return;
488  }
489  $starting_point->setStartingObject($obj_id);
490 
491  if ($starting_point->getStartingPointType() === ilUserStartingPointRepository::START_PD_CALENDAR) {
492  $starting_point->setCalendarView($cal_view);
493  $starting_point->setCalendarPeriod($cal_period);
494  }
495 
496  if ($start_point_id !== null) {
497  $this->starting_point_repository->update($starting_point);
498  } else {
499  $this->starting_point_repository->save($starting_point);
500  }
501 
502  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
503  $this->ctrl->redirect($this, 'startingPoints');
504  }
505 
506  private function showRoleSelection(
507  string $role,
508  string $role_search,
509  string $start_point,
510  string $start_object
511  ): void {
512  $parser = new ilQueryParser($role_search);
513  $parser->setMinWordLength(1);
514  $parser->setCombination(ilQueryParser::QP_COMBINATION_AND);
515  $parser->parse();
516 
517  $object_search = new ilLikeObjectSearch($parser);
518  $object_search->setFilter(['role']);
519  $res = $object_search->performSearch();
520 
521  $entries = $res->getEntries();
522 
523  $table = new ilRoleSelectionTableGUI($this, 'saveStartingPoint');
524  $table->setLimit(9999);
525  $table->disable('sort');
526  $table->addHiddenInput('role_search', $role_search);
527  $table->addHiddenInput('start_point', $start_point);
528  $table->addHiddenInput('start_object', $start_object);
529  $table->addHiddenInput('role', $role);
530  $table->addHiddenInput('role_type', '1');
531  $table->setTitle($this->lng->txt('user_role_selection'));
532  $table->addMultiCommand('saveStartingPoint', $this->lng->txt('select'));
533  $table->parse($entries);
534 
535  $this->tpl->setContent($table->getHTML());
536  }
537 
538  public function saveOrder(): void
539  {
540  if (!$this->rbac_system->checkAccess('write', $this->parent_ref_id)) {
541  throw new ilPermissionException($this->lng->txt('msg_no_perm_read'));
542  }
543 
544  $positions = $this->user_request->getPositions();
545  if (count($positions) > 0) {
546  $this->starting_point_repository->saveOrder($positions);
547  }
548 
549  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
550  $this->ctrl->redirect($this, 'startingPoints');
551  }
552 
553  protected function deleteStartingPoint(): void
554  {
555  if (!$this->rbac_system->checkAccess('write', $this->parent_ref_id)) {
556  throw new ilPermissionException($this->lng->txt('msg_no_perm_read'));
557  }
558 
559  $spoint_id = $this->user_request->getStartingPointId();
560  $req_role_id = $this->user_request->getRoleId();
561 
562  if ($req_role_id && is_numeric($spoint_id)) {
563  $sp = $this->starting_point_repository->getStartingPointById(
564  $spoint_id
565  );
566  $this->starting_point_repository->delete($sp->getId());
567  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
568  } else {
569  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_spoint_not_modified'), true);
570  }
571  $this->ctrl->redirect($this, 'startingPoints');
572  }
573 }
Class ilObjRole.
This class represents an option in a radio group.
$res
Definition: ltiservices.php:66
getCalenderSubInputs(?ilUserStartingPoint $st_point)
getRepositoryObjectInput(?ilUserStartingPoint $st_point)
static getLogger(string $a_component_id)
Get component logger.
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
saveStartingPoint()
store starting point from the form
$valid
getEditFormSpecificInputs(int $spoint_id, int $req_role_id)
TableGUI class for LTI consumer listing.
setOptions(array $a_options)
ilUserStartingPointRepository $starting_point_repository
ilGlobalTemplateInterface $tpl
showRoleSelection(string $role, string $role_search, string $start_point, string $start_object)
getCurrentStartingPointOrNullForStartingPointForm(?int $starting_point_id)
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setMinWordLength(int $a_length)
This class represents a hidden form property in a property form.
startingPoints()
table form to set up starting points depends of user roles
This class represents a property in a property form.
static echoAutoCompleteList()
Static asynchronous default auto complete function.
static _lookupTitle(int $obj_id)
Class ilUserStartingPointGUI.
global $DIC
Definition: shib_login.php:26
initUserStartingPointForm(?ilPropertyFormGUI $form=null)
setRequired(bool $a_required)
This class represents a role + autocomplete feature form input.
__construct(Container $dic, ilPlugin $plugin)
getStartingPointSelectionOption(int $value, string $caption, ?ilUserStartingPoint $st_point, array $valid)
initRoleStartingPointForm(?ilPropertyFormGUI $form=null)
getCalendarView()
Gets calendar view.
static _lookupType(int $id, bool $reference=false)
getStartingPointSelectionInput(?ilUserStartingPoint $st_point)
getCurrentTypeForStartingPointForm(?ilUserStartingPoint $starting_point)
getFormTypeSpecificStartingPointFormParts(?int $spoint_id, ?int $req_role_id)