From 94335e5a97b0f613db9976d0937f3bf89dd8aa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 2 Sep 2012 10:27:04 +0200 Subject: [PATCH] MDL-27856 support restore of cohort in the same site --- enrol/cohort/addinstance_form.php | 21 ++++++-- enrol/cohort/lang/en/enrol_cohort.php | 1 + enrol/cohort/lib.php | 76 ++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/enrol/cohort/addinstance_form.php b/enrol/cohort/addinstance_form.php index c2094937c0c..7f65b126a59 100644 --- a/enrol/cohort/addinstance_form.php +++ b/enrol/cohort/addinstance_form.php @@ -29,12 +29,15 @@ defined('MOODLE_INTERNAL') || die(); require_once("$CFG->libdir/formslib.php"); class enrol_cohort_addinstance_form extends moodleform { + + protected $course; + function definition() { global $CFG, $DB; $mform = $this->_form; - $course = $this->_customdata; - $coursecontext = context_course::instance($course->id); + $this->course = $this->_customdata; + $coursecontext = context_course::instance($this->course->id); $enrol = enrol_get_plugin('cohort'); @@ -72,8 +75,18 @@ class enrol_cohort_addinstance_form extends moodleform { $this->add_action_buttons(true, get_string('addinstance', 'enrol')); - $this->set_data(array('id'=>$course->id)); + $this->set_data(array('id'=>$this->course->id)); } - //TODO: validate duplicate role-cohort does not exist + function validation($data, $files) { + global $DB; + + $errors = parent::validation($data, $files); + + if ($DB->record_exists('enrol', array('roleid'=>$data['roleid'], 'customint1'=>$data['cohortid'], 'courseid'=>$this->course->id, 'enrol'=>'cohort'))) { + $errors['cohortid'] = get_string('instanceexists', 'enrol_cohort'); + } + + return $errors; + } } diff --git a/enrol/cohort/lang/en/enrol_cohort.php b/enrol/cohort/lang/en/enrol_cohort.php index 8196bb09bc7..5b5f79a8f9b 100644 --- a/enrol/cohort/lang/en/enrol_cohort.php +++ b/enrol/cohort/lang/en/enrol_cohort.php @@ -28,5 +28,6 @@ $string['ajaxmore'] = 'More...'; $string['cohortsearch'] = 'Search'; $string['cohort:config'] = 'Configure cohort instances'; $string['cohort:unenrol'] = 'Unenrol suspended users'; +$string['instanceexists'] = 'Cohort is already synchronised with selected role'; $string['pluginname'] = 'Cohort sync'; $string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.'; diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index 4ad304eb17b..f116b28cf1a 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -224,6 +224,78 @@ class enrol_cohort_plugin extends enrol_plugin { return $button; } + + /** + * Restore instance and map settings. + * + * @param restore_enrolments_structure_step $step + * @param stdClass $data + * @param stdClass $course + * @param int $oldid + */ + public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { + global $DB, $CFG; + + if (!$step->get_task()->is_samesite()) { + // No cohort restore from other sites. + $step->set_mapping('enrol', $oldid, 0); + return; + } + + if ($data->roleid and $DB->record_exists('cohort', array('id'=>$data->customint1))) { + $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name())); + if ($instance) { + $instanceid = $instance; + } else { + $instanceid = $this->add_instance($course, (array)$data); + } + $step->set_mapping('enrol', $oldid, $instanceid); + + require_once("$CFG->dirroot/enrol/cohort/locallib.php"); + enrol_cohort_sync($course->id, false); + + } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES) { + $data->customint1 = 0; + $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name())); + + if ($instance) { + $instanceid = $instance; + } else { + $data->status = ENROL_INSTANCE_DISABLED; + $instanceid = $this->add_instance($course, (array)$data); + } + $step->set_mapping('enrol', $oldid, $instanceid); + + require_once("$CFG->dirroot/enrol/cohort/locallib.php"); + enrol_cohort_sync($course->id, false); + + } else { + $step->set_mapping('enrol', $oldid, 0); + } + } + + /** + * Restore user enrolment. + * + * @param restore_enrolments_structure_step $step + * @param stdClass $data + * @param stdClass $instance + * @param int $oldinstancestatus + * @param int $userid + */ + public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { + global $DB; + + if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) { + // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers. + return; + } + + // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored + // but without roles and suspended. + + if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) { + $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED); + } + } } - -