function xmldb_enrol_meta_install() { global $CFG, $DB; if (isset($CFG->nonmetacoursesyncroleids)) { set_config('nosyncroleids', $CFG->nonmetacoursesyncroleids, 'enrol_meta'); unset_config('nonmetacoursesyncroleids'); } if (!$DB->record_exists('enrol', array('enrol' => 'meta'))) { // no need to syn roles and enrolments return; } // brute force course resync, this may take a while require_once "{$CFG->dirroot}/enrol/meta/locallib.php"; enrol_meta_sync(); }
/** * Test that enrolment timestart-timeend is respected in meta course. */ public function test_timeend() { global $CFG, $DB; $this->resetAfterTest(true); $timeinfuture = time() + DAYSECS; $timeinpast = time() - DAYSECS; $metalplugin = enrol_get_plugin('meta'); $manplugin = enrol_get_plugin('manual'); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $course3 = $this->getDataGenerator()->create_course(); $manual1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $student = $DB->get_record('role', array('shortname' => 'student')); $this->enable_plugin(); // Create instance of enrol_meta in course2 when there are no enrolments present. $meta2id = $metalplugin->add_instance($course2, array('customint1' => $course1->id)); $expectedenrolments = array($user1->id => array(0, 0, ENROL_USER_ACTIVE), $user2->id => array($timeinpast, 0, ENROL_USER_ACTIVE), $user3->id => array(0, $timeinfuture, ENROL_USER_ACTIVE), $user4->id => array($timeinpast, $timeinfuture, ENROL_USER_ACTIVE), $user5->id => array(0, 0, ENROL_USER_SUSPENDED)); foreach ($expectedenrolments as $userid => $data) { $expectedenrolments[$userid] = (object) (array('userid' => $userid) + array_combine(array('timestart', 'timeend', 'status'), $data)); } // Enrol users manually in course 1. foreach ($expectedenrolments as $e) { $manplugin->enrol_user($manual1, $e->userid, $student->id, $e->timestart, $e->timeend, $e->status); } $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $manual1->id), 'userid', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); // Make sure that the same enrolments are now present in course2 under meta enrolment. $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); // Create instance of enrol_meta in course3 and run sync. $meta3id = $metalplugin->add_instance($course3, array('customint1' => $course1->id)); enrol_meta_sync($course3->id); // Make sure that the same enrolments are now present in course3 under meta enrolment. $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); // Update some of the manual enrolments. $expectedenrolments[$user2->id]->timestart = $timeinpast - 60; $expectedenrolments[$user3->id]->timeend = $timeinfuture + 60; $expectedenrolments[$user4->id]->status = ENROL_USER_SUSPENDED; $expectedenrolments[$user5->id]->status = ENROL_USER_ACTIVE; foreach ($expectedenrolments as $e) { $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend); } // Make sure meta courses are also updated. $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); // Test meta sync. Imagine events are not working. $sink = $this->redirectEvents(); $expectedenrolments[$user2->id]->timestart = $timeinpast; $expectedenrolments[$user3->id]->timeend = $timeinfuture; $expectedenrolments[$user4->id]->status = ENROL_USER_ACTIVE; $expectedenrolments[$user5->id]->status = ENROL_USER_SUSPENDED; foreach ($expectedenrolments as $e) { $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend); } // Make sure meta courses are updated only for the course that was synced. enrol_meta_sync($course3->id); $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status'); $this->assertNotEquals($expectedenrolments, $enrolments); $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); $sink->close(); // Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended. $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED); $allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED)); $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status'); $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses); $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED); $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); // Disable events and repeat the same for course3 (testing sync): $sink = $this->redirectEvents(); $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED); enrol_meta_sync($course3->id); $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status'); $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses); $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED); enrol_meta_sync($course3->id); $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status'); $this->assertEquals($expectedenrolments, $enrolments); $sink->close(); }
$PAGE->set_url('/enrol/meta/addinstance.php', array('id' => $course->id)); $PAGE->set_pagelayout('admin'); navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id))); require_login($course); require_capability('moodle/course:enrolconfig', $context); $enrol = enrol_get_plugin('meta'); if (!$enrol->get_newinstance_link($course->id)) { redirect(new moodle_url('/enrol/instances.php', array('id' => $course->id))); } $mform = new enrol_meta_addinstance_form(NULL, $course); if ($mform->is_cancelled()) { redirect(new moodle_url('/enrol/instances.php', array('id' => $course->id))); } else { if ($data = $mform->get_data()) { $eid = $enrol->add_instance($course, array('customint1' => $data->link)); enrol_meta_sync($course->id); if (!empty($data->submitbuttonnext)) { redirect(new moodle_url('/enrol/meta/addinstance.php', array('id' => $course->id, 'message' => 'added'))); } else { redirect(new moodle_url('/enrol/instances.php', array('id' => $course->id))); } } } $PAGE->set_heading($course->fullname); $PAGE->set_title(get_string('pluginname', 'enrol_meta')); echo $OUTPUT->header(); if ($message === 'added') { echo $OUTPUT->notification(get_string('instanceadded', 'enrol'), 'notifysuccess'); } $mform->display(); echo $OUTPUT->footer();
public function test_sync() { global $CFG, $DB; $this->resetAfterTest(true); $metalplugin = enrol_get_plugin('meta'); $manplugin = enrol_get_plugin('manual'); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $course3 = $this->getDataGenerator()->create_course(); $course4 = $this->getDataGenerator()->create_course(); $manual1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manual2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manual3 = $DB->get_record('enrol', array('courseid' => $course3->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manual4 = $DB->get_record('enrol', array('courseid' => $course4->id, 'enrol' => 'manual'), '*', MUST_EXIST); $student = $DB->get_record('role', array('shortname' => 'student')); $teacher = $DB->get_record('role', array('shortname' => 'teacher')); $manager = $DB->get_record('role', array('shortname' => 'manager')); $this->disable_plugin(); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $student->id); $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 0); $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id); $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $manager->id); $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id); $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $teacher->id); $this->assertEquals(7, $DB->count_records('user_enrolments')); $this->assertEquals(6, $DB->count_records('role_assignments')); set_config('syncall', 0, 'enrol_meta'); set_config('nosyncroleids', $manager->id, 'enrol_meta'); require_once $CFG->dirroot . '/enrol/meta/locallib.php'; enrol_meta_sync(null, false); $this->assertEquals(7, $DB->count_records('user_enrolments')); $this->assertEquals(6, $DB->count_records('role_assignments')); $this->enable_plugin(); enrol_meta_sync(null, false); $this->assertEquals(7, $DB->count_records('user_enrolments')); $this->assertEquals(6, $DB->count_records('role_assignments')); $e1 = $metalplugin->add_instance($course3, array('customint1' => $course1->id)); $e2 = $metalplugin->add_instance($course3, array('customint1' => $course2->id)); $e3 = $metalplugin->add_instance($course4, array('customint1' => $course2->id)); $enrol1 = $DB->get_record('enrol', array('id' => $e1)); $enrol2 = $DB->get_record('enrol', array('id' => $e2)); $enrol3 = $DB->get_record('enrol', array('id' => $e3)); enrol_meta_sync($course4->id, false); $this->assertEquals(9, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher)); enrol_meta_sync(null, false); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(13, $DB->count_records('role_assignments')); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $this->assertTrue($this->is_meta_enrolled($user2, $enrol1, $student)); $this->assertFalse($this->is_meta_enrolled($user3, $enrol1)); $this->assertTrue($this->is_meta_enrolled($user4, $enrol1, $teacher)); $this->assertFalse($this->is_meta_enrolled($user5, $enrol1)); $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student)); $this->assertTrue($this->is_meta_enrolled($user2, $enrol2, $teacher)); $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher)); set_config('syncall', 1, 'enrol_meta'); enrol_meta_sync(null, false); $this->assertEquals(16, $DB->count_records('user_enrolments')); $this->assertEquals(13, $DB->count_records('role_assignments')); $this->assertTrue($this->is_meta_enrolled($user3, $enrol1, false)); $this->assertTrue($this->is_meta_enrolled($user5, $enrol1, false)); $this->assertEquals(16, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->disable_plugin(); $manplugin->unenrol_user($manual1, $user1->id); $manplugin->unenrol_user($manual2, $user1->id); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(11, $DB->count_records('role_assignments')); $this->assertEquals(14, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->enable_plugin(); set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta'); enrol_meta_sync($course4->id, false); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(11, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol3->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); enrol_meta_sync(null, false); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(11, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol1->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol2->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta'); enrol_meta_sync($course4->id, false); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, false)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol3->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); enrol_meta_sync(null, false); $this->assertEquals(14, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol1->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, false)); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrol2->id, 'status' => ENROL_USER_SUSPENDED, 'userid' => $user1->id))); set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); enrol_meta_sync($course4->id, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol3)); enrol_meta_sync(null, false); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); $this->assertFalse($this->is_meta_enrolled($user1, $enrol2)); // Now try sync triggered by events. set_config('syncall', 1, 'enrol_meta'); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $manplugin->unenrol_user($manual1, $user1->id); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); enrol_meta_sync(null, false); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); $manplugin->unenrol_user($manual1, $user1->id); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); enrol_meta_sync(null, false); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); set_config('syncall', 0, 'enrol_meta'); enrol_meta_sync(null, false); $this->assertEquals(9, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(9, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0); $this->assertEquals(10, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(10, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(10, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(10, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); role_assign($teacher->id, $user1->id, context_course::instance($course1->id)->id); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher)); enrol_meta_sync(null, false); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher)); role_unassign($teacher->id, $user1->id, context_course::instance($course1->id)->id); $this->assertEquals(10, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(10, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(10, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(10, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); $manplugin->unenrol_user($manual1, $user1->id); $this->assertEquals(9, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(9, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); set_config('syncall', 1, 'enrol_meta'); set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta'); enrol_meta_sync(null, false); $this->assertEquals(11, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $manplugin->update_user_enrol($manual1, $user1->id, ENROL_USER_SUSPENDED); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $manplugin->unenrol_user($manual1, $user1->id); $this->assertEquals(12, $DB->count_records('user_enrolments')); $this->assertEquals(9, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(12, $DB->count_records('user_enrolments')); $this->assertEquals(9, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); set_config('syncall', 1, 'enrol_meta'); set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta'); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); $manplugin->unenrol_user($manual1, $user1->id); $this->assertEquals(12, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); enrol_meta_sync(null, false); $this->assertEquals(12, $DB->count_records('user_enrolments')); $this->assertEquals(8, $DB->count_records('role_assignments')); $this->assertEquals(11, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); enrol_meta_sync(null, false); $this->assertEquals(13, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('role_assignments')); $this->assertEquals(13, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); delete_course($course1, false); $this->assertEquals(3, $DB->count_records('user_enrolments')); $this->assertEquals(3, $DB->count_records('role_assignments')); $this->assertEquals(3, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); enrol_meta_sync(null, false); $this->assertEquals(3, $DB->count_records('user_enrolments')); $this->assertEquals(3, $DB->count_records('role_assignments')); $this->assertEquals(3, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); delete_course($course2, false); $this->assertEquals(0, $DB->count_records('user_enrolments')); $this->assertEquals(0, $DB->count_records('role_assignments')); $this->assertEquals(0, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); enrol_meta_sync(null, false); $this->assertEquals(0, $DB->count_records('user_enrolments')); $this->assertEquals(0, $DB->count_records('role_assignments')); $this->assertEquals(0, $DB->count_records('user_enrolments', array('status' => ENROL_USER_ACTIVE))); delete_course($course3, false); delete_course($course4, false); }
/** * @param $subcourse * @return bool */ function subcourse_add_meta($subcourse) { global $CFG, $DB; require_once $CFG->dirroot . '/enrol/meta/locallib.php'; // Make a new enrolment instance $enrol = enrol_get_plugin('meta'); $course = $DB->get_record('course', array('id' => $subcourse->refcourse), '*', MUST_EXIST); $enrolid = $enrol->add_instance($course, array('customint1' => $subcourse->course)); enrol_meta_sync($course->id); return (bool) $enrolid; }
/** * 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 meta restore from other sites. $step->set_mapping('enrol', $oldid, 0); return; } if (!empty($data->customint2)) { $data->customint2 = $step->get_mappingid('group', $data->customint2); } if ($DB->record_exists('course', 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/meta/locallib.php"; enrol_meta_sync($data->customint1); } else { $step->set_mapping('enrol', $oldid, 0); } }
/** * CLI sync for meta course enrolments, use for debugging or immediate sync * of all courses. * * Notes: * - it is required to use the web server account when executing PHP CLI scripts * - you need to change the "www-data" to match the apache user account * - use "su" if "sudo" not available * * @package enrol_meta * @copyright 2011 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('CLI_SCRIPT', true); require __DIR__ . '/../../../config.php'; require_once $CFG->libdir . '/clilib.php'; require_once "{$CFG->dirroot}/enrol/meta/locallib.php"; // now get cli options list($options, $unrecognized) = cli_get_params(array('verbose' => false, 'help' => false), array('v' => 'verbose', 'h' => 'help')); if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); } if ($options['help']) { $help = "Execute meta course enrol sync.\n\nOptions:\n-v, --verbose Print verbose progess information\n-h, --help Print out this help\n\nExample:\n\$sudo -u www-data /usr/bin/php enrol/meta/cli/sync.php\n"; echo $help; die; } $verbose = !empty($options['verbose']); $result = enrol_meta_sync(null, $verbose); exit($result);
/** * Called for all enabled enrol plugins that returned true from is_cron_required(). * @return void */ public function cron() { global $CFG; require_once "{$CFG->dirroot}/enrol/meta/locallib.php"; enrol_meta_sync(); }
/** * Triggered via enrol_instance_updated event. * * @param \core\event\enrol_instance_updated $event * @return boolean */ public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) { global $DB; if (!enrol_is_enabled('meta')) { // This is slow, let enrol_meta_sync() deal with disabled plugin. return true; } // Does anything want to sync with this parent? $affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} ' . 'WHERE customint1 = ? AND enrol = ?', array($event->courseid, 'meta')); foreach ($affectedcourses as $courseid) { enrol_meta_sync($courseid); } return true; }
/** * Called for all enabled enrol plugins that returned true from is_cron_required(). * @return void */ public function cron() { global $CFG; // purge all roles if meta sync disabled, those can be recreated later here in cron if (!enrol_is_enabled('meta')) { role_unassign_all(array('component' => 'meta_enrol')); return; } require_once "{$CFG->dirroot}/enrol/meta/locallib.php"; enrol_meta_sync(); }
/** * Enrol users from another course into a course where one of the members is already enrolled * and is a member of the same group. */ public function test_add_to_group_with_member() { global $CFG, $DB; require_once $CFG->dirroot . '/group/lib.php'; $this->resetAfterTest(true); $metalplugin = enrol_get_plugin('meta'); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST); $student = $DB->get_record('role', array('shortname' => 'student')); $groupid = groups_create_group((object) array('name' => 'Grp', 'courseid' => $course2->id)); $this->enable_plugin(); set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); // Manually enrol user1 to course2 and add him to group. // Manually enrol user2 to course2 but do not add him to the group. enrol_get_plugin('manual')->enrol_user($manualenrol2, $user1->id, $student->id); groups_add_member($groupid, $user1->id); enrol_get_plugin('manual')->enrol_user($manualenrol2, $user2->id, $student->id); $this->assertTrue(groups_is_member($groupid, $user1->id)); $this->assertFalse(groups_is_member($groupid, $user2->id)); // Add instance of meta enrolment in course2 linking to course1 and enrol both users in course1. $metalplugin->add_instance($course2, array('customint1' => $course1->id, 'customint2' => $groupid)); enrol_get_plugin('manual')->enrol_user($manualenrol1, $user1->id, $student->id); enrol_get_plugin('manual')->enrol_user($manualenrol1, $user2->id, $student->id); // Both users now should be members of the group. $this->assertTrue(groups_is_member($groupid, $user1->id)); $this->assertTrue(groups_is_member($groupid, $user2->id)); // Ununerol both users from course1. enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id); enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user2->id); // User1 should still be member of the group because he was added there manually. User2 should no longer be there. $this->assertTrue(groups_is_member($groupid, $user1->id)); $this->assertFalse(groups_is_member($groupid, $user2->id)); // Assert that everything is the same after sync. enrol_meta_sync(); $this->assertTrue(groups_is_member($groupid, $user1->id)); $this->assertFalse(groups_is_member($groupid, $user2->id)); }