Commit | Line | Data |
---|---|---|
27806552 YB |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * External backpack library. | |
19 | * | |
20 | * @package core | |
21 | * @subpackage badges | |
22 | * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/} | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com> | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | require_once($CFG->libdir . '/filelib.php'); | |
31 | ||
32 | // Adopted from https://github.com/jbkc85/openbadges-class-php. | |
33 | // Author Jason Cameron <jbkc85@gmail.com>. | |
34 | ||
35 | class OpenBadgesBackpackHandler { | |
36 | private $backpack; | |
37 | private $email; | |
38 | private $backpackuid = 0; | |
27806552 YB |
39 | |
40 | public function __construct($record) { | |
41 | $this->backpack = $record->backpackurl; | |
42 | $this->email = $record->email; | |
43 | $this->backpackuid = isset($record->backpackuid) ? $record->backpackuid : 0; | |
27806552 YB |
44 | } |
45 | ||
e2805314 | 46 | public function curl_request($action, $collection = null) { |
27806552 YB |
47 | $curl = new curl(); |
48 | ||
49 | switch($action) { | |
50 | case 'user': | |
e2805314 | 51 | $url = $this->backpack . "/displayer/convert/email"; |
27806552 YB |
52 | $param = array('email' => $this->email); |
53 | break; | |
54 | case 'groups': | |
55 | $url = $this->backpack . '/displayer/' . $this->backpackuid . '/groups.json'; | |
56 | break; | |
57 | case 'badges': | |
e2805314 | 58 | $url = $this->backpack . '/displayer/' . $this->backpackuid . '/group/' . $collection . '.json'; |
27806552 YB |
59 | break; |
60 | } | |
61 | ||
62 | $options = array( | |
3752d823 | 63 | 'FRESH_CONNECT' => true, |
27806552 | 64 | 'RETURNTRANSFER' => true, |
3752d823 YB |
65 | 'FORBID_REUSE' => true, |
66 | 'HEADER' => 0, | |
67 | 'HTTPHEADER' => array('Expect:'), | |
dd4a197e | 68 | 'CONNECTTIMEOUT' => 3, |
27806552 YB |
69 | ); |
70 | ||
71 | if ($action == 'user') { | |
72 | $out = $curl->post($url, $param, $options); | |
73 | } else { | |
74 | $out = $curl->get($url, array(), $options); | |
75 | } | |
76 | ||
77 | return json_decode($out); | |
78 | } | |
79 | ||
80 | private function check_status($status) { | |
81 | switch($status) { | |
82 | case "missing": | |
83 | $response = array( | |
84 | 'status' => $status, | |
85 | 'message' => get_string('error:nosuchuser', 'badges') | |
86 | ); | |
87 | return $response; | |
27806552 YB |
88 | } |
89 | } | |
90 | ||
e2805314 | 91 | public function get_collections() { |
27806552 YB |
92 | $json = $this->curl_request('user', $this->email); |
93 | if (isset($json->status)) { | |
94 | if ($json->status != 'okay') { | |
95 | return $this->check_status($json->status); | |
96 | } else { | |
97 | $this->backpackuid = $json->userId; | |
98 | return $this->curl_request('groups'); | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
e2805314 | 103 | public function get_badges($collection) { |
27806552 YB |
104 | $json = $this->curl_request('user', $this->email); |
105 | if (isset($json->status)) { | |
106 | if ($json->status != 'okay') { | |
107 | return $this->check_status($json->status); | |
108 | } else { | |
e2805314 | 109 | return $this->curl_request('badges', $collection); |
27806552 YB |
110 | } |
111 | } | |
112 | } | |
113 | ||
114 | public function get_url() { | |
115 | return $this->backpack; | |
116 | } | |
117 | } |