ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgeBackpack.php
Go to the documentation of this file.
1<?php
2
24{
25 final public const URL_DISPLAYER = "https://backpack.openbadges.org/displayer/";
26 protected int $uid;
27 private readonly \ilGlobalTemplateInterface $main_tpl;
28
29 public function __construct(protected string $email)
30 {
31 global $DIC;
32 $this->main_tpl = $DIC->ui()->mainTemplate();
33 }
34
35 protected function authenticate(): bool
36 {
37 $json = $this->sendRequest(
38 self::URL_DISPLAYER . "convert/email",
39 array("email" => $this->email),
40 true
41 );
42
43 if (!isset($json->status) ||
44 $json->status !== "okay") {
45 return false;
46 }
47
48 $this->uid = $json->userId;
49 return true;
50 }
51
52 public function getGroups(): array
53 {
54 if ($this->authenticate()) {
55 $json = $this->sendRequest(
56 self::URL_DISPLAYER . $this->uid . "/groups.json"
57 );
58
59 $result = array();
60
61 foreach ($json->groups as $group) {
62 $result[$group->groupId] = array(
63 "title" => $group->name,
64 "size" => $group->badges
65 );
66 }
67
68 return $result;
69 }
70 return [];
71 }
72
73 public function getBadges(string $a_group_id): ?array
74 {
75 if ($this->authenticate()) {
76 $json = $this->sendRequest(
77 self::URL_DISPLAYER . $this->uid . "/group/" . $a_group_id . ".json"
78 );
79
80 if ($json === null) {
81 return null;
82 }
83
84 if (property_exists($json, 'status') && $json->status === "missing") {
85 return null;
86 }
87
88 $result = [];
89
90 foreach ($json->badges as $raw) {
91 $badge = $raw->assertion->badge;
92
93 // :TODO: not sure if this works reliably
94 $issued_on = is_numeric($raw->assertion->issued_on)
95 ? $raw->assertion->issued_on
96 : strtotime($raw->assertion->issued_on);
97
98 $result[] = [
99 "title" => $badge->name,
100 "description" => $badge->description,
101 "image_url" => $badge->image,
102 "image_rid" => $badge->image_rid,
103 "criteria_url" => $badge->criteria,
104 "issuer_name" => $badge->issuer->name,
105 "issuer_url" => $badge->issuer->origin,
106 "issued_on" => new ilDate($issued_on, IL_CAL_UNIX)
107 ];
108 }
109
110 return $result;
111 }
112 return null;
113 }
114
115 protected function sendRequest(
116 string $a_url,
117 array $a_param = array(),
118 bool $a_is_post = false
119 ): ?stdClass {
120 try {
121 $curl = new ilCurlConnection();
122 $curl->init(false);
123
124 $curl->setOpt(CURLOPT_FRESH_CONNECT, true);
125 $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
126 $curl->setOpt(CURLOPT_FORBID_REUSE, true);
127 $curl->setOpt(CURLOPT_HEADER, 0);
128 $curl->setOpt(CURLOPT_CONNECTTIMEOUT, 3);
129 $curl->setOpt(CURLOPT_POSTREDIR, 3);
130
131 // :TODO: SSL problems on test server
132 $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
133
134 $curl->setOpt(CURLOPT_HTTPHEADER, array(
135 "Accept: application/json",
136 "Expect:"
137 ));
138
139 if ($a_is_post) {
140 $curl->setOpt(CURLOPT_POST, 1);
141 if (count($a_param)) {
142 $curl->setOpt(CURLOPT_POSTFIELDS, http_build_query($a_param));
143 }
144 } else {
145 $curl->setOpt(CURLOPT_HTTPGET, 1);
146 if (count($a_param)) {
147 $a_url .= (!str_contains($a_url, "?") ? "?" : "") . http_build_query($a_param);
148 }
149 }
150 $curl->setOpt(CURLOPT_URL, $a_url);
151
152 $answer = $curl->exec();
153 } catch (Exception $ex) {
154 $this->main_tpl->setOnScreenMessage('failure', $ex->getMessage());
155 return null;
156 }
157
158 return json_decode($answer, false, 512, JSON_THROW_ON_ERROR);
159 }
160}
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
sendRequest(string $a_url, array $a_param=array(), bool $a_is_post=false)
__construct(protected string $email)
readonly ilGlobalTemplateInterface $main_tpl
getBadges(string $a_group_id)
init(bool $set_proxy=true)
Class for single dates.
try
handle Lrs Init
Definition: xapiproxy.php:86
global $DIC
Definition: shib_login.php:26