ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilBadgeAssignment Class Reference
+ Collaboration diagram for ilBadgeAssignment:

Public Member Functions

 __construct ($a_badge_id=null, $a_user_id=null)
 
 getBadgeId ()
 
 getUserId ()
 
 getTimestamp ()
 
 setAwardedBy ($a_id)
 
 getAwardedBy ()
 
 setPosition ($a_value)
 
 getPosition ()
 
 store ()
 
 delete ()
 
 getImagePath (ilBadge $a_badge)
 
 getStaticUrl ()
 
 deleteStaticFiles ()
 

Static Public Member Functions

static getInstancesByUserId ($a_user_id)
 
static getInstancesByBadgeId ($a_badge_id)
 
static getInstancesByParentId ($a_parent_obj_id)
 
static getAssignedUsers ($a_badge_id)
 
static exists ($a_badge_id, $a_user_id)
 
static deleteByUserId ($a_user_id)
 
static deleteByBadgeId ($a_badge_id)
 
static deleteByParentId ($a_parent_obj_id)
 
static updatePositions ($a_user_id, array $a_positions)
 
static clearBadgeCache ($a_user_id)
 

Protected Member Functions

 setBadgeId ($a_value)
 
 setUserId ($a_value)
 
 setTimestamp ($a_value)
 
 importDBRow (array $a_row)
 
 read ($a_badge_id, $a_user_id)
 
 getPropertiesForStorage ()
 
 prepareJson ($a_url)
 
 bakeImage ($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
 

Protected Attributes

 $db
 
 $badge_id
 
 $user_id
 
 $tstamp
 
 $awarded_by
 
 $pos
 
 $stored
 

Detailed Description

Definition at line 12 of file class.ilBadgeAssignment.php.

Constructor & Destructor Documentation

◆ __construct()

ilBadgeAssignment::__construct (   $a_badge_id = null,
  $a_user_id = null 
)

Definition at line 26 of file class.ilBadgeAssignment.php.

References $DIC, read(), setBadgeId(), and setUserId().

27  {
28  global $DIC;
29 
30  $this->db = $DIC->database();
31  if ($a_badge_id &&
32  $a_user_id) {
33  $this->setBadgeId($a_badge_id);
34  $this->setUserId($a_user_id);
35 
36  $this->read($a_badge_id, $a_user_id);
37  }
38  }
global $DIC
Definition: saml.php:7
read($a_badge_id, $a_user_id)
+ Here is the call graph for this function:

Member Function Documentation

◆ bakeImage()

ilBadgeAssignment::bakeImage (   $a_baked_image_path,
  $a_badge_image_path,
  $a_assertion_url 
)
protected

Definition at line 363 of file class.ilBadgeAssignment.php.

Referenced by prepareJson().

364  {
365  $suffix = strtolower(array_pop(explode(".", basename($a_badge_image_path))));
366  if ($suffix == "png") {
367  // using chamilo baker lib
368  include_once "Services/Badge/lib/baker.lib.php";
369  $png = new PNGImageBaker(file_get_contents($a_badge_image_path));
370 
371  // add payload
372  if ($png->checkChunks("tEXt", "openbadges")) {
373  $baked = $png->addChunk("tEXt", "openbadges", $a_assertion_url);
374  }
375 
376  // create baked file
377  if (!file_exists($a_baked_image_path)) {
378  file_put_contents($a_baked_image_path, $baked);
379  }
380 
381  // verify file
382  $verify = $png->extractBadgeInfo(file_get_contents($a_baked_image_path));
383  if (is_array($verify)) {
384  return true;
385  }
386  } elseif ($suffix == "svg") {
387  // :TODO: not really sure if this is correct
388  $svg = simplexml_load_file($a_badge_image_path);
389  $ass = $svg->addChild("openbadges:assertion", "", "http://openbadges.org");
390  $ass->addAttribute("verify", $a_assertion_url);
391  $baked = $svg->asXML();
392 
393  // create baked file
394  if (!file_exists($a_baked_image_path)) {
395  file_put_contents($a_baked_image_path, $baked);
396  }
397 
398  return true;
399  }
400 
401  return false;
402  }
Php library to Bake the PNG Images.
Definition: baker.lib.php:7
+ Here is the caller graph for this function:

◆ clearBadgeCache()

static ilBadgeAssignment::clearBadgeCache (   $a_user_id)
static

Definition at line 428 of file class.ilBadgeAssignment.php.

Referenced by ilBadgeProfileGUI\saveSettings().

429  {
430  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
431  $ass->deleteStaticFiles();
432  }
433  }
+ Here is the caller graph for this function:

◆ delete()

ilBadgeAssignment::delete ( )

Definition at line 238 of file class.ilBadgeAssignment.php.

References $db, $ilDB, deleteStaticFiles(), getBadgeId(), and getUserId().

239  {
240  $ilDB = $this->db;
241 
242  if (!$this->getBadgeId() ||
243  !$this->getUserId()) {
244  return;
245  }
246 
247  $this->deleteStaticFiles();
248 
249  $ilDB->manipulate("DELETE FROM badge_user_badge" .
250  " WHERE badge_id = " . $ilDB->quote($this->getBadgeId(), "integer") .
251  " AND user_id = " . $ilDB->quote($this->getUserId(), "integer"));
252  }
global $ilDB
+ Here is the call graph for this function:

◆ deleteByBadgeId()

static ilBadgeAssignment::deleteByBadgeId (   $a_badge_id)
static

Definition at line 261 of file class.ilBadgeAssignment.php.

Referenced by ilBadge\delete().

262  {
263  foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
264  $ass->delete();
265  }
266  }
+ Here is the caller graph for this function:

◆ deleteByParentId()

static ilBadgeAssignment::deleteByParentId (   $a_parent_obj_id)
static

Definition at line 268 of file class.ilBadgeAssignment.php.

269  {
270  foreach (self::getInstancesByParentId($a_parent_obj_id) as $ass) {
271  $ass->delete();
272  }
273  }

◆ deleteByUserId()

static ilBadgeAssignment::deleteByUserId (   $a_user_id)
static

Definition at line 254 of file class.ilBadgeAssignment.php.

Referenced by ilObjUser\delete().

255  {
256  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
257  $ass->delete();
258  }
259  }
+ Here is the caller graph for this function:

◆ deleteStaticFiles()

ilBadgeAssignment::deleteStaticFiles ( )

Definition at line 419 of file class.ilBadgeAssignment.php.

References $path, and ilBadgeHandler\getInstance().

Referenced by delete().

420  {
421  // remove instance files
422  include_once("./Services/Badge/classes/class.ilBadgeHandler.php");
423  $path = ilBadgeHandler::getInstance()->getInstancePath($this);
424  $path = str_replace(".json", ".*", $path);
425  array_map("unlink", glob($path));
426  }
static getInstance()
Constructor.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exists()

static ilBadgeAssignment::exists (   $a_badge_id,
  $a_user_id 
)
static

Definition at line 115 of file class.ilBadgeAssignment.php.

Referenced by ilBadgeManagementGUI\assignBadge(), ilBadgeHandler\sendNotification(), and ilBadgeHandler\triggerEvaluation().

116  {
117  $obj = new self($a_badge_id, $a_user_id);
118  return $obj->stored;
119  }
+ Here is the caller graph for this function:

◆ getAssignedUsers()

static ilBadgeAssignment::getAssignedUsers (   $a_badge_id)
static

Definition at line 104 of file class.ilBadgeAssignment.php.

References $res, and array.

Referenced by ilBadgeManagementGUI\confirmDeassignBadge().

105  {
106  $res = array();
107 
108  foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
109  $res[] = $ass->getUserId();
110  }
111 
112  return $res;
113  }
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ getAwardedBy()

ilBadgeAssignment::getAwardedBy ( )

Definition at line 161 of file class.ilBadgeAssignment.php.

References $awarded_by.

Referenced by getPropertiesForStorage().

162  {
163  return $this->awarded_by;
164  }
+ Here is the caller graph for this function:

◆ getBadgeId()

ilBadgeAssignment::getBadgeId ( )

Definition at line 131 of file class.ilBadgeAssignment.php.

References $badge_id.

Referenced by delete(), ilBadgeHandler\getInstancePath(), prepareJson(), and store().

132  {
133  return $this->badge_id;
134  }
+ Here is the caller graph for this function:

◆ getImagePath()

ilBadgeAssignment::getImagePath ( ilBadge  $a_badge)

Definition at line 352 of file class.ilBadgeAssignment.php.

References ilBadge\getImagePath(), and ilBadgeHandler\getInstance().

Referenced by prepareJson().

353  {
354  $json_path = ilBadgeHandler::getInstance()->getInstancePath($this);
355  $baked_path = dirname($json_path);
356  $baked_file = array_shift(explode(".", basename($json_path)));
357 
358  // get correct suffix from badge image
359  $suffix = strtolower(array_pop(explode(".", basename($a_badge->getImagePath()))));
360  return $baked_path . "/" . $baked_file . "." . $suffix;
361  }
getImagePath($a_full_path=true)
static getInstance()
Constructor.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInstancesByBadgeId()

static ilBadgeAssignment::getInstancesByBadgeId (   $a_badge_id)
static

Definition at line 60 of file class.ilBadgeAssignment.php.

References $DIC, $ilDB, $res, $row, and array.

Referenced by ilBadgeManagementGUI\confirmDeleteBadges(), and ilObjBadgeAdministrationGUI\confirmDeleteObjectBadges().

61  {
62  global $DIC;
63 
64  $ilDB = $DIC->database();
65 
66  $res = array();
67 
68  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
69  " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer"));
70  while ($row = $ilDB->fetchAssoc($set)) {
71  $obj = new self();
72  $obj->importDBRow($row);
73  $res[] = $obj;
74  }
75 
76  return $res;
77  }
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
global $ilDB
+ Here is the caller graph for this function:

◆ getInstancesByParentId()

static ilBadgeAssignment::getInstancesByParentId (   $a_parent_obj_id)
static

Definition at line 79 of file class.ilBadgeAssignment.php.

References $DIC, $ilDB, $res, $row, array, and ilBadge\getInstancesByParentId().

Referenced by ilBadgeUserTableGUI\getItems().

80  {
81  global $DIC;
82 
83  $ilDB = $DIC->database();
84 
85  $res = array();
86 
87  $badge_ids = array();
88  foreach (ilBadge::getInstancesByParentId($a_parent_obj_id) as $badge) {
89  $badge_ids[] = $badge->getId();
90  }
91  if (sizeof($badge_ids)) {
92  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
93  " WHERE " . $ilDB->in("badge_id", $badge_ids, "", "integer"));
94  while ($row = $ilDB->fetchAssoc($set)) {
95  $obj = new self();
96  $obj->importDBRow($row);
97  $res[] = $obj;
98  }
99  }
100 
101  return $res;
102  }
global $DIC
Definition: saml.php:7
static getInstancesByParentId($a_parent_id, array $a_filter=null)
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
global $ilDB
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInstancesByUserId()

static ilBadgeAssignment::getInstancesByUserId (   $a_user_id)
static

Definition at line 40 of file class.ilBadgeAssignment.php.

References $DIC, $ilDB, $res, $row, and array.

Referenced by ilPublicUserProfileGUI\getEmbeddable(), ilBadgePersonalTableGUI\getItems(), ilBadgeProfileGUI\listBadges(), and ilPersonalProfileGUI\showPublicProfileFields().

41  {
42  global $DIC;
43 
44  $ilDB = $DIC->database();
45 
46  $res = array();
47 
48  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
49  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
50  " ORDER BY pos");
51  while ($row = $ilDB->fetchAssoc($set)) {
52  $obj = new self();
53  $obj->importDBRow($row);
54  $res[] = $obj;
55  }
56 
57  return $res;
58  }
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
global $ilDB
+ Here is the caller graph for this function:

◆ getPosition()

ilBadgeAssignment::getPosition ( )

Definition at line 174 of file class.ilBadgeAssignment.php.

References $pos.

Referenced by getPropertiesForStorage().

175  {
176  return $this->pos;
177  }
+ Here is the caller graph for this function:

◆ getPropertiesForStorage()

ilBadgeAssignment::getPropertiesForStorage ( )
protected

Definition at line 207 of file class.ilBadgeAssignment.php.

References array, getAwardedBy(), getPosition(), getTimestamp(), and time.

Referenced by store().

208  {
209  return array(
210  "tstamp" => array("integer", (bool) $this->stored ? $this->getTimestamp() : time()),
211  "awarded_by" => array("integer", $this->getAwardedBy()),
212  "pos" => array("integer", $this->getPosition())
213  );
214  }
Create styles array
The data for the language used.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getStaticUrl()

ilBadgeAssignment::getStaticUrl ( )

Definition at line 404 of file class.ilBadgeAssignment.php.

References $path, $url, ilBadgeHandler\getInstance(), and prepareJson().

405  {
406  include_once("./Services/Badge/classes/class.ilBadgeHandler.php");
407  $path = ilBadgeHandler::getInstance()->getInstancePath($this);
408 
409  $url = ILIAS_HTTP_PATH . substr($path, 1);
410 
411  if (!file_exists($path)) {
412  $json = json_encode($this->prepareJson($url));
413  file_put_contents($path, $json);
414  }
415 
416  return $url;
417  }
$url
static getInstance()
Constructor.
+ Here is the call graph for this function:

◆ getTimestamp()

ilBadgeAssignment::getTimestamp ( )

Definition at line 151 of file class.ilBadgeAssignment.php.

References $tstamp.

Referenced by getPropertiesForStorage(), and prepareJson().

152  {
153  return $this->tstamp;
154  }
+ Here is the caller graph for this function:

◆ getUserId()

ilBadgeAssignment::getUserId ( )

Definition at line 141 of file class.ilBadgeAssignment.php.

References $user_id.

Referenced by delete(), ilBadgeHandler\getInstancePath(), prepareJson(), and store().

142  {
143  return $this->user_id;
144  }
+ Here is the caller graph for this function:

◆ importDBRow()

ilBadgeAssignment::importDBRow ( array  $a_row)
protected

Definition at line 184 of file class.ilBadgeAssignment.php.

References setAwardedBy(), setBadgeId(), setPosition(), setTimestamp(), and setUserId().

Referenced by read().

185  {
186  $this->stored = true;
187  $this->setBadgeId($a_row["badge_id"]);
188  $this->setUserId($a_row["user_id"]);
189  $this->setTimestamp($a_row["tstamp"]);
190  $this->setAwardedBy($a_row["awarded_by"]);
191  $this->setPosition($a_row["pos"]);
192  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ prepareJson()

ilBadgeAssignment::prepareJson (   $a_url)
protected

Definition at line 301 of file class.ilBadgeAssignment.php.

References ilBadgeProfileGUI\BACKPACK_EMAIL, bakeImage(), getBadgeId(), getImagePath(), ilBadgeHandler\getInstance(), getTimestamp(), getUserId(), and GuzzleHttp\Psr7\hash().

Referenced by getStaticUrl().

302  {
303  $verify = new stdClass();
304  $verify->type = "hosted";
305  $verify->url = $a_url;
306 
307  $recipient = new stdClass();
308  $recipient->type = "email";
309  $recipient->hashed = true;
310  $recipient->salt = ilBadgeHandler::getInstance()->getObiSalt();
311 
312  // https://github.com/mozilla/openbadges-backpack/wiki/How-to-hash-&-salt-in-various-languages.
313  include_once "Services/Badge/classes/class.ilBadgeProfileGUI.php";
314  $user = new ilObjUser($this->getUserId());
315  $mail = $user->getPref(ilBadgeProfileGUI::BACKPACK_EMAIL);
316  if (!$mail) {
317  $mail = $user->getEmail();
318  }
319  $recipient->identity = 'sha256$' . hash('sha256', $mail . $recipient->salt);
320 
321  // spec: should be locally unique
322  $unique_id = md5($this->getBadgeId() . "-" . $this->getUserId());
323 
324  $json = new stdClass();
325  $json->{"@context"} = "https://w3id.org/openbadges/v1";
326  $json->type = "Assertion";
327  $json->id = $a_url;
328  $json->uid = $unique_id;
329  $json->recipient = $recipient;
330 
331  include_once "Services/Badge/classes/class.ilBadge.php";
332  $badge = new ilBadge($this->getBadgeId());
333  $badge_url = $badge->getStaticUrl();
334 
335  // created baked image
336  $baked_image = $this->getImagePath($badge);
337  if ($this->bakeImage($baked_image, $badge->getImagePath(), $a_url)) {
338  // path to url
339  $parts = explode("/", $a_url);
340  array_pop($parts);
341  $parts[] = basename($baked_image);
342  $json->image = implode("/", $parts);
343  }
344 
345  $json->issuedOn = $this->getTimestamp();
346  $json->badge = $badge_url;
347  $json->verify = $verify;
348 
349  return $json;
350  }
getImagePath(ilBadge $a_badge)
bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
static getInstance()
Constructor.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read()

ilBadgeAssignment::read (   $a_badge_id,
  $a_user_id 
)
protected

Definition at line 194 of file class.ilBadgeAssignment.php.

References $db, $ilDB, $row, and importDBRow().

Referenced by __construct().

195  {
196  $ilDB = $this->db;
197 
198  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
199  " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer") .
200  " AND user_id = " . $ilDB->quote($a_user_id, "integer"));
201  $row = $ilDB->fetchAssoc($set);
202  if ($row["user_id"]) {
203  $this->importDBRow($row);
204  }
205  }
global $ilDB
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAwardedBy()

ilBadgeAssignment::setAwardedBy (   $a_id)

Definition at line 156 of file class.ilBadgeAssignment.php.

Referenced by importDBRow().

157  {
158  $this->awarded_by = (int) $a_id;
159  }
+ Here is the caller graph for this function:

◆ setBadgeId()

ilBadgeAssignment::setBadgeId (   $a_value)
protected

Definition at line 126 of file class.ilBadgeAssignment.php.

Referenced by __construct(), and importDBRow().

127  {
128  $this->badge_id = (int) $a_value;
129  }
+ Here is the caller graph for this function:

◆ setPosition()

ilBadgeAssignment::setPosition (   $a_value)

Definition at line 166 of file class.ilBadgeAssignment.php.

Referenced by importDBRow().

167  {
168  if ($a_value !== null) {
169  $a_value = (int) $a_value;
170  }
171  $this->pos = $a_value;
172  }
+ Here is the caller graph for this function:

◆ setTimestamp()

ilBadgeAssignment::setTimestamp (   $a_value)
protected

Definition at line 146 of file class.ilBadgeAssignment.php.

Referenced by importDBRow().

147  {
148  $this->tstamp = (int) $a_value;
149  }
+ Here is the caller graph for this function:

◆ setUserId()

ilBadgeAssignment::setUserId (   $a_value)
protected

Definition at line 136 of file class.ilBadgeAssignment.php.

Referenced by __construct(), and importDBRow().

137  {
138  $this->user_id = (int) $a_value;
139  }
+ Here is the caller graph for this function:

◆ store()

ilBadgeAssignment::store ( )

Definition at line 216 of file class.ilBadgeAssignment.php.

References $db, $ilDB, $keys, array, getBadgeId(), getPropertiesForStorage(), and getUserId().

Referenced by ilBadgeHandler\triggerEvaluation().

217  {
218  $ilDB = $this->db;
219 
220  if (!$this->getBadgeId() ||
221  !$this->getUserId()) {
222  return;
223  }
224 
225  $keys = array(
226  "badge_id" => array("integer", $this->getBadgeId()),
227  "user_id" => array("integer", $this->getUserId())
228  );
229  $fields = $this->getPropertiesForStorage();
230 
231  if (!(bool) $this->stored) {
232  $ilDB->insert("badge_user_badge", $fields + $keys);
233  } else {
234  $ilDB->update("badge_user_badge", $fields, $keys);
235  }
236  }
$keys
Create styles array
The data for the language used.
global $ilDB
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updatePositions()

static ilBadgeAssignment::updatePositions (   $a_user_id,
array  $a_positions 
)
static

Definition at line 275 of file class.ilBadgeAssignment.php.

References $id, $title, and array.

Referenced by ilPersonalProfileGUI\savePublicProfile().

276  {
277  $existing = array();
278  include_once "Services/Badge/classes/class.ilBadge.php";
279  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
280  $badge = new ilBadge($ass->getBadgeId());
281  $existing[$badge->getId()] = array($badge->getTitle(), $ass);
282  }
283 
284  $new_pos = 0;
285  foreach ($a_positions as $title) {
286  foreach ($existing as $id => $item) {
287  if ($title == $item[0]) {
288  $item[1]->setPosition(++$new_pos);
289  $item[1]->store();
290  unset($existing[$id]);
291  }
292  }
293  }
294  }
if(!array_key_exists('StateId', $_REQUEST)) $id
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

Field Documentation

◆ $awarded_by

ilBadgeAssignment::$awarded_by
protected

Definition at line 22 of file class.ilBadgeAssignment.php.

Referenced by getAwardedBy().

◆ $badge_id

ilBadgeAssignment::$badge_id
protected

Definition at line 19 of file class.ilBadgeAssignment.php.

Referenced by getBadgeId().

◆ $db

ilBadgeAssignment::$db
protected

Definition at line 17 of file class.ilBadgeAssignment.php.

Referenced by delete(), read(), and store().

◆ $pos

ilBadgeAssignment::$pos
protected

Definition at line 23 of file class.ilBadgeAssignment.php.

Referenced by getPosition().

◆ $stored

ilBadgeAssignment::$stored
protected

Definition at line 24 of file class.ilBadgeAssignment.php.

◆ $tstamp

ilBadgeAssignment::$tstamp
protected

Definition at line 21 of file class.ilBadgeAssignment.php.

Referenced by getTimestamp().

◆ $user_id

ilBadgeAssignment::$user_id
protected

Definition at line 20 of file class.ilBadgeAssignment.php.

Referenced by getUserId().


The documentation for this class was generated from the following file: