ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilBadgeManagementGUI.php
Go to the documentation of this file.
1 <?php
2 
29 
34 {
35  public const TABLE_ALL_OBJECTS_ACTION = 'ALL_OBJECTS';
36 
39  private ilLanguage $lng;
40  private ilCtrl $ctrl;
41  private ilTabsGUI $tabs;
45  private ilObjUser $user;
46  private \ILIAS\UI\Factory $ui_factory;
47  private int $parent_obj_id;
48  private string $parent_obj_type;
49 
54  private \ILIAS\HTTP\Services $http;
55  private Factory $refinery;
56 
57  public function __construct(
58  private readonly int $parent_ref_id,
59  ?int $a_parent_obj_id = null,
60  ?string $a_parent_obj_type = null
61  ) {
62  global $DIC;
63 
64  $this->lng = $DIC->language();
65  $this->ctrl = $DIC->ctrl();
66  $this->tabs = $DIC->tabs();
67  $this->access = $DIC->access();
68  $this->http = $DIC->http();
69  $this->refinery = $DIC->refinery();
70  $this->toolbar = $DIC->toolbar();
71  $this->ui_factory = $DIC->ui()->factory();
72  $this->resource_storage = $DIC->resourceStorage();
73  $this->upload_service = $DIC->upload();
74  $this->tpl = $DIC->ui()->mainTemplate();
75  $this->user = $DIC->user();
76  $lng = $DIC->language();
77  $this->parent_obj_id = $a_parent_obj_id
78  ?: ilObject::_lookupObjId($parent_ref_id);
79  $this->parent_obj_type = $a_parent_obj_type
80  ?: ilObject::_lookupType($this->parent_obj_id);
81 
82  if (!ilBadgeHandler::getInstance()->isObjectActive($this->parent_obj_id)) {
83  throw new ilException('inactive object');
84  }
85 
86  $lng->loadLanguageModule('badge');
87 
88  $this->request = new ilBadgeGUIRequest(
89  $DIC->http(),
90  $DIC->refinery()
91  );
92 
93  $this->session_repo = new ilBadgeManagementSessionRepository();
94  $this->badge_image_service = new ilBadgeImage(
95  $DIC->resourceStorage(),
96  $DIC->upload(),
97  $DIC->ui()->mainTemplate()
98  );
99  $this->flavour_definition = new ilBadgePictureDefinition();
100  }
101 
106  private function splitBadgeAndUserIdsFromString(array $splittable_user_ids): array
107  {
108  $user_ids = [];
109  $badge_id = null;
110 
111  if ($splittable_user_ids !== []) {
112  if ($splittable_user_ids === ['ALL_OBJECTS']) {
113  $parent_obj_id = $this->parent_obj_id;
114  if (!$parent_obj_id && $this->parent_ref_id) {
115  $parent_obj_id = ilObject::_lookupObjId($this->parent_ref_id);
116  }
117 
118  if ($this->parent_ref_id) {
119  $user_ids = ilBadgeHandler::getInstance()->getUserIds($this->parent_ref_id, $parent_obj_id);
120  }
121 
122  $badge_id = $this->http->wrapper()->query()->retrieve('bid', $this->refinery->kindlyTo()->int());
123  return [$user_ids, $badge_id];
124  } else {
125  foreach ($splittable_user_ids as $row) {
126  if (str_contains($row, '_')) {
127  $split = explode('_', $row);
128 
129  if ($badge_id === null && $split[0] !== '') {
130  $badge_id = (int) $split[0];
131  }
132 
133  if ($split[1] !== '') {
134  $user_ids[] = (int) $split[1];
135  }
136  } else {
137  return [$user_ids, 0];
138  }
139  }
140  }
141  }
142 
143  return [$user_ids, $badge_id];
144  }
145 
146  public function executeCommand(): void
147  {
148  $ilCtrl = $this->ctrl;
149 
150  $next_class = $ilCtrl->getNextClass($this);
151  $cmd = $ilCtrl->getCmd('listBadges');
152 
153  switch ($next_class) {
154  case 'ilpropertyformgui':
155  // ajax - update
156  if ($this->request->getBadgeId()) {
157  $badge = new ilBadge($this->request->getBadgeId());
158  $type = $badge->getTypeInstance();
159  $form = $this->initBadgeForm('edit', $type, $badge->getTypeId());
160  $this->setBadgeFormValues($form, $badge, $type);
161  } // ajax- create
162  else {
163  $type_id = $this->request->getType();
164  $ilCtrl->setParameter($this, 'type', $type_id);
166  $type = $handler->getTypeInstanceByUniqueId($type_id);
167  $form = $this->initBadgeForm('create', $type, $type_id);
168  }
169  $ilCtrl->forwardCommand($form);
170  break;
171 
172  default:
173  $render_default = true;
174  global $DIC;
175  $action_parameter_token = 'tid_id';
176  $parameter = 'tid_table_action';
177 
178  $query = $DIC->http()->wrapper()->query();
179  if ($query->has($action_parameter_token)) {
180  if ($query->has($action_parameter_token)) {
181  $id = $query->retrieve(
182  $action_parameter_token,
183  $DIC->refinery()->kindlyTo()->listOf($DIC->refinery()->kindlyTo()->string())
184  );
185  if (is_array($id)) {
186  $id = array_pop($id);
187  }
188  $DIC->ctrl()->setParameter($this, "tid", $id);
189  }
190  }
191 
192  $get = fn(string $key) => $query->has($key) ?
193  $query->retrieve($key, $DIC->refinery()->kindlyTo()->string()) :
194  '';
195 
196  $action = $get($parameter);
197  $return_cmd = $get('returnCmd');
198 
199  $actions = [
200  'badge_table_activate' => ['activateBadges', true],
201  'badge_table_deactivate' => ['deactivateBadges', true],
202  'badge_table_edit' => 'editBadge',
203  'badge_table_delete' => 'confirmDeleteBadges',
204  'award_revoke_badge' => 'awardBadgeUserSelection',
205  'revokeBadge' => 'confirmDeassignBadge',
206  'assignBadge' => 'assignBadge',
207  ];
208 
209  $entry = $actions[$action] ?? null;
210  if ($cmd !== 'action' || !$entry) {
211  $this->$cmd();
212  return;
213  }
214 
215  $entry = is_array($entry) ? $entry : [$entry, false];
216  $this->{$entry[0]}();
217  if ($entry[1] && in_array($return_cmd, ['awardBadgeUserSelection', 'listUsers', 'listUsers', 'listBadges'], true)) {
218  $this->$return_cmd();
219  }
220  break;
221  }
222  }
223 
224  public function getSafePostCommands(): array
225  {
226  return [];
227  }
228 
229  public function getUnsafeGetCommands(): array
230  {
231  return ['action'];
232  }
233 
234  protected function setTabs(string $a_active): void
235  {
236  $ilTabs = $this->tabs;
237  $lng = $this->lng;
238  $ilCtrl = $this->ctrl;
239 
240  $ilTabs->addSubTab(
241  'badges',
242  $lng->txt('obj_bdga'),
243  $ilCtrl->getLinkTarget($this, 'listBadges')
244  );
245 
246  $ilTabs->addSubTab(
247  'users',
248  $lng->txt('users'),
249  $ilCtrl->getLinkTarget($this, 'listUsers')
250  );
251 
252  $ilTabs->activateSubTab($a_active);
253  }
254 
255  protected function hasWrite(): bool
256  {
257  $ilAccess = $this->access;
258  return $ilAccess->checkAccess('write', '', $this->parent_ref_id);
259  }
260 
261  protected function listBadges(): void
262  {
263  $ilToolbar = $this->toolbar;
264  $lng = $this->lng;
265  $ilCtrl = $this->ctrl;
266 
267  $this->setTabs('badges');
268 
269  if ($this->hasWrite()) {
271  $valid_types = $handler->getAvailableTypesForObjType($this->parent_obj_type);
272  if ($valid_types) {
273  $options = [];
274  foreach ($valid_types as $id => $type) {
275  $ilCtrl->setParameter($this, 'type', $id);
276  $options[$id] = $this->ui_factory->link()->standard(
277  $this->parent_obj_type !== 'bdga' ? ilBadge::getExtendedTypeCaption($type) : $type->getCaption(),
278  $ilCtrl->getLinkTarget($this, 'addBadge')
279  );
280  $ilCtrl->setParameter($this, 'type', null);
281  }
282  asort($options);
283  $options = array_values($options);
284 
285  $ilToolbar->addComponent(
286  $this->ui_factory->dropdown()->standard($options)->withLabel($lng->txt('badge_create'))
287  );
288  } else {
289  $this->tpl->setOnScreenMessage('info', $lng->txt('badge_no_valid_types_for_obj'));
290  }
291 
292  $clip_ids = $this->session_repo->getBadgeIds();
293  if (count($clip_ids) > 0) {
294  if ($valid_types) {
295  $ilToolbar->addSeparator();
296  }
297 
298  $tt = [];
299  foreach ($this->getValidBadgesFromClipboard() as $badge) {
300  $tt[] = $badge->getTitle();
301  }
302  $ttid = 'bdgpst';
303 
304  $lng->loadLanguageModule('content');
305  $ilToolbar->addButton(
306  $lng->txt('cont_paste_from_clipboard') .
307  ' (' . count($tt) . ')',
308  $ilCtrl->getLinkTarget($this, 'pasteBadges'),
309  '',
310  null,
311  '',
312  $ttid
313  );
314  $ilToolbar->addButton(
315  $lng->txt('clear_clipboard'),
316  $ilCtrl->getLinkTarget($this, 'clearClipboard')
317  );
318  }
319  }
320 
321  $table = new ilBadgeTableGUI($this->parent_obj_id, $this->parent_obj_type, $this->hasWrite());
322  $this->ctrl->setParameter($this, 'returnCmd', __FUNCTION__);
323  $table->renderTable(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTarget($this, 'action'));
324  }
325 
326 
327  //
328  // badge (CRUD)
329  //
330 
331  protected function addBadge(?ilPropertyFormGUI $a_form = null): void
332  {
333  $ilCtrl = $this->ctrl;
334  $tpl = $this->tpl;
335 
336  $type_id = $this->request->getType();
337  if (!$type_id ||
338  !$this->hasWrite()) {
339  $ilCtrl->redirect($this, 'listBadges');
340  }
341 
342  $ilCtrl->setParameter($this, 'type', $type_id);
343 
345  $type = $handler->getTypeInstanceByUniqueId($type_id);
346  if (!$type) {
347  $ilCtrl->redirect($this, 'listBadges');
348  }
349 
350  if (!$a_form) {
351  $a_form = $this->initBadgeForm('create', $type, $type_id);
352  }
353 
354  $tpl->setContent($a_form->getHTML());
355  }
356 
357  protected function initBadgeForm(
358  string $a_mode,
359  ilBadgeType $a_type,
360  string $a_type_unique_id
361  ): ilPropertyFormGUI {
362  $lng = $this->lng;
363  $ilCtrl = $this->ctrl;
364 
365  $form = new ilPropertyFormGUI();
366  $form->setFormAction($ilCtrl->getFormAction($this, 'saveBadge'));
367  $form->setTitle($lng->txt('badge_badge') . ' "' . $a_type->getCaption() . '"');
368 
369  $active = new ilCheckboxInputGUI($lng->txt('active'), 'act');
370  $form->addItem($active);
371 
372  $title = new ilTextInputGUI($lng->txt('title'), 'title');
373  $title->setMaxLength(255);
374  $title->setRequired(true);
375  $form->addItem($title);
376 
377  $desc = new ilTextAreaInputGUI($lng->txt('description'), 'desc');
378  $desc->setMaxNumOfChars(4000);
379  $desc->setRequired(true);
380  $form->addItem($desc);
381 
382  $crit = new ilTextAreaInputGUI($lng->txt('badge_criteria'), 'crit');
383  $crit->setMaxNumOfChars(4000);
384  $crit->setRequired(true);
385  $form->addItem($crit);
386 
387  if ($a_mode === 'create') {
388  // upload
389 
390  $img_mode = new ilRadioGroupInputGUI($lng->txt('image'), 'img_mode');
391  $img_mode->setRequired(true);
392  $img_mode->setValue('tmpl');
393  $form->addItem($img_mode);
394 
395  $img_mode_tmpl = new ilRadioOption($lng->txt('badge_image_from_template'), 'tmpl');
396  $img_mode->addOption($img_mode_tmpl);
397 
398  $img_mode_up = new ilRadioOption($lng->txt('badge_image_from_upload'), 'up');
399  $img_mode->addOption($img_mode_up);
400 
401  $img_upload = new ilImageFileInputGUI($lng->txt('file'), 'img');
402  $img_upload->setRequired(true);
403  $img_upload->setSuffixes(['png', 'svg']);
404  $img_mode_up->addSubItem($img_upload);
405 
406  // templates
407 
408  $valid_templates = ilBadgeImageTemplate::getInstancesByType($a_type_unique_id);
409  if (count($valid_templates)) {
410  $options = [];
411  $options[''] = $lng->txt('please_select');
412  foreach ($valid_templates as $tmpl) {
413  $options[$tmpl->getId()] = $tmpl->getTitle();
414  }
415 
416  $tmpl = new ilSelectInputGUI($lng->txt('badge_image_template_form'), 'tmpl');
417  $tmpl->setRequired(true);
418  $tmpl->setOptions($options);
419  $img_mode_tmpl->addSubItem($tmpl);
420  } else {
421  // no templates, activate upload
422  $img_mode_tmpl->setDisabled(true);
423  $img_mode->setValue('up');
424  }
425  } else {
426  $img_upload = new ilImageFileInputGUI($lng->txt('image'), 'img');
427  $img_upload->setSuffixes(['png', 'svg']);
428  $img_upload->setAllowDeletion(false);
429  $img_upload->setUseCache(false);
430  $form->addItem($img_upload);
431  }
432 
433  $valid = new ilTextInputGUI($lng->txt('badge_valid'), 'valid');
434  $valid->setMaxLength(255);
435  $form->addItem($valid);
436 
437  $custom = $a_type->getConfigGUIInstance();
438  if ($custom instanceof ilBadgeTypeGUI) {
439  $custom->initConfigForm($form, $this->parent_ref_id);
440  }
441 
442  // :TODO: valid date/period
443 
444  if ($a_mode === 'create') {
445  $form->addCommandButton('saveBadge', $lng->txt('save'));
446  } else {
447  $form->addCommandButton('updateBadge', $lng->txt('save'));
448  }
449  $form->addCommandButton('listBadges', $lng->txt('cancel'));
450 
451  return $form;
452  }
453 
458  protected function saveBadge(): void
459  {
460  $ilCtrl = $this->ctrl;
461  $lng = $this->lng;
462 
463  $type_id = $this->request->getType();
464  if (!$type_id ||
465  !$this->hasWrite()) {
466  $ilCtrl->redirect($this, 'listBadges');
467  }
468 
469  $ilCtrl->setParameter($this, 'type', $type_id);
470 
472  $type = $handler->getTypeInstanceByUniqueId($type_id);
473  if (!$type) {
474  $ilCtrl->redirect($this, 'listBadges');
475  }
476 
477  $form = $this->initBadgeForm('create', $type, $type_id);
478  $custom = $type->getConfigGUIInstance();
479 
480  if ($form->checkInput() &&
481  (!$custom || $custom->validateForm($form))) {
482  $badge = new ilBadge();
483  $badge->setParentId($this->parent_obj_id); // :TODO: ref_id?
484  $badge->setTypeId($type_id);
485  $badge->setActive($form->getInput('act'));
486  $badge->setTitle($form->getInput('title'));
487  $badge->setDescription($form->getInput('desc'));
488  $badge->setCriteria($form->getInput('crit'));
489  $badge->setValid($form->getInput('valid'));
490 
491  if ($custom instanceof ilBadgeTypeGUI) {
492  $badge->setConfiguration($custom->getConfigFromForm($form));
493  }
494 
495  $badge->create();
496 
497  if ($form->getInput('img_mode') === 'up') {
498  $this->badge_image_service->processImageUpload($badge);
499  } else {
500  $tmpl = new ilBadgeImageTemplate($form->getInput('tmpl'));
501  $this->cloneBadgeTemplate($badge, new ResourceIdentification($tmpl->getImageRid()));
502  }
503 
504  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
505  $ilCtrl->redirect($this, 'listBadges');
506  }
507 
508  $form->setValuesByPost();
509  $this->addBadge($form);
510  }
511 
512  protected function editBadge(?ilPropertyFormGUI $a_form = null): void
513  {
514  $ilCtrl = $this->ctrl;
515  $tpl = $this->tpl;
516  $lng = $this->lng;
517 
518  $badge_id = $this->request->getBadgeIdFromUrl();
519  if (!$badge_id ||
520  !$this->hasWrite()) {
521  $ilCtrl->redirect($this, 'listBadges');
522  }
523 
524  $ilCtrl->setParameter($this, 'bid', $badge_id);
525 
526  $badge = new ilBadge($badge_id);
527 
528  $static_cnt = ilBadgeHandler::getInstance()->countStaticBadgeInstances($badge);
529  if ($static_cnt) {
530  $this->tpl->setOnScreenMessage('info', sprintf($lng->txt('badge_edit_with_published'), $static_cnt));
531  }
532 
533  if (!$a_form) {
534  $type = $badge->getTypeInstance();
535  $a_form = $this->initBadgeForm('edit', $type, $badge->getTypeId());
536  $this->setBadgeFormValues($a_form, $badge, $type);
537  }
538 
539  $tpl->setContent($a_form->getHTML());
540  }
541 
542  protected function setBadgeFormValues(
543  ilPropertyFormGUI $a_form,
544  ilBadge $a_badge,
545  ilBadgeType $a_type
546  ): void {
547  $a_form->getItemByPostVar('act')->setChecked($a_badge->isActive());
548  $a_form->getItemByPostVar('title')->setValue($a_badge->getTitle());
549  $a_form->getItemByPostVar('desc')->setValue($a_badge->getDescription());
550  $a_form->getItemByPostVar('crit')->setValue($a_badge->getCriteria());
551  $a_form->getItemByPostVar('img')->setValue($a_badge->getImage());
552  $a_form->getItemByPostVar('img')->setImage($a_badge->getImagePath());
553 
554  $image_src = $this->badge_image_service->getImageFromBadge($a_badge);
555  if ($image_src !== '') {
556  $a_form->getItemByPostVar('img')->setImage($image_src);
557  }
558 
559  $a_form->getItemByPostVar('valid')->setValue($a_badge->getValid());
560 
561  $custom = $a_type->getConfigGUIInstance();
562  if ($custom instanceof ilBadgeTypeGUI) {
563  $custom->importConfigToForm($a_form, $a_badge->getConfiguration());
564  }
565  }
566 
571  protected function updateBadge(): void
572  {
573  $ilCtrl = $this->ctrl;
574  $lng = $this->lng;
575 
576  $badge_id = $this->request->getBadgeId();
577  if (!$badge_id ||
578  !$this->hasWrite()) {
579  $ilCtrl->redirect($this, 'listBadges');
580  }
581 
582  $ilCtrl->setParameter($this, 'bid', $badge_id);
583 
584  $badge = new ilBadge($badge_id);
585  $type = $badge->getTypeInstance();
586  $custom = $type->getConfigGUIInstance();
587  if ($custom &&
588  !($custom instanceof ilBadgeTypeGUI)) {
589  $custom = null;
590  }
591  $form = $this->initBadgeForm('update', $type, $badge->getTypeId());
592  if ($form->checkInput() &&
593  (!$custom || $custom->validateForm($form))) {
594  $badge->setActive($form->getInput('act'));
595  $badge->setTitle($form->getInput('title'));
596  $badge->setDescription($form->getInput('desc'));
597  $badge->setCriteria($form->getInput('crit'));
598  $badge->setValid($form->getInput('valid'));
599 
600  $image = $form->getInput('img');
601  if (isset($image['name']) && $image['name'] !== '') {
602  $this->removeResourceStorageImage($badge);
603  $this->badge_image_service->processImageUpload($badge);
604  }
605 
606  if ($custom) {
607  $badge->setConfiguration($custom->getConfigFromForm($form));
608  }
609  $tmpl_id = $form->getInput('tmpl');
610  if ($tmpl_id !== '') {
611  $this->removeResourceStorageImage($badge);
612  $tmpl = new ilBadgeImageTemplate($tmpl_id);
613  $this->cloneBadgeTemplate($badge, new ResourceIdentification($tmpl->getImageRid()));
614  }
615 
616  $badge->update();
617  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
618  $ilCtrl->redirect($this, 'listBadges');
619  }
620 
621  $this->tpl->setOnScreenMessage('failure', $lng->txt('form_input_not_valid'));
622  $form->setValuesByPost();
623  $this->editBadge($form);
624  }
625 
626  protected function confirmDeleteBadges(): void
627  {
628  $ilCtrl = $this->ctrl;
629  $lng = $this->lng;
630  $tpl = $this->tpl;
631  $ilTabs = $this->tabs;
632 
633  $badge_ids = $this->request->getMultiActionBadgeIdsFromUrl();
634  if ($badge_ids === ['ALL_OBJECTS']) {
635  $badge_ids = [];
636  foreach (ilBadge::getInstancesByParentId($this->parent_obj_id) as $badge) {
637  $badge_ids[] = $badge->getId();
638  }
639  }
640 
641  $ilTabs->clearTargets();
642  $ilTabs->setBackTarget(
643  $lng->txt('back'),
644  $ilCtrl->getLinkTarget($this, 'listBadges')
645  );
646 
647  $confirmation_gui = new ilConfirmationGUI();
648  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
649  $confirmation_gui->setHeaderText($lng->txt('badge_deletion_confirmation'));
650  $confirmation_gui->setCancel($lng->txt('cancel'), 'listBadges');
651  $confirmation_gui->setConfirm($lng->txt('delete'), 'deleteBadges');
652 
653  foreach ($badge_ids as $badge_id) {
654  $badge = new ilBadge((int) $badge_id);
655  $confirmation_gui->addItem(
656  'id[]',
657  (string) $badge_id,
658  $badge->getTitle() .
659  ' (' . count(ilBadgeAssignment::getInstancesByBadgeId($badge_id)) . ')'
660  );
661  }
662 
663  $tpl->setContent($confirmation_gui->getHTML());
664  }
665 
666  protected function deleteBadges(): void
667  {
668  $ilCtrl = $this->ctrl;
669  $lng = $this->lng;
670 
671  $badge_ids = $this->request->getIds();
672 
673  if (count($badge_ids) > 0) {
674  foreach ($badge_ids as $badge_id) {
675  $badge = new ilBadge((int) $badge_id);
676  $badge->delete();
677  }
678  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
679  } else {
680  $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_select_one'), true);
681  }
682 
683  $ilCtrl->redirect($this, 'listBadges');
684  }
685 
686 
687  //
688  // badges multi action
689  //
690 
694  protected function getBadgesFromMultiAction(): array
695  {
696  $ilCtrl = $this->ctrl;
697 
698  $badge_ids = $this->request->getIds();
699  if (!$badge_ids ||
700  !$this->hasWrite()) {
701  $ilCtrl->redirect($this, 'listBadges');
702  }
703 
704  return $badge_ids;
705  }
706 
707  protected function copyBadges(): void
708  {
709  $ilCtrl = $this->ctrl;
710 
711  $badge_ids = $this->getBadgesFromMultiAction();
712 
713  $clip_ids = $this->session_repo->getBadgeIds();
714  $clip_ids = array_unique(
715  array_merge($clip_ids, $badge_ids)
716  );
717  $this->session_repo->setBadgeIds(array_map(intval(...), $clip_ids));
718 
719  $ilCtrl->redirect($this, 'listBadges');
720  }
721 
722  protected function clearClipboard(): void
723  {
724  $ilCtrl = $this->ctrl;
725 
726  $this->session_repo->clear();
727  $ilCtrl->redirect($this, 'listBadges');
728  }
729 
733  protected function getValidBadgesFromClipboard(): array
734  {
735  $res = [];
736 
737  $valid_types = array_keys(ilBadgeHandler::getInstance()->getAvailableTypesForObjType($this->parent_obj_type));
738 
739  foreach ($this->session_repo->getBadgeIds() as $badge_id) {
740  $badge = new ilBadge($badge_id);
741  if (in_array($badge->getTypeId(), $valid_types, true)) {
742  $res[] = $badge;
743  }
744  }
745 
746  return $res;
747  }
748 
749  protected function pasteBadges(): void
750  {
751  $ilCtrl = $this->ctrl;
752 
753  $clip_ids = $this->session_repo->getBadgeIds();
754  if (!$this->hasWrite() || count($clip_ids) === 0) {
755  $ilCtrl->redirect($this, 'listBadges');
756  }
757 
758  $copy_suffix = $this->lng->txt("copy_of_suffix");
759  foreach ($this->getValidBadgesFromClipboard() as $badge) {
760  $badge->copy($this->parent_obj_id, $copy_suffix);
761  }
762 
763  $ilCtrl->redirect($this, 'listBadges');
764  }
765 
766  protected function toggleBadges(bool $a_status): void
767  {
768  $ilCtrl = $this->ctrl;
769  $lng = $this->lng;
770 
771  $badge_ids = $this->request->getMultiActionBadgeIdsFromUrl();
772  if (count($badge_ids) > 0) {
773  foreach ($badge_ids as $badge_id) {
774  if ($badge_id === self::TABLE_ALL_OBJECTS_ACTION) {
775  foreach (ilBadge::getInstancesByParentId($this->parent_obj_id) as $badge) {
776  $badge = new ilBadge($badge->getId());
777  $badge->setActive($a_status);
778  $badge->update();
779  }
780  } else {
781  $badge = new ilBadge((int) $badge_id);
782  $badge->setActive($a_status);
783  $badge->update();
784  }
785  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
786  }
787  } else {
788  $this->tpl->setOnScreenMessage('failure', $lng->txt('badge_select_one'), true);
789  }
790 
791  $ilCtrl->redirect($this, 'listBadges');
792  }
793 
794  protected function activateBadges(): void
795  {
796  $this->toggleBadges(true);
797  }
798 
799  protected function deactivateBadges(): void
800  {
801  $this->toggleBadges(false);
802  }
803 
804 
805  //
806  // users
807  //
808 
809  protected function listUsers(): void
810  {
811  $lng = $this->lng;
812  $ilCtrl = $this->ctrl;
813  $ilToolbar = $this->toolbar;
814  $tpl = $this->tpl;
815 
816  $this->setTabs('users');
817 
818  if ($this->hasWrite()) {
819  $manual = ilBadgeHandler::getInstance()->getAvailableManualBadges(
820  $this->parent_obj_id,
821  $this->parent_obj_type
822  );
823  if (count($manual)) {
824  $drop = new ilSelectInputGUI($lng->txt('badge_badge'), 'bid');
825  $drop->setOptions($manual);
826  $ilToolbar->addInputItem($drop, true);
827 
828  $ilToolbar->setFormAction($ilCtrl->getFormAction($this, 'selectBadgeForAwardingOrRevoking'));
829  $ilToolbar->addFormButton($lng->txt('badge_award_badge'), 'selectBadgeForAwardingOrRevoking');
830  }
831  }
832 
833  $tbl = new ilBadgeUserTableGUI($this->parent_ref_id);
834  $this->ctrl->setParameter($this, 'returnCmd', __FUNCTION__);
835  $tbl->renderTable(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTarget($this, 'action'));
836  }
837 
838  private function selectBadgeForAwardingOrRevoking(): never
839  {
840  $this->ctrl->setParameter(
841  $this,
842  'bid',
843  $this->http->wrapper()->post()->retrieve('bid', $this->refinery->kindlyTo()->int())
844  );
845  $this->ctrl->redirect($this, 'awardBadgeUserSelection');
846  }
847 
848  protected function awardBadgeUserSelection(): void
849  {
850  $badge_ids = $this->request->getMultiActionBadgeIdsFromUrl();
851  $bid = null;
852 
853  if ($badge_ids === []) {
854  if ($this->http->wrapper()->post()->has('bid')) {
855  $bid = $this->http->wrapper()->post()->retrieve('bid', $this->refinery->kindlyTo()->int());
856  } elseif ($this->http->wrapper()->query()->has('bid')) {
857  $bid = $this->http->wrapper()->query()->retrieve('bid', $this->refinery->kindlyTo()->int());
858  }
859  } elseif (count($badge_ids) === 1) {
860  $bid = (int) $badge_ids[0];
861  }
862 
863  if (!$bid ||
864  !$this->hasWrite()) {
865  $this->ctrl->redirect($this, 'listUsers');
866  }
867 
868  $manual = array_keys(
869  ilBadgeHandler::getInstance()->getAvailableManualBadges($this->parent_obj_id, $this->parent_obj_type)
870  );
871 
872  if (!in_array($bid, $manual, true)) {
873  $this->ctrl->redirect($this, 'listUsers');
874  }
875 
876  $back_target = 'listUsers';
877  if ($this->request->getTgt() === 'bdgl') {
878  $this->ctrl->saveParameter($this, 'tgt');
879  $back_target = 'listBadges';
880  }
881 
882  $this->tabs->clearTargets();
883  $this->tabs->setBackTarget(
884  $this->lng->txt('back'),
885  $this->ctrl->getLinkTarget($this, $back_target)
886  );
887 
888  $this->ctrl->setParameter($this, 'bid', $bid);
889 
890  $badge = new ilBadge($bid);
891 
892  $tbl = new ilBadgeUserTableGUI($this->parent_ref_id, $badge);
893  $this->ctrl->setParameter($this, 'returnCmd', __FUNCTION__);
894  $tbl->renderTable(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTarget($this, 'action'));
895  }
896 
897  protected function assignBadge(): void
898  {
899  $ilCtrl = $this->ctrl;
900  $ilUser = $this->user;
901  $lng = $this->lng;
902 
903  $splittable_user_ids = $this->request->getBadgeAssignableUsers();
904  [$user_ids, $badge_id] = $this->splitBadgeAndUserIdsFromString($splittable_user_ids);
905 
906  if (!$user_ids ||
907  !$badge_id ||
908  !$this->hasWrite()) {
909  $ilCtrl->redirect($this, 'listUsers');
910  }
911 
912  $new_badges = [];
913  foreach ($user_ids as $user_id) {
914  if (!ilBadgeAssignment::exists($badge_id, $user_id)) {
915  $ass = new ilBadgeAssignment($badge_id, $user_id);
916  $ass->setAwardedBy($ilUser->getId());
917  $ass->store();
918 
919  $new_badges[$user_id][] = $badge_id;
920  }
921  }
922 
923  ilBadgeHandler::getInstance()->sendNotification($new_badges, $this->parent_ref_id);
924 
925  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
926  $ilCtrl->redirect($this, 'listUsers');
927  }
928 
929  protected function confirmDeassignBadge(): void
930  {
931  $ilCtrl = $this->ctrl;
932  $lng = $this->lng;
933  $tpl = $this->tpl;
934  $ilTabs = $this->tabs;
935 
936  $splittable_user_ids = $this->request->getMultiActionBadgeIdsFromUrl();
937  [$user_ids, $badge_id] = $this->splitBadgeAndUserIdsFromString($splittable_user_ids);
938 
939  if (!$user_ids ||
940  !$badge_id ||
941  !$this->hasWrite()) {
942  $ilCtrl->redirect($this, 'listUsers');
943  }
944 
945  $ilTabs->clearTargets();
946  $ilTabs->setBackTarget(
947  $lng->txt('back'),
948  $ilCtrl->getLinkTarget($this, 'listUsers')
949  );
950 
951  $badge = new ilBadge($badge_id);
952 
953  $ilCtrl->setParameter($this, 'bid', $badge->getId());
954 
955  $confirmation_gui = new ilConfirmationGUI();
956  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
957  $confirmation_gui->setHeaderText(
958  sprintf($lng->txt('badge_assignment_deletion_confirmation'), $badge->getTitle())
959  );
960  $confirmation_gui->setCancel($lng->txt('cancel'), 'listUsers');
961  $confirmation_gui->setConfirm($lng->txt('badge_remove_badge'), 'deassignBadge');
962 
963  $assigned_users = ilBadgeAssignment::getAssignedUsers($badge->getId());
964 
965  foreach ($user_ids as $user_id) {
966  if (in_array($user_id, $assigned_users, true)) {
967  $confirmation_gui->addItem(
968  "id[$user_id]",
969  (string) $badge_id,
970  ilUserUtil::getNamePresentation($user_id, false, false, '', true)
971  );
972  }
973  }
974 
975  $tpl->setContent($confirmation_gui->getHTML());
976  }
977 
978  protected function deassignBadge(): void
979  {
980  $ilCtrl = $this->ctrl;
981  $lng = $this->lng;
982 
983  $post_values = $this->request->getIds();
984  $user_ids = [];
985  $badge_id = null;
986  foreach ($post_values as $usr_id => $found_badge_id) {
987  $badge_id = $found_badge_id;
988  $user_ids[] = $usr_id;
989  }
990 
991  if (!$user_ids ||
992  !$badge_id ||
993  !$this->hasWrite()) {
994  $ilCtrl->redirect($this, 'listUsers');
995  }
996 
997  foreach ($user_ids as $user_id) {
998  $ass = new ilBadgeAssignment((int) $badge_id, (int) $user_id);
999  $ass->delete();
1000  }
1001 
1002  $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
1003  $ilCtrl->redirect($this, 'listUsers');
1004  }
1005 
1009  protected function cloneBadgeTemplate(ilBadge $badge, ?ResourceIdentification $rid): void
1010  {
1011  if ($rid !== null) {
1012  $new_rid = $this->badge_image_service->cloneBadgeImageByRid($rid);
1013  $badge->setImageRid($new_rid);
1014  $badge->update();
1015  }
1016  }
1017 
1018  protected function removeResourceStorageImage(ilBadge $badge): void
1019  {
1020  if ($badge->getImageRid() !== '') {
1021  $this->resource_storage->manage()->remove(
1022  new ResourceIdentification($badge->getImageRid()),
1024  );
1025  }
1026  }
1027 }
initBadgeForm(string $a_mode, ilBadgeType $a_type, string $a_type_unique_id)
This class represents an option in a radio group.
$res
Definition: ltiservices.php:66
ilBadgeManagementGUI: ilPropertyFormGUI
static getInstancesByType(string $a_type_unique_id)
getConfiguration()
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...
static getInstancesByParentId(int $a_parent_id, ?array $a_filter=null)
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) ...
ilBadgePictureDefinition $flavour_definition
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$valid
getConfigGUIInstance()
Get GUI config instance.
setContent(string $a_html)
Sets content for standard template.
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)
getSafePostCommands()
This method must return a list of safe POST commands.
cloneBadgeTemplate(ilBadge $badge, ?ResourceIdentification $rid)
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getNextClass($a_gui_class=null)
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
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:
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getExtendedTypeCaption(ilBadgeType $a_type)
redirect(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false)
addBadge(?ilPropertyFormGUI $a_form=null)
static getInstancesByBadgeId(int $a_badge_id)
global $DIC
Definition: shib_login.php:26
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...
editBadge(?ilPropertyFormGUI $a_form=null)
splitBadgeAndUserIdsFromString(array $splittable_user_ids)
clearTargets()
clear all targets
Class FileUpload.
Definition: FileUpload.php:37
setRequired(bool $a_required)
__construct(private readonly int $parent_ref_id, ?int $a_parent_obj_id=null, ?string $a_parent_obj_type=null)
static exists(int $a_badge_id, int $a_user_id)
$handler
Definition: oai.php:29
This class represents an image file property in a property form.
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This class represents a text area property in a property form.
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...
setImageRid(?string $image_rid)