/** * Called for all enabled enrol plugins that returned true from is_cron_required(). * @return void */ public function cron() { global $CFG; if (!enrol_is_enabled('category')) { return; } require_once "{$CFG->dirroot}/enrol/category/locallib.php"; enrol_category_sync_full(); }
/** * Called for all enabled enrol plugins that returned true from is_cron_required(). * @return void */ public function cron() { global $CFG; if (!enrol_is_enabled('category')) { return; } require_once "{$CFG->dirroot}/enrol/category/locallib.php"; $trace = new null_progress_trace(); enrol_category_sync_full($trace); }
function xmldb_enrol_category_install() { global $CFG, $DB; if (!$DB->record_exists_select('role_assignments', "contextid IN (SELECT id FROM {context} WHERE contextlevel = ?)", array(CONTEXT_COURSECAT))) { // fresh install or nobody used category enrol return; } // existing sites need a way to keep category role enrols, but we do not want to encourage that on new sites // extremely ugly hack, the sync depends on the capabilities so we unfortunately force update of caps here // note: this is not officially supported and should not be copied elsewhere! :-( update_capabilities('enrol_category'); $syscontext = get_context_instance(CONTEXT_SYSTEM); $archetypes = array('student', 'teacher', 'editingteacher'); $enableplugin = false; foreach ($archetypes as $archetype) { $roles = get_archetype_roles($archetype); foreach ($roles as $role) { if (!$DB->record_exists_select('role_assignments', "roleid = ? AND contextid IN (SELECT id FROM {context} WHERE contextlevel = ?)", array($role->id, CONTEXT_COURSECAT))) { continue; } assign_capability('enrol/category:synchronised', CAP_ALLOW, $role->id, $syscontext->id, true); $levels = get_role_contextlevels($role->id); $levels[] = CONTEXT_COURSECAT; set_role_contextlevels($role->id, $levels); $enableplugin = true; } } if (!$enableplugin) { return; } // enable this plugin $enabledplugins = explode(',', $CFG->enrol_plugins_enabled); $enabledplugins[] = 'category'; $enabledplugins = array_unique($enabledplugins); set_config('enrol_plugins_enabled', implode(',', $enabledplugins)); // brute force course resync, this may take a while require_once "{$CFG->dirroot}/enrol/category/locallib.php"; enrol_category_sync_full(); }
public function test_sync_full() { 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())); $result = enrol_category_sync_full(); $this->assertSame(0, $result); $this->disable_plugin(); role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)); $this->enable_plugin(); $result = enrol_category_sync_full(); $this->assertSame(0, $result); $this->assertEquals(3, $DB->count_records('user_enrolments', array())); $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->disable_plugin(); role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id); role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id)); role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id)); role_assign($teacherrole->id, $user3->id, context_coursecat::instance($cat2->id)); role_assign($managerrole->id, $user3->id, context_course::instance($course3->id)); $this->enable_plugin(); $result = enrol_category_sync_full(); $this->assertSame(0, $result); $this->assertEquals(5, $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->assertTrue(is_enrolled(context_course::instance($course2->id), $user3->id)); $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user3->id)); $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user3->id)); // Cleanup everything. $this->assertNotEmpty($DB->count_records('role_assignments', array())); $this->assertNotEmpty($DB->count_records('user_enrolments', array())); $this->disable_plugin(); role_unassign_all(array('roleid' => $studentrole->id)); role_unassign_all(array('roleid' => $managerrole->id)); role_unassign_all(array('roleid' => $teacherrole->id)); $result = enrol_category_sync_full(); $this->assertSame(2, $result); $this->assertEquals(0, $DB->count_records('role_assignments', array())); $this->assertNotEmpty($DB->count_records('user_enrolments', array())); $this->disable_role_sync($studentrole->id); $this->disable_role_sync($teacherrole->id); $this->enable_plugin(); $result = enrol_category_sync_full(); $this->assertSame(0, $result); $this->assertEquals(0, $DB->count_records('role_assignments', array())); $this->assertEquals(0, $DB->count_records('user_enrolments', array())); $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'category'))); }
// Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * CLI sync for full category enrol synchronisation. * * Sample execution: * $sudo -u www-data /usr/bin/php /var/www/moodle/enrol/category/cli/sync.php * * 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 * @subpackage category * @copyright 2010 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('CLI_SCRIPT', true); require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php'; require_once "{$CFG->dirroot}/enrol/category/locallib.php"; if (!enrol_is_enabled('category')) { die('enrol_category plugin is disabled, sync is disabled'); } enrol_category_sync_full();
* * 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_category * @copyright 2010 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('CLI_SCRIPT', true); require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php'; require_once "{$CFG->dirroot}/enrol/category/locallib.php"; require_once "{$CFG->libdir}/clilib.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 course category enrolment 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/category/cli/sync.php\n"; echo $help; die; } if (!enrol_is_enabled('category')) { cli_error('enrol_category plugin is disabled, synchronisation stopped', 2); } $verbose = !empty($options['verbose']); return enrol_category_sync_full($verbose);
* - use "su" if "sudo" not available * * @package enrol_category * @copyright 2010 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->dirroot}/enrol/category/locallib.php"; require_once "{$CFG->libdir}/clilib.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 course category enrolment sync.\n\nOptions:\n-v, --verbose Print verbose progress information\n-h, --help Print out this help\n\nExample:\n\$ sudo -u www-data /usr/bin/php enrol/category/cli/sync.php\n"; echo $help; die; } if (!enrol_is_enabled('category')) { cli_error('enrol_category plugin is disabled, synchronisation stopped', 2); } if (empty($options['verbose'])) { $trace = new null_progress_trace(); } else { $trace = new text_progress_trace(); } $result = enrol_category_sync_full($trace); exit($result);