Merge branch 'w51_MDL-37297_m25_enrolcatprogress' of git://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2013-01-08 10:48:13 +08:00
commit a57ce0fde4
4 changed files with 33 additions and 30 deletions

View File

@ -32,7 +32,7 @@
define('CLI_SCRIPT', true);
require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
require(__DIR__.'/../../../config.php');
require_once("$CFG->dirroot/enrol/category/locallib.php");
require_once("$CFG->libdir/clilib.php");
@ -49,7 +49,7 @@ if ($options['help']) {
"Execute course category enrolment sync.
Options:
-v, --verbose Print verbose progess information
-v, --verbose Print verbose progress information
-h, --help Print out this help
Example:
@ -64,5 +64,12 @@ 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);
if (empty($options['verbose'])) {
$trace = new null_progress_trace();
} else {
$trace = new text_progress_trace();
}
$result = enrol_category_sync_full($trace);
exit($result);

View File

@ -70,7 +70,8 @@ class enrol_category_plugin extends enrol_plugin {
}
require_once("$CFG->dirroot/enrol/category/locallib.php");
enrol_category_sync_full();
$trace = new null_progress_trace();
enrol_category_sync_full($trace);
}
/**

View File

@ -252,14 +252,15 @@ function enrol_category_sync_course($course) {
* - reorder categories
* - disable enrol_category and enable it again
*
* @param bool $verbose
* @param progress_trace $trace
* @return int exit code - 0 is ok, 1 means error, 2 if plugin disabled
*/
function enrol_category_sync_full($verbose = false) {
function enrol_category_sync_full(progress_trace $trace) {
global $DB;
if (!enrol_is_enabled('category')) {
$trace->finished();
return 2;
}
@ -273,23 +274,20 @@ function enrol_category_sync_full($verbose = false) {
// Any interesting roles worth synchronising?
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
// yay, nothing to do, so let's remove all leftovers
if ($verbose) {
mtrace("No roles with 'enrol/category:synchronised' capability found.");
}
$trace->output("No roles with 'enrol/category:synchronised' capability found.");
if ($instances = $DB->get_records('enrol', array('enrol'=>'category'))) {
$trace->output("Deleting all category enrol instances...");
foreach ($instances as $instance) {
if ($verbose) {
mtrace(" deleting category enrol instance from course {$instance->courseid}");
}
$trace->output("deleting category enrol instance from course {$instance->courseid}", 1);
$plugin->delete_instance($instance);
}
$trace->output("...all instances deleted.");
}
$trace->finished();
return 0;
}
$rolenames = role_fix_names($roles, null, ROLENAME_SHORT, true);
if ($verbose) {
mtrace('Synchronising category enrolments for roles: '.implode(', ', $rolenames).'...');
}
$trace->output('Synchronising category enrolments for roles: '.implode(', ', $rolenames).'...');
list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
$params['courselevel'] = CONTEXT_COURSE;
@ -357,9 +355,7 @@ function enrol_category_sync_full($verbose = false) {
unset($instance->userid);
unset($instance->estart);
$plugin->enrol_user($instance, $userid, null, $estart);
if ($verbose) {
mtrace(" enrolling: user $userid ==> course $instance->courseid");
}
$trace->output("enrolling: user $userid ==> course $instance->courseid", 1);
}
$rs->close();
@ -378,15 +374,12 @@ function enrol_category_sync_full($verbose = false) {
$userid = $instance->userid;
unset($instance->userid);
$plugin->unenrol_user($instance, $userid);
if ($verbose) {
mtrace(" unenrolling: user $userid ==> course $instance->courseid");
}
$trace->output("unenrolling: user $userid ==> course $instance->courseid", 1);
}
$rs->close();
if ($verbose) {
mtrace('...user enrolment synchronisation finished.');
}
$trace->output('...user enrolment synchronisation finished.');
$trace->finished();
return 0;
}

View File

@ -276,6 +276,8 @@ class enrol_category_testcase extends advanced_testcase {
$this->resetAfterTest();
$trace = new null_progress_trace();
// Setup a few courses and categories.
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
@ -309,13 +311,13 @@ class enrol_category_testcase extends advanced_testcase {
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();
$result = enrol_category_sync_full($trace);
$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();
$result = enrol_category_sync_full($trace);
$this->assertSame(0, $result);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
@ -329,7 +331,7 @@ class enrol_category_testcase extends advanced_testcase {
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();
$result = enrol_category_sync_full($trace);
$this->assertSame(0, $result);
$this->assertEquals(5, $DB->count_records('user_enrolments', array()));
$this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id));
@ -348,7 +350,7 @@ class enrol_category_testcase extends advanced_testcase {
role_unassign_all(array('roleid'=>$managerrole->id));
role_unassign_all(array('roleid'=>$teacherrole->id));
$result = enrol_category_sync_full();
$result = enrol_category_sync_full($trace);
$this->assertSame(2, $result);
$this->assertEquals(0, $DB->count_records('role_assignments', array()));
$this->assertNotEmpty($DB->count_records('user_enrolments', array()));
@ -356,7 +358,7 @@ class enrol_category_testcase extends advanced_testcase {
$this->disable_role_sync($teacherrole->id);
$this->enable_plugin();
$result = enrol_category_sync_full();
$result = enrol_category_sync_full($trace);
$this->assertSame(0, $result);
$this->assertEquals(0, $DB->count_records('role_assignments', array()));
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));