ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilBadgeProfileGUI.php
Go to the documentation of this file.
1<?php
2
23
25{
26 private const string LIST_BADGES_ACTION = 'listBadges';
27 private const string MANAGE_BADGES_ACTION = 'manageBadges';
28 private const string TABLE_ACTIONS = 'handleTableActions';
30
31 public const string TABLE_ALL_OBJECTS_ACTION = 'ALL_OBJECTS';
32
33 final public const string BACKPACK_EMAIL = 'badge_mozilla_bp';
34
35 private readonly ilBadgeGUIRequest $request;
36 private readonly ilCtrlInterface $ctrl;
37 private readonly ilLanguage $lng;
39 private readonly ilTabsGUI $tabs;
40 private readonly ilObjUser $user;
41 private readonly \ILIAS\Refinery\Factory $refinery;
42 private readonly \ILIAS\HTTP\GlobalHttpState $http;
44 private readonly TileView $tile_view;
45
46 public function __construct()
47 {
48 global $DIC;
49
50 $this->ctrl = $DIC->ctrl();
51 $this->lng = $DIC->language();
52 $this->tpl = $DIC['tpl'];
53 $this->tabs = $DIC->tabs();
54 $this->user = $DIC->user();
55 $this->refinery = $DIC->refinery();
56 $this->http = $DIC->http();
57 $this->request = new ilBadgeGUIRequest(
58 $DIC->http(),
59 $DIC->refinery()
60 );
61
62 $this->noti_repo = new BadgeNotificationPrefRepository();
63
64 $this->tile_view = new TileView(
65 $DIC,
66 self::class,
67 new Tile($DIC),
68 new PresentationHeader($DIC, self::class)
69 );
70 }
71
72 public function executeCommand(): void
73 {
74 $this->lng->loadLanguageModule('badge');
75
76 switch ($this->ctrl->getNextClass()) {
77 default:
78 $cmd = $this->ctrl->getCmd(self::DEFAULT_ACTION);
79 if ($cmd === '' || $cmd === null || !method_exists($this, $cmd . 'Cmd')) {
81 }
82 $cmd .= 'Cmd';
83
84 $this->$cmd();
85 break;
86 }
87 }
88
89 public function getSafePostCommands(): array
90 {
91 return [];
92 }
93
94 private function getTableAction(): ?string
95 {
96 return $this->http->wrapper()->query()->retrieve(
97 'badge_table_action',
98 $this->refinery->byTrying([
99 $this->refinery->kindlyTo()->string(),
100 $this->refinery->always(null)
101 ])
102 );
103 }
104
105 private function handleTableActionsCmd(): void
106 {
107 match ($this->getTableAction()) {
108 'obj_badge_activate' => $this->activate(),
109 'obj_badge_deactivate' => $this->deactivate(),
110 default => $this->ctrl->redirect($this, self::DEFAULT_ACTION),
111 };
112 }
113
114 public function getUnsafeGetCommands(): array
115 {
116 return [self::TABLE_ACTIONS];
117 }
118
119 private function listBadgesCmd(): void
120 {
121 $this->tpl->setContent($this->renderDeck($this->tile_view->show()));
122 $this->noti_repo->updateLastCheckedTimestamp();
123 }
124
125 private function renderDeck(string $deck): string
126 {
127 $template = new ilTemplate('tpl.badge_backpack.html', true, true, 'components/ILIAS/Badge/');
128 $template->setVariable('DECK', $deck);
129 return $template->get();
130 }
131
132 private function manageBadgesCmd(): void
133 {
135 $tpl->renderTable(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTarget($this, self::TABLE_ACTIONS));
136 }
137
138 private function getMultiSelection(): array
139 {
140 $badge_ids = array_filter(
141 $this->http->wrapper()->query()->retrieve(
142 'badge_id',
143 $this->refinery->byTrying([
144 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
145 $this->refinery->always([])
146 ])
147 )
148 );
149
150 if ($badge_ids === [self::TABLE_ALL_OBJECTS_ACTION]) {
151 $badge_assignments = array_filter(
153 static fn(ilBadgeAssignment $ass): bool => (bool) $ass->getTimestamp()
154 );
155 } else {
156 $badge_assignments = array_filter(
157 array_map(
158 fn(int $badge_id): ilBadgeAssignment => new ilBadgeAssignment(
159 $badge_id,
160 $this->user->getId()
161 ),
162 $this->request->getBadgeIds()
163 ),
164 static fn(ilBadgeAssignment $ass): bool => (bool) $ass->getTimestamp()
165 );
166 }
167
168 if (!empty($badge_assignments)) {
169 return $badge_assignments;
170 }
171
172 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('select_one'), true);
173 $this->ctrl->redirect($this, self::MANAGE_BADGES_ACTION);
174 }
175
176 private function activate(): void
177 {
178 foreach ($this->getMultiSelection() as $ass) {
179 // already active?
180 if (!$ass->getPosition()) {
181 $ass->setPosition(999);
182 $ass->store();
183 }
184 }
185
186 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('position_updated'), true);
187 $this->ctrl->redirect($this, self::MANAGE_BADGES_ACTION);
188 }
189
190 private function deactivate(): void
191 {
192 foreach ($this->getMultiSelection() as $ass) {
193 // already inactive?
194 if ($ass->getPosition()) {
195 $ass->setPosition(null);
196 $ass->store();
197 }
198 }
199
200 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('position_updated'), true);
201 $this->ctrl->redirect($this, self::MANAGE_BADGES_ACTION);
202 }
203
204 private function activateInCardCmd(): void
205 {
206 foreach ($this->getMultiSelection() as $ass) {
207 // already active?
208 if (!$ass->getPosition()) {
209 $ass->setPosition(999);
210 $ass->store();
211 }
212 }
213
214 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('position_updated'), true);
215 $this->ctrl->redirect($this, self::LIST_BADGES_ACTION);
216 }
217
218 private function deactivateInCardCmd(): void
219 {
220 foreach ($this->getMultiSelection() as $ass) {
221 // already inactive?
222 if ($ass->getPosition()) {
223 $ass->setPosition(null);
224 $ass->store();
225 }
226 }
227
228 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('position_updated'), true);
229 $this->ctrl->redirect($this, self::LIST_BADGES_ACTION);
230 }
231
232 private function setBackpackSubTabs(): void
233 {
234 $this->tabs->addSubTab(
235 'backpack_badges',
236 $this->lng->txt('obj_bdga'),
237 $this->ctrl->getLinkTarget($this, 'listBackpackGroups')
238 );
239
240 $this->tabs->addSubTab(
241 'backpack_settings',
242 $this->lng->txt('settings'),
243 $this->ctrl->getLinkTarget($this, 'editSettings')
244 );
245
246 $this->tabs->activateTab('backpack_badges');
247 }
248
249 private function listBackpackGroupsCmd(): void
250 {
251 $this->setBackpackSubTabs();
252 $this->tabs->activateSubTab('backpack_badges');
253
254 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_INFO, $this->lng->txt('badge_backpack_gallery_info'));
255
256 $bp = new ilBadgeBackpack($this->getBackpackMail());
257 $bp_groups = $bp->getGroups();
258
259 if (!count($bp_groups)) {
260 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_INFO, $this->lng->txt('badge_backpack_no_groups'));
261 return;
262 }
263
264 $tmpl = new ilTemplate('tpl.badge_backpack.html', true, true, 'components/ILIAS/Badge/');
265
266 $tmpl->setVariable('BACKPACK_TITLE', $this->lng->txt('badge_backpack_list'));
267
269
270 foreach ($bp_groups as $group_id => $group) {
271 $bp_badges = $bp->getBadges($group_id);
272 if (count($bp_badges)) {
273 foreach ($bp_badges as $badge) {
274 $tmpl->setCurrentBlock('badge_bl');
275 $tmpl->setVariable('BADGE_TITLE', $badge['title']);
276 $tmpl->setVariable('BADGE_DESC', $badge['description']);
277 $tmpl->setVariable('BADGE_IMAGE', $badge['image_url']);
278 $tmpl->setVariable('BADGE_CRITERIA', $badge['criteria_url']);
279 $tmpl->setVariable('BADGE_ISSUER', $badge['issuer_name']);
280 $tmpl->setVariable('BADGE_ISSUER_URL', $badge['issuer_url']);
281 $tmpl->setVariable('BADGE_DATE', ilDatePresentation::formatDate($badge['issued_on']));
282 $tmpl->parseCurrentBlock();
283 }
284 }
285
286 $tmpl->setCurrentBlock('group_bl');
287 $tmpl->setVariable('GROUP_TITLE', $group['title']);
288 $tmpl->parseCurrentBlock();
289 }
290
291 $this->tpl->setContent($tmpl->get());
292 }
293
294 //
295 // settings
296 //
297
298 private function getBackpackMail(): string
299 {
300 $mail = $this->user->getPref(self::BACKPACK_EMAIL);
301 if (!$mail) {
302 $mail = $this->user->getEmail();
303 }
304
305 return $mail;
306 }
307
309 {
310 $form = new ilPropertyFormGUI();
311 $form->setFormAction($this->ctrl->getFormAction($this, 'saveSettings'));
312 $form->setTitle($this->lng->txt('settings'));
313
314 $email = new ilEMailInputGUI($this->lng->txt('badge_backpack_email'), 'email');
315 $email->setInfo($this->lng->txt('badge_backpack_email_info'));
316 $email->setValue($this->getBackpackMail());
317 $form->addItem($email);
318
319 $form->addCommandButton('saveSettings', $this->lng->txt('save'));
320
321 return $form;
322 }
323
324 private function editSettingsCmd(?ilPropertyFormGUI $a_form = null): void
325 {
326 $this->ctrl->redirect($this, self::LIST_BADGES_ACTION);
327 }
328
329 private function saveSettingsCmd(): void
330 {
331 $form = $this->initSettingsForm();
332 if ($form->checkInput()) {
333 $new_email = $form->getInput('email');
334 $old_email = $this->getBackpackMail();
335
336 $this->user->writePref(self::BACKPACK_EMAIL, $new_email);
337
338 // if email was changed: delete badge files
339 if ($new_email !== $old_email) {
341 }
342
343 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('settings_saved'), true);
344 $this->ctrl->redirect($this, 'editSettings');
345 }
346
347 $form->setValuesByPost();
348 $this->editSettingsCmd($form);
349 }
350}
Badge notification repository (using user preferences.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static clearBadgeCache(int $a_user_id)
static getInstancesByUserId(int $a_user_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly BadgeNotificationPrefRepository $noti_repo
readonly ILIAS HTTP GlobalHttpState $http
getSafePostCommands()
This method must return a list of safe POST commands.
readonly ilGlobalTemplateInterface $tpl
const string TABLE_ALL_OBJECTS_ACTION
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
final const string BACKPACK_EMAIL
readonly ilTabsGUI $tabs
readonly ilCtrlInterface $ctrl
readonly ilBadgeGUIRequest $request
readonly ilObjUser $user
readonly ilLanguage $lng
readonly ILIAS Refinery Factory $refinery
editSettingsCmd(?ilPropertyFormGUI $a_form=null)
readonly TileView $tile_view
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
This class represents a email property in a property form.
language handling
User class.
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
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.
global $DIC
Definition: shib_login.php:26