Skip to content

Commit

Permalink
MDL-50964 core_group: New Web Service core_group_get_activity_groupmode
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Sep 11, 2015
1 parent f495510 commit c0ef63e
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
66 changes: 66 additions & 0 deletions group/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,72 @@ public static function get_course_user_groups_returns() {
);
}

/**
* 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(),
)
);
}

}

/**
Expand Down
46 changes: 46 additions & 0 deletions group/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,50 @@ public function test_get_course_user_groups() {
$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);
}

}
}
10 changes: 10 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@
'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',
Expand Down Expand Up @@ -1136,6 +1145,7 @@
'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',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit c0ef63e

Please sign in to comment.