2023-06-16 23:12:25 +02:00
|
|
|
<?php
|
|
|
|
namespace local_treestudyplan;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
use \local_treestudyplan\studyplan;
|
|
|
|
|
|
|
|
class enrolcohortsync {
|
2023-06-19 22:48:33 +02:00
|
|
|
private const METHOD = "cohort";
|
2023-06-16 23:12:25 +02:00
|
|
|
private $studyplan;
|
|
|
|
|
|
|
|
function __construct($studyplan){
|
|
|
|
$this->studyplan = $studyplan;
|
2023-06-19 22:48:33 +02:00
|
|
|
$this->studyplanid = $studyplan->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
static private function array_remove_value(&$array,$value){
|
|
|
|
while (($key = array_search($value, $array)) !== false) {
|
|
|
|
unset($array[$key]);
|
|
|
|
}
|
2023-06-16 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function sync(){
|
2023-06-19 22:48:33 +02:00
|
|
|
global $DB;
|
|
|
|
// 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
|
2023-06-16 23:12:25 +02:00
|
|
|
|
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
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();
|
2023-06-16 23:12:25 +02:00
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
// And find the cohorts that are linked to this studyplan.
|
|
|
|
$cohortids = $this->studyplan->get_linked_cohort_ids();
|
2023-06-16 23:12:25 +02:00
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
// 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"),
|
|
|
|
];
|
|
|
|
|
|
|
|
$instancenewparams = [
|
|
|
|
'customint1' => $cohortid,
|
|
|
|
'enrol' => self::METHOD,
|
|
|
|
'roleid' => get_config("local_treestudyplan","csync_roleid"),
|
|
|
|
];
|
2023-06-16 23:12:25 +02:00
|
|
|
|
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
// Create group:
|
2023-06-16 23:12:25 +02:00
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
// 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)]);
|
|
|
|
}
|
2023-06-16 23:12:25 +02:00
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
} 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 {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
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"),
|
|
|
|
];
|
|
|
|
|
|
|
|
$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)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 23:12:25 +02:00
|
|
|
}
|