MDL-78909 mod_lti: Fix for restrict to category.

This commit is contained in:
Ilya Tregubov 2023-08-31 12:11:06 +08:00
parent f9b1bac756
commit 3eceb14342
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
3 changed files with 48 additions and 1 deletions

View File

@ -88,6 +88,18 @@ class toggle_showinactivitychooser extends external_api {
// It is course tool - just update it.
lti_update_type($ltitype, $config);
} else {
$coursecategory = $DB->get_field('course', 'category', ['id' => $courseid]);
$sql = "SELECT COUNT(*) AS count
FROM {lti_types_categories} tc
WHERE tc.typeid = :typeid";
$restrictedtool = $DB->get_record_sql($sql, ['typeid' => $tooltypeid]);
if ($restrictedtool->count) {
$record = $DB->get_record('lti_types_categories', ['typeid' => $tooltypeid, 'categoryid' => $coursecategory]);
if (!$record) {
throw new \moodle_exception('You are not allowed to change this setting for this tool.');
}
}
// This is site tool, but we would like to have course level setting for it.
$lticoursevisible = $DB->get_record('lti_coursevisible', ['typeid' => $tooltypeid, 'courseid' => $courseid]);
if (!$lticoursevisible) {

View File

@ -82,7 +82,10 @@ class toggle_showinactivitychooser_test extends \mod_lti_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecat1 = $this->getDataGenerator()->create_category();
$coursecat2 = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $coursecat1->id]);
$editingteacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$this->setUser($editingteacher);
@ -106,6 +109,34 @@ class toggle_showinactivitychooser_test extends \mod_lti_testcase {
$this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible1);
$this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible2);
$this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible3);
$ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
$ltigenerator->create_tool_types([
'name' => 'site tool preconfigured and activity chooser, restricted to category 1',
'baseurl' => 'http://example.com/tool/1',
'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
'state' => LTI_TOOL_STATE_CONFIGURED,
'lti_coursecategories' => $coursecat1->id
]);
$tool = $DB->get_record('lti_types', ['name' => 'site tool preconfigured and activity chooser, restricted to category 1']);
toggle_showinactivitychooser::execute($tool->id, $course->id, false);
$actual = $DB->get_record_sql($sql, [$tool->id, $course->id]);
$this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible1);
$this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible2);
$this->assertEquals(LTI_COURSEVISIBLE_PRECONFIGURED, $actual->coursevisible3);
$ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
$ltigenerator->create_tool_types([
'name' => 'site tool preconfigured and activity chooser, restricted to category 2',
'baseurl' => 'http://example.com/tool/1',
'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
'state' => LTI_TOOL_STATE_CONFIGURED,
'lti_coursecategories' => $coursecat2->id
]);
$tool = $DB->get_record('lti_types', ['name' => 'site tool preconfigured and activity chooser, restricted to category 2']);
$this->expectException('moodle_exception');
$this->expectExceptionMessage('You are not allowed to change this setting for this tool.');
toggle_showinactivitychooser::execute($tool->id, $course->id, true);
}
}

View File

@ -108,6 +108,10 @@ class mod_lti_generator extends testing_module_generator {
ARRAY_FILTER_USE_BOTH
);
$config = array_diff_key($data, $type);
// This is ugly - we store it in two places.
if (isset($data['coursevisible'])) {
$config['lti_coursevisible'] = $data['coursevisible'];
}
return ['type' => (object) $type, 'config' => (object) $config];
}