ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 getBadgesForUser ($a_user_id, $a_ts_from, $a_ts_to)
 Get badges for user. More...
 
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.

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 }
read($a_badge_id, $a_user_id)
global $DIC
Definition: saml.php:7

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

+ 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 390 of file class.ilBadgeAssignment.php.

391 {
392 $suffix = strtolower(array_pop(explode(".", basename($a_badge_image_path))));
393 if ($suffix == "png") {
394 // using chamilo baker lib
395 include_once "Services/Badge/lib/baker.lib.php";
396 $png = new PNGImageBaker(file_get_contents($a_badge_image_path));
397
398 // add payload
399 if ($png->checkChunks("tEXt", "openbadges")) {
400 $baked = $png->addChunk("tEXt", "openbadges", $a_assertion_url);
401 }
402
403 // create baked file
404 if (!file_exists($a_baked_image_path)) {
405 file_put_contents($a_baked_image_path, $baked);
406 }
407
408 // verify file
409 $verify = $png->extractBadgeInfo(file_get_contents($a_baked_image_path));
410 if (is_array($verify)) {
411 return true;
412 }
413 } elseif ($suffix == "svg") {
414 // :TODO: not really sure if this is correct
415 $svg = simplexml_load_file($a_badge_image_path);
416 $ass = $svg->addChild("openbadges:assertion", "", "http://openbadges.org");
417 $ass->addAttribute("verify", $a_assertion_url);
418 $baked = $svg->asXML();
419
420 // create baked file
421 if (!file_exists($a_baked_image_path)) {
422 file_put_contents($a_baked_image_path, $baked);
423 }
424
425 return true;
426 }
427
428 return false;
429 }
Php library to Bake the PNG Images.
Definition: baker.lib.php:8

◆ clearBadgeCache()

static ilBadgeAssignment::clearBadgeCache (   $a_user_id)
static

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

456 {
457 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
458 $ass->deleteStaticFiles();
459 }
460 }

Referenced by ilBadgeProfileGUI\saveSettings().

+ Here is the caller graph for this function:

◆ delete()

ilBadgeAssignment::delete ( )

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

239 {
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

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

+ 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.

262 {
263 foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
264 $ass->delete();
265 }
266 }

Referenced by ilBadge\delete().

+ 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.

255 {
256 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
257 $ass->delete();
258 }
259 }

Referenced by ilObjUser\delete().

+ Here is the caller graph for this function:

◆ deleteStaticFiles()

ilBadgeAssignment::deleteStaticFiles ( )

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

447 {
448 // remove instance files
449 include_once("./Services/Badge/classes/class.ilBadgeHandler.php");
450 $path = ilBadgeHandler::getInstance()->getInstancePath($this);
451 $path = str_replace(".json", ".*", $path);
452 array_map("unlink", glob($path));
453 }
$path
Definition: aliased.php:25
static getInstance()
Constructor.

References $path, and ilBadgeHandler\getInstance().

Referenced by delete().

+ 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.

116 {
117 $obj = new self($a_badge_id, $a_user_id);
118 return $obj->stored;
119 }

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

+ 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.

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

References $res.

Referenced by ilBadgeManagementGUI\confirmDeassignBadge().

+ 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().

+ Here is the caller graph for this function:

◆ getBadgeId()

ilBadgeAssignment::getBadgeId ( )

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

132 {
133 return $this->badge_id;
134 }

References $badge_id.

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

+ Here is the caller graph for this function:

◆ getBadgesForUser()

static ilBadgeAssignment::getBadgesForUser (   $a_user_id,
  $a_ts_from,
  $a_ts_to 
)
static

Get badges for user.

Parameters
int$a_user_id
int$a_ts_from
int$a_ts_to
Returns
array

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

304 {
305 global $DIC;
306
307 $db = $DIC->database();
308
309 $set = $db->queryF(
310 "SELECT bdg.parent_id, ub.tstamp, bdg.title FROM badge_user_badge ub JOIN badge_badge bdg" .
311 " ON (ub.badge_id = bdg.id) " .
312 " WHERE ub.user_id = %s AND ub.tstamp >= %s AND ub.tstamp <= %s",
313 array("integer","integer","integer"),
314 array($a_user_id, $a_ts_from, $a_ts_to)
315 );
316 $res = [];
317 while ($rec = $db->fetchAssoc($set)) {
318 $res[] = $rec;
319 }
320 return $res;
321 }

References $db, $DIC, and $res.

Referenced by ilBadgeLearningHistoryProvider\getEntries().

+ Here is the caller graph for this function:

◆ getImagePath()

ilBadgeAssignment::getImagePath ( ilBadge  $a_badge)

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

380 {
381 $json_path = ilBadgeHandler::getInstance()->getInstancePath($this);
382 $baked_path = dirname($json_path);
383 $baked_file = array_shift(explode(".", basename($json_path)));
384
385 // get correct suffix from badge image
386 $suffix = strtolower(array_pop(explode(".", basename($a_badge->getImagePath()))));
387 return $baked_path . "/" . $baked_file . "." . $suffix;
388 }
getImagePath($a_full_path=true)

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

+ Here is the call graph for this function:

◆ getInstancesByBadgeId()

static ilBadgeAssignment::getInstancesByBadgeId (   $a_badge_id)
static

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

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 }
$row

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

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

+ 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.

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 }
static getInstancesByParentId($a_parent_id, array $a_filter=null)

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

Referenced by ilBadgeUserTableGUI\getItems().

+ 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.

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 }

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

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

+ Here is the caller graph for this function:

◆ getPosition()

ilBadgeAssignment::getPosition ( )

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

175 {
176 return $this->pos;
177 }

References $pos.

Referenced by getPropertiesForStorage().

+ Here is the caller graph for this function:

◆ getPropertiesForStorage()

ilBadgeAssignment::getPropertiesForStorage ( )
protected

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

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 }

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

Referenced by store().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getStaticUrl()

ilBadgeAssignment::getStaticUrl ( )

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

432 {
433 include_once("./Services/Badge/classes/class.ilBadgeHandler.php");
434 $path = ilBadgeHandler::getInstance()->getInstancePath($this);
435
436 $url = ILIAS_HTTP_PATH . substr($path, 1);
437
438 if (!file_exists($path)) {
439 $json = json_encode($this->prepareJson($url));
440 file_put_contents($path, $json);
441 }
442
443 return $url;
444 }
$url

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

+ Here is the call graph for this function:

◆ getTimestamp()

ilBadgeAssignment::getTimestamp ( )

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

152 {
153 return $this->tstamp;
154 }

References $tstamp.

Referenced by getPropertiesForStorage().

+ Here is the caller graph for this function:

◆ getUserId()

ilBadgeAssignment::getUserId ( )

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

142 {
143 return $this->user_id;
144 }

References $user_id.

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

+ Here is the caller graph for this function:

◆ importDBRow()

ilBadgeAssignment::importDBRow ( array  $a_row)
protected

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

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 }

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

Referenced by read().

+ 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 328 of file class.ilBadgeAssignment.php.

329 {
330 $verify = new stdClass();
331 $verify->type = "hosted";
332 $verify->url = $a_url;
333
334 $recipient = new stdClass();
335 $recipient->type = "email";
336 $recipient->hashed = true;
337 $recipient->salt = ilBadgeHandler::getInstance()->getObiSalt();
338
339 // https://github.com/mozilla/openbadges-backpack/wiki/How-to-hash-&-salt-in-various-languages.
340 include_once "Services/Badge/classes/class.ilBadgeProfileGUI.php";
341 $user = new ilObjUser($this->getUserId());
343 if (!$mail) {
344 $mail = $user->getEmail();
345 }
346 $recipient->identity = 'sha256$' . hash('sha256', $mail . $recipient->salt);
347
348 // spec: should be locally unique
349 $unique_id = md5($this->getBadgeId() . "-" . $this->getUserId());
350
351 $json = new stdClass();
352 $json->{"@context"} = "https://w3id.org/openbadges/v1";
353 $json->type = "Assertion";
354 $json->id = $a_url;
355 $json->uid = $unique_id;
356 $json->recipient = $recipient;
357
358 include_once "Services/Badge/classes/class.ilBadge.php";
359 $badge = new ilBadge($this->getBadgeId());
360 $badge_url = $badge->getStaticUrl();
361
362 // created baked image
363 $baked_image = $this->getImagePath($badge);
364 if ($this->bakeImage($baked_image, $badge->getImagePath(), $a_url)) {
365 // path to url
366 $parts = explode("/", $a_url);
367 array_pop($parts);
368 $parts[] = basename($baked_image);
369 $json->image = implode("/", $parts);
370 }
371
372 $json->issuedOn = $this->getTimestamp();
373 $json->badge = $badge_url;
374 $json->verify = $verify;
375
376 return $json;
377 }
bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
getImagePath(ilBadge $a_badge)
$user
Definition: migrateto20.php:57
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406

References $user, ilBadgeProfileGUI\BACKPACK_EMAIL, getBadgeId(), ilBadgeHandler\getInstance(), getUserId(), and GuzzleHttp\Psr7\hash().

+ Here is the call graph for this function:

◆ read()

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

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

195 {
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 }

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

Referenced by __construct().

+ 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.

157 {
158 $this->awarded_by = (int) $a_id;
159 }

Referenced by importDBRow().

+ Here is the caller graph for this function:

◆ setBadgeId()

ilBadgeAssignment::setBadgeId (   $a_value)
protected

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

127 {
128 $this->badge_id = (int) $a_value;
129 }

Referenced by __construct(), and importDBRow().

+ Here is the caller graph for this function:

◆ setPosition()

ilBadgeAssignment::setPosition (   $a_value)

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

167 {
168 if ($a_value !== null) {
169 $a_value = (int) $a_value;
170 }
171 $this->pos = $a_value;
172 }

Referenced by importDBRow().

+ Here is the caller graph for this function:

◆ setTimestamp()

ilBadgeAssignment::setTimestamp (   $a_value)
protected

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

147 {
148 $this->tstamp = (int) $a_value;
149 }

Referenced by importDBRow().

+ Here is the caller graph for this function:

◆ setUserId()

ilBadgeAssignment::setUserId (   $a_value)
protected

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

137 {
138 $this->user_id = (int) $a_value;
139 }

Referenced by __construct(), and importDBRow().

+ Here is the caller graph for this function:

◆ store()

ilBadgeAssignment::store ( )

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

217 {
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

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

+ Here is the call graph for this function:

◆ updatePositions()

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

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

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

References $id, and $title.

Referenced by ilPersonalProfileGUI\savePublicProfile().

+ 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(), getBadgesForUser(), 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: