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