Maybe I've made some progress. I realised that the built-in Web-service function for enrolling users probably needs to set up these instances in the same way that my unenrolment function would. So I grepped enrol_manual_enrol_users. It turns out to be mentioned in enrol/manual/db/services.php , which will tell me which class and method implements it.
And in enrol/manual/db/services.php , I found this:
$functions = array(
...
'enrol_manual_enrol_users' => array(
'classname' => 'enrol_manual_external',
'methodname' => 'enrol_users',
'classpath' => 'enrol/manual/externallib.php',
'description' => 'Manual enrol users',
'capabilities'=> 'enrol/manual:enrol',
'type' => 'write',
),
);
So it's implemented by method enrol_users of class enrol_manual_external.
So I looked at enrol/manual/externallib.php . Where I found "class enrol_manual_external extends external_api", and in it, this method:
/**
* Enrolment of users
* Function throw an exception at the first error encountered.
* @param array $enrolments An array of user enrolment
* @return null
*/
public static function enrol_users($enrolments) {
global $DB, $CFG;
require_once($CFG->libdir . '/enrollib.php');
$params = self::validate_parameters(self::enrol_users_parameters(),
array('enrolments' => $enrolments));
$transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs
//(except if the DB doesn't support it)
//retrieve the manual enrolment plugin
$enrol = enrol_get_plugin('manual');
if (empty($enrol)) {
throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
}
foreach ($params['enrolments'] as $enrolment) {
// Ensure the current user is allowed to run this function in the enrolment context
$context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']);
self::validate_context($context);
//check that the user has the permission to manual enrol
require_capability('enrol/manual:enrol', $context);
//throw an exception if user is not able to assign the role
$roles = get_assignable_roles($context);
if (!key_exists($enrolment['roleid'], $roles)) {
$errorparams = new stdClass();
$errorparams->roleid = $enrolment['roleid'];
$errorparams->courseid = $enrolment['courseid'];
$errorparams->userid = $enrolment['userid'];
throw new moodle_exception('wsusercannotassign', 'enrol_manual', '', $errorparams);
}
//check manual enrolment plugin instance is enabled/exist
$enrolinstances = enrol_get_instances($enrolment['courseid'], true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance = $courseenrolinstance;
break;
}
}
if (empty($instance)) {
$errorparams = new stdClass();
$errorparams->courseid = $enrolment['courseid'];
throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams);
}
//check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin)
if (!$enrol->allow_enrol($instance)) {
$errorparams = new stdClass();
$errorparams->roleid = $enrolment['roleid'];
$errorparams->courseid = $enrolment['courseid'];
$errorparams->userid = $enrolment['userid'];
throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams);
}
//finally proceed the enrolment
$enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0;
$enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0;
$enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ?
ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE;
$enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'],
$enrolment['timestart'], $enrolment['timeend'], $enrolment['status']);
}
$transaction->allow_commit();
}
So I suspect that if I put in some "un"s, this might work:
/**
* Unenrolment of users
* Function throw an exception at the first error encountered.
* @param array $unenrolments An array of user unenrolments.
* @return null
*/
public static function unenrol_users( $unenrolments ) {
global $DB, $CFG;
require_once($CFG->libdir . '/enrollib.php');
$params = self::validate_parameters(self::unenrol_users_parameters(),
array( 'unenrolments' => $unenrolments) );
$transaction = $DB->start_delegated_transaction(); //rollback all unenrolment if an error occurs
//(except if the DB doesn't support it)
//retrieve the manual enrolment plugin
$enrol = enrol_get_plugin('manual');
if (empty($enrol)) {
throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
}
foreach ($params['unenrolments'] as $unenrolment) {
// Ensure the current user is allowed to run this function in the enrolment context
$context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']);
self::validate_context($context);
//check that the user has the permission to manual unenrol
require_capability('enrol/manual:unenrol', $context);
// Should we unassign roles here?
//check manual enrolment plugin instance is enabled/exist
$enrolinstances = enrol_get_instances($enrolment['courseid'], true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance = $courseenrolinstance;
break;
}
}
if (empty($instance)) {
$errorparams = new stdClass();
$errorparams->courseid = $enrolment['courseid'];
throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams);
}
//check that the plugin accepts unenrolment (it should always the case, it's hard coded in the plugin)
if (!$enrol->allow_unenrol($instance)) {
$errorparams = new stdClass();
$errorparams->roleid = $enrolment['roleid'];
$errorparams->courseid = $enrolment['courseid'];
$errorparams->userid = $enrolment['userid'];
throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams);
}
// Probably we need the role ID. Can one unenrol a user
// in one role but not in another?
//finally proceed the enrolment
$enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0;
$enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0;
$enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ?
ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE;
// Don't know whether we want similar assignments.
$enrol->unenrol_user($instance, $enrolment['userid'], $enrolment['roleid'],
$enrolment['timestart'], $enrolment['timeend'], $enrolment['status']);
// Don't know whether we want similar parameters.
}
$transaction->allow_commit();
}
Moodle developers, any comments?
Jocelyn