Skip to content

Commit

Permalink
MDL-59198 lti: Make params LTI2 compatible
Browse files Browse the repository at this point in the history
LTI2 recommends to include both upper case and lower case of custom params.
This patch makes our implementation confer to that standard.
  • Loading branch information
ankitagarwal committed Aug 29, 2017
1 parent 27466d7 commit e4f7cfe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 9 additions & 6 deletions mod/lti/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ function lti_get_enabled_capabilities($tool) {
* @param string $customstr String containing the parameters
* @param boolean $islti2 True if an LTI 2 tool is being launched
*
* @return Array of custom parameters
* @return array of custom parameters
*/
function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
$customstr = str_replace("\r\n", "\n", $customstr);
Expand All @@ -1179,11 +1179,12 @@ function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $is
continue;
}
$key = trim(core_text::substr($line, 0, $pos));
$key = lti_map_keyname($key, false);
$val = trim(core_text::substr($line, $pos + 1, strlen($line)));
$val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
$key2 = lti_map_keyname($key);
$retval['custom_'.$key2] = $val;
if ($islti2 && ($key != $key2)) {
if ($key != $key2) {
$retval['custom_'.$key] = $val;
}
}
Expand Down Expand Up @@ -1269,14 +1270,16 @@ function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2)
* Used for building the names of the different custom parameters
*
* @param string $key Parameter name
*
* @param bool $tolower Do we want to convert the key into lower case?
* @return string Processed name
*/
function lti_map_keyname($key) {
function lti_map_keyname($key, $tolower = true) {
$newkey = "";
$key = core_text::strtolower(trim($key));
if ($tolower) {
$key = core_text::strtolower(trim($key));
}
foreach (str_split($key) as $ch) {
if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') || (!$tolower && ($ch >= 'A' && $ch <= 'Z'))) {
$newkey .= $ch;
} else {
$newkey .= '_';
Expand Down
10 changes: 8 additions & 2 deletions mod/lti/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ public function test_split_custom_parameters() {
$this->assertEquals(lti_split_custom_parameters(null, $tool, array(), "x=1\ny=2", false),
array('custom_x' => '1', 'custom_y' => '2'));

// Check params with caps.
$this->assertEquals(lti_split_custom_parameters(null, $tool, array(), "X=1", false),
array('custom_x' => '1', 'custom_X' => '1'));

// Removed repeat of previous test with a semicolon separator.

$this->assertEquals(lti_split_custom_parameters(null, $tool, array(), 'Review:Chapter=1.2.56', false),
array('custom_review_chapter' => '1.2.56'));
array('custom_review_chapter' => '1.2.56', 'custom_Review_Chapter' => '1.2.56'));

$this->assertEquals(lti_split_custom_parameters(null, $tool, array(),
'Complex!@#$^*(){}[]KEY=Complex!@#$^*;(){}[]½Value', false),
array('custom_complex____________key' => 'Complex!@#$^*;(){}[]½Value'));
array(
'custom_complex____________key' => 'Complex!@#$^*;(){}[]½Value',
'custom_Complex____________KEY' => 'Complex!@#$^*;(){}[]½Value'));

// Test custom parameter that returns $USER property.
$user = $this->getDataGenerator()->create_user(array('middlename' => 'SOMETHING'));
Expand Down

0 comments on commit e4f7cfe

Please sign in to comment.