26 public function __construct($a_badge_id = null, $a_user_id = null)
30 $this->db = $DIC->database();
36 $this->
read($a_badge_id, $a_user_id);
44 $ilDB = $DIC->database();
48 $set =
$ilDB->query(
"SELECT * FROM badge_user_badge" .
49 " WHERE user_id = " .
$ilDB->quote($a_user_id,
"integer") .
53 $obj->importDBRow(
$row);
64 $ilDB = $DIC->database();
68 $set =
$ilDB->query(
"SELECT * FROM badge_user_badge" .
69 " WHERE badge_id = " .
$ilDB->quote($a_badge_id,
"integer"));
72 $obj->importDBRow(
$row);
83 $ilDB = $DIC->database();
89 $badge_ids[] = $badge->getId();
91 if (
sizeof($badge_ids)) {
92 $set =
$ilDB->query(
"SELECT * FROM badge_user_badge" .
93 " WHERE " .
$ilDB->in(
"badge_id", $badge_ids,
"",
"integer"));
96 $obj->importDBRow(
$row);
108 foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
109 $res[] = $ass->getUserId();
115 public static function exists($a_badge_id, $a_user_id)
117 $obj =
new self($a_badge_id, $a_user_id);
128 $this->badge_id = (int) $a_value;
138 $this->user_id = (int) $a_value;
148 $this->tstamp = (int) $a_value;
158 $this->awarded_by = (int) $a_id;
168 if ($a_value !== null) {
169 $a_value = (int) $a_value;
171 $this->pos = $a_value;
186 $this->stored =
true;
194 protected function read($a_badge_id, $a_user_id)
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"));
202 if (
$row[
"user_id"]) {
210 "tstamp" => array(
"integer", (
bool) $this->stored ? $this->
getTimestamp() : time()),
211 "awarded_by" => array(
"integer", $this->
getAwardedBy()),
226 "badge_id" => array(
"integer", $this->
getBadgeId()),
227 "user_id" => array(
"integer", $this->
getUserId())
231 if (!(
bool) $this->stored) {
232 $ilDB->insert(
"badge_user_badge", $fields +
$keys);
234 $ilDB->update(
"badge_user_badge", $fields,
$keys);
238 public function delete()
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"));
256 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
263 foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
270 foreach (self::getInstancesByParentId($a_parent_obj_id) as $ass) {
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);
285 foreach ($a_positions as
$title) {
286 foreach ($existing as
$id => $item) {
287 if ($title == $item[0]) {
288 $item[1]->setPosition(++$new_pos);
290 unset($existing[
$id]);
307 $db = $DIC->database();
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)
317 while ($rec =
$db->fetchAssoc($set)) {
331 $verify->type =
"hosted";
332 $verify->url = $a_url;
335 $recipient->type =
"email";
336 $recipient->hashed =
true;
340 include_once
"Services/Badge/classes/class.ilBadgeProfileGUI.php";
344 $mail =
$user->getEmail();
346 $recipient->identity =
'sha256$' .
hash(
'sha256', $mail . $recipient->salt);
352 $json->{
"@context"} =
"https://w3id.org/openbadges/v1";
353 $json->type =
"Assertion";
355 $json->uid = $unique_id;
356 $json->recipient = $recipient;
358 include_once
"Services/Badge/classes/class.ilBadge.php";
360 $badge_url = $badge->getStaticUrl();
364 if ($this->
bakeImage($baked_image, $badge->getImagePath(), $a_url)) {
366 $parts = explode(
"/", $a_url);
368 $parts[] = basename($baked_image);
369 $json->image = implode(
"/", $parts);
373 $json->badge = $badge_url;
374 $json->verify = $verify;
382 $baked_path = dirname($json_path);
383 $baked_file = array_shift(explode(
".", basename($json_path)));
386 $suffix = strtolower(array_pop(explode(
".", basename($a_badge->
getImagePath()))));
387 return $baked_path .
"/" . $baked_file .
"." . $suffix;
390 protected function bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
392 $suffix = strtolower(array_pop(explode(
".", basename($a_badge_image_path))));
393 if ($suffix ==
"png") {
395 include_once
"Services/Badge/lib/baker.lib.php";
396 $png =
new PNGImageBaker(file_get_contents($a_badge_image_path));
399 if ($png->checkChunks(
"tEXt",
"openbadges")) {
400 $baked = $png->addChunk(
"tEXt",
"openbadges", $a_assertion_url);
404 if (!file_exists($a_baked_image_path)) {
405 file_put_contents($a_baked_image_path, $baked);
409 $verify = $png->extractBadgeInfo(file_get_contents($a_baked_image_path));
410 if (is_array($verify)) {
413 } elseif ($suffix ==
"svg") {
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();
421 if (!file_exists($a_baked_image_path)) {
422 file_put_contents($a_baked_image_path, $baked);
433 include_once(
"./Services/Badge/classes/class.ilBadgeHandler.php");
436 $url = ILIAS_HTTP_PATH . substr(
$path, 1);
438 if (!file_exists(
$path)) {
440 file_put_contents(
$path, $json);
449 include_once(
"./Services/Badge/classes/class.ilBadgeHandler.php");
452 array_map(
"unlink", glob(
$path));
457 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
458 $ass->deleteStaticFiles();
getImagePath($a_full_path=true)
static exists($a_badge_id, $a_user_id)
static getAssignedUsers($a_badge_id)
__construct($a_badge_id=null, $a_user_id=null)
if(!array_key_exists('StateId', $_REQUEST)) $id
static deleteByParentId($a_parent_obj_id)
Php library to Bake the PNG Images.
getImagePath(ilBadge $a_badge)
static getInstancesByParentId($a_parent_id, array $a_filter=null)
foreach($_POST as $key=> $value) $res
getPropertiesForStorage()
static deleteByBadgeId($a_badge_id)
static updatePositions($a_user_id, array $a_positions)
bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
read($a_badge_id, $a_user_id)
static deleteByUserId($a_user_id)
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
static getInstancesByBadgeId($a_badge_id)
static clearBadgeCache($a_user_id)
static getInstancesByUserId($a_user_id)
static getBadgesForUser($a_user_id, $a_ts_from, $a_ts_to)
Get badges for user.
static getInstance()
Constructor.
importDBRow(array $a_row)
static getInstancesByParentId($a_parent_obj_id)