diff --git a/enrol/externallib.php b/enrol/externallib.php index 9e2338d7e3a..8f4ef7f0efb 100644 --- a/enrol/externallib.php +++ b/enrol/externallib.php @@ -655,7 +655,10 @@ class core_role_external extends external_api { array( 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'), - 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'), + 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from', VALUE_OPTIONAL), + 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to unassign the user role in ++ (block, course, coursecat, system, user, module)', VALUE_OPTIONAL), + 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL), ) ) ) @@ -679,7 +682,7 @@ class core_role_external extends external_api { foreach ($params['unassignments'] as $unassignment) { // Ensure the current user is allowed to run this function in the unassignment context - $context = context::instance_by_id($unassignment['contextid'], IGNORE_MISSING); + $context = self::get_context_from_params($unassignment); self::validate_context($context); require_capability('moodle/role:assign', $context); @@ -689,7 +692,7 @@ class core_role_external extends external_api { throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']); } - role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']); + role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id); } $transaction->allow_commit(); diff --git a/enrol/tests/externallib_role_test.php b/enrol/tests/externallib_role_test.php index a3ed01e3307..f3b77f01a5a 100644 --- a/enrol/tests/externallib_role_test.php +++ b/enrol/tests/externallib_role_test.php @@ -115,7 +115,7 @@ class core_role_external_testcase extends externallib_advanced_testcase { $users = get_role_users(3, $context); $this->assertEquals(count($users), 1); - // Call the external function. Assign teacher role to $USER. + // Call the external function. Unassign teacher role using contextid. core_role_external::unassign_roles(array( array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id))); @@ -123,6 +123,19 @@ class core_role_external_testcase extends externallib_advanced_testcase { $users = get_role_users(3, $context); $this->assertEquals(count($users), 0); + // Add teacher role to $USER on course context. + role_assign(3, $USER->id, $context->id); + $users = get_role_users(3, $context); + $this->assertEquals(count($users), 1); + + // Call the external function. Unassign teacher role using context level and instanceid. + core_role_external::unassign_roles(array( + array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id))); + + // Check the role has been unassigned on course context. + $users = get_role_users(3, $context); + $this->assertEquals(count($users), 0); + // Call without required capability. $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid); $this->setExpectedException('moodle_exception'); diff --git a/lib/accesslib.php b/lib/accesslib.php index dbb20318e70..ce4fe111e13 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1695,7 +1695,7 @@ function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0, * * @param int $roleid * @param int $userid - * @param int|context $contextid + * @param int $contextid * @param string $component * @param int $itemid * @return void