@@ -74,13 +74,15 @@ public function cmi5launch_create_course($id, $tenanttoken, $filename) {
74
74
$ databody = $ filename ->get_content ();
75
75
// Sends the stream to the specified URL.
76
76
$ result = $ this ->cmi5launch_send_request_to_cmi5_player_post ($ databody , $ url , $ filetype , $ tenanttoken );
77
-
77
+
78
78
// Check result and display message if not 200.
79
79
$ resulttest = $ this ->cmi5launch_connectors_error_message ($ result , "creating the course " );
80
80
81
81
if ($ resulttest == true ) {
82
82
// Return an array with course info.
83
83
return $ result ;
84
+ } else {
85
+ return false ;
84
86
}
85
87
}
86
88
@@ -118,6 +120,8 @@ public function cmi5launch_create_tenant($urltosend, $username, $password, $newt
118
120
119
121
// Return an array with tenant name and info.
120
122
return $ returnedinfo ;
123
+ } else {
124
+ return false ;
121
125
};
122
126
}
123
127
@@ -149,6 +153,8 @@ public function cmi5launch_retrieve_registration_with_get($registration, $id) {
149
153
if ($ resulttest == true ) {
150
154
151
155
return $ result ;
156
+ } else {
157
+ return false ;
152
158
}
153
159
}
154
160
@@ -207,6 +213,8 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
207
213
$ registration = $ registrationinfo ["code " ];
208
214
209
215
return $ registration ;
216
+ } else {
217
+ return false ;
210
218
}
211
219
}
212
220
@@ -236,11 +244,13 @@ public function cmi5launch_retrieve_token($url, $username, $password, $audience,
236
244
$ result = $ this ->cmi5launch_send_request_to_cmi5_player_post ($ data , $ url , $ filetype , $ username , $ password );
237
245
238
246
// Check result and display message if not 200.
239
- $ resulttest = $ this ->cmi5launch_connectors_error_message ($ result , "retrieving the registration " );
247
+ $ resulttest = $ this ->cmi5launch_connectors_error_message ($ result , "retrieving the token " );
240
248
241
249
if ($ resulttest == true ) {
242
250
243
251
return $ result ;
252
+ } else {
253
+ return false ;
244
254
}
245
255
}
246
256
@@ -269,6 +279,7 @@ public function cmi5launch_retrieve_url($id, $auindex) {
269
279
$ playerurl = $ settings ['cmi5launchplayerurl ' ];
270
280
$ courseid = $ userscourse ->courseid ;
271
281
282
+
272
283
// Build URL for launch URL request.
273
284
$ url = $ playerurl . "/api/v1/course/ " . $ courseid ."/launch-url/ " . $ auindex ;
274
285
@@ -301,6 +312,8 @@ public function cmi5launch_retrieve_url($id, $auindex) {
301
312
$ urldecoded = json_decode ($ result , true );
302
313
303
314
return $ urldecoded ;
315
+ } else {
316
+ return false ;
304
317
}
305
318
}
306
319
@@ -322,13 +335,14 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
322
335
} else if ("json " ) {
323
336
$ contenttype = "application/json \r\n" ;
324
337
}
325
-
338
+
326
339
// If number of args is greater than one it is for retrieving tenant info and args are username and password.
327
340
if (count ($ tokenorpassword ) == 2 ) {
328
341
329
342
$ username = $ tokenorpassword [0 ];
330
343
$ password = $ tokenorpassword [1 ];
331
344
345
+
332
346
// Use key 'http' even if you send the request to https://...
333
347
// There can be multiple headers but as an array under the ONE header.
334
348
// Content(body) must be JSON encoded here, as that is what CMI5 player accepts.
@@ -342,13 +356,11 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
342
356
),
343
357
);
344
358
345
- // The options are here placed into a stream to be sent.
346
- $ context = stream_context_create ($ options );
347
-
348
359
// Sends the stream to the specified URL and stores results.
349
360
// The false is use_include_path, which we dont want in this case, we want to go to the url.
350
- $ result = file_get_contents ( $ url , false , $ context );
361
+ $ result = $ this -> cmi5launch_stream_and_send ( $ url , $ options );
351
362
363
+
352
364
// Else the args are what we need for posting a course.
353
365
} else {
354
366
@@ -370,12 +382,11 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
370
382
),
371
383
);
372
384
373
- // The options are placed into a stream to be sent.
374
- $ context = stream_context_create (($ options ));
385
+
375
386
376
387
// Sends the stream to the specified URL and stores results.
377
388
// The false is use_include_path, which we dont want in this case, we want to go to the url.
378
- $ result = file_get_contents ( $ url , false , $ context );
389
+ $ result = $ this -> cmi5launch_stream_and_send ( $ url , $ options );
379
390
380
391
}
381
392
@@ -404,14 +415,14 @@ public function cmi5launch_send_request_to_cmi5_player_get($token, $url) {
404
415
),
405
416
);
406
417
407
- // The options are here placed into a stream to be sent.
408
- $ context = stream_context_create (($ options ));
409
418
410
- // Sends the stream to the specified URL and stores results. False is to not use_include_path, we want to go to the url.
411
- $ launchresponse = file_get_contents ( $ url , false , $ context );
419
+ // Sends the stream to the specified URL and stores results.
420
+ // The false is use_include_path, which we dont want in this case, we want to go to the url.
421
+ $ launchresponse = $ this ->cmi5launch_stream_and_send ( $ url , $ options );
412
422
413
423
$ sessiondecoded = json_decode ($ launchresponse , true );
414
424
425
+ // Return response.
415
426
return $ sessiondecoded ;
416
427
}
417
428
@@ -442,6 +453,8 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
442
453
if ($ resulttest == true ) {
443
454
444
455
return $ result ;
456
+ } else {
457
+ return false ;
445
458
}
446
459
}
447
460
@@ -454,7 +467,7 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
454
467
* @param string $type - The type missing to be added to the error message.
455
468
* @return bool
456
469
*/
457
- public function cmi5launch_connectors_error_message ($ resulttotest , $ type ) {
470
+ public static function cmi5launch_connectors_error_message ($ resulttotest , $ type ) {
458
471
459
472
// Decode result because if it is not 200 then something went wrong
460
473
// If it's a string, decode it.
@@ -464,12 +477,24 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) {
464
477
$ resulttest = $ resulttotest ;
465
478
}
466
479
467
- if ($ resulttest === false || array_key_exists ("statusCode " , $ resulttest ) && $ resulttest ["statusCode " ] != 200 ) {
480
+ // I think splittin these to return two seperate messages deppennnding on whether player is running is better.
481
+ // Player cannot return an error if not runnin,
482
+ if ($ resulttest === false ){
483
+
484
+ echo "<br> " ;
485
+
486
+ echo "Something went wrong " . $ type . ". CMI5 Player is not communicating. Is it running? " ;
468
487
469
488
echo "<br> " ;
470
489
471
- echo "Something went wrong " . $ type . ". CMI5 Player returned " . var_dump ($ resulttotest );
490
+ return false ;
491
+ }
492
+ else if ( array_key_exists ("statusCode " , $ resulttest ) && $ resulttest ["statusCode " ] != 200 ) {
493
+
494
+ echo "<br> " ;
472
495
496
+ echo "Something went wrong " . $ type . ". CMI5 Player returned " . $ resulttest ["statusCode " ] . " error. With message ' "
497
+ . $ resulttest ["message " ] . "'. " ;
473
498
echo "<br> " ;
474
499
475
500
return false ;
@@ -479,4 +504,27 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) {
479
504
return true ;
480
505
}
481
506
}
507
+
508
+ /**
509
+ * Wrapper function to allow for testing where file_get_contents cannot be overriden.
510
+ * Also has create_stream as this makes a resource which interfers with testing.
511
+ * @param mixed $url - the url to be sent to
512
+ * @param mixed $use_include_path
513
+ * @param mixed $context - the data to be sent
514
+ * @param mixed $offset
515
+ * @param mixed $maxlen
516
+ * @return mixed $result - either a string or false.
517
+ */
518
+ public function cmi5launch_stream_and_send ($ options , $ url ) {
519
+
520
+ // The options are placed into a stream to be sent.
521
+ $ context = stream_context_create (($ options ));
522
+
523
+ // Sends the stream to the specified URL and stores results.
524
+ // The false is use_include_path, which we dont want in this case, we want to go to the url.
525
+ $ result = file_get_contents ( $ url , false , $ context );
526
+
527
+ // Return result.
528
+ return $ result ;
529
+ }
482
530
}
0 commit comments