diff --git a/mod/lti/classes/external/toggle_showinactivitychooser.php b/mod/lti/classes/external/toggle_showinactivitychooser.php index 5c44241615d..99c3b24e60f 100644 --- a/mod/lti/classes/external/toggle_showinactivitychooser.php +++ b/mod/lti/classes/external/toggle_showinactivitychooser.php @@ -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) { diff --git a/mod/lti/tests/external/toggle_showinactivitychooser_test.php b/mod/lti/tests/external/toggle_showinactivitychooser_test.php index 650c82c555f..e4ef2ef1d4c 100644 --- a/mod/lti/tests/external/toggle_showinactivitychooser_test.php +++ b/mod/lti/tests/external/toggle_showinactivitychooser_test.php @@ -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); } } diff --git a/mod/lti/tests/generator/lib.php b/mod/lti/tests/generator/lib.php index d4032b67588..783c1d02db3 100644 --- a/mod/lti/tests/generator/lib.php +++ b/mod/lti/tests/generator/lib.php @@ -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]; }