ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBadgeProfileGUI.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $ctrl;
16
20 protected $lng;
21
25 protected $tpl;
26
30 protected $tabs;
31
35 protected $user;
36
40 protected $access;
41
45 protected $factory;
46
50 protected $renderer;
51
55 protected $noti_repo;
56
60 public function __construct()
61 {
62 global $DIC;
63
64 $this->ctrl = $DIC->ctrl();
65 $this->lng = $DIC->language();
66 $this->tpl = $DIC["tpl"];
67 $this->tabs = $DIC->tabs();
68 $this->user = $DIC->user();
69 $this->access = $DIC->access();
70 $this->factory = $DIC->ui()->factory();
71 $this->renderer = $DIC->ui()->renderer();
72
73 $this->noti_repo = new \ILIAS\Badge\Notification\BadgeNotificationPrefRepository();
74 }
75
76 const BACKPACK_EMAIL = "badge_mozilla_bp";
77
78 public function executeCommand()
79 {
83
84 $lng->loadLanguageModule("badge");
85
86 //$tpl->setTitle($lng->txt("obj_bdga"));
87 //$tpl->setTitleIcon(ilUtil::getImagePath("icon_bdga.svg"));
88
89 switch ($ilCtrl->getNextClass()) {
90 default:
91 $this->setTabs();
92 $cmd = $ilCtrl->getCmd("listBadges");
93 $this->$cmd();
94 break;
95 }
96 }
97
98 protected function setTabs()
99 {
100 $ilTabs = $this->tabs;
103
104 if (ilBadgeHandler::getInstance()->isObiActive()) {
105 $ilTabs->addTab(
106 "ilias_badges",
107 $lng->txt("badge_personal_badges"),
108 $ilCtrl->getLinkTarget($this, "listBadges")
109 );
110
111 $ilTabs->addTab(
112 "backpack_badges",
113 $lng->txt("badge_backpack_list"),
114 $ilCtrl->getLinkTarget($this, "listBackpackGroups")
115 );
116 }
117 }
118
119
120 //
121 // list
122 //
123
124 protected function getSubTabs($a_active)
125 {
126 $ilTabs = $this->tabs;
129
130 if (ilBadgeHandler::getInstance()->isObiActive()) {
131 $ilTabs->addSubTab(
132 "list",
133 $lng->txt("badge_profile_view"),
134 $ilCtrl->getLinkTarget($this, "listBadges")
135 );
136 $ilTabs->addSubTab(
137 "manage",
138 $lng->txt("badge_profile_manage"),
139 $ilCtrl->getLinkTarget($this, "manageBadges")
140 );
141 $ilTabs->activateTab("ilias_badges");
142 $ilTabs->activateSubTab($a_active);
143 } else {
144 $ilTabs->addTab(
145 "list",
146 $lng->txt("badge_profile_view"),
147 $ilCtrl->getLinkTarget($this, "listBadges")
148 );
149 $ilTabs->addTab(
150 "manage",
151 $lng->txt("badge_profile_manage"),
152 $ilCtrl->getLinkTarget($this, "manageBadges")
153 );
154 $ilTabs->activateTab($a_active);
155 }
156 }
157
158 protected function listBadges()
159 {
162
163 $this->getSubTabs("list");
164
165 $data = array();
166
167 // see ilBadgePersonalTableGUI::getItems()
168 foreach (ilBadgeAssignment::getInstancesByUserId($ilUser->getId()) as $ass) {
169 $badge = new ilBadge($ass->getBadgeId());
170
171 $data[] = array(
172 "id" => $badge->getId(),
173 "title" => $badge->getTitle(),
174 "description" => $badge->getDescription(),
175 "image" => $badge->getImagePath(),
176 "name" => $badge->getImage(),
177 "issued_on" => $ass->getTimestamp(),
178 "active" => (bool) $ass->getPosition(),
179 "object" => $badge->getParentMeta(),
180 "renderer" => new ilBadgeRenderer($ass)
181 );
182 }
183
184 // :TODO:
185 $data = ilUtil::sortArray($data, "issued_on", "desc", true);
186
187 $tmpl = new ilTemplate("tpl.badge_backpack.html", true, true, "Services/Badge");
188
190
191 $cards = array();
192 $badge_components = array();
193
194 foreach ($data as $badge) {
195 $modal = $this->factory->modal()->roundtrip(
196 $badge["title"],
197 $this->factory->legacy($badge["renderer"]->renderModalContent())
198 )->withCancelButtonLabel("ok");
199 $image = $this->factory->image()->responsive($badge["image"], $badge["name"])
200 ->withAction($modal->getShowSignal());
201
202 $this->ctrl->setParameter($this, "badge_id", $badge["id"]);
203 $url = $this->ctrl->getLinkTarget($this, $badge["active"]
204 ? "deactivateInCard"
205 : "activateInCard");
206 $this->ctrl->setParameter($this, "badge_id", "");
207 $profile_button = $this->factory->button()->standard(
208 $this->lng->txt(!$badge["active"] ? "badge_add_to_profile" : "badge_remove_from_profile"),
209 $url
210 );
211
212 if ($badge["object"]["type"] != "bdga") {
213 $parent_icon = $this->factory->symbol()->icon()->custom(
214 ilObject::_getIcon($badge["object"]["id"], "big", $badge["object"]["type"]),
215 $this->lng->txt("obj_" . $badge["object"]["type"]),
216 "medium"
217 );
218
219 $parent_ref_id = array_shift(ilObject::_getAllReferences($badge["object"]["id"]));
220 if ($this->access->checkAccess("read", "", $parent_ref_id)) {
221 $parent_link = $this->factory->link()->standard($badge["object"]["title"], ilLink::_getLink($parent_ref_id));
222 } else {
223 $parent_link = $this->factory->legacy($badge["object"]["title"]);
224 }
225
226 $badge_sections = [
227 $this->factory->listing()->descriptive([
228 $this->lng->txt("object") => $this->factory->legacy(
229 $this->renderer->render($parent_icon) . $this->renderer->render($parent_link)
230 )
231 ]),
232 $profile_button
233 ];
234 } else {
235 $badge_sections = [$profile_button];
236 }
237
238 $cards[] = $this->factory->card()->standard($badge["title"], $image)->withSections($badge_sections)
239 ->withTitleAction($modal->getShowSignal());
240
241 $badge_components[] = $modal;
242 }
243
244 $deck = $this->factory->deck($cards)->withNormalCardsSize();
245 $badge_components[] = $deck;
246
247 $tmpl->setVariable("DECK", $this->renderer->render($badge_components));
248 $tpl->setContent($tmpl->get());
249
250 $this->noti_repo->updateLastCheckedTimestamp();
251 }
252
253 protected function manageBadges()
254 {
256
257 $this->getSubTabs("manage");
258
259 $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
260
261 $tpl->setContent($tbl->getHTML());
262 }
263
264 protected function applyFilter()
265 {
266 $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
267 $tbl->resetOffset();
268 $tbl->writeFilterToSession();
269 $this->manageBadges();
270 }
271
272 protected function resetFilter()
273 {
274 $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
275 $tbl->resetOffset();
276 $tbl->resetFilter();
277 $this->manageBadges();
278 }
279
280 protected function getMultiSelection()
281 {
285
286 $ids = $_POST["badge_id"];
287 if (!$ids && is_numeric($_GET["badge_id"])) {
288 $ids = array($_GET["badge_id"]);
289 }
290 if (is_array($ids)) {
291 $res = array();
292 foreach ($ids as $id) {
293 $ass = new ilBadgeAssignment($id, $ilUser->getId());
294 if ($ass->getTimestamp()) {
295 $res[] = $ass;
296 }
297 }
298
299 return $res;
300 } else {
301 ilUtil::sendFailure($lng->txt("select_one"), true);
302 $ilCtrl->redirect($this, "manageBadges");
303 }
304 }
305
306 protected function activate()
307 {
310
311 foreach ($this->getMultiSelection() as $ass) {
312 // already active?
313 if (!$ass->getPosition()) {
314 $ass->setPosition(999);
315 $ass->store();
316 }
317 }
318
319 ilUtil::sendSuccess($lng->txt("settings_saved"), true);
320 $ilCtrl->redirect($this, "manageBadges");
321 }
322
323 protected function deactivate()
324 {
327
328 foreach ($this->getMultiSelection() as $ass) {
329 // already inactive?
330 if ($ass->getPosition()) {
331 $ass->setPosition(null);
332 $ass->store();
333 }
334 }
335
336 ilUtil::sendSuccess($lng->txt("settings_saved"), true);
337 $ilCtrl->redirect($this, "manageBadges");
338 }
339
340 protected function activateInCard()
341 {
344
345 foreach ($this->getMultiSelection() as $ass) {
346 // already active?
347 if (!$ass->getPosition()) {
348 $ass->setPosition(999);
349 $ass->store();
350 }
351 }
352
353 ilUtil::sendSuccess($lng->txt("settings_saved"), true);
354 $ilCtrl->redirect($this, "listBadges");
355 }
356
357 protected function deactivateInCard()
358 {
361
362 foreach ($this->getMultiSelection() as $ass) {
363 // already inactive?
364 if ($ass->getPosition()) {
365 $ass->setPosition(null);
366 $ass->store();
367 }
368 }
369
370 ilUtil::sendSuccess($lng->txt("settings_saved"), true);
371 $ilCtrl->redirect($this, "listBadges");
372 }
373
374
375 //
376 // (mozilla) backpack
377 //
378
379 protected function addToBackpackMulti()
380 {
382 $ilTabs = $this->tabs;
385
386 $res = array();
387 foreach ($this->getMultiSelection() as $ass) {
388 $url = $this->prepareBadge($ass->getBadgeId());
389 if ($url !== false) {
390 $badge = new ilBadge($ass->getBadgeId());
391 $titles[] = $badge->getTitle();
392 $res[] = $url;
393 }
394 }
395
396 // :TODO: use local copy instead?
397 $tpl->addJavascript("https://backpack.openbadges.org/issuer.js", false);
398
399 $tpl->addJavascript("Services/Badge/js/ilBadge.js");
400 $tpl->addOnloadCode("il.Badge.publishMulti(['" . implode("','", $res) . "']);");
401
402 $ilTabs->clearTargets();
403 $ilTabs->setBackTarget(
404 $lng->txt("back"),
405 $ilCtrl->getLinkTarget($this, "manageBadges")
406 );
407
408 ilUtil::sendInfo(sprintf($lng->txt("badge_add_to_backpack_multi"), implode(", ", $titles)));
409 }
410
411 protected function setBackpackSubTabs()
412 {
413 $ilTabs = $this->tabs;
416
417 $ilTabs->addSubTab(
418 "backpack_badges",
419 $lng->txt("obj_bdga"),
420 $ilCtrl->getLinkTarget($this, "listBackpackGroups")
421 );
422
423 $ilTabs->addSubTab(
424 "backpack_settings",
425 $lng->txt("settings"),
426 $ilCtrl->getLinkTarget($this, "editSettings")
427 );
428
429 $ilTabs->activateTab("backpack_badges");
430 }
431
432 protected function listBackpackGroups()
433 {
437 $ilTabs = $this->tabs;
438
439 if (!ilBadgeHandler::getInstance()->isObiActive()) {
440 $ilCtrl->redirect($this, "listBadges");
441 }
442
443 $this->setBackpackSubTabs();
444 $ilTabs->activateSubTab("backpack_badges");
445
446 ilUtil::sendInfo($lng->txt("badge_backpack_gallery_info"));
447
448 $bp = new ilBadgeBackpack($this->getBackpackMail());
449 $bp_groups = $bp->getGroups();
450
451 if (!is_array($bp_groups)) {
452 ilUtil::sendInfo(sprintf($lng->txt("badge_backpack_connect_failed"), $this->getBackpackMail()));
453 return;
454 } elseif (!sizeof($bp_groups)) {
455 ilUtil::sendInfo($lng->txt("badge_backpack_no_groups"));
456 return;
457 }
458
459 $tmpl = new ilTemplate("tpl.badge_backpack.html", true, true, "Services/Badge");
460
461 $tmpl->setVariable("BACKPACK_TITLE", $lng->txt("badge_backpack_list"));
462
464
465 foreach ($bp_groups as $group_id => $group) {
466 $bp_badges = $bp->getBadges($group_id);
467 if (sizeof($bp_badges)) {
468 foreach ($bp_badges as $idx => $badge) {
469 $tmpl->setCurrentBlock("badge_bl");
470 $tmpl->setVariable("BADGE_TITLE", $badge["title"]);
471 $tmpl->setVariable("BADGE_DESC", $badge["description"]);
472 $tmpl->setVariable("BADGE_IMAGE", $badge["image_url"]);
473 $tmpl->setVariable("BADGE_CRITERIA", $badge["criteria_url"]);
474 $tmpl->setVariable("BADGE_ISSUER", $badge["issuer_name"]);
475 $tmpl->setVariable("BADGE_ISSUER_URL", $badge["issuer_url"]);
476 $tmpl->setVariable("BADGE_DATE", ilDatePresentation::formatDate($badge["issued_on"]));
477 $tmpl->parseCurrentBlock();
478 }
479 }
480
481 $tmpl->setCurrentBlock("group_bl");
482 $tmpl->setVariable("GROUP_TITLE", $group["title"]);
483 $tmpl->parseCurrentBlock();
484 }
485
486 $tpl->setContent($tmpl->get());
487 }
488
489 protected function prepareBadge($a_badge_id)
490 {
492
493 // check if current user has given badge
494 $ass = new ilBadgeAssignment($a_badge_id, $ilUser->getId());
495 if ($ass->getTimestamp()) {
496 $url = null;
497 try {
498 $url = $ass->getStaticUrl();
499 } catch (Exception $ex) {
500 }
501 if ($url) {
502 return $url;
503 }
504 }
505
506 return false;
507 }
508
509 protected function addToBackpack()
510 {
512
513 if (!$ilCtrl->isAsynch() ||
514 !ilBadgeHandler::getInstance()->isObiActive()) {
515 return false;
516 }
517
518 $res = new stdClass();
519
520 $url = false;
521 $badge_id = (int) $_GET["id"];
522 if ($badge_id) {
523 $url = $this->prepareBadge($badge_id);
524 }
525
526 if ($url !== false) {
527 $res->error = false;
528 $res->url = $url;
529 } else {
530 $res->error = true;
531 $res->message = "missing badge id";
532 }
533
534 echo json_encode($res);
535 exit();
536 }
537
538
539 //
540 // settings
541 //
542
543 protected function getBackpackMail()
544 {
546
547 $mail = $ilUser->getPref(self::BACKPACK_EMAIL);
548 if (!$mail) {
549 $mail = $ilUser->getEmail();
550 }
551 return $mail;
552 }
553
554 protected function initSettingsForm()
555 {
558
559 $form = new ilPropertyFormGUI();
560 $form->setFormAction($ilCtrl->getFormAction($this, "saveSettings"));
561 $form->setTitle($lng->txt("settings"));
562
563 $email = new ilEMailInputGUI($lng->txt("badge_backpack_email"), "email");
564 // $email->setRequired(true);
565 $email->setInfo($lng->txt("badge_backpack_email_info"));
566 $email->setValue($this->getBackpackMail());
567 $form->addItem($email);
568
569 $form->addCommandButton("saveSettings", $lng->txt("save"));
570
571 return $form;
572 }
573
574 protected function editSettings(ilPropertyFormGUI $a_form = null)
575 {
578 $ilTabs = $this->tabs;
579
580 if (!ilBadgeHandler::getInstance()->isObiActive()) {
581 $ilCtrl->redirect($this, "listBadges");
582 }
583
584 $this->setBackpackSubTabs();
585 $ilTabs->activateSubTab("backpack_settings");
586
587 if (!$a_form) {
588 $a_form = $this->initSettingsForm();
589 }
590
591 $tpl->setContent($a_form->getHTML());
592 }
593
594 protected function saveSettings()
595 {
599
600 $form = $this->initSettingsForm();
601 if ($form->checkInput()) {
602 $new_email = $form->getInput("email");
603 $old_email = $this->getBackpackMail();
604
605 ilObjUser::_writePref($ilUser->getId(), self::BACKPACK_EMAIL, $new_email);
606
607 // if email was changed: delete badge files
608 if ($new_email != $old_email) {
610 }
611
612 ilUtil::sendSuccess($lng->txt("settings_saved"), true);
613 $ilCtrl->redirect($this, "editSettings");
614 }
615
616 $form->setValuesByPost();
617 $this->editSettings($form);
618 }
619}
user()
Definition: user.php:4
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
Class ilBadgeAssignment.
static getInstancesByUserId($a_user_id)
static clearBadgeCache($a_user_id)
static getInstance()
Constructor.
TableGUI class for user badge listing.
Class ilBadgeProfileGUI.
editSettings(ilPropertyFormGUI $a_form=null)
Class ilBadgeRenderer.
Class ilBadge.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
static setUseRelativeDates($a_status)
set use relative dates
This class represents a email property in a property form.
static _writePref($a_usr_id, $a_keyword, $a_value)
static _getAllReferences($a_id)
get all reference ids of object
This class represents a property form user interface.
special template class to simplify handling of ITX/PEAR
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:29
if( $orgName !==null) if($spconfig->hasValue('contacts')) $email
Definition: metadata.php:285
$url
foreach($_POST as $key=> $value) $res
$data
Definition: storeScorm.php:23
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46