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