/** * Called after updating/inserting course. * * @param bool $inserted true if course just inserted * @param stdClass $course * @param stdClass $data form data * @return void */ public function course_updated($inserted, $course, $data) { global $CFG; if (!enrol_is_enabled('category')) { return; } // Sync category enrols. require_once "{$CFG->dirroot}/enrol/category/locallib.php"; enrol_category_sync_course($course); }
public function test_sync_course() { global $DB; $this->resetAfterTest(); // Setup a few courses and categories. $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(); $cat3 = $this->getDataGenerator()->create_category(array('parent' => $cat2->id)); $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $cat2->id)); $course3 = $this->getDataGenerator()->create_course(array('category' => $cat3->id)); $course4 = $this->getDataGenerator()->create_course(array('category' => $cat3->id)); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $this->enable_role_sync($studentrole->id); $this->enable_role_sync($teacherrole->id); $this->enable_plugin(); $this->assertEquals(0, $DB->count_records('role_assignments', array())); role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id)); role_assign($managerrole->id, $user3->id, context_course::instance($course1->id)); role_assign($managerrole->id, $user3->id, context_course::instance($course2->id)); $this->assertEquals(0, $DB->count_records('user_enrolments', array())); $this->disable_plugin(); // Stops the event handlers. role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)); $this->assertEquals(0, $DB->count_records('user_enrolments', array())); $this->enable_plugin(); enrol_category_sync_course($course2); $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id)); $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user1->id)); $this->assertFalse(is_enrolled(context_course::instance($course4->id), $user1->id)); $this->assertEquals(1, $DB->count_records('user_enrolments', array())); enrol_category_sync_course($course2); enrol_category_sync_course($course3); enrol_category_sync_course($course4); $this->assertFalse(is_enrolled(context_course::instance($course1->id), $user1->id)); $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id)); $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id)); $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id)); $this->assertEquals(3, $DB->count_records('user_enrolments', array())); $this->disable_plugin(); // Stops the event handlers. role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id)); role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id)); role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id); $this->assertEquals(3, $DB->count_records('user_enrolments', array())); $this->enable_plugin(); enrol_category_sync_course($course2); $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user1->id)); $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user2->id)); $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user4->id)); enrol_category_sync_course($course1); enrol_category_sync_course($course3); enrol_category_sync_course($course4); $this->assertEquals(2, $DB->count_records('user_enrolments', array())); $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id)); $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id)); $this->disable_role_sync($studentrole->id); enrol_category_sync_course($course1); enrol_category_sync_course($course2); enrol_category_sync_course($course3); enrol_category_sync_course($course4); $this->assertEquals(1, $DB->count_records('user_enrolments', array())); $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id)); $this->assertEquals(1, $DB->count_records('enrol', array('enrol' => 'category'))); $this->disable_role_sync($teacherrole->id); enrol_category_sync_course($course1); enrol_category_sync_course($course2); enrol_category_sync_course($course3); enrol_category_sync_course($course4); $this->assertEquals(0, $DB->count_records('user_enrolments', array())); $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'category'))); }
/** * Automatic enrol sync executed during restore. * Useful for automatic sync by course->idnumber or course category. * @param stdClass $course course record */ public function restore_sync_course($course) { global $CFG; require_once "{$CFG->dirroot}/enrol/category/locallib.php"; enrol_category_sync_course($course); }