Skip to content

Commit

Permalink
MDL-67612 mod_lti: tcguid default to siteid
Browse files Browse the repository at this point in the history
  • Loading branch information
claudevervoort authored and stronk7 committed Mar 31, 2020
1 parent afb0af9 commit c986e55
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 19 deletions.
17 changes: 11 additions & 6 deletions mod/lti/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,19 @@ public function definition() {
// Add setup parameters fieldset.
$mform->addElement('header', 'setupoptions', get_string('miscellaneous', 'lti'));

// Adding option to change id that is placed in context_id.
$idoptions = array();
$idoptions[0] = get_string('id', 'lti');
$idoptions[1] = get_string('courseid', 'lti');
$options = array(
LTI_DEFAULT_ORGID_SITEID => get_string('siteid', 'lti'),
LTI_DEFAULT_ORGID_SITEHOST => get_string('sitehost', 'lti'),
);

$mform->addElement('text', 'lti_organizationid', get_string('organizationid', 'lti'));
$mform->addElement('select', 'lti_organizationid_default', get_string('organizationid_default', 'lti'), $options);
$mform->setType('lti_organizationid_default', PARAM_TEXT);
$mform->setDefault('lti_organizationid_default', LTI_DEFAULT_ORGID_SITEID);
$mform->addHelpButton('lti_organizationid_default', 'organizationid_default', 'lti');

$mform->addElement('text', 'lti_organizationid', get_string('organizationidguid', 'lti'));
$mform->setType('lti_organizationid', PARAM_TEXT);
$mform->addHelpButton('lti_organizationid', 'organizationid', 'lti');
$mform->addHelpButton('lti_organizationid', 'organizationidguid', 'lti');

$mform->addElement('text', 'lti_organizationurl', get_string('organizationurl', 'lti'));
$mform->setType('lti_organizationurl', PARAM_URL);
Expand Down
2 changes: 2 additions & 0 deletions mod/lti/lang/en/deprecated.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
leaveblank,mod_lti
organizationid,mod_lti
organizationid_help,mod_lti
18 changes: 14 additions & 4 deletions mod/lti/lang/en/lti.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@
$string['optionalsettings'] = 'Optional settings';
$string['organization'] = 'Organization details';
$string['organizationdescr'] = 'Organization description';
$string['organizationid'] = 'Organization ID';
$string['organizationid_help'] = 'A unique identifier for this Moodle instance. Typically, the DNS name of the organization is used.
If this field is left blank, the host name of this Moodle site will be used as the default value.';
$string['organizationid_default'] = 'Default organization ID';
$string['siteid'] = 'Site ID';
$string['sitehost'] = 'Site hostname';
$string['organizationid_default_help'] = 'Default value to use for Organization ID. Site ID identifies this installation of moodle.';
$string['organizationidguid'] = 'Organization ID';
$string['organizationidguid_help'] = 'A unique identifier for this Moodle instance passed to the tool as the Platform Instance GUID.
If this field is left blank, the default value will be used.';
$string['organizationurl'] = 'Organization URL';
$string['organizationurl_help'] = 'The base URL of this Moodle instance.
Expand Down Expand Up @@ -576,3 +580,9 @@
$string['using_tool_configuration'] = 'Using tool configuration: ';
$string['validurl'] = 'A valid URL must start with http(s)://';
$string['viewsubmissions'] = 'View submissions and grading screen';

// Deprecated since Moodle 3.9.
$string['organizationid'] = 'Organization ID';
$string['organizationid_help'] = 'A unique identifier for this Moodle instance. Typically, the DNS name of the organization is used.
If this field is left blank, the host name of this Moodle site will be used as the default value.';
39 changes: 30 additions & 9 deletions mod/lti/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
define('LTI_VERSION_2', 'LTI-2p0');
define('LTI_VERSION_1P3', '1.3.0');

define('LTI_DEFAULT_ORGID_SITEID', 'SITEID');
define('LTI_DEFAULT_ORGID_SITEHOST', 'SITEHOST');

define('LTI_ACCESS_TOKEN_LIFE', 3600);

// Standard prefix for JWT claims.
Expand Down Expand Up @@ -537,13 +540,6 @@ function lti_get_launch_data($instance, $nonce = '') {
$typeconfig['forcessl'] = '0';
}

// Default the organizationid if not specified.
if (empty($typeconfig['organizationid'])) {
$urlparts = parse_url($CFG->wwwroot);

$typeconfig['organizationid'] = $urlparts['host'];
}

if (isset($tool->toolproxyid)) {
$toolproxy = lti_get_tool_proxy($tool->toolproxyid);
$key = $toolproxy->guid;
Expand Down Expand Up @@ -589,7 +585,7 @@ function lti_get_launch_data($instance, $nonce = '') {
}
}

$orgid = $typeconfig['organizationid'];
$orgid = lti_get_organizationid($typeconfig);

$course = $PAGE->course;
$islti2 = isset($tool->toolproxyid);
Expand Down Expand Up @@ -760,6 +756,25 @@ function lti_build_registration_request($toolproxy) {
return $requestparams;
}


/** get Organization ID using default if no value provided
* @param object $typeconfig
* @return string
*/
function lti_get_organizationid($typeconfig) {
global $CFG;
// Default the organizationid if not specified.
if (empty($typeconfig['organizationid'])) {
if (($typeconfig['organizationid_default'] ?? LTI_DEFAULT_ORGID_SITEHOST) == LTI_DEFAULT_ORGID_SITEHOST) {
$urlparts = parse_url($CFG->wwwroot);
return $urlparts['host'];
} else {
return md5(get_site_identifier());
}
}
return $typeconfig['organizationid'];
}

/**
* Build source ID
*
Expand Down Expand Up @@ -1145,7 +1160,7 @@ function lti_build_content_item_selection_request($id, $course, moodle_url $retu
}

// Get standard request parameters and merge to the request parameters.
$orgid = !empty($typeconfig['organizationid']) ? $typeconfig['organizationid'] : '';
$orgid = lti_get_organizationid($typeconfig);
$standardparams = lti_build_standard_message(null, $orgid, $tool->ltiversion, 'ContentItemSelectionRequest');
$requestparams = array_merge($requestparams, $standardparams);

Expand Down Expand Up @@ -2501,6 +2516,12 @@ function lti_get_type_type_config($id) {
$type->lti_forcessl = $config['forcessl'];
}

if (isset($config['organizationid_default'])) {
$type->lti_organizationid_default = $config['organizationid_default'];
} else {
// Tool was configured before this option was available and the default then was host.
$type->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEHOST;
}
if (isset($config['organizationid'])) {
$type->lti_organizationid = $config['organizationid'];
}
Expand Down
108 changes: 108 additions & 0 deletions mod/lti/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public function test_lti_build_content_item_selection_request() {
$this->assertFalse(isset($params['resource_link_description']));
$this->assertFalse(isset($params['launch_presentation_return_url']));
$this->assertFalse(isset($params['lis_result_sourcedid']));
$this->assertEquals($params['tool_consumer_instance_guid'], 'www.example.com');

// Custom parameters.
$title = 'My custom title';
Expand Down Expand Up @@ -1415,4 +1416,111 @@ public function test_lti_build_login_request() {
$this->assertEquals('some-client-id', $request['client_id']);
$this->assertEquals('some-type-id', $request['lti_deployment_id']);
}

/**
* Test default orgid is host if not specified in config (tool installed in earlier version of Moodle).
*/
public function test_lti_get_launch_data_default_organizationid_unset_usehost() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$config = new stdClass();
$config->lti_organizationid = '';
$course = $this->getDataGenerator()->create_course();
$type = $this->create_type($config);
$link = $this->create_instance($type, $course);
$launchdata = lti_get_launch_data($link);
$this->assertEquals($launchdata[1]['tool_consumer_instance_guid'], 'www.example.com');
}

/**
* Test default org id is set to host when config is usehost.
*/
public function test_lti_get_launch_data_default_organizationid_set_usehost() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$config = new stdClass();
$config->lti_organizationid = '';
$config->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEHOST;
$course = $this->getDataGenerator()->create_course();
$type = $this->create_type($config);
$link = $this->create_instance($type, $course);
$launchdata = lti_get_launch_data($link);
$this->assertEquals($launchdata[1]['tool_consumer_instance_guid'], 'www.example.com');
}

/**
* Test default org id is set to site id when config is usesiteid.
*/
public function test_lti_get_launch_data_default_organizationid_set_usesiteid() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$config = new stdClass();
$config->lti_organizationid = '';
$config->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEID;
$course = $this->getDataGenerator()->create_course();
$type = $this->create_type($config);
$link = $this->create_instance($type, $course);
$launchdata = lti_get_launch_data($link);
$this->assertEquals($launchdata[1]['tool_consumer_instance_guid'], md5(get_site_identifier()));
}

/**
* Test orgid can be overridden in which case default is ignored.
*/
public function test_lti_get_launch_data_default_organizationid_orgid_override() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$config = new stdClass();
$config->lti_organizationid = 'overridden!';
$config->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEID;
$course = $this->getDataGenerator()->create_course();
$type = $this->create_type($config);
$link = $this->create_instance($type, $course);
$launchdata = lti_get_launch_data($link);
$this->assertEquals($launchdata[1]['tool_consumer_instance_guid'], 'overridden!');
}

/**
* Create an LTI Tool.
*
* @param object $config tool config.
*
* @return object tool.
*/
private function create_type(object $config) {
$type = new stdClass();
$type->state = LTI_TOOL_STATE_CONFIGURED;
$type->name = "Test tool";
$type->description = "Example description";
$type->clientid = "Test client ID";
$type->baseurl = $this->getExternalTestFileUrl('/test.html');

$configbase = new stdClass();
$configbase->lti_acceptgrades = LTI_SETTING_NEVER;
$configbase->lti_sendname = LTI_SETTING_NEVER;
$configbase->lti_sendemailaddr = LTI_SETTING_NEVER;
$mergedconfig = (object) array_merge( (array) $configbase, (array) $config);
$typeid = lti_add_type($type, $mergedconfig);
return lti_get_type($typeid);
}

/**
* Create an LTI Instance for the tool in a given course.
*
* @param object $type tool for which an instance should be added.
* @param object $course course where the instance should be added.
*
* @return object instance.
*/
private function create_instance(object $type, object $course) {
$generator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
return $generator->create_instance(array('course' => $course->id,
'toolurl' => $type->baseurl,
'typeid' => $type->id
), array());
}
}

0 comments on commit c986e55

Please sign in to comment.