/** * 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 (!empty($data->customint2)) { $data->customint2 = $step->get_mappingid('group', $data->customint2); } 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->id; } else { $instanceid = $this->add_instance($course, (array) $data); } $step->set_mapping('enrol', $oldid, $instanceid); require_once "{$CFG->dirroot}/enrol/delayedcohort/locallib.php"; $trace = new null_progress_trace(); enrol_delayedcohort_sync($trace, $course->id); $trace->finished(); } 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->id; } else { $data->status = ENROL_INSTANCE_DISABLED; $instanceid = $this->add_instance($course, (array) $data); } $step->set_mapping('enrol', $oldid, $instanceid); require_once "{$CFG->dirroot}/enrol/delayedcohort/locallib.php"; $trace = new null_progress_trace(); enrol_delayedcohort_sync($trace, $course->id); $trace->finished(); } else { $step->set_mapping('enrol', $oldid, 0); } } }
// Some browsers reorder collections by key. $outcome->response['cohorts'] = array_values($outcome->response['cohorts']); break; case 'enrolcohort': require_capability('moodle/course:enrolconfig', $context); require_capability('enrol/cohort:config', $context); $roleid = required_param('roleid', PARAM_INT); $cohortid = required_param('cohortid', PARAM_INT); $roles = $manager->get_assignable_roles(); if (!enrol_delayedcohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) { throw new enrol_ajax_exception('errorenrolcohort'); } $enrol = enrol_get_plugin('delayedcohort'); $enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid)); $trace = new null_progress_trace(); enrol_delayedcohort_sync($trace, $manager->get_course()->id); $trace->finished(); break; case 'enrolcohortusers': //TODO: this should be moved to enrol_manual, see MDL-35618. require_capability('enrol/manual:enrol', $context); $roleid = required_param('roleid', PARAM_INT); $cohortid = required_param('cohortid', PARAM_INT); $roles = $manager->get_assignable_roles(); if (!enrol_delayedcohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) { throw new enrol_ajax_exception('errorenrolcohort'); } $result = enrol_delayedcohort_enrol_all_users($manager, $cohortid, $roleid); if ($result === false) { throw new enrol_ajax_exception('errorenrolcohortusers'); }
public function test_sync_all_courses() { global $DB; $this->resetAfterTest(); $trace = new null_progress_trace(); // Setup a few courses and categories. $cohortplugin = enrol_get_plugin('delayedcohort'); $manualplugin = enrol_get_plugin('manual'); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->assertNotEmpty($studentrole); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->assertNotEmpty($teacherrole); $managerrole = $DB->get_record('role', array('shortname' => 'manager')); $this->assertNotEmpty($managerrole); $cat1 = $this->getDataGenerator()->create_category(); $cat2 = $this->getDataGenerator()->create_category(); $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); $course3 = $this->getDataGenerator()->create_course(array('category' => $cat2->id)); $course4 = $this->getDataGenerator()->create_course(array('category' => $cat2->id)); $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat1->id)->id)); $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat2->id)->id)); $cohort3 = $this->getDataGenerator()->create_cohort(); $this->disable_plugin(); // Prevents event sync. $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); $this->assertEquals(2, $DB->count_records('role_assignments', array())); $this->assertEquals(2, $DB->count_records('user_enrolments', array())); $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort1->id, 'roleid' => $studentrole->id)); $cohortinstance1 = $DB->get_record('enrol', array('id' => $id)); $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort2->id, 'roleid' => $teacherrole->id)); $cohortinstance2 = $DB->get_record('enrol', array('id' => $id)); $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id)); $cohortinstance3 = $DB->get_record('enrol', array('id' => $id)); cohort_add_member($cohort1->id, $user1->id); cohort_add_member($cohort1->id, $user2->id); cohort_add_member($cohort1->id, $user4->id); cohort_add_member($cohort2->id, $user3->id); cohort_add_member($cohort3->id, $user3->id); $this->assertEquals(2, $DB->count_records('role_assignments', array())); $this->assertEquals(2, $DB->count_records('user_enrolments', array())); // Test sync of one course only. enrol_delayedcohort_sync($trace, null); $this->assertEquals(2, $DB->count_records('role_assignments', array())); $this->assertEquals(2, $DB->count_records('user_enrolments', array())); $this->enable_plugin(); enrol_delayedcohort_sync($trace, null); $this->assertEquals(7, $DB->count_records('user_enrolments', array())); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user1->id))); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user2->id))); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user4->id))); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance2->id, 'userid' => $user3->id))); $this->assertEquals(7, $DB->count_records('role_assignments', array())); $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user1->id, 'roleid' => $studentrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user2->id, 'roleid' => $studentrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user4->id, 'roleid' => $studentrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user3->id, 'roleid' => $teacherrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance2->id))); $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); $DB->delete_records('cohort_members', array('cohortid' => $cohort2->id, 'userid' => $user3->id)); // Use low level DB api to prevent events! enrol_delayedcohort_sync($trace, $course1->id); $this->assertEquals(7, $DB->count_records('user_enrolments', array())); $this->assertEquals(6, $DB->count_records('role_assignments', array())); $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user3->id, 'roleid' => $teacherrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance2->id))); $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); $DB->delete_records('cohort_members', array('cohortid' => $cohort1->id, 'userid' => $user1->id)); // Use low level DB api to prevent events! enrol_delayedcohort_sync($trace, $course1->id); $this->assertEquals(5, $DB->count_records('user_enrolments', array())); $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance2->id, 'userid' => $user3->id))); $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user1->id))); $this->assertEquals(5, $DB->count_records('role_assignments', array())); $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user3->id, 'roleid' => $teacherrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance2->id))); $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user1->id, 'roleid' => $studentrole->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); $DB->delete_records('cohort_members', array('cohortid' => $cohort1->id)); // Use low level DB api to prevent events! $DB->delete_records('cohort', array('id' => $cohort1->id)); // Use low level DB api to prevent events! enrol_delayedcohort_sync($trace, $course1->id); $this->assertEquals(5, $DB->count_records('user_enrolments', array())); $this->assertEquals(3, $DB->count_records('role_assignments', array())); $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); enrol_delayedcohort_sync($trace, $course1->id); $this->assertEquals(3, $DB->count_records('user_enrolments', array())); $this->assertEquals(3, $DB->count_records('role_assignments', array())); // Test group sync. $this->disable_plugin(); // No event sync $id = groups_create_group((object) array('name' => 'Group 1', 'courseid' => $course1->id)); $group1 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST); $id = groups_create_group((object) array('name' => 'Group 2', 'courseid' => $course1->id)); $group2 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST); $id = groups_create_group((object) array('name' => 'Group 2', 'courseid' => $course2->id)); $group3 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat1->id)->id)); $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort1->id, 'roleid' => $studentrole->id, 'customint2' => $group1->id)); $cohortinstance1 = $DB->get_record('enrol', array('id' => $id)); $this->assertTrue(groups_add_member($group1, $user4)); $this->assertTrue(groups_add_member($group2, $user4)); $this->assertEquals(3, $DB->count_records('user_enrolments', array())); $this->assertEquals(3, $DB->count_records('role_assignments', array())); $this->assertFalse(groups_is_member($group1->id, $user1->id)); cohort_add_member($cohort1->id, $user1->id); cohort_add_member($cohort1->id, $user4->id); cohort_add_member($cohort2->id, $user4->id); cohort_add_member($cohort2->id, $user3->id); $this->enable_plugin(); enrol_delayedcohort_sync($trace, null); $this->assertEquals(8, $DB->count_records('user_enrolments', array())); $this->assertEquals(8, $DB->count_records('role_assignments', array())); $this->assertTrue(groups_is_member($group1->id, $user1->id)); $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4)); $this->assertTrue(groups_is_member($group1->id, $user4->id)); $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user4->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3)); $this->assertFalse(groups_is_member($group3->id, $user3->id)); $cohortinstance1->customint2 = $group2->id; $DB->update_record('enrol', $cohortinstance1); $cohortinstance3->customint2 = $group3->id; $DB->update_record('enrol', $cohortinstance3); enrol_delayedcohort_sync($trace, null); $this->assertFalse(groups_is_member($group1->id, $user1->id)); $this->assertTrue(groups_is_member($group2->id, $user1->id)); $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user1->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue(groups_is_member($group1->id, $user4->id)); $this->assertTrue(groups_is_member($group2->id, $user4->id)); $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user4->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group2->id, 'userid' => $user4->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance1->id))); $this->assertTrue(groups_is_member($group3->id, $user3->id)); $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group3->id, 'userid' => $user3->id, 'component' => 'enrol_delayedcohort', 'itemid' => $cohortinstance3->id))); cohort_remove_member($cohort1->id, $user1->id); $this->assertFalse(groups_is_member($group1->id, $user1->id)); cohort_remove_member($cohort1->id, $user4->id); $this->assertTrue(groups_is_member($group1->id, $user4->id)); $this->assertTrue(groups_is_member($group2->id, $user4->id)); }
if ($data = $mform->get_data()) { if ($data->id) { // NOTE: no cohort changes here!!! if ($data->roleid != $instance->roleid) { // The sync script can only add roles, for perf reasons it does not modify them. role_unassign_all(array('contextid' => $context->id, 'roleid' => $instance->roleid, 'component' => 'enrol_delayedcohort', 'itemid' => $instance->id)); } $instance->name = $data->name; $instance->status = $data->status; $instance->roleid = $data->roleid; $instance->customint2 = $data->customint2; $instance->customint3 = $data->customint3; // trigger date $instance->customint4 = $data->customint4; // end date $instance->timemodified = time(); $DB->update_record('enrol', $instance); } else { $enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status, 'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $data->customint2)); } $trace = new null_progress_trace(); enrol_delayedcohort_sync($trace, $course->id); $trace->finished(); redirect($returnurl); } } $PAGE->set_heading($course->fullname); $PAGE->set_title(get_string('pluginname', 'enrol_delayedcohort')); echo $OUTPUT->header(); $mform->display(); echo $OUTPUT->footer();