Removed auto enable check rom enrolcohortsync class

This commit is contained in:
PMKuipers 2023-06-26 11:40:24 +02:00
parent 29a2472bd2
commit 70a005d574
3 changed files with 105 additions and 107 deletions

View file

@ -7,6 +7,7 @@ use \local_treestudyplan\studyplan;
class enrolcohortsync { class enrolcohortsync {
private const METHOD = "cohort"; private const METHOD = "cohort";
private $studyplan; private $studyplan;
private $studyplanid;
function __construct($studyplan){ function __construct($studyplan){
$this->studyplan = $studyplan; $this->studyplan = $studyplan;
@ -24,124 +25,121 @@ class enrolcohortsync {
// TODO: Determine if it would be better to add a database table of our own to store references between studyplan and cohort sync enrolment // TODO: Determine if it would be better to add a database table of our own to store references between studyplan and cohort sync enrolment
// instead of using customtext4 for this // instead of using customtext4 for this
$enrol = enrol_get_plugin(self::METHOD);
// Find the courses that need to be synced to the associated cohorts
$courseids = $this->studyplan->get_linked_course_ids();
if(get_config("local_treestudyplan","csync_enable")){ // And find the cohorts that are linked to this studyplan.
$enrol = enrol_get_plugin(self::METHOD); $cohortids = $this->studyplan->get_linked_cohort_ids();
// Find the courses that need to be synced to the associated cohorts
$courseids = $this->studyplan->get_linked_course_ids();
// And find the cohorts that are linked to this studyplan. // Next, for each course that is linked:
$cohortids = $this->studyplan->get_linked_cohort_ids(); foreach($courseids as $courseid){
$course = get_course($courseid);
// Next, for each course that is linked: // first create any nonexistent links
foreach($courseids as $courseid){ foreach($cohortids as $cohortid){
$course = get_course($courseid); $cohort = $DB->get_record('cohort',['id'=>$cohortid]);
// first create any nonexistent links
foreach($cohortids as $cohortid){
$cohort = $DB->get_record('cohort',['id'=>$cohortid]);
$instanceparams = [ $instanceparams = [
'courseid' => $courseid, 'courseid' => $courseid,
'customint1' => $cohortid, 'customint1' => $cohortid,
'enrol' => self::METHOD, 'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"), 'roleid' => get_config("local_treestudyplan","csync_roleid"),
]; ];
$instancenewparams = [ $instancenewparams = [
'customint1' => $cohortid, 'customint1' => $cohortid,
'enrol' => self::METHOD, 'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"), 'roleid' => get_config("local_treestudyplan","csync_roleid"),
]; ];
// Create group: // Create group:
// 1: check if a link exists // 1: check if a link exists
// If not, make it (maybe use some of the custom text to list the studyplans involved) // If not, make it (maybe use some of the custom text to list the studyplans involved)
if ($instance = $DB->get_record('enrol', $instanceparams)) { if ($instance = $DB->get_record('enrol', $instanceparams)) {
// it already exists // it already exists
// check if this studyplan is already referenced in customtext4 in json format // check if this studyplan is already referenced in customtext4 in json format
if(empty($instance->customtext4)){ if(empty($instance->customtext4)){
// Cohort sync was already done, but without studyplan info // Cohort sync was already done, but without studyplan info
// make sure we add a "manual" link to the plan list // make sure we add a "manual" link to the plan list
$plans = ["manual"]; $plans = ["manual"];
} }
else{ else{
$plans = json_decode($instance->customtext4); $plans = json_decode($instance->customtext4);
} }
if(!$in_array($this->studyplanid ,$plans)){ if(!in_array($this->studyplanid ,$plans)){
// if not, add it to the reference // if not, add it to the reference
$plans[] = $this->studyplanid; $plans[] = $this->studyplanid;
$enrol->update_instance($instance,["customtext4"=>json_encode($plans)]); $enrol->update_instance($instance,["customtext4"=>json_encode($plans)]);
} }
} else {
// If method members should be added to a group, create it or get its ID.
if (get_config("local_treestudyplan","csync_creategroup")) {
// Make or get new new cohort group - but only on creating of instances
$groupname = $cohort->name." ".strtolower(get_string('defaultgroupname', 'core'));
// and make sure the
$instancenewparams['customint2'] = uploadenrolmentmethods_get_group($courseid, $groupname);
}
if ($instanceid = $enrol->add_instance($course, $instancenewparams)) {
// also record the (as of yet only) studyplans id requiring this association
// in the customtext4 field in json format
$enrol->update_instance($instance,["customtext4"=>json_encode([$this->studyplanid])]);
// Successfully added a valid new instance, so now instantiate it.
// First synchronise the enrolment.
$cohorttrace = new \null_progress_trace();
enrol_cohort_sync($cohorttrace, $cohortid);
$cohorttrace->finished();
} else { } else {
// If method members should be added to a group, create it or get its ID. // Instance not added for some reason, so report an error somewhere
// (or not)
}
}
}
if (get_config("local_treestudyplan","csync_creategroup")) { // 2: Check if there are cohort links for this studyplan in this course that should be removed
// Make or get new new cohort group - but only on creating of instances // A: Check if there are cohort links that are no longer related to this studyplan
$groupname = $cohort->name." ".strtolower(get_string('defaultgroupname', 'core')); // B: Check if these links are valid through another studyplan...
// and make sure the // If no one uses the link anymore, deactivate it...
$instancenewparams['customint2'] = uploadenrolmentmethods_get_group($courseid, $groupname); // INFO: This does not remove the sync from courses that are unlinked from a studplan. But maybe we do not want that anyway
} // since it is generally a good idea to keep student access to courses available
if ($instanceid = $enrol->add_instance($course, $instancenewparams)) {
// also record the (as of yet only) studyplans id requiring this association
// in the customtext4 field in json format
$enrol->update_instance($instance,["customtext4"=>json_encode([$this->studyplanid])]);
// Successfully added a valid new instance, so now instantiate it.
// First synchronise the enrolment.
$cohorttrace = new null_progress_trace();
enrol_cohort_sync($cohorttrace, $cohortid);
$cohorttrace->finished();
} else {
// Instance not added for some reason, so report an error somewhere
// (or not)
}
}
}
// 2: Check if there are cohort links for this studyplan in this course that should be removed if(get_config("local_treestudyplan","csync_autoremove")){
// A: Check if there are cohort links that are no longer related to this studyplan // Only try the autoremove if the option is enabled
// B: Check if these links are valid through another studyplan...
// If no one uses the link anymore, deactivate it...
// INFO: This does not remove the sync from courses that are unlinked from a studplan. But maybe we do not want that anyway
// since it is generally a good idea to keep student access to courses available
if(get_config("local_treestudyplan","csync_autoremove")){ // find all cohort syncs for this course
// Only try the autoremove if the option is enabled $searchparams = [
'courseid' => $courseid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
// find all cohort syncs for this course $records = $DB->get_records("enrol",$searchparams);
$searchparams = [ foreach($records as $instance){
'courseid' => $courseid, if(!empty($instance->customtext4)){ // only check the records that have studyplan information in the customtext4 field
'enrol' => self::METHOD, // first check if the cohort is not one of the cohort id's we have associated
'roleid' => get_config("local_treestudyplan","csync_roleid"), if(!in_array($instance->customint1,$cohortids)){
]; // So it may or may not need to be removed
$plans = json_decode($instance->customtext4);
$records = $DB->get_records("enrol",$searchparams); if($plans !== null && is_array($plans)){
foreach($records as $instance){ //if a valid array is not returned, better leave it be, we don't want to mess with it
if(!empty($instance->customtext4)){ // only check the records that have studyplan information in the customtext4 field // otherwise, check if we should remove it
// first check if the cohort is not one of the cohort id's we have associated if(in_array($this->studyplanid,$plans)){
if(!in_array($instance->customint1,$cohortids)){ //if this plan was referenced before
// So it may or may not need to be removed // first remove the link
$plans = json_decode($instance->customtext4); self::array_remove_value($plans,$this->studyplanid);
if($plans !== null && is_array($plans)){ if(count($plans) == 0){
//if a valid array is not returned, better leave it be, we don't want to mess with it // delete the sync if there are no studyplan references left
// otherwise, check if we should remove it $enrol->delete_instance($instance);
if(in_array($this->studyplanid,$plans)){ } else {
//if this plan was referenced before // otherwise just update the references so this studyplan is no longer linked
// first remove the link $enrol->update_instance($instance,["customtext4"=>json_encode($plans)]);
self::array_remove_value($plans,$this->studyplanid);
if(count($plans) == 0){
// delete the sync if there are no studyplan references left
$enrol->delete_instance($instance);
} else {
// otherwise just update the references so this studyplan is no longer linked
$enrol->update_instance($instance,["customtext4"=>json_encode($plans)]);
}
} }
} }
} }

View file

@ -59,9 +59,9 @@ $string['settingdesc_display_field'] = 'Select the field to use for the display
$string['settingspage_csync'] = 'Studyplan cohort sync'; $string['settingspage_csync'] = 'Studyplan cohort sync';
$string['setting_csync_heading'] = 'Automatically create a cohort sync in all courses linked to a studyplan for all cohorts linked to a studyplan'; $string['setting_csync_heading'] = 'Automatically create a cohort sync in all courses linked to a studyplan for all cohorts linked to a studyplan';
$string['settingdesc_csync_heading'] = ''; $string['settingdesc_csync_heading'] = '';
$string['setting_csync_enable_field'] = 'Enable'; $string['setting_csync_enable_field'] = 'Automatic cohort sync';
$string['settingdesc_csync_enable_field'] = 'Enable automatic cohort sync'; $string['settingdesc_csync_enable_field'] = 'Enable automatic cohort sync';
$string['setting_csync_autoremove_field'] = 'Automatisch deletion'; $string['setting_csync_autoremove_field'] = 'Automatic deletion';
$string['settingdesc_csync_autoremove_field'] = 'Delete syncronization from courses once a cohort is removed from the studyplan'; $string['settingdesc_csync_autoremove_field'] = 'Delete syncronization from courses once a cohort is removed from the studyplan';
$string['setting_csync_creategroup_field'] = 'Create groups'; $string['setting_csync_creategroup_field'] = 'Create groups';
$string['settingdesc_csync_creategroup_field'] = 'Create a group in the course for each cohort sync'; $string['settingdesc_csync_creategroup_field'] = 'Create a group in the course for each cohort sync';

View file

@ -61,7 +61,7 @@ $string['settingdesc_display_field'] = 'Kies welk veld gebruikt moet worden als
$string['settingspage_csync'] = 'Studieplan site-groep synchronisatie'; $string['settingspage_csync'] = 'Studieplan site-groep synchronisatie';
$string['setting_csync_heading'] = 'Site-groepen die aan een studieplan gekoppeld zijn automatisch als site-groep synchronisatie koppelen aan alle cursussen in het studieplan.'; $string['setting_csync_heading'] = 'Site-groepen die aan een studieplan gekoppeld zijn automatisch als site-groep synchronisatie koppelen aan alle cursussen in het studieplan.';
$string['settingdesc_csync_heading'] = ''; $string['settingdesc_csync_heading'] = '';
$string['setting_csync_enable_field'] = 'Inschakelen'; $string['setting_csync_enable_field'] = 'Automatisch koppelen inschakelen';
$string['settingdesc_csync_enable_field'] = 'Automatische koppelen van site-groep synchronisatie inschakelen'; $string['settingdesc_csync_enable_field'] = 'Automatische koppelen van site-groep synchronisatie inschakelen';
$string['setting_csync_autoremove_field'] = 'Automatisch verwijderen'; $string['setting_csync_autoremove_field'] = 'Automatisch verwijderen';
$string['settingdesc_csync_autoremove_field'] = 'Verwijder sychronisaties in cursussen als deze uit het studieplan worden verwijderd'; $string['settingdesc_csync_autoremove_field'] = 'Verwijder sychronisaties in cursussen als deze uit het studieplan worden verwijderd';