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