-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
495 lines (297 loc) · 13.1 KB
/
index.php
File metadata and controls
495 lines (297 loc) · 13.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* Plugin Name: Absorb API Client
* Description: Absorb API Client Plugin. This plugin allows WP to interface with the Absorb LMS to get course information in order to create links to courses within WordPress/WooCommerce. Then allowing a logged in user, after purchasing courses, to processe the Single Sign On to Absorb from these links using lightSAML.
* Version: 0.2
* Author: Greg Pymm
* Author URI: http://pymm.com
* License: No license available at this time.
*
*
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if( ! class_exists('AbsorbAPIClientPlugin') ) :
class AbsorbAPIClientPlugin {
private $message = null;
private $debug = false;
private $api_client;
private $order_id;
private $products_array;
private $absorb_private_key;
private $absorb_admin_username;
private $absorb_admin_password;
private $absorb_dept_id;
private $BASE_URL;
function __construct($options)
{
$this->absorb_private_key = $options['absorb_private_key'];
$this->absorb_admin_username = $options['absorb_admin_username'];
$this->absorb_admin_password = $options['absorb_admin_password'];
$this->absorb_dept_id = $options['absorb_dept_id'];
$this->BASE_URL = $options['base_url'];
}
function initialize (){
// Require
require_once 'src/AbsorbAPIClient.php';
// Any WOOCommerce action can be used here
// add_action( 'woocommerce_new_order', array($this, 'process') );
add_action( 'woocommerce_order_status_completed', array($this, 'process') );
}
function message($message){
$this->message = $message;
error_log( $this->message );
if( $this->debug) echo '<br>' . $this->message . '<br>';
}
function process($order_id)
{
$this -> order_id = $order_id;
$this->api_client = new AbsorbAPIClient(array(
'absorb_private_key' => $this->absorb_private_key,
'absorb_admin_username' => $this->absorb_admin_username,
'absorb_admin_password' => $this->absorb_admin_password,
'base_url' => $this->BASE_URL
));
$this->api_client -> register();
// After Checkout
// Register API client.
// User Setup
// -- Does a user with this email address already exists?
// -- Yes
// -- Retrieve User ID from LMS by email address.
// -- save user ID
// -- NO
// -- -- Create new user with full info from WP and DEPT ID if exists.
// -- Retrieve User ID from LMS response
// -- Save user ID
$this->setup_user();
// Continue
//
// Collect all WP Product IDs from Cart
//
// $this->get_products();
// For each product
// -- Retrieve the External ID and LMS Dept ID.
// -- Retrieve course ID from LMS by External ID
// -- Do we have a Dept ID?
// -- YES
// -- -- Udpate User with Dept ID and User ID.
// -- NO
// -- -- Continue
// $this->process_enrollments();
// -- Enroll User in this course with Course ID and User ID.
//
// Repeat
// Notes:
// Code is cleaned up.
//The Plugin now handles multiple Products/Courses
// Everything has been made more modular.
// It is possible for there to be multiple users with the same email address in the LMS. I added a rough validation for this. It will fail if there is, and report and error.
// It is possible for there to be multiple Courses with the same External ID in the LMS. I added a rough validation for this. It will fail if there is, and report and error.
// When created a new user a Dept ID is required, so I am using the Non-Member ID. Currently hard-coded in the plugin.
// Added a the ability to turn debugging on and off. set debug to "true" or false is vars.
// LMS Username will be the email address.
// I have set the WP User ID to the User External ID in the LMS.
// In Wordpress the last name field can be blank, but is required for LMS.
// Added a validation for non-logged in user.
// Changed the ExternalID for users in the LMS to "{WP User ID}::{WP User email}
//
// TODO
// Externalize credentials and certificate and privatekeys
}
function setup_user(){
$api_client = $this -> api_client;
$order = new WC_Order( $this -> order_id );
$current_user = $order -> get_user();
// Get current logged-in WP user <- Fails when admin changes status
// $current_user = wp_get_current_user();
$this->message ('User from Order:' . $current_user->user_email );
if($this->debug) echo "<b>Current logged in user email: </b><br>" . $current_user->user_email;
if($this->debug) echo "<br><b>Current logged in user ID: </b><br>" . $current_user->ID;
if($this->debug) echo "<br>---------<br>";
// var_dump($current_user);
if(!empty($current_user->user_email)){
$user = $api_client ->user_exists(array(
'email' => $current_user->user_email)
);
}else{
$this->message ('User not logged in.');
die();
}
// var_dump($user);
/* Validation
* Check for Mulitple users with this email address
*/
if ( count($user) === 1 ) {
$user = $user[0];
}else if ( count($user) > 1 ){
$this->message ('Mutliple Users exist with that email address');
die();
}
if($this->debug) echo "<b>Does user exist in LMS? </b><br>";
if($this->debug) echo "<b>LMS User Data:</b><br>";
if($this->debug) var_dump($user);
if( !empty($user->Id ) ){
if($this->debug) echo "<br><b>Yes. Absorb User ID returned from LMS: </b><br>";
if($this->debug) echo ( $user -> Id);
// Save User ID
$api_client -> lms_user = $user;
}else{
if($this->debug) echo "<br><b>No. User does not exist, create user in LMS: </b><br><br>";
$absorb_dept_id = $this -> absorb_dept_id ; //'XXXXXX-786d-4adf-83b5-70179b27a437'; // Non Member Department a337f2f2-49c1-4342-8458-b7cb71e44109
$last_name = $current_user->user_lastname;
$first_name = $current_user->user_firstname;
$user_info = (object) array(
"DepartmentId" => $absorb_dept_id,
"FirstName" => empty($first_name) ? 'FIRSTNAME' : $first_name,
"LastName" => empty($last_name) ? 'LASTNAME' : $last_name,
"Username" => $current_user->user_email,
"Password" => $current_user->user_pass,
"EmailAddress" => $current_user->user_email,
"ExternalId" => $current_user->ID . '::' . $current_user->user_email
);
$new_user = $api_client -> create_user( $user_info );
if( !$new_user->Id ){
$this->message ('Problem Creating a new user in the LMS:' . $this -> absorb_dept_id );
if($this->debug) var_dump($new_user);
}else{
// Save New User ID
$user_info -> Id = $new_user -> Id;
// $api_client -> lms_user_id = $new_user->Id;
$api_client -> lms_user = $user_info;
if($this->debug) var_dump( $api_client -> lms_user );
$this->message ('User Created in LMS:' . $this -> absorb_dept_id );
}
}
if($this->debug) echo("<br>---------<br><br>");
}
function get_products(){
$products_array = array();
$cart = new WC_Cart();
$cart -> get_cart_from_session();
$cart_items = $cart->get_cart();
if($this->debug) echo "<b>Our Product Data from Cart: </b><br><br>";
foreach ($cart_items as $key => $item) {
$product = $item['data'];
$product_id = $item['product_id'];
$external_id = get_post_meta($product_id, '_external_id', true);
$lms_dept_id = get_post_meta($product_id, '_lms_department_id', true);
$course_name = $product -> post -> post_title;
array_push($products_array, array(
'product_id' => $product_id,
'external_id' => $external_id,
'dept_id' => $lms_dept_id,
'course_name' => $course_name,
));
}
if($this->debug) var_dump($products_array);
$this->products_array = $products_array;
}
function process_enrollments(){
$api_client = $this -> api_client;
$products_array = $this->products_array;
foreach ($products_array as $key => $course) {
$ext_id = $course['external_id'];
$dept_id = $course['dept_id'];
$course_name = $course['course_name'];
if($this->debug){
echo("<br>---------<br>");
echo "<b>Enroll User in: </b><br>" . $course_name. "<br>";
echo "<b>Absorb Course External ID from Purchased course in WP: </b><br>" . $ext_id;
echo "<br><b>Absorb Department ID from Purchased course in WP: </b><br>" . $dept_id;
echo("<br>---------<br>");
echo "<b>Absorb Course by External ID from LMS: </b><br>" ;
}
if(!empty( $ext_id )){
// * API Call - GET COURSE
$course_by_ext_id = $api_client ->get_course( array( 'external_id' => $ext_id ));
}else{
$this->message( "There is no External ID for this product in WP. User will no be enrolled in this course." );
$course_by_ext_id = null;
if($this->debug) echo("<br>---------<br>");
continue;
}
if($this->debug) var_dump( $course_by_ext_id );
if($this->debug) echo("<br>---------<br>");
/* Validation
* Check for Mulitple courses with this External ID
*/
if ( count($course_by_ext_id) === 1 ) {
$course_by_ext_id = $course_by_ext_id[0];
}else if ( count($course_by_ext_id) > 1 ){
$this->message ('Mutliple Courses exist with that External ID');
die();
}
$lms_id_from_ext = $course_by_ext_id->Id;
if($this->debug) echo "<b>Absorb Course ID by External ID from LMS: </b><br>" . $lms_id_from_ext ;
// * Update the User if this course has a Department ID (Member)
if($this->debug) echo "<br><b>Update Dept ID of User in LMS: </b><br>" . $dept_id ;
if(!empty( $dept_id )){
// * API Call - UPDATE USER
$updated_user = $api_client -> update_user(array(
"dept_id" => $dept_id,
));
if($this->debug) echo "<br><b>Updated User Data: </b><br>" ;
if($this->debug) var_dump( $updated_user );
}else{
$this->message( "There is no Department ID for this product in WP. User will not be updated" );
}
// * Enroll User in Course
if($this->debug) echo("<br>---------<br><br>");
// * API Call - ENROLL
$enrollment = $api_client -> enroll(array(
'course_id' => $lms_id_from_ext,
));
if($this->debug) echo "<b>Enroll User In Course in LMS: </b><br>";
if($this->debug) var_dump( $enrollment );
if($this->debug) echo "<br><b>WP Purchased Product Name: </b>" . $course_by_ext_id -> Name . "<br>";
if($this->debug) echo "<b>LMS Course Name: </b> " . $course_name . "<br>";
if($this->debug) echo("<br>---------<br><br>");
$this -> create_SSO_course_link( $lms_id_from_ext, $course_by_ext_id -> Name );
} // end foreach
}
function create_SSO_course_link( $lms_id, $product_name ){
$api_client = empty( $this -> api_client ) ? new AbsorbAPIClient() : $this->api_client;
$current_user = wp_get_current_user();
if($this->debug) echo "<b>Absorb Single Sign On to LMS from WP: </b><br>";
// Format and Sign the SAML
$saml_xml = $api_client -> CreateSamlResponse( array('email' => $current_user->user_email ) );
// Create link
echo '<form action="https://ifm.myabsorb.com/account/saml" method="POST" id="samlform">
Signed SAML <br><textarea name="samlresponse" id="samlresponse" rows="2" cols="80">' . base64_encode($saml_xml) . '</textarea><br>
<input type="hidden" name="relaystate" id="relaystate" value="https://ifm.myabsorb.com/account/saml?CourseId=' . $lms_id . '" size="100">
<!-- input type="submit" value="Submit" -->
<b>This link submits the above SSO Form and logs in the user: </b><br>
<a href="javascript:{}" onclick="document.getElementById(\'samlform\').submit(); return false;">' . $product_name . '</a>
</form>';
echo "<b>Alternatively Build course link from Course ID if user is already logged in: </b><br>";
$course_url = 'https://ifm.myabsorb.com/#/courses/course/' . $lms_id;
echo("<a href='".$course_url."' target='_blank'>" . $product_name . "</a><br><br>");
// var_dump($auth_token);
if($this->debug) echo("<br><br>---------<br>");
// error_reporting(E_ERROR | E_PARSE);
}
}
/*
* $absorb_api_client_plugin
*
*
*/
function _absorb_api_client_plugin() {
global $absorb_api_client_plugin;
if( !isset($absorb_api_client_plugin) ) {
// error_log('new absorb_api_client_plugin');
$absorb_api_client_plugin = new AbsorbAPIClientPlugin(array(
'absorb_private_key' => (defined('ABSORB_PRIV_KEY')) ? constant('ABSORB_PRIV_KEY') : null,
'absorb_admin_username' => defined('ABSORB_USER') ? constant('ABSORB_USER') : null,
'absorb_admin_password' => defined('ABSORB_PASS') ? constant('ABSORB_PASS') : null,
'absorb_dept_id' => defined('ABSORB_DEPT_ID') ? constant('ABSORB_DEPT_ID') : null,
'base_url' => defined('ABSORB_URL') ? constant('ABSORB_URL') : null
));
$absorb_api_client_plugin->initialize();
}
return $absorb_api_client_plugin;
}
// initialize
_absorb_api_client_plugin();
endif; // Does class exists
?>