ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBadgeBackpack.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  protected $email; // [string]
15  protected $uid; // [int]
16 
17  const URL_DISPLAYER = "https://backpack.openbadges.org/displayer/";
18 
19  public function __construct($a_email)
20  {
21  $this->email = $a_email;
22  }
23 
24  protected function authenticate()
25  {
26  $json = $this->sendRequest(
27  self::URL_DISPLAYER."convert/email",
28  array("email"=>$this->email),
29  true
30  );
31 
32  if(!isset($json->status) ||
33  $json->status != "okay")
34  {
35  return false;
36  }
37 
38  $this->uid = $json->userId;
39  return true;
40  }
41 
42  public function getGroups()
43  {
44  if($this->authenticate())
45  {
46  $json = $this->sendRequest(
47  self::URL_DISPLAYER.$this->uid."/groups.json"
48  );
49 
50  $result = array();
51 
52  foreach($json->groups as $group)
53  {
54  $result[$group->groupId] = array(
55  "title" => $group->name,
56  "size" => $group->badges
57  );
58  }
59 
60  return $result;
61  }
62  }
63 
64  public function getBadges($a_group_id)
65  {
66  if($this->authenticate())
67  {
68  $json = $this->sendRequest(
69  self::URL_DISPLAYER.$this->uid."/group/".$a_group_id.".json"
70  );
71 
72  if($json->status &&
73  $json->status == "missing")
74  {
75  return false;
76  }
77 
78  $result = array();
79 
80  foreach($json->badges as $raw)
81  {
82  $badge = $raw->assertion->badge;
83 
84  // :TODO: not sure if this works reliably
85  $issued_on = is_numeric($raw->assertion->issued_on)
86  ? $raw->assertion->issued_on
87  : strtotime($raw->assertion->issued_on);
88 
89  $result[] = array(
90  "title" => $badge->name,
91  "description" => $badge->description,
92  "image_url" => $badge->image,
93  "criteria_url" => $badge->criteria,
94  "issuer_name" => $badge->issuer->name,
95  "issuer_url" => $badge->issuer->origin,
96  "issued_on" => new ilDate($issued_on, IL_CAL_UNIX)
97  );
98  }
99 
100  return $result;
101  }
102  }
103 
104  protected function sendRequest($a_url, array $a_param = array(), $a_is_post = false)
105  {
106  try
107  {
108  include_once "Services/WebServices/Curl/classes/class.ilCurlConnection.php";
109  $curl = new ilCurlConnection();
110  $curl->init();
111 
112  $curl->setOpt(CURLOPT_FRESH_CONNECT, true);
113  $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
114  $curl->setOpt(CURLOPT_FORBID_REUSE, true);
115  $curl->setOpt(CURLOPT_HEADER, 0);
116  $curl->setOpt(CURLOPT_CONNECTTIMEOUT, 3);
117  $curl->setOpt(CURLOPT_POSTREDIR, 3);
118 
119  // :TODO: SSL problems on test server
120  $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
121 
122  $curl->setOpt(CURLOPT_HTTPHEADER, array(
123  "Accept: application/json",
124  "Expect:"
125  ));
126 
127  if((bool)$a_is_post)
128  {
129  $curl->setOpt(CURLOPT_POST, 1);
130  if(sizeof($a_param))
131  {
132  $curl->setOpt(CURLOPT_POSTFIELDS, http_build_query($a_param));
133  }
134  }
135  else
136  {
137  $curl->setOpt(CURLOPT_HTTPGET, 1);
138  if(sizeof($a_param))
139  {
140  $a_url = $a_url.
141  (strpos($a_url, "?") === false ? "?" : "").
142  http_build_query($a_param);
143  }
144  }
145  $curl->setOpt(CURLOPT_URL, $a_url);
146 
147  $answer = $curl->exec();
148  }
149  catch (Exception $ex)
150  {
151  ilUtil::sendFailure($ex->getMessage());
152  return;
153  }
154 
155  return json_decode($answer);
156  }
157 }
$result
const IL_CAL_UNIX
Class for single dates.
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
sendRequest($a_url, array $a_param=array(), $a_is_post=false)