MDL-27856 support restore of cohort in the same site

This commit is contained in:
Petr Škoda
2012-09-07 11:13:27 +02:00
parent 7a7b8a1f19
commit 94335e5a97
3 changed files with 92 additions and 6 deletions
+17 -4
View File
@@ -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;
}
}
+1
View File
@@ -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.';
+74 -2
View File
@@ -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);
}
}
}