Skip to content

Commit

Permalink
MDL-50966 core_group: New WS core_group_get_activity_allowed_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Sep 11, 2015
1 parent f495510 commit 7107c2f
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 13 deletions.
143 changes: 131 additions & 12 deletions group/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,8 @@ public static function get_course_user_groups($courseid, $userid, $groupingid =
list($group->description, $group->descriptionformat) =
external_format_text($group->description, $group->descriptionformat,
$context->id, 'group', 'description', $group->id);
$usergroups[] = (array)$group;
$group->courseid = $course->id;
$usergroups[] = $group;
}
}

Expand All @@ -1274,17 +1275,135 @@ public static function get_course_user_groups($courseid, $userid, $groupingid =
public static function get_course_user_groups_returns() {
return new external_single_structure(
array(
'groups' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'group record id'),
'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
'description' => new external_value(PARAM_RAW, 'group description text'),
'descriptionformat' => new external_format_value('description'),
'idnumber' => new external_value(PARAM_RAW, 'id number')
)
)
),
'groups' => new external_multiple_structure(self::group_description()),
'warnings' => new external_warnings(),
)
);
}

/**
* Create group return value description.
*
* @return external_single_structure The group description
*/
public static function group_description() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'group record id'),
'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
'description' => new external_value(PARAM_RAW, 'group description text'),
'descriptionformat' => new external_format_value('description'),
'idnumber' => new external_value(PARAM_RAW, 'id number'),
'courseid' => new external_value(PARAM_INT, 'course id', VALUE_OPTIONAL),
)
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_activity_allowed_groups_parameters() {
return new external_function_parameters(
array(
'cmid' => new external_value(PARAM_INT, 'course module id'),
'userid' => new external_value(PARAM_INT, 'id of user, empty for current user', VALUE_OPTIONAL, 0)
)
);
}

/**
* Gets a list of groups that the user is allowed to access within the specified activity.
*
* @throws moodle_exception
* @param int $cmid course module id
* @param int $userid id of user.
* @return array of group objects (id, name, description, format) and possible warnings.
* @since Moodle 3.0
*/
public static function get_activity_allowed_groups($cmid, $userid = 0) {
global $USER;

// Warnings array, it can be empty at the end but is mandatory.
$warnings = array();

$params = array(
'cmid' => $cmid,
'userid' => $userid
);
$params = self::validate_parameters(self::get_activity_allowed_groups_parameters(), $params);
$cmid = $params['cmid'];
$userid = $params['userid'];

$cm = get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST);

// Security checks.
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($cm->course);
self::validate_context($context);

if (empty($userid)) {
$userid = $USER->id;
}

$user = core_user::get_user($userid, 'id, deleted', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
throw new moodle_exception('invaliduserid');
}

// Check if we have permissions for retrieve the information.
if ($user->id != $USER->id) {
if (!has_capability('moodle/course:managegroups', $context)) {
throw new moodle_exception('accessdenied', 'admin');
}

// Validate if the user is enrolled in the course.
if (!is_enrolled($coursecontext, $user->id)) {
// We return a warning because the function does not fail for not enrolled users.
$warning = array();
$warning['item'] = 'course';
$warning['itemid'] = $cm->course;
$warning['warningcode'] = '1';
$warning['message'] = "User $user->id is not enrolled in course $cm->course";
$warnings[] = $warning;
}
}

$usergroups = array();
if (empty($warnings)) {
$groups = groups_get_activity_allowed_groups($cm, $user->id);

foreach ($groups as $group) {
list($group->description, $group->descriptionformat) =
external_format_text($group->description, $group->descriptionformat,
$coursecontext->id, 'group', 'description', $group->id);
$group->courseid = $cm->course;
$usergroups[] = $group;
}
}

$results = array(
'groups' => $usergroups,
'warnings' => $warnings
);
return $results;
}

/**
* Returns description of method result value.
*
* @return external_description A single structure containing groups and possible warnings.
* @since Moodle 3.0
*/
public static function get_activity_allowed_groups_returns() {
return new external_single_structure(
array(
'groups' => new external_multiple_structure(self::group_description()),
'warnings' => new external_warnings(),
)
);
Expand Down
90 changes: 90 additions & 0 deletions group/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,94 @@ public function test_get_course_user_groups() {
$this->assertCount(1, $groups['warnings']);

}

/**
* Test get_activity_allowed_groups
*/
public function test_get_activity_allowed_groups() {
global $DB;

$this->resetAfterTest(true);

$generator = self::getDataGenerator();

$student = $generator->create_user();
$otherstudent = $generator->create_user();
$teacher = $generator->create_user();
$course = $generator->create_course();
$othercourse = $generator->create_course();

$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$generator->enrol_user($student->id, $course->id, $studentrole->id);
$generator->enrol_user($otherstudent->id, $othercourse->id, $studentrole->id);
$generator->enrol_user($teacher->id, $course->id, $teacherrole->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);

$group1data = array();
$group1data['courseid'] = $course->id;
$group1data['name'] = 'Group Test 1';
$group1data['description'] = 'Group Test 1 description';
$group1data['idnumber'] = 'TEST1';
$group2data = array();
$group2data['courseid'] = $course->id;
$group2data['name'] = 'Group Test 2';
$group2data['description'] = 'Group Test 2 description';
$group2data['idnumber'] = 'TEST2';
$group1 = $generator->create_group($group1data);
$group2 = $generator->create_group($group2data);

groups_add_member($group1->id, $student->id);
groups_add_member($group2->id, $student->id);

$this->setUser($student);

// First try possible errors.
try {
$data = core_group_external::get_activity_allowed_groups($cm2->id);
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

try {
$data = core_group_external::get_activity_allowed_groups($cm3->id);
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

// Retrieve my groups.
$groups = core_group_external::get_activity_allowed_groups($cm1->id);
$groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
$this->assertCount(2, $groups['groups']);

foreach ($groups['groups'] as $group) {
if ($group['name'] == $group1data['name']) {
$this->assertEquals($group1data['description'], $group['description']);
$this->assertEquals($group1data['idnumber'], $group['idnumber']);
} else {
$this->assertEquals($group2data['description'], $group['description']);
$this->assertEquals($group2data['idnumber'], $group['idnumber']);
}
}

$this->setUser($teacher);
// Retrieve other users groups.
$groups = core_group_external::get_activity_allowed_groups($cm1->id, $student->id);
$groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
$this->assertCount(2, $groups['groups']);

// Check warnings. Trying to get groups for a user not enrolled in course.
$groups = core_group_external::get_activity_allowed_groups($cm1->id, $otherstudent->id);
$groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
$this->assertCount(1, $groups['warnings']);

}
}
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_allowed_groups' => array(
'classname' => 'core_group_external',
'methodname' => 'get_activity_allowed_groups',
'classpath' => 'group/externallib.php',
'description' => 'Gets a list of groups that the user is allowed to access within the specified 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_allowed_groups',
'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 7107c2f

Please sign in to comment.