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