diff --git a/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php b/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php index ff4ae4bd5fc..20e80122fe2 100644 --- a/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php +++ b/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php @@ -35,6 +35,7 @@ use external_value; use external_single_structure; use external_warnings; use context_module; +use mod_h5pactivity\local\manager; /** * This is the external method for getting access information for a h5p activity. @@ -81,10 +82,16 @@ class get_h5pactivity_access_information extends external_api { $result = []; // Return all the available capabilities. + $manager = manager::create_from_coursemodule($cm); $capabilities = load_capability_def('mod_h5pactivity'); foreach ($capabilities as $capname => $capdata) { $field = 'can' . str_replace('mod/h5pactivity:', '', $capname); - $result[$field] = has_capability($capname, $context); + // For mod/h5pactivity:submit we need to check if tracking is enabled in the h5pactivity for the current user. + if ($field == 'cansubmit') { + $result[$field] = $manager->is_tracking_enabled(); + } else { + $result[$field] = has_capability($capname, $context); + } } $result['warnings'] = []; diff --git a/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php b/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php index fcef64d79c4..00c587caa5d 100644 --- a/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php +++ b/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php @@ -46,25 +46,36 @@ class get_h5pactivity_access_information_testcase extends externallib_advanced_t /** * Test the behaviour of get_h5pactivity_access_information(). + * + * @dataProvider get_h5pactivity_access_information_data + * @param string $role user role in course + * @param int $enabletracking if tracking is enabled + * @param array $enabledcaps capabilities enabled */ - public function test_get_h5pactivity_access_information() { + public function test_get_h5pactivity_access_information(string $role, int $enabletracking, array $enabledcaps) { $this->resetAfterTest(); $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); - $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); - $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $activity = $this->getDataGenerator()->create_module('h5pactivity', + [ + 'course' => $course, + 'enabletracking' => $enabletracking + ] + ); - // Check the access information for a student. - $this->setUser($student); + if ($role) { + $user = $this->getDataGenerator()->create_and_enrol($course, $role); + $this->setUser($user); + } + + // Check the access information. $result = get_h5pactivity_access_information::execute($activity->id); $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result); $this->assertCount(0, $result['warnings']); unset($result['warnings']); - // Check default values for capabilities for student. - $enabledcaps = ['canview', 'cansubmit']; + // Check the values for capabilities. foreach ($result as $capname => $capvalue) { if (in_array($capname, $enabledcaps)) { $this->assertTrue($capvalue); @@ -72,26 +83,55 @@ class get_h5pactivity_access_information_testcase extends externallib_advanced_t $this->assertFalse($capvalue); } } + } - // Check the access information for a teacher. - $this->setUser($teacher); - $result = get_h5pactivity_access_information::execute($activity->id); - $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result); - $this->assertCount(0, $result['warnings']); - unset($result['warnings']); + /** + * Data provider for get_h5pactivity_access_information. + * + * @return array + */ + public function get_h5pactivity_access_information_data(): array { + return [ + 'Admin, tracking enabled' => [ + '', 1, ['canview', 'canreviewattempts', 'canaddinstance'] + ], + 'Admin, tracking disabled' => [ + '', 0, ['canview', 'canreviewattempts', 'canaddinstance'] + ], + 'Student, tracking enabled' => [ + 'student', 1, ['canview', 'cansubmit'] + ], + 'Student, tracking disabled' => [ + 'student', 0, ['canview'] + ], + 'Teacher, tracking enabled' => [ + 'editingteacher', 1, [ + 'canview', + 'canreviewattempts', + 'canaddinstance' + ] + ], + 'Teacher, tracking disabled' => [ + 'editingteacher', 0, [ + 'canview', + 'canreviewattempts', + 'canaddinstance' + ] + ], + ]; + } - // Check default values for capabilities for teacher. - $enabledcaps = ['canview', 'canaddinstance', 'canreviewattempts']; - foreach ($result as $capname => $capvalue) { - if (in_array($capname, $enabledcaps)) { - $this->assertTrue($capvalue); - } else { - $this->assertFalse($capvalue); - } - } + /** + * Test dml_missing_record_exception in get_h5pactivity_access_information. + */ + public function test_dml_missing_record_exception() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); // Call the WS using an unexisting h5pactivityid. $this->expectException(dml_missing_record_exception::class); - $result = get_h5pactivity_access_information::execute($activity->id + 1); + $result = get_h5pactivity_access_information::execute(1); } } \ No newline at end of file