ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
4include_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 {
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 $image_path = ilWACSignedPath::signFile($this->badge->getImagePath());
100 $btpl->setVariable("BADGE_IMG", $image_path);
101 $btpl->setVariable("BADGE_TXT", $this->badge->getTitle());
102 $btpl->setVariable("BADGE_ID", "badge_" .
103 ($this->assignment
104 ? $this->assignment->getUserId()
105 : "") . "_" .
106 $this->badge->getId() . "_" .
107 $hash);
108 return $btpl->get();
109 }
110
111 public function getHref()
112 {
115
116 if (!self::$init) {
117 self::$init = true;
118
119 $url = $ilCtrl->getLinkTargetByClass(
120 "ilBadgeHandlerGUI",
121 "render",
122 "",
123 true,
124 false
125 );
126
127 $tpl->addJavaScript("Services/Badge/js/ilBadgeRenderer.js");
128 $tpl->addOnLoadCode('il.BadgeRenderer.init("' . $url . '");');
129 }
130
131 $hash = $this->getBadgeHash();
132
133 return "#\" data-id=\"badge_" .
134 ($this->assignment
135 ? $this->assignment->getUserId()
136 : "") . "_" .
137 $this->badge->getId() . "_" .
138 $hash;
139 }
140
141 protected function getBadgeHash()
142 {
143 return md5("bdg-" .
144 ($this->assignment
145 ? $this->assignment->getUserId()
146 : "") . "-" .
147 $this->badge->getId());
148 }
149
150 public function renderModal()
151 {
153
154 include_once "Services/UIComponent/Modal/classes/class.ilModalGUI.php";
155
156 // only needed for modal-js-calls
157 // ilModalGUI::initJS();
158
159 $modal = ilModalGUI::getInstance();
160 $modal->setId("badge_modal_" . $this->getBadgeHash());
161 $modal->setType(ilModalGUI::TYPE_SMALL);
162 $modal->setHeading($this->badge->getTitle());
163
164 $lng->loadLanguageModule("badge");
165
166 $tpl = new ilTemplate("tpl.badge_modal.html", true, true, "Services/Badge");
167
168 $tpl->setVariable("IMG_SRC", $this->badge->getImagePath());
169 $tpl->setVariable("IMG_TXT", $this->badge->getImage());
170
171 $tpl->setVariable("TXT_DESC", $lng->txt("description"));
172 $tpl->setVariable("DESC", nl2br($this->badge->getDescription()));
173
174 $tpl->setVariable("TXT_CRITERIA", $lng->txt("badge_criteria"));
175 $tpl->setVariable("CRITERIA", nl2br($this->badge->getCriteria()));
176
177 if ($this->assignment) {
178 $tpl->setVariable("TXT_TSTAMP", $lng->txt("badge_issued_on"));
179 $tpl->setVariable(
180 "TSTAMP",
181 ilDatePresentation::formatDate(new ilDateTime($this->assignment->getTimestamp(), IL_CAL_UNIX))
182 );
183 }
184
185 if ($this->badge->getParentId()) {
186 $parent = $this->badge->getParentMeta();
187 if ($parent["type"] != "bdga") {
188 $tpl->setVariable("TXT_PARENT", $lng->txt("object"));
189 $tpl->setVariable("PARENT", $parent["title"]);
190 $tpl->setVariable("PARENT_TYPE", $lng->txt("obj_" . $parent["type"]));
191 $tpl->setVariable(
192 "PARENT_ICON",
193 ilObject::_getIcon($parent["id"], "big", $parent["type"])
194 );
195 }
196 }
197
198 if ($this->badge->getValid()) {
199 $tpl->setVariable("TXT_VALID", $lng->txt("badge_valid"));
200 $tpl->setVariable("VALID", $this->badge->getValid());
201 }
202
203 $modal->setBody($tpl->get());
204
205 return $modal->getHTML();
206 }
207}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
__construct(ilBadgeAssignment $a_assignment=null, ilBadge $a_badge=null)
static initFromId($a_id)
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
@classDescription Date and time handling
static getInstance()
Get instance.
special template class to simplify handling of ITX/PEAR
static signFile($path_to_file)
if(!array_key_exists('StateId', $_REQUEST)) $id
global $ilCtrl
Definition: ilias.php:18
$url
global $DIC
Definition: saml.php:7