ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBadgeManagementGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
28 {
31  private ilLanguage $lng;
32  private ilCtrl $ctrl;
33  private ilTabsGUI $tabs;
37  private ilObjUser $user;
38  private \ILIAS\UI\Factory $ui_factory;
39  private int $parent_obj_id;
40  private string $parent_obj_type;
41 
42  public function __construct(
43  private readonly int $parent_ref_id,
44  int $a_parent_obj_id = null,
45  string $a_parent_obj_type = null
46  ) {
47  global $DIC;
48 
49  $this->lng = $DIC->language();
50  $this->ctrl = $DIC->ctrl();
51  $this->tabs = $DIC->tabs();
52  $this->access = $DIC->access();
53  $this->toolbar = $DIC->toolbar();
54  $this->ui_factory = $DIC->ui()->factory();
55  $this->tpl = $DIC['tpl'];
56  $this->user = $DIC->user();
57  $lng = $DIC->language();
58  $this->parent_obj_id = $a_parent_obj_id
59  ?: ilObject::_lookupObjId($parent_ref_id);
60  $this->parent_obj_type = $a_parent_obj_type
61  ?: ilObject::_lookupType($this->parent_obj_id);
62 
63  if (!ilBadgeHandler::getInstance()->isObjectActive($this->parent_obj_id)) {
64  throw new ilException('inactive object');
65  }
66 
67  $lng->loadLanguageModule('badge');
68 
69  $this->request = new ilBadgeGUIRequest(
70  $DIC->http(),
71  $DIC->refinery()
72  );
73 
74  $this->session_repo = new ilBadgeManagementSessionRepository();
75  }
76 
77  public function executeCommand(): void
78  {
79  $ilCtrl = $this->ctrl;
80 
81  $next_class = $ilCtrl->getNextClass($this);
82  $cmd = $ilCtrl->getCmd('listBadges');
83 
84  switch ($next_class) {
85  case 'ilpropertyformgui':
86  // ajax - update
87  if ($this->request->getBadgeId()) {
88  $badge = new ilBadge($this->request->getBadgeId());
89  $type = $badge->getTypeInstance();
90  $form = $this->initBadgeForm('edit', $type, $badge->getTypeId());
91  $this->setBadgeFormValues($form, $badge, $type);
92  } // ajax- create
93  else {
94  $type_id = $this->request->getType();
95  $ilCtrl->setParameter($this, 'type', $type_id);
97  $type = $handler->getTypeInstanceByUniqueId($type_id);
98  $form = $this->initBadgeForm('create', $type, $type_id);
99  }
100  $ilCtrl->forwardCommand($form);
101  break;
102 
103  default:
104  $this->$cmd();
105  break;
106  }
107  }
108 
109  protected function setTabs(string $a_active): void
110  {
111  $ilTabs = $this->tabs;
112  $lng = $this->lng;
113  $ilCtrl = $this->ctrl;
114 
115  $ilTabs->addSubTab(
116  'badges',
117  $lng->txt('obj_bdga'),
118  $ilCtrl->getLinkTarget($this, 'listBadges')
119  );
120 
121  $ilTabs->addSubTab(
122  'users',
123  $lng->txt('users'),
124  $ilCtrl->getLinkTarget($this, 'listUsers')
125  );
126 
127  $ilTabs->activateSubTab($a_active);
128  }
129 
130  protected function hasWrite(): bool
131  {
132  $ilAccess = $this->access;
133  return $ilAccess->checkAccess('write', '', $this->parent_ref_id);
134  }
135 
136  protected function listBadges(): void
137  {
138  $ilToolbar = $this->toolbar;
139  $lng = $this->lng;
140  $ilCtrl = $this->ctrl;
141  $tpl = $this->tpl;
142 
143  $this->setTabs('badges');
144 
145  if ($this->hasWrite()) {
147  $valid_types = $handler->getAvailableTypesForObjType($this->parent_obj_type);
148  if ($valid_types) {
149  $options = [];
150  foreach ($valid_types as $id => $type) {
151  $ilCtrl->setParameter($this, 'type', $id);
152  $options[$id] = $this->ui_factory->link()->standard(
153  $this->parent_obj_type !== 'bdga' ? ilBadge::getExtendedTypeCaption($type) : $type->getCaption(
154  ),
155  $ilCtrl->getLinkTarget($this, 'addBadge')
156  );
157  $ilCtrl->setParameter($this, 'type', null);
158  }
159  asort($options);
160  $options = array_values($options);
161 
162  $ilToolbar->addComponent(
163  $this->ui_factory->dropdown()->standard($options)->withLabel($lng->txt('badge_create'))
164  );
165  } else {
166  $this->tpl->setOnScreenMessage('info', $lng->txt('badge_no_valid_types_for_obj'));
167  }
168 
169  $clip_ids = $this->session_repo->getBadgeIds();
170  if (count($clip_ids) > 0) {
171  if ($valid_types) {
172  $ilToolbar->addSeparator();
173  }
174 
175  $tt = [];
176  foreach ($this->getValidBadgesFromClipboard() as $badge) {
177  $tt[] = $badge->getTitle();
178  }
179  $ttid = 'bdgpst';
181  $ttid,
182  implode('<br />', $tt),
183  '',
184  'bottom center',
185  'top center',
186  false
187  );
188 
189  $lng->loadLanguageModule('content');
190  $ilToolbar->addButton(
191  $lng->txt('cont_paste_from_clipboard') .
192  ' (' . count($tt) . ')',
193  $ilCtrl->getLinkTarget($this, 'pasteBadges'),
194  '',
195  null,
196  '',
197  $ttid
198  );
199  $ilToolbar->addButton(
200  $lng->txt('clear_clipboard'),
201  $ilCtrl->getLinkTarget($this, 'clearClipboard')
202  );
203  }
204  }
205 
206  $tbl = new ilBadgeTableGUI($this, 'listBadges', $this->parent_obj_id, $this->hasWrite());
207  $tpl->setContent($tbl->getHTML());
208  }
209 
210  protected function applyBadgeFilter(): void
211  {
212  $tbl = new ilBadgeTableGUI($this, 'listBadges', $this->parent_obj_id, $this->hasWrite());
213  $tbl->resetOffset();
214  $tbl->writeFilterToSession();
215  $this->listBadges();
216  }
217 
218  protected function resetBadgeFilter(): void
219  {
220  $tbl = new ilBadgeTableGUI($this, 'listBadges', $this->parent_obj_id, $this->hasWrite());
221  $tbl->resetOffset();
222  $tbl->resetFilter();
223  $this->listBadges();
224  }
225 
226 
227  //
228  // badge (CRUD)
229  //
230 
231  protected function addBadge(ilPropertyFormGUI $a_form = null): void
232  {
233  $ilCtrl = $this->ctrl;
234  $tpl = $this->tpl;
235 
236  $type_id = $this->request->getType();
237  if (!$type_id ||
238  !$this->hasWrite()) {
239  $ilCtrl->redirect($this, 'listBadges');
240  }
241 
242  $ilCtrl->setParameter($this, 'type', $type_id);
243 
245  $type = $handler->getTypeInstanceByUniqueId($type_id);
246  if (!$type) {
247  $ilCtrl->redirect($this, 'listBadges');
248  }
249 
250  if (!$a_form) {
251  $a_form = $this->initBadgeForm('create', $type, $type_id);
252  }
253 
254  $tpl->setContent($a_form->getHTML());
255  }
256 
257  protected function initBadgeForm(
258  string $a_mode,
259  ilBadgeType $a_type,
260  string $a_type_unique_id
261  ): ilPropertyFormGUI {
262  $lng = $this->lng;
263  $ilCtrl = $this->ctrl;
264 
265  $form = new ilPropertyFormGUI();
266  $form->setFormAction($ilCtrl->getFormAction($this, 'saveBadge'));
267  $form->setTitle($lng->txt('badge_badge') . ' "' . $a_type->getCaption() . '"');
268 
269  $active = new ilCheckboxInputGUI($lng->txt('active'), 'act');
270  $form->addItem($active);
271 
272  $title = new ilTextInputGUI($lng->txt('title'), 'title');
273  $title->setMaxLength(255);
274  $title->setRequired(true);
275  $form->addItem($title);
276 
277  $desc = new ilTextAreaInputGUI($lng->txt('description'), 'desc');
278  $desc->setMaxNumOfChars(4000);
279  $desc->setRequired(true);
280  $form->addItem($desc);
281 
282  $crit = new ilTextAreaInputGUI($lng->txt('badge_criteria'), 'crit');
283  $crit->setMaxNumOfChars(4000);
284  $crit->setRequired(true);
285  $form->addItem($crit);
286 
287  if ($a_mode === 'create') {
288  // upload
289 
290  $img_mode = new ilRadioGroupInputGUI($lng->txt('image'), 'img_mode');
291  $img_mode->setRequired(true);
292  $img_mode->setValue('tmpl');
293  $form->addItem($img_mode);
294 
295  $img_mode_tmpl = new ilRadioOption($lng->txt('badge_image_from_template'), 'tmpl');
296  $img_mode->addOption($img_mode_tmpl);
297 
298  $img_mode_up = new ilRadioOption($lng->txt('badge_image_from_upload'), 'up');
299  $img_mode->addOption($img_mode_up);
300 
301  $img_upload = new ilImageFileInputGUI($lng->txt('file'), 'img');
302  $img_upload->setRequired(true);
303  $img_upload->setSuffixes(['png', 'svg']);
304  $img_mode_up->addSubItem($img_upload);
305 
306  // templates
307 
308  $valid_templates = ilBadgeImageTemplate::getInstancesByType($a_type_unique_id);
309  if (count($valid_templates)) {
310  $options = [];
311  $options[''] = $lng->txt('please_select');
312  foreach ($valid_templates as $tmpl) {
313  $options[$tmpl->getId()] = $tmpl->getTitle();
314  }
315 
316  $tmpl = new ilSelectInputGUI($lng->txt('badge_image_template_form'), 'tmpl');
317  $tmpl->setRequired(true);
318  $tmpl->setOptions($options);
319  $img_mode_tmpl->addSubItem($tmpl);
320  } else {
321  // no templates, activate upload
322  $img_mode_tmpl->setDisabled(true);
323  $img_mode->setValue('up');
324  }
325  } else {
326  $img_upload = new ilImageFileInputGUI($lng->txt('image'), 'img');
327  $img_upload->setSuffixes(['png', 'svg']);
328  $img_upload->setAllowDeletion(false);
329  $img_upload->setUseCache(false);
330  $form->addItem($img_upload);
331  }
332 
333  $valid = new ilTextInputGUI($lng->txt('badge_valid'), 'valid');
334  $valid->setMaxLength(255);
335  $form->addItem($valid);
336 
337  $custom = $a_type->getConfigGUIInstance();
338  if ($custom instanceof ilBadgeTypeGUI) {
339  $custom->initConfigForm($form, $this->parent_ref_id);
340  }
341 
342  // :TODO: valid date/period
343 
344  if ($a_mode === 'create') {
345  $form->addCommandButton('saveBadge', $lng->txt('save'));
346  } else {
347  $form->addCommandButton('updateBadge', $lng->txt('save'));
348  }
349  $form->addCommandButton('listBadges', $lng->txt('cancel'));
350 
351  return $form;
352  }
353 
354  protected function saveBadge(): void
355  {
356  $ilCtrl = $this->ctrl;
357  $lng = $this->lng;
358 
359  $type_id = $this->request->getType();
360  if (!$type_id ||
361  !$this->hasWrite()) {
362  $ilCtrl->redirect($this, 'listBadges');
363  }
364 
365  $ilCtrl->setParameter($this, 'type', $type_id);
366 
368  $type = $handler->getTypeInstanceByUniqueId($type_id);
369  if (!$type) {
370  $ilCtrl->redirect($this, 'listBadges');
371  }
372 
373  $form = $this->initBadgeForm('create', $type, $type_id);
374  $custom = $type->getConfigGUIInstance();
375 
376  if ($form->checkInput() &&
377  (!$custom || $custom->validateForm($form))) {
378  $badge = new ilBadge();
379  $badge->setParentId($this->parent_obj_id); // :TODO: ref_id?
380  $badge->setTypeId($type_id);
381  $badge->setActive($form->getInput('act'));
382  $badge->setTitle($form->getInput('title'));
383  $badge->setDescription($form->getInput('desc'));
384  $badge->setCriteria($form->getInput('crit'));
385  $badge->setValid($form->getInput('valid'));
386 
387  if ($custom instanceof ilBadgeTypeGUI) {
388  $badge->setConfiguration($custom->getConfigFromForm($form));
389  }
390 
391  $badge->create();
392 
393 
394 
395  try {
396  if ($form->getInput('img_mode') === 'up') {
397  $badge->uploadImage($_FILES['img']);
398  } else {
399  $tmpl = new ilBadgeImageTemplate($form->getInput('tmpl'));
400  $badge->importImage($tmpl->getImage(), $tmpl->getImagePath());
401  }
402  } catch (BadgeException $e) {
403  $delete = false;
404  switch ($e->getCode()) {
405  case BadgeException::EXCEPTION_FILE_NOT_FOUND:
406  $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_uploaded_image_file_not_found'), true);
407  $delete = true;
408  break;
409  case BadgeException::EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED:
410  $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_create_image_processing_failed'), true);
411  $delete = true;
412  break;
413  }
414 
415  if ($delete) {
416  $badge->delete();
417  $ilCtrl->redirect($this, "listBadges");
418  }
419  }
420 
421  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
422  $ilCtrl->redirect($this, 'listBadges');
423  }
424 
425  $form->setValuesByPost();
426  $this->addBadge($form);
427  }
428 
429  protected function editBadge(ilPropertyFormGUI $a_form = null): void
430  {
431  $ilCtrl = $this->ctrl;
432  $tpl = $this->tpl;
433  $lng = $this->lng;
434 
435  $badge_id = $this->request->getBadgeId();
436  if (!$badge_id ||
437  !$this->hasWrite()) {
438  $ilCtrl->redirect($this, 'listBadges');
439  }
440 
441  $ilCtrl->setParameter($this, 'bid', $badge_id);
442 
443  $badge = new ilBadge($badge_id);
444 
445  $static_cnt = ilBadgeHandler::getInstance()->countStaticBadgeInstances($badge);
446  if ($static_cnt) {
447  $this->tpl->setOnScreenMessage('info', sprintf($lng->txt('badge_edit_with_published'), $static_cnt));
448  }
449 
450  if (!$a_form) {
451  $type = $badge->getTypeInstance();
452  $a_form = $this->initBadgeForm('edit', $type, $badge->getTypeId());
453  $this->setBadgeFormValues($a_form, $badge, $type);
454  }
455 
456  $tpl->setContent($a_form->getHTML());
457  }
458 
459  protected function setBadgeFormValues(
460  ilPropertyFormGUI $a_form,
461  ilBadge $a_badge,
462  ilBadgeType $a_type
463  ): void {
464  $a_form->getItemByPostVar('act')->setChecked($a_badge->isActive());
465  $a_form->getItemByPostVar('title')->setValue($a_badge->getTitle());
466  $a_form->getItemByPostVar('desc')->setValue($a_badge->getDescription());
467  $a_form->getItemByPostVar('crit')->setValue($a_badge->getCriteria());
468  $a_form->getItemByPostVar('img')->setValue($a_badge->getImage());
469  $a_form->getItemByPostVar('img')->setImage($a_badge->getImagePath());
470  $a_form->getItemByPostVar('valid')->setValue($a_badge->getValid());
471 
472  $custom = $a_type->getConfigGUIInstance();
473  if ($custom instanceof ilBadgeTypeGUI) {
474  $custom->importConfigToForm($a_form, $a_badge->getConfiguration());
475  }
476  }
477 
478  protected function updateBadge(): void
479  {
480  $ilCtrl = $this->ctrl;
481  $lng = $this->lng;
482 
483  $badge_id = $this->request->getBadgeId();
484  if (!$badge_id ||
485  !$this->hasWrite()) {
486  $ilCtrl->redirect($this, 'listBadges');
487  }
488 
489  $ilCtrl->setParameter($this, 'bid', $badge_id);
490 
491  $badge = new ilBadge($badge_id);
492  $type = $badge->getTypeInstance();
493  $custom = $type->getConfigGUIInstance();
494  if ($custom &&
495  !($custom instanceof ilBadgeTypeGUI)) {
496  $custom = null;
497  }
498  $form = $this->initBadgeForm('update', $type, $badge->getTypeId());
499  if ($form->checkInput() &&
500  (!$custom || $custom->validateForm($form))) {
501  $badge->setActive($form->getInput('act'));
502  $badge->setTitle($form->getInput('title'));
503  $badge->setDescription($form->getInput('desc'));
504  $badge->setCriteria($form->getInput('crit'));
505  $badge->setValid($form->getInput('valid'));
506 
507  if ($custom) {
508  $badge->setConfiguration($custom->getConfigFromForm($form));
509  }
510 
511  $badge->update();
512 
513  try {
514  $badge->uploadImage($_FILES["img"]);
515  } catch (BadgeException $e) {
516  if ($e->getCode() === BadgeException::EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED) {
517  $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_update_image_processing_failed'), true);
518  $ilCtrl->redirect($this, "listBadges");
519  }
520  }
521 
522  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
523  $ilCtrl->redirect($this, 'listBadges');
524  }
525 
526  $this->tpl->setOnScreenMessage('failure', $lng->txt('form_input_not_valid'));
527  $form->setValuesByPost();
528  $this->editBadge($form);
529  }
530 
531  protected function confirmDeleteBadges(): void
532  {
533  $ilCtrl = $this->ctrl;
534  $lng = $this->lng;
535  $tpl = $this->tpl;
536  $ilTabs = $this->tabs;
537 
538  $badge_ids = $this->getBadgesFromMultiAction();
539 
540  $ilTabs->clearTargets();
541  $ilTabs->setBackTarget(
542  $lng->txt('back'),
543  $ilCtrl->getLinkTarget($this, 'listBadges')
544  );
545 
546  $confirmation_gui = new ilConfirmationGUI();
547  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
548  $confirmation_gui->setHeaderText($lng->txt('badge_deletion_confirmation'));
549  $confirmation_gui->setCancel($lng->txt('cancel'), 'listBadges');
550  $confirmation_gui->setConfirm($lng->txt('delete'), 'deleteBadges');
551 
552  foreach ($badge_ids as $badge_id) {
553  $badge = new ilBadge($badge_id);
554  $confirmation_gui->addItem(
555  'id[]',
556  (string) $badge_id,
557  $badge->getTitle() .
558  ' (' . count(ilBadgeAssignment::getInstancesByBadgeId($badge_id)) . ')'
559  );
560  }
561 
562  $tpl->setContent($confirmation_gui->getHTML());
563  }
564 
565  protected function deleteBadges(): void
566  {
567  $ilCtrl = $this->ctrl;
568  $lng = $this->lng;
569 
570  $badge_ids = $this->getBadgesFromMultiAction();
571 
572  foreach ($badge_ids as $badge_id) {
573  $badge = new ilBadge($badge_id);
574  $badge->delete();
575  }
576 
577  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
578  $ilCtrl->redirect($this, 'listBadges');
579  }
580 
581 
582  //
583  // badges multi action
584  //
585 
589  protected function getBadgesFromMultiAction(): array
590  {
591  $ilCtrl = $this->ctrl;
592 
593  $badge_ids = $this->request->getIds();
594  if (!$badge_ids ||
595  !$this->hasWrite()) {
596  $ilCtrl->redirect($this, 'listBadges');
597  }
598 
599  return $badge_ids;
600  }
601 
602  protected function copyBadges(): void
603  {
604  $ilCtrl = $this->ctrl;
605 
606  $badge_ids = $this->getBadgesFromMultiAction();
607 
608  $clip_ids = $this->session_repo->getBadgeIds();
609  $clip_ids = array_unique(
610  array_merge($clip_ids, $badge_ids)
611  );
612  $this->session_repo->setBadgeIds($clip_ids);
613 
614  $ilCtrl->redirect($this, 'listBadges');
615  }
616 
617  protected function clearClipboard(): void
618  {
619  $ilCtrl = $this->ctrl;
620 
621  $this->session_repo->clear();
622  $ilCtrl->redirect($this, 'listBadges');
623  }
624 
628  protected function getValidBadgesFromClipboard(): array
629  {
630  $res = [];
631 
632  $valid_types = array_keys(ilBadgeHandler::getInstance()->getAvailableTypesForObjType($this->parent_obj_type));
633 
634  foreach ($this->session_repo->getBadgeIds() as $badge_id) {
635  $badge = new ilBadge($badge_id);
636  if (in_array($badge->getTypeId(), $valid_types, true)) {
637  $res[] = $badge;
638  }
639  }
640 
641  return $res;
642  }
643 
644  protected function pasteBadges(): void
645  {
646  $ilCtrl = $this->ctrl;
647 
648  $clip_ids = $this->session_repo->getBadgeIds();
649  if (!$this->hasWrite() || count($clip_ids) === 0) {
650  $ilCtrl->redirect($this, 'listBadges');
651  }
652 
653  foreach ($this->getValidBadgesFromClipboard() as $badge) {
654  $badge->copy($this->parent_obj_id);
655  }
656 
657  $ilCtrl->redirect($this, 'listBadges');
658  }
659 
660  protected function toggleBadges(bool $a_status): void
661  {
662  $ilCtrl = $this->ctrl;
663  $lng = $this->lng;
664 
665  $badge_ids = $this->getBadgesFromMultiAction();
666 
667  foreach ($badge_ids as $badge_id) {
668  $badge = new ilBadge($badge_id);
669  $badge->setActive($a_status);
670  $badge->update();
671  }
672 
673  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
674  $ilCtrl->redirect($this, 'listBadges');
675  }
676 
677  protected function activateBadges(): void
678  {
679  $this->toggleBadges(true);
680  }
681 
682  protected function deactivateBadges(): void
683  {
684  $this->toggleBadges(false);
685  }
686 
687 
688  //
689  // users
690  //
691 
692  protected function listUsers(): void
693  {
694  $lng = $this->lng;
695  $ilCtrl = $this->ctrl;
696  $ilToolbar = $this->toolbar;
697  $tpl = $this->tpl;
698 
699  $this->setTabs('users');
700 
701  if ($this->hasWrite()) {
702  $manual = ilBadgeHandler::getInstance()->getAvailableManualBadges(
703  $this->parent_obj_id,
704  $this->parent_obj_type
705  );
706  if (count($manual)) {
707  $drop = new ilSelectInputGUI($lng->txt('badge_badge'), 'bid');
708  $drop->setOptions($manual);
709  $ilToolbar->addInputItem($drop, true);
710 
711  $ilToolbar->setFormAction($ilCtrl->getFormAction($this, 'awardBadgeUserSelection'));
712  $ilToolbar->addFormButton($lng->txt('badge_award_badge'), 'awardBadgeUserSelection');
713  }
714  }
715 
716  $tbl = new ilBadgeUserTableGUI($this, 'listUsers', $this->parent_ref_id);
717  $tpl->setContent($tbl->getHTML());
718  }
719 
720  protected function applyListUsers(): void
721  {
722  $tbl = new ilBadgeUserTableGUI($this, 'listUsers', $this->parent_ref_id);
723  $tbl->resetOffset();
724  $tbl->writeFilterToSession();
725  $this->listUsers();
726  }
727 
728  protected function resetListUsers(): void
729  {
730  $tbl = new ilBadgeUserTableGUI($this, 'listUsers', $this->parent_ref_id);
731  $tbl->resetOffset();
732  $tbl->resetFilter();
733  $this->listUsers();
734  }
735 
736  protected function awardBadgeUserSelection(): void
737  {
738  $ilCtrl = $this->ctrl;
739  $tpl = $this->tpl;
740  $ilTabs = $this->tabs;
741  $lng = $this->lng;
742 
743  $bid = $this->request->getBadgeId();
744  if (!$bid ||
745  !$this->hasWrite()) {
746  $ilCtrl->redirect($this, 'listUsers');
747  }
748 
749  $manual = array_keys(
750  ilBadgeHandler::getInstance()->getAvailableManualBadges($this->parent_obj_id, $this->parent_obj_type)
751  );
752  if (!in_array($bid, $manual, true)) {
753  $ilCtrl->redirect($this, 'listUsers');
754  }
755 
756  $back_target = 'listUsers';
757  if ($this->request->getTgt() === 'bdgl') {
758  $ilCtrl->saveParameter($this, 'tgt');
759  $back_target = 'listBadges';
760  }
761 
762  $ilTabs->clearTargets();
763  $ilTabs->setBackTarget(
764  $lng->txt('back'),
765  $ilCtrl->getLinkTarget($this, $back_target)
766  );
767 
768  $ilCtrl->setParameter($this, 'bid', $bid);
769 
770  $badge = new ilBadge($bid);
771 
772  $tbl = new ilBadgeUserTableGUI($this, 'awardBadgeUserSelection', $this->parent_ref_id, $badge);
773  $tpl->setContent($tbl->getHTML());
774  }
775 
776  protected function applyAwardBadgeUserSelection(): void
777  {
778  $tbl = new ilBadgeUserTableGUI($this, 'awardBadgeUserSelection', $this->parent_ref_id);
779  $tbl->resetOffset();
780  $tbl->writeFilterToSession();
781  $this->awardBadgeUserSelection();
782  }
783 
784  protected function resetAwardBadgeUserSelection(): void
785  {
786  $tbl = new ilBadgeUserTableGUI($this, 'awardBadgeUserSelection', $this->parent_ref_id);
787  $tbl->resetOffset();
788  $tbl->resetFilter();
789  $this->awardBadgeUserSelection();
790  }
791 
792  protected function assignBadge(): void
793  {
794  $ilCtrl = $this->ctrl;
795  $ilUser = $this->user;
796  $lng = $this->lng;
797 
798  $user_ids = $this->request->getIds();
799  $badge_id = $this->request->getBadgeId();
800  if (!$user_ids ||
801  !$badge_id ||
802  !$this->hasWrite()) {
803  $ilCtrl->redirect($this, 'listUsers');
804  }
805 
806  $new_badges = [];
807  foreach ($user_ids as $user_id) {
808  if (!ilBadgeAssignment::exists($badge_id, (int) $user_id)) {
809  $ass = new ilBadgeAssignment($badge_id, (int) $user_id);
810  $ass->setAwardedBy($ilUser->getId());
811  $ass->store();
812 
813  $new_badges[$user_id][] = $badge_id;
814  }
815  }
816 
817  ilBadgeHandler::getInstance()->sendNotification($new_badges, $this->parent_ref_id);
818 
819  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
820  $ilCtrl->redirect($this, 'listUsers');
821  }
822 
823  protected function confirmDeassignBadge(): void
824  {
825  $ilCtrl = $this->ctrl;
826  $lng = $this->lng;
827  $tpl = $this->tpl;
828  $ilTabs = $this->tabs;
829 
830  $user_ids = $this->request->getIds();
831  $badge_id = $this->request->getBadgeId();
832  if (!$user_ids ||
833  !$badge_id ||
834  !$this->hasWrite()) {
835  $ilCtrl->redirect($this, 'listUsers');
836  }
837 
838  $ilTabs->clearTargets();
839  $ilTabs->setBackTarget(
840  $lng->txt('back'),
841  $ilCtrl->getLinkTarget($this, 'listUsers')
842  );
843 
844  $badge = new ilBadge($badge_id);
845 
846  $ilCtrl->setParameter($this, 'bid', $badge->getId());
847 
848  $confirmation_gui = new ilConfirmationGUI();
849  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
850  $confirmation_gui->setHeaderText(
851  sprintf($lng->txt('badge_assignment_deletion_confirmation'), $badge->getTitle())
852  );
853  $confirmation_gui->setCancel($lng->txt('cancel'), 'listUsers');
854  $confirmation_gui->setConfirm($lng->txt('delete'), 'deassignBadge');
855 
856  $assigned_users = ilBadgeAssignment::getAssignedUsers($badge->getId());
857 
858  foreach ($user_ids as $user_id) {
859  if (in_array($user_id, $assigned_users)) {
860  $confirmation_gui->addItem(
861  'id[]',
862  $user_id,
863  ilUserUtil::getNamePresentation($user_id, false, false, '', true)
864  );
865  }
866  }
867 
868  $tpl->setContent($confirmation_gui->getHTML());
869  }
870 
871  protected function deassignBadge(): void
872  {
873  $ilCtrl = $this->ctrl;
874  $lng = $this->lng;
875 
876  $user_ids = $this->request->getIds();
877  $badge_id = $this->request->getBadgeId();
878  if (!$user_ids ||
879  !$badge_id ||
880  !$this->hasWrite()) {
881  $ilCtrl->redirect($this, 'listUsers');
882  }
883 
884  foreach ($user_ids as $user_id) {
885  $ass = new ilBadgeAssignment($badge_id, (int) $user_id);
886  $ass->delete();
887  }
888 
889  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
890  $ilCtrl->redirect($this, 'listUsers');
891  }
892 }
__construct(private readonly int $parent_ref_id, int $a_parent_obj_id=null, string $a_parent_obj_type=null)
initBadgeForm(string $a_mode, ilBadgeType $a_type, string $a_type_unique_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:69
Class ilBadgeManagementGUI.
static getInstancesByType(string $a_type_unique_id)
getConfiguration()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
redirect(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false)
getItemByPostVar(string $a_post_var)
checkAccess(string $a_permission, string $a_cmd, int $a_ref_id, string $a_type="", ?int $a_obj_id=null, ?int $a_tree_id=null)
check access for an object (provide $a_type and $a_obj_id if available for better performance) ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$valid
getConfigGUIInstance()
Get GUI config instance.
editBadge(ilPropertyFormGUI $a_form=null)
ilBadgeManagementSessionRepository $session_repo
static getAssignedUsers(int $a_badge_id)
setSuffixes(array $a_suffixes)
loadLanguageModule(string $a_module)
Load language module.
setOptions(array $a_options)
setBadgeFormValues(ilPropertyFormGUI $a_form, ilBadge $a_badge, ilBadgeType $a_type)
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
getNextClass($a_gui_class=null)
This class represents a property in a property form.
addBadge(ilPropertyFormGUI $a_form=null)
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
setContent(string $a_html)
Sets content for standard template.
static getExtendedTypeCaption(ilBadgeType $a_type)
static getInstancesByBadgeId(int $a_badge_id)
getImagePath(bool $a_full_path=true)
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
clearTargets()
clear all targets
setRequired(bool $a_required)
static addTooltip(string $a_el_id, string $a_text, string $a_container="", string $a_my="bottom center", string $a_at="top center", bool $a_use_htmlspecialchars=true)
static exists(int $a_badge_id, int $a_user_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents an image file property in a property form.
TableGUI class for badge listing.
This class represents a text area property in a property form.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$handler
Definition: index.php:18