From f569aac26bf05c5e8c43920cfeeb729d5f21e5d2 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 22 Sep 2015 16:45:17 +0200 Subject: [PATCH] New WS for retrieving activity modules information --- db/services.php | 20 ++++- externallib.php | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ version.php | 4 +- 3 files changed, 226 insertions(+), 3 deletions(-) diff --git a/db/services.php b/db/services.php index 427423d..46cdf17 100755 --- a/db/services.php +++ b/db/services.php @@ -117,7 +117,23 @@ 'classpath' => 'local/aspiredu/externallib.php', 'description' => 'Get all custom course settings', 'type' => 'read' - ) + ), + + 'local_aspiredu_core_course_get_course_module' => array( + 'classname' => 'local_aspiredu_external', + 'methodname' => 'core_course_get_course_module', + 'classpath' => 'local/aspiredu/externallib.php', + 'description' => 'Return information about a course module', + 'type' => 'read' + ), + + 'local_aspiredu_core_course_get_course_module_from_instance' => array( + 'classname' => 'local_aspiredu_external', + 'methodname' => 'core_course_get_course_module_from_instance', + 'classpath' => 'local/aspiredu/externallib.php', + 'description' => 'Return information about a course module', + 'type' => 'read' + ), ); // We define the services to install as pre-build services. A pre-build service is not editable by administrator. @@ -146,6 +162,8 @@ 'local_aspiredu_mod_assign_get_assignments', 'local_aspiredu_mod_assign_get_submissions', 'local_aspiredu_get_custom_course_settings', + 'local_aspiredu_core_course_get_course_module', + 'local_aspiredu_core_course_get_course_module_from_instance', ), 'restrictedusers' => 1, 'enabled' => 1, diff --git a/externallib.php b/externallib.php index 4568471..a2dc226 100755 --- a/externallib.php +++ b/externallib.php @@ -2222,4 +2222,209 @@ public static function get_custom_course_settings_returns() { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function core_course_get_course_module_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value(PARAM_INT, 'The course module id') + ) + ); + } + + /** + * Return information about a course module. + * + * @param int $cmid the course module id + * @return array of warnings and the course module + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function core_course_get_course_module($cmid) { + + $params = self::validate_parameters(self::core_course_get_course_module_parameters(), + array( + 'cmid' => $cmid, + )); + + $warnings = array(); + + $cm = get_coursemodule_from_id(null, $params['cmid'], 0, true, MUST_EXIST); + $context = context_module::instance($cm->id); + self::validate_context($context); + + // If the user has permissions to manage the activity, return all the information. + if (has_capability('moodle/course:manageactivities', $context)) { + $info = $cm; + } else { + // Return information is safe to show to any user. + $info = new stdClass(); + $info->id = $cm->id; + $info->course = $cm->course; + $info->module = $cm->module; + $info->modname = $cm->modname; + $info->instance = $cm->instance; + $info->section = $cm->section; + $info->sectionnum = $cm->sectionnum; + $info->groupmode = $cm->groupmode; + $info->groupingid = $cm->groupingid; + $info->completion = $cm->completion; + } + // Format name. + $info->name = format_string($cm->name, true, array('context' => $context)); + + $result = array(); + $result['cm'] = $info; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function core_course_get_course_module_returns() { + return new external_single_structure( + array( + 'cm' => new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'The course module id'), + 'course' => new external_value(PARAM_INT, 'The course id'), + 'module' => new external_value(PARAM_INT, 'The module type id'), + 'name' => new external_value(PARAM_TEXT, 'The activity name'), + 'modname' => new external_value(PARAM_COMPONENT, 'The module component name (forum, assign, etc..)'), + 'instance' => new external_value(PARAM_INT, 'The activity instance id'), + 'section' => new external_value(PARAM_INT, 'The module section id'), + 'sectionnum' => new external_value(PARAM_INT, 'The module section number'), + 'groupmode' => new external_value(PARAM_INT, 'Group mode'), + 'groupingid' => new external_value(PARAM_INT, 'Grouping id'), + 'completion' => new external_value(PARAM_INT, 'If completion is enabled'), + 'idnumber' => new external_value(PARAM_RAW, 'Module id number', VALUE_OPTIONAL), + 'added' => new external_value(PARAM_INT, 'Time added', VALUE_OPTIONAL), + 'score' => new external_value(PARAM_INT, 'Score', VALUE_OPTIONAL), + 'intent' => new external_value(PARAM_INT, 'Indentation', VALUE_OPTIONAL), + 'visible' => new external_value(PARAM_INT, 'If visible', VALUE_OPTIONAL), + 'visibleold' => new external_value(PARAM_INT, 'Visible old', VALUE_OPTIONAL), + 'completiongradeitemnumber' => new external_value(PARAM_INT, 'Completion grade item', VALUE_OPTIONAL), + 'completionview' => new external_value(PARAM_INT, 'Completion view setting', VALUE_OPTIONAL), + 'completionexpected' => new external_value(PARAM_INT, 'Completion time expected', VALUE_OPTIONAL), + 'showdescription' => new external_value(PARAM_INT, 'If the description is showed', VALUE_OPTIONAL), + 'availability' => new external_value(PARAM_RAW, 'Availability settings', VALUE_OPTIONAL), + ) + ), + 'warnings' => new external_warnings() + ) + ); + } + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function core_course_get_course_module_from_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 core_course_get_course_module_from_instance($module, $instance) { + + $params = self::validate_parameters(self::core_course_get_course_module_from_instance_parameters(), + array( + 'module' => $module, + 'instance' => $instance, + )); + + $warnings = array(); + + $cm = get_coursemodule_from_instance($params['module'], $params['instance'], 0, true, MUST_EXIST); + $context = context_module::instance($cm->id); + self::validate_context($context); + + // If the user has permissions to manage the activity, return all the information. + if (has_capability('moodle/course:manageactivities', $context)) { + $info = $cm; + } else { + // Return information is safe to show to any user. + $info = new stdClass(); + $info->id = $cm->id; + $info->course = $cm->course; + $info->module = $cm->module; + $info->modname = $cm->modname; + $info->instance = $cm->instance; + $info->section = $cm->section; + $info->sectionnum = $cm->sectionnum; + $info->groupmode = $cm->groupmode; + $info->groupingid = $cm->groupingid; + $info->completion = $cm->completion; + } + // Format name. + $info->name = format_string($cm->name, true, array('context' => $context)); + + $result = array(); + $result['cm'] = $info; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function core_course_get_course_module_from_instance_returns() { + return new external_single_structure( + array( + 'cm' => new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'The course module id'), + 'course' => new external_value(PARAM_INT, 'The course id'), + 'module' => new external_value(PARAM_INT, 'The module type id'), + 'name' => new external_value(PARAM_TEXT, 'The activity name'), + 'modname' => new external_value(PARAM_COMPONENT, 'The module component name (forum, assign, etc..)'), + 'instance' => new external_value(PARAM_INT, 'The activity instance id'), + 'section' => new external_value(PARAM_INT, 'The module section id'), + 'sectionnum' => new external_value(PARAM_INT, 'The module section number'), + 'groupmode' => new external_value(PARAM_INT, 'Group mode'), + 'groupingid' => new external_value(PARAM_INT, 'Grouping id'), + 'completion' => new external_value(PARAM_INT, 'If completion is enabled'), + 'idnumber' => new external_value(PARAM_RAW, 'Module id number', VALUE_OPTIONAL), + 'added' => new external_value(PARAM_INT, 'Time added', VALUE_OPTIONAL), + 'score' => new external_value(PARAM_INT, 'Score', VALUE_OPTIONAL), + 'intent' => new external_value(PARAM_INT, 'Indentation', VALUE_OPTIONAL), + 'visible' => new external_value(PARAM_INT, 'If visible', VALUE_OPTIONAL), + 'visibleold' => new external_value(PARAM_INT, 'Visible old', VALUE_OPTIONAL), + 'completiongradeitemnumber' => new external_value(PARAM_INT, 'Completion grade item', VALUE_OPTIONAL), + 'completionview' => new external_value(PARAM_INT, 'Completion view setting', VALUE_OPTIONAL), + 'completionexpected' => new external_value(PARAM_INT, 'Completion time expected', VALUE_OPTIONAL), + 'showdescription' => new external_value(PARAM_INT, 'If the description is showed', VALUE_OPTIONAL), + 'availability' => new external_value(PARAM_RAW, 'Availability settings', VALUE_OPTIONAL), + ) + ), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/version.php b/version.php index 169b7b6..c33c817 100755 --- a/version.php +++ b/version.php @@ -22,8 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$plugin->version = 2015010120; +$plugin->version = 2015010122; $plugin->requires = 2012120300; $plugin->component = 'local_aspiredu'; -$plugin->release = '1.5'; +$plugin->release = '1.6'; $plugin->maturity = MATURITY_STABLE; \ No newline at end of file