ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  "criteria_url" => $badge->criteria,
103  "issuer_name" => $badge->issuer->name,
104  "issuer_url" => $badge->issuer->origin,
105  "issued_on" => new ilDate($issued_on, IL_CAL_UNIX)
106  ];
107  }
108 
109  return $result;
110  }
111  return null;
112  }
113 
114  protected function sendRequest(
115  string $a_url,
116  array $a_param = array(),
117  bool $a_is_post = false
118  ): ?stdClass {
119  try {
120  $curl = new ilCurlConnection();
121  $curl->init(false);
122 
123  $curl->setOpt(CURLOPT_FRESH_CONNECT, true);
124  $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
125  $curl->setOpt(CURLOPT_FORBID_REUSE, true);
126  $curl->setOpt(CURLOPT_HEADER, 0);
127  $curl->setOpt(CURLOPT_CONNECTTIMEOUT, 3);
128  $curl->setOpt(CURLOPT_POSTREDIR, 3);
129 
130  // :TODO: SSL problems on test server
131  $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
132 
133  $curl->setOpt(CURLOPT_HTTPHEADER, array(
134  "Accept: application/json",
135  "Expect:"
136  ));
137 
138  if ($a_is_post) {
139  $curl->setOpt(CURLOPT_POST, 1);
140  if (count($a_param)) {
141  $curl->setOpt(CURLOPT_POSTFIELDS, http_build_query($a_param));
142  }
143  } else {
144  $curl->setOpt(CURLOPT_HTTPGET, 1);
145  if (count($a_param)) {
146  $a_url .= (!str_contains($a_url, "?") ? "?" : "") . http_build_query($a_param);
147  }
148  }
149  $curl->setOpt(CURLOPT_URL, $a_url);
150 
151  $answer = $curl->exec();
152  } catch (Exception $ex) {
153  $this->main_tpl->setOnScreenMessage('failure', $ex->getMessage());
154  return null;
155  }
156 
157  return json_decode($answer, false, 512, JSON_THROW_ON_ERROR);
158  }
159 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
global $DIC
Definition: feed.php:28
sendRequest(string $a_url, array $a_param=array(), bool $a_is_post=false)
getBadges(string $a_group_id)
__construct(protected string $email)
readonly ilGlobalTemplateInterface $main_tpl