MDL-43820 enrol: Add method to find plugin enrol instance

When upload courses we need to match instance with provided data.
This commit is contained in:
Ilya Tregubov 2023-08-01 11:35:45 +08:00
parent d76e211be6
commit bf7ff76bcd
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
3 changed files with 25 additions and 8 deletions

View File

@ -1022,16 +1022,10 @@ class tool_uploadcourse_course {
}
$enrolmentplugins = tool_uploadcourse_helper::get_enrolment_plugins();
$instances = enrol_get_instances($course->id, false);
foreach ($enrolmentdata as $enrolmethod => $method) {
$instance = null;
foreach ($instances as $i) {
if ($i->enrol == $enrolmethod) {
$instance = $i;
break;
}
}
$plugin = $enrolmentplugins[$enrolmethod];
$instance = $plugin->find_instance($method, $course->id);
$todelete = isset($method['delete']) && $method['delete'];
$todisable = isset($method['disable']) && $method['disable'];

View File

@ -1,6 +1,16 @@
This files describes API changes in /enrol/* - plugins,
information provided here is intended especially for developers.
=== 4.4 ===
* New find_instance() function has been created. It finds a matching enrolment instance in a given course using provided
enrolment data (for example cohort idnumber for cohort enrolment). Defaults to null. Override this function in your
enrolment plugin if you want it to be supported in CSV course upload. Please be aware that sometimes it is not possible
to uniquely find an instance based on given data. For example lti entolment records do not have any unique data except
id in mod_lti_enrol table. Such plugins should not be supported in CSV course upload. The exception is self enrolment
plugin since it is already supported in CSV course upload. For self enrolment plugin, find_instance() returns first
available instance in the course.
=== 4.3 ===
* New is_csv_upload_supported() function has been created. It checks whether enrolment plugin is supported

View File

@ -3509,4 +3509,17 @@ abstract class enrol_plugin {
return null;
}
/**
* Finds matching instances for a given course.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return stdClass|null Matching instance
*/
public function find_instance(array $enrolmentdata, int $courseid) : ?stdClass {
// By default, we assume we can't uniquely identify an instance so better not update any.
// Plugins can override this if they can uniquely identify an instance.
return null;
}
}