From 13bb68199e64b3d5d8aa4a68d69b061c4de953e2 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 28 Sep 2015 09:35:49 +0200 Subject: [PATCH] MDL-51579 core_course: New Web Service get_course_module_by_instance --- course/externallib.php | 48 +++++++++++++++++++++ course/tests/externallib_test.php | 70 +++++++++++++++++++++++++++++++ lib/db/services.php | 9 ++++ version.php | 2 +- 4 files changed, 128 insertions(+), 1 deletion(-) diff --git a/course/externallib.php b/course/externallib.php index e36b26801c5..34243cda457 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -2418,6 +2418,54 @@ class core_course_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function get_course_module_by_instance_parameters() { + return new external_function_parameters( + array( + 'module' => new external_value(PARAM_COMPONENT, 'The module name'), + 'instance' => new external_value(PARAM_INT, 'The module instance id') + ) + ); + } + + /** + * Return information about a course module. + * + * @param int $module the module name + * @param int $instance the module instance + * @return array of warnings and the course module + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function get_course_module_by_instance($module, $instance) { + + $params = self::validate_parameters(self::get_course_module_by_instance_parameters(), + array( + 'module' => $module, + 'instance' => $instance, + )); + + $warnings = array(); + $cm = get_coursemodule_from_instance($params['module'], $params['instance'], 0, false, MUST_EXIST); + + return self::get_course_module($cm->id); + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function get_course_module_by_instance_returns() { + return self::get_course_module_returns(); + } + } /** diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 875d8823139..e443b33cd3e 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -1584,4 +1584,74 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals($chat->id, $result['cm']['instance']); } + + /** + * Test get_course_module_by_instance + */ + public function test_get_course_module_by_instance() { + global $DB; + + $this->resetAfterTest(true); + + $this->setAdminUser(); + $course = self::getDataGenerator()->create_course(); + $record = array( + 'course' => $course->id, + 'name' => 'First Chat' + ); + $options = array( + 'idnumber' => 'ABC', + 'visible' => 0 + ); + // Hidden activity. + $chat = self::getDataGenerator()->create_module('chat', $record, $options); + + // Test admin user can see the complete hidden activity. + $result = core_course_external::get_course_module_by_instance('chat', $chat->id); + $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); + + $this->assertCount(0, $result['warnings']); + // Test we retrieve all the fields. + $this->assertCount(22, $result['cm']); + $this->assertEquals($record['name'], $result['cm']['name']); + $this->assertEquals($options['idnumber'], $result['cm']['idnumber']); + + $student = $this->getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + + self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); + $this->setUser($student); + + // The user shouldn't be able to see the activity. + try { + core_course_external::get_course_module_by_instance('chat', $chat->id); + $this->fail('Exception expected due to invalid permissions.'); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + // Make module visible. + set_coursemodule_visible($chat->cmid, 1); + + // Test student user. + $result = core_course_external::get_course_module_by_instance('chat', $chat->id); + $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); + + $this->assertCount(0, $result['warnings']); + // Test we retrieve only the few files we can see. + $this->assertCount(11, $result['cm']); + $this->assertEquals($chat->cmid, $result['cm']['id']); + $this->assertEquals($course->id, $result['cm']['course']); + $this->assertEquals('chat', $result['cm']['modname']); + $this->assertEquals($chat->id, $result['cm']['instance']); + + // Try with an invalid module name. + try { + core_course_external::get_course_module_by_instance('abc', $chat->id); + $this->fail('Exception expected due to invalid module name.'); + } catch (dml_read_exception $e) { + $this->assertEquals('dmlreadexception', $e->errorcode); + } + + } } diff --git a/lib/db/services.php b/lib/db/services.php index 84b9be16f3b..05e1b998a5b 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -750,6 +750,14 @@ $functions = array( 'type' => 'read' ), + 'core_course_get_course_module_by_instance' => array( + 'classname' => 'core_course_external', + 'methodname' => 'get_course_module_by_instance', + 'classpath' => 'course/externallib.php', + 'description' => 'Return information about a given module name and instance id', + 'type' => 'read' + ), + // === course category related functions === 'core_course_get_categories' => array( @@ -1200,6 +1208,7 @@ $services = array( 'core_course_view_course', 'core_course_search_courses', 'core_course_get_course_module', + 'core_course_get_course_module_by_instance', 'core_completion_get_activities_completion_status', 'core_notes_get_course_notes', 'core_completion_get_course_completion_status', diff --git a/version.php b/version.php index 4732f168d81..0713a533f00 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015100200.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015100200.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.