-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.php
201 lines (171 loc) · 5.99 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* ZEND Web Services Plugin for block MHAAIRS
*
* @package block_mhaairs
* @copyright 2014 Itamar Tzadok <itamar@substantialmethods.com>
* @copyright 2013 Moodlerooms inc.
* @author Teresa Hardy <thardy@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Class for the mhaairs connect requests.
*
* @package block_mhaairs
* @copyright 2014 Itamar Tzadok <itamar@substantialmethods.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_mhaairs_connect {
/**
* Returns help urls from Tegrity.
* If not forcing fetch, tries to retrieve from cache.
* Fetched data is cached.
*
* @param bool $forcefetch
* @return bool|array|string
*/
public static function get_help_urls($forcefetch = false) {
// Must have customer number.
if (!$customernumber = self::get_customer_number()) {
return false;
}
// Return cache if exists.
if (!$forcefetch) {
$cached = self::get_cache('help');
if ($cached !== null) {
return $cached;
}
}
// Fetch data from connect.
$resultdata = self::request($customernumber, 'GetHelpLinks');
// Cache.
self::set_cache('help', $resultdata);
return $resultdata;
}
/**
* Returns available service from Tegrity.
* If not forcing fetch, tries to retrieve from cache.
* Fetched data is cached.
*
* @param bool $forcefetch
* @return bool|array|string
*/
public static function get_services($forcefetch = false) {
// Must have customer number.
if (!$customernumber = self::get_customer_number()) {
return false;
}
// Return cache if exists.
if (!$forcefetch) {
$cached = self::get_cache('services');
if ($cached !== null) {
return $cached;
}
}
// Fetch data from connect.
$resultdata = self::request($customernumber, 'GetCustomerAvailableTools');
// Cache.
self::set_cache('services', $resultdata);
return $resultdata;
}
/**
*
*/
protected static function request($customernumber, $endpoint) {
static $zendready = false;
if (!$zendready) {
$dir = get_config('core', 'dirroot');
set_include_path(get_include_path().PATH_SEPARATOR.$dir.'/blocks/mhaairs/lib');
$zendready = true;
}
// @codingStandardsIgnoreStart
require_once('Zend/Json.php');
require_once('Zend/Oauth/Consumer.php');
require_once('Zend/Oauth/Client.php');
// @codingStandardsIgnoreEnd
$baseurl = 'http://mhaairs.tegrity.com/v1/Config/';
$url = $baseurl.$customernumber.'/'.$endpoint;
$aconfig = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
'requestMethod' => Zend_Oauth::GET,
'signatureMethod' => 'HMAC-SHA1',
'consumerKey' => 'SSOConfig',
'consumerSecret' => '3DC9C384'
);
$resultdata = false;
try {
$tacc = new Zend_Oauth_Token_Access();
$client = $tacc->getHttpClient($aconfig, $url);
$client->setMethod(Zend_Oauth_Client::GET);
$client->setEncType(Zend_Oauth_Client::ENC_URLENCODED);
$response = $client->request();
$resultdata = $response->getBody();
// Get content type.
$resulttype = $response->getHeader(Zend_Oauth_Client::CONTENT_TYPE);
// Is this Json encoded data?
if (stripos($resulttype, 'application/json') !== false) {
$resultdata = Zend_Json::decode($resultdata);
}
// By default set the status to the HTTP response status.
$status = $response->getStatus();
$description = $response->getMessage();
if ($status != 200) {
$resultdata = false;
}
} catch (Exception $e) {
$status = (string)$e->getCode();
$description = $e->getMessage();
}
$logmsg = $status . ": " . $description;
// TODO
// There used to be add_to_log here recording the requested endpoint and log message.
// This should be replaced with events system as soon as we decide what should be
// logged.
return $resultdata;
}
/**
*
*/
protected static function get_cache($name) {
$cachename = "block_mhaairs_cache{$name}";
$cached = get_config('core', $cachename);
if ($cached !== false) {
$result = unserialize($cached);
return $result;
}
return null;
}
/**
*
*/
protected static function set_cache($name, $data) {
$cachename = "block_mhaairs_cache{$name}";
$tostore = serialize($data);
set_config($cachename, $tostore);
}
/**
*
*/
protected static function get_customer_number() {
global $CFG;
if (empty($CFG->block_mhaairs_customer_number)) {
return false;
}
return $CFG->block_mhaairs_customer_number;
}
}