forked from MultinetInteractive/eduadmin-api-phpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheduadmin-api-client.php
230 lines (205 loc) · 8.39 KB
/
eduadmin-api-client.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
if ( ! class_exists( 'EduAdmin_OData_Client' ) ) {
/**
* Class EduAdmin_OData_Client
*/
class EduAdmin_OData_Client {
protected static $_instance = null;
/**
* @var string User Agent used by the REST client
*/
/**
* @var EduAdminToken API Token
*/
public $api_token = null;
/**
* @var array
*/
public $timers;
/**
* @var EduAdminRESTClient
*/
public $rest = null;
/**
* @return EduAdmin_OData_Client
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* @var EduAdmin_ODataHolder
*/
public $OData = null;
/**
* @var EduAdmin_RESTHolder
*/
public $REST = null;
/**
* EduAdmin_OData_Client constructor.
*/
public function __construct() {
$this->timers = array();
$t = $this->start_timer( 'InitRESTClient' );
$this->includes();
$this->stop_timer( $t );
}
/**
* @param string $name Name of the timer
*
* @return string Returns the unique name for the created timer
*/
public function start_timer( $name ) {
$timer_id = count( $this->timers ) + 1;
$this->timers[ $name . '_' . $timer_id ] = microtime( true );
return $name . '_' . $timer_id;
}
/**
* @param string $name The unique name of the timer (Returned from StartTimer)
*/
public function stop_timer( $name ) {
$this->timers[ $name ] = microtime( true ) - $this->timers[ $name ];
}
private function includes() {
include_once 'rest-client.php';
include_once 'odata-client.php';
include_once 'eduadmin-api-classes.php';
/* OData Classes */
include_once 'subclasses/odata/eduadmin-bookings.php';
include_once 'subclasses/odata/eduadmin-categories.php';
include_once 'subclasses/odata/eduadmin-countries.php';
include_once 'subclasses/odata/eduadmin-courselevels.php';
include_once 'subclasses/odata/eduadmin-coursetemplates.php';
include_once 'subclasses/odata/eduadmin-customergroups.php';
include_once 'subclasses/odata/eduadmin-customers.php';
include_once 'subclasses/odata/eduadmin-customfields.php';
include_once 'subclasses/odata/eduadmin-events.php';
include_once 'subclasses/odata/eduadmin-evaluationanswers.php';
include_once 'subclasses/odata/eduadmin-evaluations.php';
include_once 'subclasses/odata/eduadmin-grades.php';
include_once 'subclasses/odata/eduadmin-interestregistrations.php';
include_once 'subclasses/odata/eduadmin-locations.php';
include_once 'subclasses/odata/eduadmin-participants.php';
include_once 'subclasses/odata/eduadmin-paymentterms.php';
include_once 'subclasses/odata/eduadmin-personnel.php';
include_once 'subclasses/odata/eduadmin-persons.php';
include_once 'subclasses/odata/eduadmin-pricenames.php';
include_once 'subclasses/odata/eduadmin-programmebookings.php';
include_once 'subclasses/odata/eduadmin-programmes.php';
include_once 'subclasses/odata/eduadmin-programmestarts.php';
include_once 'subclasses/odata/eduadmin-regions.php';
include_once 'subclasses/odata/eduadmin-reports.php';
include_once 'subclasses/odata/eduadmin-subjects.php';
/* /OData Classes */
/* REST Classes */
include_once 'subclasses/rest/eduadmin-booking.php';
include_once 'subclasses/rest/eduadmin-consent.php';
include_once 'subclasses/rest/eduadmin-coupon.php';
include_once 'subclasses/rest/eduadmin-customer.php';
include_once 'subclasses/rest/eduadmin-event.php';
include_once 'subclasses/rest/eduadmin-interestregistration.php';
include_once 'subclasses/rest/eduadmin-organisation.php';
include_once 'subclasses/rest/eduadmin-participant.php';
include_once 'subclasses/rest/eduadmin-person.php';
include_once 'subclasses/rest/eduadmin-personnel.php';
include_once 'subclasses/rest/eduadmin-programmebooking.php';
include_once 'subclasses/rest/eduadmin-programmestart.php';
include_once 'subclasses/rest/eduadmin-report.php';
/* /REST Classes */
$this->rest = new EduAdminRESTClient();
// Load OData classes
$this->OData = new EduAdmin_ODataHolder();
$this->OData->Bookings = new EduAdmin_OData_Bookings();
$this->OData->Categories = new EduAdmin_OData_Categories();
$this->OData->Countries = new EduAdmin_OData_Countries();
$this->OData->CourseLevels = new EduAdmin_OData_CourseLevels();
$this->OData->CourseTemplates = new EduAdmin_OData_CourseTemplates();
$this->OData->CustomerGroups = new EduAdmin_OData_CustomerGroups();
$this->OData->Customers = new EduAdmin_OData_Customers();
$this->OData->CustomFields = new EduAdmin_OData_CustomFields();
$this->OData->Events = new EduAdmin_OData_Events();
$this->OData->Grades = new EduAdmin_OData_Grades();
$this->OData->InterestRegistrations = new EduAdmin_OData_InterestRegistrations();
$this->OData->Locations = new EduAdmin_OData_Locations();
$this->OData->Participants = new EduAdmin_OData_Participants();
$this->OData->PaymentTerms = new EduAdmin_OData_PaymentTerms();
$this->OData->Personnel = new EduAdmin_OData_Personnel();
$this->OData->Persons = new EduAdmin_OData_Persons();
$this->OData->PriceNames = new EduAdmin_OData_Pricenames();
$this->OData->ProgrammeBookings = new EduAdmin_OData_ProgrammeBookings();
$this->OData->Programmes = new EduAdmin_OData_Programmes();
$this->OData->ProgrammeStarts = new EduAdmin_OData_ProgrammeStarts();
$this->OData->Regions = new EduAdmin_OData_Regions();
$this->OData->Reports = new EduAdmin_OData_Reports();
$this->OData->Subjects = new EduAdmin_OData_Subjects();
// Load REST classes
$this->REST = new EduAdmin_RESTHolder();
$this->REST->Booking = new EduAdmin_REST_Booking();
$this->REST->Consent = new EduAdmin_REST_Consent();
$this->REST->Coupon = new EduAdmin_REST_Coupon();
$this->REST->Customer = new EduAdmin_REST_Customer();
$this->REST->Event = new EduAdmin_REST_Event();
$this->REST->InterestRegistration = new EduAdmin_REST_InterestRegistration();
$this->REST->Organisation = new EduAdmin_REST_Organisation();
$this->REST->Participant = new EduAdmin_REST_Participant();
$this->REST->Person = new EduAdmin_REST_Person();
$this->REST->Personnel = new EduAdmin_REST_Personnel();
$this->REST->ProgrammeBooking = new EduAdmin_REST_ProgrammeBooking();
$this->REST->ProgrammeStart = new EduAdmin_REST_ProgrammeStart();
$this->REST->Report = new EduAdmin_REST_Report();
// If we're inside a WordPress-plugin
if ( function_exists( 'add_action' ) ) {
add_action( 'eduadmin-showtimers', array( $this, 'render_timers' ) );
}
}
public function render_timers() {
echo "<!-- EduAdmin API (OData/REST) Client - Timers -->\n";
$total_value = 0;
foreach ( EDUAPI()->timers as $timer => $value ) {
echo '<!-- ' . $timer . ': ' . round( $value * 1000, 2 ) . "ms -->\n";
$total_value += $value;
}
echo '<!-- EduAdmin Total: ' . round( $total_value * 1000, 2 ) . "ms -->\n";
echo "<!-- /EduAdmin API (OData/REST) Client - Timers -->\n";
}
/**
* @param $api_user
* @param $api_pass
*/
public function SetCredentials( $api_user, $api_pass ) {
EduAdminRESTClient::$api_user = $api_user;
EduAdminRESTClient::$api_pass = $api_pass;
}
/**
* @return EduAdminToken
* @throws Exception
*/
public function GetToken() {
if ( ! isset( EduAdminRESTClient::$api_user ) || ! isset( EduAdminRESTClient::$api_pass ) ) {
throw new Exception( 'You must use SetCredentials before fetching token.' );
}
$result = $this->rest->POST( '/token', array(
'username' => EduAdminRESTClient::$api_user,
'password' => EduAdminRESTClient::$api_pass,
'grant_type' => 'password',
), 'GetToken', false );
$this->api_token = new EduAdminToken( $result );
return $this->api_token;
}
/**
* @param EduAdminToken $token
*/
public function SetToken( $token ) {
$this->api_token = $token;
}
}
/**
* @return EduAdmin_OData_Client
*/
function EDUAPI() {
return EduAdmin_OData_Client::instance();
}
$GLOBALS['edu-api'] = EDUAPI();
}