/** * Test lti_get_url_thumbprint against various URLs */ public function test_lti_get_url_thumbprint() { // Note: trailing and double slash are expected right now. Must evaluate if it must be removed at some point. $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://MOODLE.ORG')); $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://www.moodle.org')); $this->assertEquals('moodle.org/', lti_get_url_thumbprint('https://www.moodle.org')); $this->assertEquals('moodle.org/', lti_get_url_thumbprint('moodle.org')); $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('http://moodle.org/this/is/moodle')); $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('https://moodle.org/this/is/moodle')); $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle')); $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle?foo=bar')); }
function lti_get_best_tool_by_url($url, $tools, $courseid = null) { if (count($tools) === 0) { return null; } $urllower = lti_get_url_thumbprint($url); foreach ($tools as $tool) { $tool->_matchscore = 0; $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl); if ($urllower === $toolbaseurllower) { //100 points for exact thumbprint match $tool->_matchscore += 100; } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) { //50 points if tool thumbprint starts with the base URL thumbprint $tool->_matchscore += 50; } //Prefer course tools over site tools if (!empty($courseid)) { //Minus 10 points for not matching the course id (global tools) if ($tool->course != $courseid) { $tool->_matchscore -= 10; } } } $bestmatch = array_reduce($tools, function($value, $tool) { if ($tool->_matchscore > $value->_matchscore) { return $tool; } else { return $value; } }, (object)array('_matchscore' => -1)); //None of the tools are suitable for this URL if ($bestmatch->_matchscore <= 0) { return null; } return $bestmatch; }