Skip to content

Commit 0b5ba8a

Browse files
committed
All hardcoded text replaced in cmi5connector
1 parent 6781f93 commit 0b5ba8a

File tree

4 files changed

+94
-70
lines changed

4 files changed

+94
-70
lines changed

classes/local/cmi5_connectors.php

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');
3030
class cmi5_connectors {
3131

32+
const CMI5LAUNCH_PLAYER_TENANT_URL = "/api/v1/tenant";
33+
const CMI5LAUNCH_PLAYER_COURSE_URL = "/api/v1/course";
34+
const CMI5LAUNCH_PLAYER_REGISTRATION_URL = "/api/v1/registration";
35+
const CMI5LAUNCH_PLAYER_AUTH_URL = "/api/v1/auth";
36+
const CMI5LAUNCH_LAUNCH_URL = "/launch-url/";
37+
const CMI5LAUNCH_PLAYER_SESSION_URL = "/api/v1/session/";
3238
public function cmi5launch_get_create_tenant() {
3339
return [$this, 'cmi5launch_create_tenant'];
3440
}
@@ -79,7 +85,7 @@ public function cmi5launch_create_course($id, $tenanttoken, $filename) {
7985
$settings = cmi5launch_settings($id);
8086

8187
// Build URL to import course to.
82-
$url = $settings['cmi5launchplayerurl'] . "/api/v1/course";
88+
$url = $settings['cmi5launchplayerurl'] . self::CMI5LAUNCH_PLAYER_COURSE_URL;
8389

8490
// To determine the headers.
8591
$filetype = "zip";
@@ -94,24 +100,20 @@ public function cmi5launch_create_course($id, $tenanttoken, $filename) {
94100
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
95101
try {
96102
// Check result and display message if not 200.
97-
$resulttest = $this->cmi5launch_connectors_error_message($result, "creating the course");
103+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchcourseerror', 'cmi5launch'));
98104

99-
// echo "Is it resulting?";
100105
if ($resulttest == true) {
101106
// Return an array with course info.
102107
return $result;
103-
// I think this is the problem, it is coming back and throwing another error! I thought it would stop... do I need a kill in the error message hander?
104-
// Its throwing BOB??? The third path is executing, THATS the problem!
105-
// either way though, shouldn't the error funtion have ITS own test? like,
106-
// what we need to test here is is resulttrue is true or not
108+
107109
} else {
108110
// This should never be false, it should throw an exception if it is, so we can just return the result
109111
// But catch all else that miht go wrong
110-
throw new playerException("creating the course.");
112+
throw new playerException(get_string('cmi5launchcourseerror', 'cmi5launch'));
111113
}
112114
}// catch all else that might go wrong
113115
catch (\Throwable $e){
114-
throw new playerException("creating the course" . $e);
116+
throw new playerException(get_string('cmi5launchcourseerror', 'cmi5launch'). $e);
115117
}
116118

117119
}
@@ -135,7 +137,7 @@ public function cmi5launch_create_tenant($newtenantname) {
135137
$password = $settings['cmi5launchbasepass'];
136138

137139
// Build URL for launch URL request.
138-
$url = $playerurl . "/api/v1/tenant";
140+
$url = $playerurl . self::CMI5LAUNCH_PLAYER_TENANT_URL;
139141

140142
// The body of the request must be made as array first.
141143
$data = array(
@@ -162,12 +164,12 @@ public function cmi5launch_create_tenant($newtenantname) {
162164
return $result;
163165
} else {
164166

165-
throw new playerException("creating the tenant.");
167+
throw new playerException(get_string('cmi5launchtenanterror', 'cmi5launch'));
166168
}
167169
}// catch all else that might go wrong
168170
catch (\Throwable $e){
169171

170-
throw new playerException("Uncaught error creating the tenant" . $e);
172+
throw new playerException(get_string('cmi5launchtenantuncaughterror', 'cmi5launch') . $e);
171173
}
172174

173175
}
@@ -195,20 +197,20 @@ public function cmi5launch_retrieve_registration_with_get($registration, $id) {
195197
$result = $this->cmi5launch_send_request_to_cmi5_player_get('cmi5launch_stream_and_send', $token, $url);
196198

197199
// Check result and display message if not 200.
198-
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the registration");
200+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchregistrationerror', 'cmi5launch'));
199201

200202
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
201203
try {
202204
if ($resulttest == true) {
203205
return $result;
204206
} else {
205207

206-
throw new playerException("retrieving the registration information.");
208+
throw new playerException(get_string('cmi5launchregistrationinfoerror', 'cmi5launch'));
207209
}
208210
}// catch all else that might go wrong
209211
catch (\Throwable $e){
210212

211-
throw new playerException("Uncaught error retrieving the registration information." . $e);
213+
throw new playerException( get_string('cmi5launchregistrationuncaughterror', 'cmi5launch'). $e);
212214
}
213215

214216
}
@@ -234,7 +236,7 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
234236
global $CFG;
235237

236238
// Build URL for launch URL request.
237-
$url = $playerurl . "/api/v1/registration";
239+
$url = $playerurl . self::CMI5LAUNCH_PLAYER_REGISTRATION_URL;
238240

239241
// The body of the request must be made as array first.
240242
$data = array(
@@ -256,7 +258,7 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
256258
$result = $this->cmi5launch_send_request_to_cmi5_player_post('cmi5launch_stream_and_send', $data, $url, $filetype, $token);
257259

258260
// Check result and display message if not 200.
259-
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the registration");
261+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchregistrationerror', 'cmi5launch'));
260262

261263
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
262264
try {
@@ -270,12 +272,12 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
270272

271273
return $registration;
272274
} else {
273-
throw new playerException("retrieving the registration information.");
275+
throw new playerException(get_string('cmi5launchregistrationinfoerror', 'cmi5launch'));
274276
}
275277
}// catch all else that might go wrong
276278
catch (\Throwable $e){
277279

278-
throw new playerException("Uncaught error retrieving the registration information." . $e);
280+
throw new playerException(get_string('cmi5launchregistrationuncaughterror', 'cmi5launch'). $e);
279281
}
280282

281283
}
@@ -290,10 +292,6 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
290292
*/
291293
public function cmi5launch_retrieve_token($audience, $tenantid) {
292294

293-
// Honestly the params can be rabbbed through settings right? So I thinks we can change this whole func.
294-
// but if it is called, will it need to go tooo secret back page?
295-
// and can we make it same page, like if pthere is no prompt? which is fdiff then null right? Or maybe another page just to be certain.
296-
297295
global $CFG, $cmi5launchid;
298296

299297
$settings = cmi5launch_settings($cmi5launchid);
@@ -304,7 +302,7 @@ public function cmi5launch_retrieve_token($audience, $tenantid) {
304302
$password = $settings['cmi5launchbasepass'];
305303

306304
// Build URL for launch URL request.
307-
$url = $playerurl . "/api/v1/auth";
305+
$url = $playerurl . self::CMI5LAUNCH_PLAYER_AUTH_URL;
308306

309307
// The body of the request must be made as array first.
310308
$data = array(
@@ -320,7 +318,7 @@ public function cmi5launch_retrieve_token($audience, $tenantid) {
320318
$result = $this->cmi5launch_send_request_to_cmi5_player_post('cmi5launch_stream_and_send', $data, $url, $filetype, $username, $password);
321319

322320
// Check result and display message if not 200.
323-
$resulttest = $this->cmi5launch_connectors_error_message($result, 'retrieving the tenant token.');
321+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchtokenerror', 'cmi5launch'));
324322

325323
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
326324
try {
@@ -332,11 +330,11 @@ public function cmi5launch_retrieve_token($audience, $tenantid) {
332330
return $token;
333331

334332
} else {
335-
throw new playerException("retrieving the tenant token.");
333+
throw new playerException(get_string('cmi5launchtokenerror', 'cmi5launch'));
336334
}
337335
}// catch all else that might go wrong
338336
catch (\Throwable $e){
339-
throw new playerException("Uncaught error retrieving the tenant token." . $e);
337+
throw new playerException(get_string('cmi5launchtokenuncaughtterror', 'cmi5launch'). $e);
340338
}
341339
}
342340

@@ -367,7 +365,7 @@ public function cmi5launch_retrieve_url($id, $auindex) {
367365

368366

369367
// Build URL for launch URL request.
370-
$url = $playerurl . "/api/v1/course/" . $courseid ."/launch-url/" . $auindex;
368+
$url = $playerurl . self::CMI5LAUNCH_PLAYER_COURSE_URL . "/" . $courseid . self::CMI5LAUNCH_LAUNCH_URL . $auindex;
371369

372370
$data = array(
373371
'actor' => array(
@@ -389,7 +387,7 @@ public function cmi5launch_retrieve_url($id, $auindex) {
389387
$result = $this->cmi5launch_send_request_to_cmi5_player_post('cmi5launch_stream_and_send', $data, $url, $filetype, $token);
390388

391389
// Check result and display message if not 200.
392-
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the launch url from player.");
390+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchurlerror', 'cmi5launch'));
393391

394392
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
395393
try {
@@ -399,11 +397,11 @@ public function cmi5launch_retrieve_url($id, $auindex) {
399397

400398
return $urldecoded;
401399
} else {
402-
throw new playerException("retrieving the launch url from player.");
400+
throw new playerException(get_string('cmi5launchurlerror', 'cmi5launch'));
403401
}
404402
}// catch all else that might go wrong
405403
catch (\Throwable $e){
406-
throw new playerException("Uncaught error retrieving the launch url from player." . $e);
404+
throw new playerException(get_string('cmi5launchurluncaughterror', 'cmi5launch') . $e);
407405
}
408406

409407

@@ -509,7 +507,7 @@ public function cmi5launch_send_request_to_cmi5_player_post($cmi5launch_stream_a
509507
restore_exception_handler();
510508
restore_error_handler();
511509
//
512-
throw new playerException("communicating with player, sending or crafting a POST request: " . $e);
510+
throw new playerException( get_string('cmi5launchposterror', 'cmi5launch') . $e);
513511
}
514512

515513
}
@@ -547,7 +545,7 @@ public function cmi5launch_send_request_to_cmi5_player_get($cmi5launch_stream_an
547545

548546
} catch (\Throwable $e) {
549547
// echo" are we here?";
550-
throw new playerException("communicating with player, sending or crafting a GET request: " . $e);
548+
throw new playerException(get_string('cmi5launchgeterror', 'cmi5launch'). $e);
551549
}
552550
}
553551

@@ -567,13 +565,13 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
567565
$playerurl = $settings['cmi5launchplayerurl'];
568566

569567
// Build URL for launch URL request.
570-
$url = $playerurl . "/api/v1/session/" . $sessionid;
568+
$url = $playerurl . self::CMI5LAUNCH_PLAYER_SESSION_URL . $sessionid;
571569

572570
// Sends the stream to the specified URL.
573571
$result = $this->cmi5launch_send_request_to_cmi5_player_get('cmi5launch_stream_and_send', $token, $url);
574572

575573
// Check result and display message if not 200.
576-
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the session information.");
574+
$resulttest = $this->cmi5launch_connectors_error_message($result, get_string('cmi5launchsessioninfoerror', 'cmi5launch') );
577575

578576
// Now this will never return false, it will throw an exception if it fails, so we can just return the result
579577
try {
@@ -582,12 +580,12 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
582580
return $result;
583581

584582
} else {
585-
throw new playerException("retrieving the session information.");
583+
throw new playerException(get_string('cmi5launchsessioninfoerror', 'cmi5launch'));
586584
}
587585
}// catch all else that might go wrong
588586
catch (\Throwable $e){
589587

590-
throw new playerException("Uncaught error retrieving the session information." . $e);
588+
throw new playerException( get_string("cmi5launchsessioninfouncaughterror", 'cmi5launch') . $e);
591589
}
592590
}
593591

@@ -616,14 +614,14 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) {
616614
if ($resulttest === false ){
617615

618616

619-
$errormessage = $type . " CMI5 Player is not communicating. Is it running?";
617+
$errormessage = $type . get_string('cmi5launchcommerror', 'cmi5launch');
620618

621619
throw new playerException($errormessage);
622620
}
623621
else if( array_key_exists("statusCode", $resulttest) && $resulttest["statusCode"] != 200) {
624622

625623

626-
$errormessage = $type . " CMI5 Player returned " . $resulttest["statusCode"] . " error. With message '"
624+
$errormessage = $type . get_string('cmi5launchreturned', 'cmi5launch'). $resulttest["statusCode"] . get_string('cmi5launchwith', 'cmi5launch') . " '"
627625
. $resulttest["message"] . "'." ;
628626

629627
// echo"whatt is error messae before throwing::: " . $errormessage;

cmi5PHP/tests/cmi5_connectorsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function get_content() {
163163
};
164164
$test = false;
165165
// Expected exceptions
166-
$exceptionmessage = "Player communication error. Something went wrong creating the course CMI5 Player returned 404 error. With message 'testmessage'." ;
166+
$exceptionmessage = "Player communication error. Something went wrong creating the course. CMI5 Player returned 404 error. With message 'testmessage'." ;
167167

168168
// Mock a cmi5 connector object but only stub ONE method, as we want to test the other methods.
169169
// Create a mock of the send_request class as we don't actually want
@@ -235,7 +235,7 @@ public function get_content() {
235235
// The string just needs to be returned as is. We do expect create_tenant to only call this once.
236236
$csc->expects($this->once())
237237
->method('cmi5launch_connectors_error_message')
238-
->with($errormessage, 'creating the course') // for tomorrow, is thi failing because with only evaluates strings? Like do we need to string the array out>
238+
->with($errormessage, 'creating the course.') // for tomorrow, is thi failing because with only evaluates strings? Like do we need to string the array out>
239239
// IRL it returns something that needs to be json decoded, so lets pass somethin that is encoded>
240240
->willReturn(false);
241241

@@ -485,7 +485,7 @@ public function testcmi5launch_retrieve_registration_with_get_fail()
485485
->willReturn($errormessage);
486486

487487
// Expected exceptions
488-
$exceptionmessage = "Player communication error. Something went wrong retrieving the registration CMI5 Player returned 404 error. With message 'testmessage'." ;
488+
$exceptionmessage = "Player communication error. Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'." ;
489489

490490
// Expected exceptions and messages
491491
$this->expectExceptionMessage($exceptionmessage);
@@ -533,7 +533,7 @@ public function testcmi5launch_retrieve_registration_with_get_fail_2()
533533

534534
$mockedclass->expects($this->once())
535535
->method('cmi5launch_connectors_error_message')
536-
->with($errormessage, "retrieving the registration")
536+
->with($errormessage, "retrieving the registration.")
537537
->willReturn(false);
538538

539539
// Expected exceptions
@@ -649,7 +649,7 @@ public function testcmi5launch_retrieve_registration_with_post_fail_2()
649649

650650
$mockedclass->expects($this->once())
651651
->method('cmi5launch_connectors_error_message')
652-
->with($errormessage, "retrieving the registration")
652+
->with($errormessage, "retrieving the registration.")
653653
->willReturn(false);
654654

655655
// Expected exceptions
@@ -705,7 +705,7 @@ public function testcmi5launch_retrieve_registration_with_post_fail()
705705
->willReturn($errormessage);
706706

707707
// Expected exceptions
708-
$exceptionmessage = "Player communication error. Something went wrong retrieving the registration CMI5 Player returned 404 error. With message 'testmessage'" ;
708+
$exceptionmessage = "Player communication error. Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'" ;
709709

710710
// Expected exceptions and messages
711711
$this->expectExceptionMessage($exceptionmessage);

lang/en/cmi5launch.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,47 @@
132132
";
133133

134134
// Info for settings warnings.
135-
$string['cmi5launchtenantname_warning'] = '<b>Tenant name is ';
136-
$string['cmi5launchtenantid_warning'] = '. Tenant ID is ';
135+
$string['cmi5launchtenantnameis'] = '<b>Tenant name is ';
136+
$string['cmi5launchtenantidis'] = '. Tenant ID is ';
137137
$string['cmi5launchtenant_warning'] = "</b><div><br> The tenant name and ID have been set. They cannot be changed without causing problems with existing cmi5 launch link activities. To change, plugin must be uninstalled and reinstalled.</div> <div><br></div>";
138138

139+
// Token generation button.
140+
$string['cmi5launchlinktotoken'] = "</br>
141+
<p id=name >
142+
<div class='input-group rounded'>
143+
<button class='btn btn-secondary' type='reset' name='tokenbutton' onclick='totokenpage()'>
144+
<span class='button-label'>Generate new bearer token</span>
145+
</button>
146+
</div>
147+
</p>
148+
";
149+
150+
// Lines for error messages.
151+
152+
153+
154+
155+
// cmi5_connectors
156+
$string['cmi5launchcommerror'] = " CMI5 Player is not communicating. Is it running?";
157+
$string['cmi5launchreturned'] = " CMI5 Player returned ";
158+
$string['cmi5launchwith'] = " error. With message";
159+
160+
$string['cmi5launchcourseerror'] = "creating the course.";
161+
$string['cmi5launchtenanterror'] = "creating the tenant.";
162+
$string['cmi5launchtenantuncaughterror'] = "creating the tenant.";
163+
$string['cmi5launchregistrationerror'] = "retrieving the registration.";
164+
$string['cmi5launchregistrationinfoerror'] = "retrieving the registration information.";
165+
$string['cmi5launchregistrationuncaughterror'] = "Uncaught error retrieving the registration information.";
166+
$string['cmi5launchtokenerror'] = "retrieving the tenant token.";
167+
$string['cmi5launchtokenuncaughterror'] = "Uncaught error retrieving the tenant token.";
168+
$string['cmi5launchurlerror'] = "retrieving the launch url from player.";
169+
$string['cmi5launchurluncaughterror'] = "Uncaught error retrieving the launch url from player.";
170+
$string['cmi5launchposterror'] = "communicating with player, sending or crafting a POST request: ";
171+
$string['cmi5launchgeterror'] = "communicating with player, sending or crafting a GET request: ";
172+
$string['cmi5launchsessioninfoerror'] = "retrieving the session information.";
173+
$string['cmi5launchsessioninfouncaughterror'] = "Uncaught error retrieving the session information.";
174+
175+
139176
// Grading info - MB.
140177
// Headers.
141178
$string['defaultgradesettings'] = 'Default values for cmi5 launch link activity grades';

0 commit comments

Comments
 (0)