ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilBadgeProfileGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 include_once "Services/Badge/classes/class.ilBadgeHandler.php";
4 
14 {
18  protected $ctrl;
19 
23  protected $lng;
24 
28  protected $tpl;
29 
33  protected $tabs;
34 
38  protected $user;
39 
40 
44  public function __construct()
45  {
46  global $DIC;
47 
48  $this->ctrl = $DIC->ctrl();
49  $this->lng = $DIC->language();
50  $this->tpl = $DIC["tpl"];
51  $this->tabs = $DIC->tabs();
52  $this->user = $DIC->user();
53  }
54 
55  const BACKPACK_EMAIL = "badge_mozilla_bp";
56 
57  public function executeCommand()
58  {
60  $lng = $this->lng;
61  $tpl = $this->tpl;
62 
63  $lng->loadLanguageModule("badge");
64 
65  $tpl->setTitle($lng->txt("obj_bdga"));
66  $tpl->setTitleIcon(ilUtil::getImagePath("icon_bdga.svg"));
67 
68  switch ($ilCtrl->getNextClass()) {
69  default:
70  $this->setTabs();
71  $cmd = $ilCtrl->getCmd("listBadges");
72  $this->$cmd();
73  break;
74  }
75  }
76 
77  protected function setTabs()
78  {
79  $ilTabs = $this->tabs;
80  $lng = $this->lng;
82 
83  if (ilBadgeHandler::getInstance()->isObiActive()) {
84  $ilTabs->addTab(
85  "ilias_badges",
86  $lng->txt("badge_personal_badges"),
87  $ilCtrl->getLinkTarget($this, "listBadges")
88  );
89 
90  $ilTabs->addTab(
91  "backpack_badges",
92  $lng->txt("badge_backpack_list"),
93  $ilCtrl->getLinkTarget($this, "listBackpackGroups")
94  );
95  }
96  }
97 
98 
99  //
100  // list
101  //
102 
103  protected function getSubTabs($a_active)
104  {
105  $ilTabs = $this->tabs;
106  $lng = $this->lng;
108 
109  if (ilBadgeHandler::getInstance()->isObiActive()) {
110  $ilTabs->addSubTab(
111  "list",
112  $lng->txt("badge_profile_view"),
113  $ilCtrl->getLinkTarget($this, "listBadges")
114  );
115  $ilTabs->addSubTab(
116  "manage",
117  $lng->txt("badge_profile_manage"),
118  $ilCtrl->getLinkTarget($this, "manageBadges")
119  );
120  $ilTabs->activateTab("ilias_badges");
121  $ilTabs->activateSubTab($a_active);
122  } else {
123  $ilTabs->addTab(
124  "list",
125  $lng->txt("badge_profile_view"),
126  $ilCtrl->getLinkTarget($this, "listBadges")
127  );
128  $ilTabs->addTab(
129  "manage",
130  $lng->txt("badge_profile_manage"),
131  $ilCtrl->getLinkTarget($this, "manageBadges")
132  );
133  $ilTabs->activateTab($a_active);
134  }
135  }
136 
137  protected function listBadges()
138  {
139  $tpl = $this->tpl;
141 
142  $this->getSubTabs("list");
143 
144  $data = array();
145 
146  // see ilBadgePersonalTableGUI::getItems()
147  include_once "Services/Badge/classes/class.ilBadge.php";
148  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
149  include_once "Services/Badge/classes/class.ilBadgeRenderer.php";
150  foreach (ilBadgeAssignment::getInstancesByUserId($ilUser->getId()) as $ass) {
151  $badge = new ilBadge($ass->getBadgeId());
152 
153  $data[] = array(
154  "id" => $badge->getId(),
155  "title" => $badge->getTitle(),
156  "description" => $badge->getDescription(),
157  "image" => $badge->getImagePath(),
158  "issued_on" => $ass->getTimestamp(),
159  "renderer" => new ilBadgeRenderer($ass)
160  );
161  }
162 
163  // :TODO:
164  $data = ilUtil::sortArray($data, "issued_on", "desc", true);
165 
166  $tmpl = new ilTemplate("tpl.badge_backpack.html", true, true, "Services/Badge");
167 
169 
170  foreach ($data as $badge) {
171  $tmpl->setCurrentBlock("badge_bl");
172  $tmpl->setVariable("BADGE_TITLE", $badge["title"]);
173  // $tmpl->setVariable("BADGE_DESC", $badge["description"]); :TODO:
174  $tmpl->setVariable("BADGE_IMAGE", $badge["image"]);
175  $tmpl->setVariable("BADGE_CRITERIA", $badge["renderer"]->getHref());
176  $tmpl->setVariable("BADGE_DATE", ilDatePresentation::formatDate(new ilDateTime($badge["issued_on"], IL_CAL_UNIX)));
177  $tmpl->parseCurrentBlock();
178  }
179 
180  $tpl->setContent($tmpl->get());
181  }
182 
183  protected function manageBadges()
184  {
185  $tpl = $this->tpl;
186 
187  $this->getSubTabs("manage");
188 
189  include_once "Services/Badge/classes/class.ilBadgePersonalTableGUI.php";
190  $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
191 
192  $tpl->setContent($tbl->getHTML());
193  }
194 
195  protected function applyFilter()
196  {
197  include_once "Services/Badge/classes/class.ilBadgePersonalTableGUI.php";
198  $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
199  $tbl->resetOffset();
200  $tbl->writeFilterToSession();
201  $this->manageBadges();
202  }
203 
204  protected function resetFilter()
205  {
206  include_once "Services/Badge/classes/class.ilBadgePersonalTableGUI.php";
207  $tbl = new ilBadgePersonalTableGUI($this, "manageBadges");
208  $tbl->resetOffset();
209  $tbl->resetFilter();
210  $this->manageBadges();
211  }
212 
213  protected function getMultiSelection()
214  {
215  $lng = $this->lng;
218 
219  $ids = $_POST["badge_id"];
220  if (!$ids && is_numeric($_GET["badge_id"])) {
221  $ids = array($_GET["badge_id"]);
222  }
223  if (is_array($ids)) {
224  $res = array();
225  include_once "Services/Badge/classes/class.ilBadge.php";
226  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
227  foreach ($ids as $id) {
228  $ass = new ilBadgeAssignment($id, $ilUser->getId());
229  if ($ass->getTimestamp()) {
230  $res[] = $ass;
231  }
232  }
233 
234  return $res;
235  } else {
236  ilUtil::sendFailure($lng->txt("select_one"), true);
237  $ilCtrl->redirect($this, "manageBadges");
238  }
239  }
240 
241  protected function activate()
242  {
243  $lng = $this->lng;
245 
246  foreach ($this->getMultiSelection() as $ass) {
247  // already active?
248  if (!$ass->getPosition()) {
249  $ass->setPosition(999);
250  $ass->store();
251  }
252  }
253 
254  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
255  $ilCtrl->redirect($this, "manageBadges");
256  }
257 
258  protected function deactivate()
259  {
260  $lng = $this->lng;
262 
263  foreach ($this->getMultiSelection() as $ass) {
264  // already inactive?
265  if ($ass->getPosition()) {
266  $ass->setPosition(null);
267  $ass->store();
268  }
269  }
270 
271  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
272  $ilCtrl->redirect($this, "manageBadges");
273  }
274 
275 
276  //
277  // (mozilla) backpack
278  //
279 
280  protected function addToBackpackMulti()
281  {
282  $tpl = $this->tpl;
283  $ilTabs = $this->tabs;
285  $lng = $this->lng;
286 
287  $res = array();
288  foreach ($this->getMultiSelection() as $ass) {
289  $url = $this->prepareBadge($ass->getBadgeId());
290  if ($url !== false) {
291  $badge = new ilBadge($ass->getBadgeId());
292  $titles[] = $badge->getTitle();
293  $res[] = $url;
294  }
295  }
296 
297  // :TODO: use local copy instead?
298  $tpl->addJavascript("https://backpack.openbadges.org/issuer.js", false);
299 
300  $tpl->addJavascript("Services/Badge/js/ilBadge.js");
301  $tpl->addOnloadCode("il.Badge.publishMulti(['" . implode("','", $res) . "']);");
302 
303  $ilTabs->clearTargets();
304  $ilTabs->setBackTarget(
305  $lng->txt("back"),
306  $ilCtrl->getLinkTarget($this, "manageBadges")
307  );
308 
309  ilUtil::sendInfo(sprintf($lng->txt("badge_add_to_backpack_multi"), implode(", ", $titles)));
310  }
311 
312  protected function setBackpackSubTabs()
313  {
314  $ilTabs = $this->tabs;
315  $lng = $this->lng;
317 
318  $ilTabs->addSubTab(
319  "backpack_badges",
320  $lng->txt("obj_bdga"),
321  $ilCtrl->getLinkTarget($this, "listBackpackGroups")
322  );
323 
324  $ilTabs->addSubTab(
325  "backpack_settings",
326  $lng->txt("settings"),
327  $ilCtrl->getLinkTarget($this, "editSettings")
328  );
329 
330  $ilTabs->activateTab("backpack_badges");
331  }
332 
333  protected function listBackpackGroups()
334  {
335  $lng = $this->lng;
336  $tpl = $this->tpl;
338  $ilTabs = $this->tabs;
339 
340  if (!ilBadgeHandler::getInstance()->isObiActive()) {
341  $ilCtrl->redirect($this, "listBadges");
342  }
343 
344  $this->setBackpackSubTabs();
345  $ilTabs->activateSubTab("backpack_badges");
346 
347  ilUtil::sendInfo($lng->txt("badge_backpack_gallery_info"));
348 
349  include_once "Services/Badge/classes/class.ilBadgeBackpack.php";
350  $bp = new ilBadgeBackpack($this->getBackpackMail());
351  $bp_groups = $bp->getGroups();
352 
353  if (!is_array($bp_groups)) {
354  ilUtil::sendInfo(sprintf($lng->txt("badge_backpack_connect_failed"), $this->getBackpackMail()));
355  return;
356  } elseif (!sizeof($bp_groups)) {
357  ilUtil::sendInfo($lng->txt("badge_backpack_no_groups"));
358  return;
359  }
360 
361  $tmpl = new ilTemplate("tpl.badge_backpack.html", true, true, "Services/Badge");
362 
363  $tmpl->setVariable("BACKPACK_TITLE", $lng->txt("badge_backpack_list"));
364 
366 
367  foreach ($bp_groups as $group_id => $group) {
368  $bp_badges = $bp->getBadges($group_id);
369  if (sizeof($bp_badges)) {
370  foreach ($bp_badges as $idx => $badge) {
371  $tmpl->setCurrentBlock("badge_bl");
372  $tmpl->setVariable("BADGE_TITLE", $badge["title"]);
373  $tmpl->setVariable("BADGE_DESC", $badge["description"]);
374  $tmpl->setVariable("BADGE_IMAGE", $badge["image_url"]);
375  $tmpl->setVariable("BADGE_CRITERIA", $badge["criteria_url"]);
376  $tmpl->setVariable("BADGE_ISSUER", $badge["issuer_name"]);
377  $tmpl->setVariable("BADGE_ISSUER_URL", $badge["issuer_url"]);
378  $tmpl->setVariable("BADGE_DATE", ilDatePresentation::formatDate($badge["issued_on"]));
379  $tmpl->parseCurrentBlock();
380  }
381  }
382 
383  $tmpl->setCurrentBlock("group_bl");
384  $tmpl->setVariable("GROUP_TITLE", $group["title"]);
385  $tmpl->parseCurrentBlock();
386  }
387 
388  $tpl->setContent($tmpl->get());
389  }
390 
391  protected function prepareBadge($a_badge_id)
392  {
394 
395  // check if current user has given badge
396  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
397  $ass = new ilBadgeAssignment($a_badge_id, $ilUser->getId());
398  if ($ass->getTimestamp()) {
399  $url = null;
400  try {
401  $url = $ass->getStaticUrl();
402  } catch (Exception $ex) {
403  }
404  if ($url) {
405  return $url;
406  }
407  }
408 
409  return false;
410  }
411 
412  protected function addToBackpack()
413  {
415 
416  if (!$ilCtrl->isAsynch() ||
417  !ilBadgeHandler::getInstance()->isObiActive()) {
418  return false;
419  }
420 
421  $res = new stdClass();
422 
423  $url = false;
424  $badge_id = (int) $_GET["id"];
425  if ($badge_id) {
426  $url = $this->prepareBadge($badge_id);
427  }
428 
429  if ($url !== false) {
430  $res->error = false;
431  $res->url = $url;
432  } else {
433  $res->error = true;
434  $res->message = "missing badge id";
435  }
436 
437  echo json_encode($res);
438  exit();
439  }
440 
441 
442  //
443  // settings
444  //
445 
446  protected function getBackpackMail()
447  {
449 
450  $mail = $ilUser->getPref(self::BACKPACK_EMAIL);
451  if (!$mail) {
452  $mail = $ilUser->getEmail();
453  }
454  return $mail;
455  }
456 
457  protected function initSettingsForm()
458  {
459  $lng = $this->lng;
461 
462  include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
463  $form = new ilPropertyFormGUI();
464  $form->setFormAction($ilCtrl->getFormAction($this, "saveSettings"));
465  $form->setTitle($lng->txt("settings"));
466 
467  $email = new ilEMailInputGUI($lng->txt("badge_backpack_email"), "email");
468  // $email->setRequired(true);
469  $email->setInfo($lng->txt("badge_backpack_email_info"));
470  $email->setValue($this->getBackpackMail());
471  $form->addItem($email);
472 
473  $form->addCommandButton("saveSettings", $lng->txt("save"));
474 
475  return $form;
476  }
477 
478  protected function editSettings(ilPropertyFormGUI $a_form = null)
479  {
480  $tpl = $this->tpl;
482  $ilTabs = $this->tabs;
483 
484  if (!ilBadgeHandler::getInstance()->isObiActive()) {
485  $ilCtrl->redirect($this, "listBadges");
486  }
487 
488  $this->setBackpackSubTabs();
489  $ilTabs->activateSubTab("backpack_settings");
490 
491  if (!$a_form) {
492  $a_form = $this->initSettingsForm();
493  }
494 
495  $tpl->setContent($a_form->getHTML());
496  }
497 
498  protected function saveSettings()
499  {
501  $lng = $this->lng;
503 
504  $form = $this->initSettingsForm();
505  if ($form->checkInput()) {
506  $new_email = $form->getInput("email");
507  $old_email = $this->getBackpackMail();
508 
509  ilObjUser::_writePref($ilUser->getId(), self::BACKPACK_EMAIL, $new_email);
510 
511  // if email was changed: delete badge files
512  if ($new_email != $old_email) {
513  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
515  }
516 
517  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
518  $ilCtrl->redirect($this, "editSettings");
519  }
520 
521  $form->setValuesByPost();
522  $this->editSettings($form);
523  }
524 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
if($orgName !==null) if($spconfig->hasValue('contacts')) $email
Definition: metadata.php:193
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
$tbl
Definition: example_048.php:81
static _writePref($a_usr_id, $a_keyword, $a_value)
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date public.
if(!array_key_exists('StateId', $_REQUEST)) $id
static setUseRelativeDates($a_status)
set use relative dates
This class represents a email property in a property form.
const IL_CAL_UNIX
user()
Definition: user.php:4
global $ilCtrl
Definition: ilias.php:18
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
editSettings(ilPropertyFormGUI $a_form=null)
foreach($_POST as $key=> $value) $res
if(isset($_POST['submit'])) $form
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
special template class to simplify handling of ITX/PEAR
Date and time handling
$ilUser
Definition: imgupload.php:18
TableGUI class for user badge listing.
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$url
static clearBadgeCache($a_user_id)
$_POST["username"]
static getInstancesByUserId($a_user_id)
static getInstance()
Constructor.