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