From c0ef63eb383d4dc78b166708eaf10df59c2421e3 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 30 Jul 2015 17:31:46 +0200 Subject: [PATCH] MDL-50964 core_group: New Web Service core_group_get_activity_groupmode --- group/externallib.php | 66 ++++++++++++++++++++++++++++++++++++++++ group/tests/externallib_test.php | 46 ++++++++++++++++++++++++++++ lib/db/services.php | 10 ++++++ version.php | 2 +- 4 files changed, 123 insertions(+), 1 deletion(-) diff --git a/group/externallib.php b/group/externallib.php index 1f9b739037c..99dd3be4b9c 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -1290,6 +1290,72 @@ class core_group_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function get_activity_groupmode_parameters() { + return new external_function_parameters( + array( + 'cmid' => new external_value(PARAM_INT, 'course module id') + ) + ); + } + + /** + * Returns effective groupmode used in a given activity. + * + * @throws moodle_exception + * @param int $cmid course module id. + * @return array containing the group mode and possible warnings. + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function get_activity_groupmode($cmid) { + global $USER; + + // Warnings array, it can be empty at the end but is mandatory. + $warnings = array(); + + $params = array( + 'cmid' => $cmid + ); + $params = self::validate_parameters(self::get_activity_groupmode_parameters(), $params); + $cmid = $params['cmid']; + + $cm = get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST); + + // Security checks. + $context = context_module::instance($cm->id); + self::validate_context($context); + + $groupmode = groups_get_activity_groupmode($cm); + + $results = array( + 'groupmode' => $groupmode, + 'warnings' => $warnings + ); + return $results; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 3.0 + */ + public static function get_activity_groupmode_returns() { + return new external_single_structure( + array( + 'groupmode' => new external_value(PARAM_INT, 'group mode: + 0 for no groups, 1 for separate groups, 2 for visible groups'), + 'warnings' => new external_warnings(), + ) + ); + } + } /** diff --git a/group/tests/externallib_test.php b/group/tests/externallib_test.php index 7dc9c65a987..8b1cf3c845e 100644 --- a/group/tests/externallib_test.php +++ b/group/tests/externallib_test.php @@ -453,4 +453,50 @@ class core_group_externallib_testcase extends externallib_advanced_testcase { $this->assertCount(1, $groups['warnings']); } + + /** + * Test get_activity_groupmode + */ + public function test_get_activity_groupmode() { + global $DB; + + $this->resetAfterTest(true); + + $generator = self::getDataGenerator(); + + $student = $generator->create_user(); + $course = $generator->create_course(); + $othercourse = $generator->create_course(); + + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $generator->enrol_user($student->id, $course->id, $studentrole->id); + + $forum1 = $generator->create_module("forum", array('course' => $course->id), array('groupmode' => VISIBLEGROUPS)); + $forum2 = $generator->create_module("forum", array('course' => $othercourse->id)); + $forum3 = $generator->create_module("forum", array('course' => $course->id), array('visible' => 0)); + + // Request data for tests. + $cm1 = get_coursemodule_from_instance("forum", $forum1->id); + $cm2 = get_coursemodule_from_instance("forum", $forum2->id); + $cm3 = get_coursemodule_from_instance("forum", $forum3->id); + + $this->setUser($student); + + $data = core_group_external::get_activity_groupmode($cm1->id); + $data = external_api::clean_returnvalue(core_group_external::get_activity_groupmode_returns(), $data); + $this->assertEquals(VISIBLEGROUPS, $data['groupmode']); + + try { + $data = core_group_external::get_activity_groupmode($cm2->id); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + try { + $data = core_group_external::get_activity_groupmode($cm3->id); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + } } diff --git a/lib/db/services.php b/lib/db/services.php index ba378420a7c..5f1585499fb 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -323,6 +323,15 @@ $functions = array( 'capabilities' => 'moodle/course:managegroups', ), + 'core_group_get_activity_groupmode' => array( + 'classname' => 'core_group_external', + 'methodname' => 'get_activity_groupmode', + 'classpath' => 'group/externallib.php', + 'description' => 'Returns effective groupmode used in a given activity.', + 'type' => 'read', + 'capabilities' => '', + ), + 'core_notes_get_course_notes' => array( 'classname' => 'core_notes_external', 'methodname' => 'get_course_notes', @@ -1136,6 +1145,7 @@ $services = array( 'core_message_get_blocked_users', 'gradereport_user_get_grades_table', 'core_group_get_course_user_groups', + 'core_group_get_activity_groupmode', 'core_user_remove_user_device', 'core_course_get_courses', 'core_completion_update_activity_completion_status_manually', diff --git a/version.php b/version.php index aaf23726acf..626a117a4d7 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015091000.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015091000.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -- 2.11.4.GIT