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 {
private const METHOD = "cohort";
private $studyplan;
private $studyplanid;
function __construct($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
// 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")){
$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();
// And find the cohorts that are linked to this studyplan.
$cohortids = $this->studyplan->get_linked_cohort_ids();
// And find the cohorts that are linked to this studyplan.
$cohortids = $this->studyplan->get_linked_cohort_ids();
// Next, for each course that is linked:
foreach($courseids as $courseid){
$course = get_course($courseid);
// first create any nonexistent links
foreach($cohortids as $cohortid){
$cohort = $DB->get_record('cohort',['id'=>$cohortid]);
// Next, for each course that is linked:
foreach($courseids as $courseid){
$course = get_course($courseid);
// first create any nonexistent links
foreach($cohortids as $cohortid){
$cohort = $DB->get_record('cohort',['id'=>$cohortid]);
$instanceparams = [
'courseid' => $courseid,
'customint1' => $cohortid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
$instanceparams = [
'courseid' => $courseid,
'customint1' => $cohortid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
$instancenewparams = [
'customint1' => $cohortid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
$instancenewparams = [
'customint1' => $cohortid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
// Create group:
// Create group:
// 1: check if a link exists
// If not, make it (maybe use some of the custom text to list the studyplans involved)
if ($instance = $DB->get_record('enrol', $instanceparams)) {
// it already exists
// check if this studyplan is already referenced in customtext4 in json format
if(empty($instance->customtext4)){
// Cohort sync was already done, but without studyplan info
// make sure we add a "manual" link to the plan list
$plans = ["manual"];
}
else{
$plans = json_decode($instance->customtext4);
}
if(!$in_array($this->studyplanid ,$plans)){
// if not, add it to the reference
$plans[] = $this->studyplanid;
$enrol->update_instance($instance,["customtext4"=>json_encode($plans)]);
}
// 1: check if a link exists
// If not, make it (maybe use some of the custom text to list the studyplans involved)
if ($instance = $DB->get_record('enrol', $instanceparams)) {
// it already exists
// check if this studyplan is already referenced in customtext4 in json format
if(empty($instance->customtext4)){
// Cohort sync was already done, but without studyplan info
// make sure we add a "manual" link to the plan list
$plans = ["manual"];
}
else{
$plans = json_decode($instance->customtext4);
}
if(!in_array($this->studyplanid ,$plans)){
// if not, add it to the reference
$plans[] = $this->studyplanid;
$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 {
// 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")) {
// 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 {
// 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
// A: Check if there are cohort links that are no longer related to this studyplan
// 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
// 2: Check if there are cohort links for this studyplan in this course that should be removed
// A: Check if there are cohort links that are no longer related to this studyplan
// 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")){
// Only try the autoremove if the option is enabled
if(get_config("local_treestudyplan","csync_autoremove")){
// Only try the autoremove if the option is enabled
// find all cohort syncs for this course
$searchparams = [
'courseid' => $courseid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
// find all cohort syncs for this course
$searchparams = [
'courseid' => $courseid,
'enrol' => self::METHOD,
'roleid' => get_config("local_treestudyplan","csync_roleid"),
];
$records = $DB->get_records("enrol",$searchparams);
foreach($records as $instance){
if(!empty($instance->customtext4)){ // only check the records that have studyplan information in the customtext4 field
// first check if the cohort is not one of the cohort id's we have associated
if(!in_array($instance->customint1,$cohortids)){
// So it may or may not need to be removed
$plans = json_decode($instance->customtext4);
if($plans !== null && is_array($plans)){
//if a valid array is not returned, better leave it be, we don't want to mess with it
// otherwise, check if we should remove it
if(in_array($this->studyplanid,$plans)){
//if this plan was referenced before
// first remove the link
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)]);
}
$records = $DB->get_records("enrol",$searchparams);
foreach($records as $instance){
if(!empty($instance->customtext4)){ // only check the records that have studyplan information in the customtext4 field
// first check if the cohort is not one of the cohort id's we have associated
if(!in_array($instance->customint1,$cohortids)){
// So it may or may not need to be removed
$plans = json_decode($instance->customtext4);
if($plans !== null && is_array($plans)){
//if a valid array is not returned, better leave it be, we don't want to mess with it
// otherwise, check if we should remove it
if(in_array($this->studyplanid,$plans)){
//if this plan was referenced before
// first remove the link
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['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['setting_csync_enable_field'] = 'Enable';
$string['setting_csync_enable_field'] = '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['setting_csync_creategroup_field'] = 'Create groups';
$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['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['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['setting_csync_autoremove_field'] = 'Automatisch verwijderen';
$string['settingdesc_csync_autoremove_field'] = 'Verwijder sychronisaties in cursussen als deze uit het studieplan worden verwijderd';