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);
 
   48        $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
   49            " WHERE user_id = " . 
$ilDB->quote($a_user_id, 
"integer") .
 
   53            $obj->importDBRow(
$row);
 
   68        $set = 
$ilDB->query(
"SELECT * FROM badge_user_badge" .
 
   69            " WHERE badge_id = " . 
$ilDB->quote($a_badge_id, 
"integer"));
 
   72            $obj->importDBRow(
$row);
 
   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) {
 
  288                    $item[1]->setPosition(++$new_pos);
 
  290                    unset($existing[
$id]);
 
  303        $verify = 
new stdClass();
 
  304        $verify->type = 
"hosted";
 
  305        $verify->url = $a_url;
 
  307        $recipient = 
new stdClass();
 
  308        $recipient->type = 
"email";
 
  309        $recipient->hashed = 
true;
 
  313        include_once 
"Services/Badge/classes/class.ilBadgeProfileGUI.php";
 
  317            $mail = $user->getEmail();
 
  319        $recipient->identity = 
'sha256$' . 
hash(
'sha256', $mail . $recipient->salt);
 
  324        $json = 
new stdClass();
 
  325        $json->{
"@context"} = 
"https://w3id.org/openbadges/v1";
 
  326        $json->type = 
"Assertion";
 
  328        $json->uid = $unique_id;
 
  329        $json->recipient = $recipient;
 
  331        include_once 
"Services/Badge/classes/class.ilBadge.php";
 
  333        $badge_url = $badge->getStaticUrl();
 
  337        if ($this->
bakeImage($baked_image, $badge->getImagePath(), $a_url)) {
 
  339            $parts = explode(
"/", $a_url);
 
  341            $parts[] = basename($baked_image);
 
  342            $json->image = implode(
"/", $parts);
 
  346        $json->badge =  $badge_url;
 
  347        $json->verify = $verify;
 
  355        $baked_path = dirname($json_path);
 
  356        $baked_file = array_shift(explode(
".", basename($json_path)));
 
  359        $suffix = strtolower(array_pop(explode(
".", basename($a_badge->
getImagePath()))));
 
  360        return $baked_path . 
"/" . $baked_file . 
"." . $suffix;
 
  363    protected function bakeImage($a_baked_image_path, $a_badge_image_path, $a_assertion_url)
 
  365        $suffix = strtolower(array_pop(explode(
".", basename($a_badge_image_path))));
 
  366        if ($suffix == 
"png") {
 
  368            include_once 
"Services/Badge/lib/baker.lib.php";
 
  369            $png = 
new PNGImageBaker(file_get_contents($a_badge_image_path));
 
  372            if ($png->checkChunks(
"tEXt", 
"openbadges")) {
 
  373                $baked = $png->addChunk(
"tEXt", 
"openbadges", $a_assertion_url);
 
  377            if (!file_exists($a_baked_image_path)) {
 
  378                file_put_contents($a_baked_image_path, $baked);
 
  382            $verify = $png->extractBadgeInfo(file_get_contents($a_baked_image_path));
 
  383            if (is_array($verify)) {
 
  386        } elseif ($suffix == 
"svg") {
 
  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();
 
  394            if (!file_exists($a_baked_image_path)) {
 
  395                file_put_contents($a_baked_image_path, $baked);
 
  406        include_once(
"./Services/Badge/classes/class.ilBadgeHandler.php");
 
  409        $url = ILIAS_HTTP_PATH . substr(
$path, 1);
 
  411        if (!file_exists(
$path)) {
 
  412            $json = json_encode($this->prepareJson(
$url));
 
  413            file_put_contents(
$path, $json);
 
  422        include_once(
"./Services/Badge/classes/class.ilBadgeHandler.php");
 
  425        array_map(
"unlink", glob(
$path));
 
  430        foreach (self::getInstancesByUserId($a_user_id) as $ass) {
 
  431            $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)
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 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 deleteByParentId($a_parent_obj_id)
static getInstance()
Constructor.
static getInstancesByParentId($a_parent_id, array $a_filter=null)
getImagePath($a_full_path=true)
if(!array_key_exists('StateId', $_REQUEST)) $id
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
foreach($_POST as $key=> $value) $res