ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilBadgeRenderer.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/Badge/classes/class.ilBadge.php";
5 
15 {
19  protected $tpl;
20 
24  protected $ctrl;
25 
29  protected $lng;
30 
31  protected $assignment; // [ilBadgeAssignment]
32  protected $badge; // [ilBadge]
33 
34  protected static $init; // [bool]
35 
36  public function __construct(ilBadgeAssignment $a_assignment = null, ilBadge $a_badge = null)
37  {
38  global $DIC;
39 
40  $this->tpl = $DIC["tpl"];
41  $this->ctrl = $DIC->ctrl();
42  $this->lng = $DIC->language();
43  if ($a_assignment) {
44  $this->assignment = $a_assignment;
45  $this->badge = new ilBadge($this->assignment->getBadgeId());
46  } else {
47  $this->badge = $a_badge;
48  }
49  }
50 
51  public static function initFromId($a_id)
52  {
53  $id = explode("_", $_GET["id"]);
54  if (sizeof($id) == 3) {
55  $user_id = $id[0];
56  $badge_id = $id[1];
57  $hash = $id[2];
58 
59  if ($user_id) {
60  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
61  $assignment = new ilBadgeAssignment($badge_id, $user_id);
62  if ($assignment->getTimestamp()) {
63  $obj = new self($assignment);
64  }
65  } else {
66  include_once "Services/Badge/classes/class.ilBadge.php";
67  $badge = new ilBadge($badge_id);
68  $obj = new self(null, $badge);
69  }
70  if ($hash == $obj->getBadgeHash()) {
71  return $obj;
72  }
73  }
74  }
75 
76  public function getHTML()
77  {
78  $tpl = $this->tpl;
80 
81  if (!self::$init) {
82  self::$init = true;
83 
84  $url = $ilCtrl->getLinkTargetByClass(
85  "ilBadgeHandlerGUI",
86  "render",
87  "",
88  true,
89  false
90  );
91 
92  $tpl->addJavaScript("Services/Badge/js/ilBadgeRenderer.js");
93  $tpl->addOnLoadCode('il.BadgeRenderer.init("' . $url . '");');
94  }
95 
96  $hash = $this->getBadgeHash();
97 
98  $btpl = new ilTemplate("tpl.badge_renderer.html", true, true, "Services/Badge");
99  $btpl->setVariable("BADGE_IMG", $this->badge->getImagePath());
100  $btpl->setVariable("BADGE_TXT", $this->badge->getTitle());
101  $btpl->setVariable("BADGE_ID", "badge_" .
102  ($this->assignment
103  ? $this->assignment->getUserId()
104  : "") . "_" .
105  $this->badge->getId() . "_" .
106  $hash);
107  return $btpl->get();
108  }
109 
110  public function getHref()
111  {
113  $tpl = $this->tpl;
114 
115  if (!self::$init) {
116  self::$init = true;
117 
118  $url = $ilCtrl->getLinkTargetByClass(
119  "ilBadgeHandlerGUI",
120  "render",
121  "",
122  true,
123  false
124  );
125 
126  $tpl->addJavaScript("Services/Badge/js/ilBadgeRenderer.js");
127  $tpl->addOnLoadCode('il.BadgeRenderer.init("' . $url . '");');
128  }
129 
130  $hash = $this->getBadgeHash();
131 
132  return "#\" data-id=\"badge_" .
133  ($this->assignment
134  ? $this->assignment->getUserId()
135  : "") . "_" .
136  $this->badge->getId() . "_" .
137  $hash;
138  }
139 
140  protected function getBadgeHash()
141  {
142  return md5("bdg-" .
143  ($this->assignment
144  ? $this->assignment->getUserId()
145  : "") . "-" .
146  $this->badge->getId());
147  }
148 
149  public function renderModal()
150  {
151  $lng = $this->lng;
152 
153  include_once "Services/UIComponent/Modal/classes/class.ilModalGUI.php";
154 
155  // only needed for modal-js-calls
156  // ilModalGUI::initJS();
157 
158  $modal = ilModalGUI::getInstance();
159  $modal->setId("badge_modal_" . $this->getBadgeHash());
160  $modal->setType(ilModalGUI::TYPE_SMALL);
161  $modal->setHeading($this->badge->getTitle());
162 
163  $lng->loadLanguageModule("badge");
164 
165  $tpl = new ilTemplate("tpl.badge_modal.html", true, true, "Services/Badge");
166 
167  $tpl->setVariable("IMG_SRC", $this->badge->getImagePath());
168  $tpl->setVariable("IMG_TXT", $this->badge->getImage());
169 
170  $tpl->setVariable("TXT_DESC", $lng->txt("description"));
171  $tpl->setVariable("DESC", nl2br($this->badge->getDescription()));
172 
173  $tpl->setVariable("TXT_CRITERIA", $lng->txt("badge_criteria"));
174  $tpl->setVariable("CRITERIA", nl2br($this->badge->getCriteria()));
175 
176  if ($this->assignment) {
177  $tpl->setVariable("TXT_TSTAMP", $lng->txt("badge_issued_on"));
178  $tpl->setVariable(
179  "TSTAMP",
180  ilDatePresentation::formatDate(new ilDateTime($this->assignment->getTimestamp(), IL_CAL_UNIX))
181  );
182  }
183 
184  if ($this->badge->getParentId()) {
185  $parent = $this->badge->getParentMeta();
186  if ($parent["type"] != "bdga") {
187  $tpl->setVariable("TXT_PARENT", $lng->txt("object"));
188  $tpl->setVariable("PARENT", $parent["title"]);
189  $tpl->setVariable("PARENT_TYPE", $lng->txt("obj_" . $parent["type"]));
190  $tpl->setVariable(
191  "PARENT_ICON",
192  ilObject::_getIcon($parent["id"], "big", $parent["type"])
193  );
194  }
195  }
196 
197  if ($this->badge->getValid()) {
198  $tpl->setVariable("TXT_VALID", $lng->txt("badge_valid"));
199  $tpl->setVariable("VALID", $this->badge->getValid());
200  }
201 
202  $modal->setBody($tpl->get());
203 
204  return $modal->getHTML();
205  }
206 }
static _getIcon( $a_obj_id="", $a_size="big", $a_type="", $a_offline=false)
Get icon for repository item.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date public.
if(!array_key_exists('StateId', $_REQUEST)) $id
const IL_CAL_UNIX
global $ilCtrl
Definition: ilias.php:18
special template class to simplify handling of ITX/PEAR
Date and time handling
static getInstance()
Get instance.
static initFromId($a_id)
$url
__construct(ilBadgeAssignment $a_assignment=null, ilBadge $a_badge=null)