24    public function __construct($a_badge_id = 
null, $a_user_id = 
null)
 
   28        $this->db = 
$DIC->database();
 
   34            $this->
read($a_badge_id, $a_user_id);
 
   51        $noti_repo = new \ILIAS\Badge\Notification\BadgeNotificationPrefRepository($user);
 
   53        $last = $noti_repo->getLastCheckedTimestamp();
 
   58            $last = time() - (24 * 60 * 60);
 
   63                "SELECT count(*) cnt FROM badge_user_badge " .
 
   64                " WHERE user_id = %s AND tstamp >= %s",
 
   65                [
"integer", 
"integer"],
 
   68            $rec = 
$db->fetchAssoc($set);
 
   69            return (
int) $rec[
"cnt"];
 
   87            "SELECT max(tstamp) maxts FROM badge_user_badge " .
 
   88            " WHERE user_id = %s",
 
   92        $rec = 
$db->fetchAssoc($set);
 
   93        return (
int) $rec[
"maxts"];
 
  104        $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
  105            " WHERE user_id = " . 
$ilDB->quote($a_user_id, 
"integer") .
 
  107        while ($row = 
$ilDB->fetchAssoc($set)) {
 
  109            $obj->importDBRow($row);
 
  125        $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
  126            " WHERE badge_id = " . 
$ilDB->quote($a_badge_id, 
"integer"));
 
  127        while ($row = 
$ilDB->fetchAssoc($set)) {
 
  129            $obj->importDBRow($row);
 
  144        $badge_ids = array();
 
  146            $badge_ids[] = $badge->getId();
 
  148        if (
sizeof($badge_ids)) {
 
  149            $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
  150            " WHERE " . 
$ilDB->in(
"badge_id", $badge_ids, 
"", 
"integer"));
 
  151            while ($row = 
$ilDB->fetchAssoc($set)) {
 
  153                $obj->importDBRow($row);
 
  165        foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
 
  166            $res[] = $ass->getUserId();
 
  172    public static function exists($a_badge_id, $a_user_id)
 
  174        $obj = 
new self($a_badge_id, $a_user_id);
 
  185        $this->badge_id = (int) $a_value;
 
  195        $this->user_id = (int) $a_value;
 
  205        $this->tstamp = (int) $a_value;
 
  215        $this->awarded_by = (int) $a_id;
 
  225        if ($a_value !== 
null) {
 
  226            $a_value = (int) $a_value;
 
  228        $this->pos = $a_value;
 
  243        $this->stored = 
true;
 
  251    protected function read($a_badge_id, $a_user_id)
 
  255        $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
  256            " WHERE badge_id = " . 
$ilDB->quote($a_badge_id, 
"integer") .
 
  257            " AND user_id = " . 
$ilDB->quote($a_user_id, 
"integer"));
 
  258        $row = 
$ilDB->fetchAssoc($set);
 
  259        if ($row[
"user_id"]) {
 
  267            "tstamp" => array(
"integer", (
bool) $this->stored ? $this->
getTimestamp() : time()),
 
  268            "awarded_by" => array(
"integer", $this->
getAwardedBy()),
 
  283            "badge_id" => array(
"integer", $this->
getBadgeId()),
 
  284            "user_id" => array(
"integer", $this->
getUserId())
 
  288        if (!(
bool) $this->stored) {
 
  289            $ilDB->insert(
"badge_user_badge", $fields + 
$keys);
 
  291            $ilDB->update(
"badge_user_badge", $fields, 
$keys);
 
  295    public function delete()
 
  306        $ilDB->manipulate(
"DELETE FROM badge_user_badge" .
 
  307            " WHERE badge_id = " . 
$ilDB->quote($this->getBadgeId(), 
"integer") .
 
  308            " AND user_id = " . 
$ilDB->quote($this->getUserId(), 
"integer"));
 
  313        foreach (self::getInstancesByUserId($a_user_id) as $ass) {
 
  320        foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
 
  327        foreach (self::getInstancesByParentId($a_parent_obj_id) as $ass) {
 
  335        foreach (self::getInstancesByUserId($a_user_id) as $ass) {
 
  336            $badge = 
new ilBadge($ass->getBadgeId());
 
  337            $existing[$badge->getId()] = array($badge->getTitle(), $ass);
 
  341        foreach ($a_positions as $title) {
 
  342            foreach ($existing as $id => $item) {
 
  343                if ($title == $item[0]) {
 
  344                    $item[1]->setPosition(++$new_pos);
 
  346                    unset($existing[$id]);
 
  366            "SELECT bdg.parent_id, ub.tstamp, bdg.title FROM badge_user_badge ub JOIN badge_badge bdg" .
 
  367            " ON (ub.badge_id = bdg.id) " .
 
  368            " WHERE ub.user_id = %s AND ub.tstamp >= %s AND ub.tstamp <= %s",
 
  369            array(
"integer",
"integer",
"integer"),
 
  370            array($a_user_id, $a_ts_from, $a_ts_to)
 
  373        while ($rec = 
$db->fetchAssoc($set)) {
 
  386        $verify = 
new stdClass();
 
  387        $verify->type = 
"hosted";
 
  388        $verify->url = $a_url;
 
  390        $recipient = 
new stdClass();
 
  391        $recipient->type = 
"email";
 
  392        $recipient->hashed = 
true;
 
  399            $mail = $user->getEmail();
 
  401        $recipient->identity = 
'sha256$' . hash(
'sha256', $mail . $recipient->salt);
 
  406        $json = 
new stdClass();
 
  407        $json->{
"@context"} = 
"https://w3id.org/openbadges/v1";
 
  408        $json->type = 
"Assertion";
 
  410        $json->uid = $unique_id;
 
  411        $json->recipient = $recipient;
 
  414        $badge_url = $badge->getStaticUrl();
 
  418        if ($this->
bakeImage($baked_image, $badge->getImagePath(), $a_url)) {
 
  420            $parts = explode(
"/", $a_url);
 
  422            $parts[] = basename($baked_image);
 
  423            $json->image = implode(
"/", $parts);
 
  427        $json->badge = $badge_url;
 
  428        $json->verify = $verify;
 
  436        $baked_path = dirname($json_path);
 
  437        $baked_file = array_shift(explode(
".", basename($json_path)));
 
  440        $suffix = strtolower(array_pop(explode(
".", basename($a_badge->
getImagePath()))));
 
  441        return $baked_path . 
"/" . $baked_file . 
"." . $suffix;
 
  444    protected function bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
 
  446        $suffix = strtolower(array_pop(explode(
".", basename($a_badge_image_path))));
 
  447        if ($suffix == 
"png") {
 
  449            include_once 
"Services/Badge/lib/baker.lib.php";
 
  450            $png = 
new PNGImageBaker(file_get_contents($a_badge_image_path));
 
  453            if ($png->checkChunks(
"tEXt", 
"openbadges")) {
 
  454                $baked = $png->addChunk(
"tEXt", 
"openbadges", $a_assertion_url);
 
  458            if (!file_exists($a_baked_image_path)) {
 
  459                file_put_contents($a_baked_image_path, $baked);
 
  463            $verify = $png->extractBadgeInfo(file_get_contents($a_baked_image_path));
 
  464            if (is_array($verify)) {
 
  467        } elseif ($suffix == 
"svg") {
 
  469            $svg = simplexml_load_file($a_badge_image_path);
 
  470            $ass = $svg->addChild(
"openbadges:assertion", 
"", 
"http://openbadges.org");
 
  471            $ass->addAttribute(
"verify", $a_assertion_url);
 
  472            $baked = $svg->asXML();
 
  475            if (!file_exists($a_baked_image_path)) {
 
  476                file_put_contents($a_baked_image_path, $baked);
 
  491        if (!file_exists($path)) {
 
  492            $json = json_encode($this->prepareJson(
$url));
 
  493            file_put_contents($path, $json);
 
  503        $path = str_replace(
".json", 
".*", $path);
 
  504        array_map(
"unlink", glob($path));
 
  509        foreach (self::getInstancesByUserId($a_user_id) as $ass) {
 
  510            $ass->deleteStaticFiles();
 
An exception for terminatinating execution or to throw for unit testing.
Php library to Bake the PNG Images.
static getInstancesByUserId($a_user_id)
static getNewCounter(int $a_user_id)
Get new counter.
bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
static getAssignedUsers($a_badge_id)
getPropertiesForStorage()
static clearBadgeCache($a_user_id)
static deleteByBadgeId($a_badge_id)
static getInstancesByBadgeId($a_badge_id)
read($a_badge_id, $a_user_id)
static getBadgesForUser($a_user_id, $a_ts_from, $a_ts_to)
Get badges for user.
static deleteByUserId($a_user_id)
__construct($a_badge_id=null, $a_user_id=null)
getImagePath(ilBadge $a_badge)
static updatePositions($a_user_id, array $a_positions)
importDBRow(array $a_row)
static exists($a_badge_id, $a_user_id)
static getInstancesByParentId($a_parent_obj_id)
static getLatestTimestamp(int $a_user_id)
Get latest badge.
static deleteByParentId($a_parent_obj_id)
static getInstance()
Constructor.
static getInstancesByParentId($a_parent_id, array $a_filter=null)
getImagePath($a_full_path=true)
foreach($_POST as $key=> $value) $res