mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-51579 core_course: New Web Service get_course_module_by_instance
This commit is contained in:
parent
35d3e8b00b
commit
13bb68199e
4 changed files with 128 additions and 1 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue