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