ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.PublicUserProfileBadgesRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge;
22 
23 use ilBadge;
25 use ilBadgeRenderer;
28 use ilLanguage;
29 use ilTemplate;
30 
32 {
34  private ilLanguage $lng;
35  private IRSS $irss;
36 
37  public function __construct(
38  ?ilGlobalTemplateInterface $main_tpl = null,
39  ?ilLanguage $lng = null
40  ) {
41  global $DIC;
42  $this->main_tpl = $main_tpl ?? $DIC->ui()->mainTemplate();
43  $this->lng = $lng ?? $DIC->language();
44  $this->lng->loadLanguageModule('badge');
45 
46  $this->irss = $DIC->resourceStorage();
47  }
48 
49  public function render(int $user_id): string
50  {
51  $tpl = new ilTemplate(
52  'tpl.usr_public_profile_badges.html',
53  true,
54  true,
55  'components/ILIAS/Badge'
56  );
57 
58  $this->main_tpl->addJavaScript('assets/js/PublicProfileBadges.js');
59  $this->main_tpl->addOnLoadCode('new BadgeToggle("ilbdgprcont", "ilbdgprfhdm", "ilbdgprfhdl", "ilNoDisplay");');
60  $user_badges = ilBadgeAssignment::getInstancesByUserId($user_id);
61  if ($user_badges) {
62  $has_public_badge = false;
63  $cnt = 0;
64 
65  $cut = 20;
66 
67  $badge_by_id = [];
68  $image_rids = [];
69  foreach ($user_badges as $ass) {
70  $badge = new ilBadge($ass->getBadgeId());
71  $image_rids[] = $badge->getImageRid();
72  $badge_by_id[$badge->getId()] = $badge;
73  }
74 
75  $this->irss->preload(array_filter($image_rids));
76 
77  foreach ($user_badges as $ass) {
78  // only active
79  if ($ass->getPosition()) {
80  $cnt++;
81 
82  $renderer = new ilBadgeRenderer($ass, $badge_by_id[$ass->getBadgeId()]);
83 
84  // limit to 20, [MORE] link
85  if ($cnt > $cut) {
86  $tpl->setCurrentBlock('hidden_badge');
87  $tpl->touchBlock('hidden_badge');
88  $tpl->parseCurrentBlock();
89  }
90 
91  $tpl->setCurrentBlock('badge_bl');
92  $tpl->setVariable('BADGE', $renderer->getHTML());
93  $tpl->parseCurrentBlock();
94 
95  $has_public_badge = true;
96  }
97  }
98 
99  if ($cnt > $cut) {
100  $tpl->setVariable('BADGE_HIDDEN_TXT_MORE', $this->lng->txt('badge_profile_more'));
101  $tpl->setVariable('BADGE_HIDDEN_TXT_LESS', $this->lng->txt('badge_profile_less'));
102  }
103 
104  if ($has_public_badge) {
105  $tpl->setVariable('TXT_BADGES', $this->lng->txt('obj_bdga'));
106  }
107  }
108 
109  return $tpl->get();
110  }
111 }
$renderer
static getInstancesByUserId(int $a_user_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
__construct(?ilGlobalTemplateInterface $main_tpl=null, ?ilLanguage $lng=null)