mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 18:36:42 +02:00
MDL-28592 webservices: Add manual unenrol users to external services
This commit is contained in:
parent
20d38830ae
commit
28e31ae43f
4 changed files with 245 additions and 0 deletions
|
@ -156,6 +156,75 @@ class enrol_manual_external extends external_api {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function unenrol_users_parameters() {
|
||||
return new external_function_parameters(array(
|
||||
'enrolments' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'The user that is going to be unenrolled'),
|
||||
'courseid' => new external_value(PARAM_INT, 'The course to unenrol the user from'),
|
||||
'roleid' => new external_value(PARAM_INT, 'The user role', VALUE_OPTIONAL),
|
||||
)
|
||||
)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unenrolment of users.
|
||||
*
|
||||
* @param array $enrolments an array of course user and role ids
|
||||
* @throws coding_exception
|
||||
* @throws dml_transaction_exception
|
||||
* @throws invalid_parameter_exception
|
||||
* @throws moodle_exception
|
||||
* @throws required_capability_exception
|
||||
* @throws restricted_context_exception
|
||||
*/
|
||||
public static function unenrol_users($enrolments) {
|
||||
global $CFG, $DB;
|
||||
$params = self::validate_parameters(self::unenrol_users_parameters(), array('enrolments' => $enrolments));
|
||||
require_once($CFG->libdir . '/enrollib.php');
|
||||
$transaction = $DB->start_delegated_transaction(); // Rollback all enrolment if an error occurs.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
if (empty($enrol)) {
|
||||
throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
|
||||
}
|
||||
|
||||
foreach ($params['enrolments'] as $enrolment) {
|
||||
$context = context_course::instance($enrolment['courseid']);
|
||||
self::validate_context($context);
|
||||
require_capability('enrol/manual:unenrol', $context);
|
||||
$instance = $DB->get_record('enrol', array('courseid' => $enrolment['courseid'], 'enrol' => 'manual'));
|
||||
if (!$instance) {
|
||||
throw new moodle_exception('wsnoinstance', 'enrol_manual', $enrolment);
|
||||
}
|
||||
$user = $DB->get_record('user', array('id' => $enrolment['userid']));
|
||||
if (!$user) {
|
||||
throw new invalid_parameter_exception('User id not exist: '.$enrolment['userid']);
|
||||
}
|
||||
if (!$enrol->allow_unenrol($instance)) {
|
||||
throw new moodle_exception('wscannotunenrol', 'enrol_manual', '', $enrolment);
|
||||
}
|
||||
$enrol->unenrol_user($instance, $enrolment['userid']);
|
||||
}
|
||||
$transaction->allow_commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public static function unenrol_users_returns() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue