Permalink
Browse files

MDL-34864 add verbose option to CLI enrol_category sync

  • Loading branch information...
1 parent 3d7f23c commit a2043a3168db1c8220d3e4da6f73fa238557f475 @skodak skodak committed Aug 14, 2012
Showing with 53 additions and 5 deletions.
  1. +28 −2 enrol/category/cli/sync.php
  2. +25 −3 enrol/category/locallib.php
@@ -36,9 +36,35 @@
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.
+
+Options:
+-v, --verbose Print verbose progess information
+-h, --help Print out this help
+
+Example:
+\$ sudo -u www-data /usr/bin/php enrol/category/cli/sync.php
+";
+ echo $help;
+ die;
+}
+
if (!enrol_is_enabled('category')) {
- die('enrol_category plugin is disabled, sync is disabled');
+ cli_error('enrol_category plugin is disabled, synchronisation stopped');
}
-enrol_category_sync_full();
+$verbose = !empty($options['verbose']);
+return enrol_category_sync_full($verbose);
@@ -233,12 +233,12 @@ function enrol_category_sync_course($course) {
}
}
-function enrol_category_sync_full() {
+function enrol_category_sync_full($verbose = false) {
global $DB;
if (!enrol_is_enabled('category')) {
- return;
+ return 2;
}
// we may need a lot of time here
@@ -251,12 +251,22 @@ function enrol_category_sync_full() {
// 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.");
+ }
if ($instances = $DB->get_records('enrol', array('enrol'=>'category'))) {
foreach ($instances as $instance) {
+ if ($verbose) {
+ mtrace(" deleting category enrol instance from course {$instance->courseid}");
+ }
$plugin->delete_instance($instance);
}
}
- return;
+ return 0;
+ }
+ $rolenames = role_fix_names($roles, null, ROLENAME_SHORT, true);
+ if ($verbose) {
+ mtrace('Synchronising category enrolments for roles: '.implode(', ', $rolenames).'...');
}
list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
@@ -325,6 +335,9 @@ function enrol_category_sync_full() {
unset($instance->userid);
unset($instance->estart);
$plugin->enrol_user($instance, $userid, null, $estart);
+ if ($verbose) {
+ mtrace(" enrolling: user $userid ==> course $instance->courseid");
+ }
}
$rs->close();
@@ -343,6 +356,15 @@ function enrol_category_sync_full() {
$userid = $instance->userid;
unset($instance->userid);
$plugin->unenrol_user($instance, $userid);
+ if ($verbose) {
+ mtrace(" unenrolling: user $userid ==> course $instance->courseid");
+ }
}
$rs->close();
+
+ if ($verbose) {
+ mtrace('...user enrolment synchronisation finished.');
+ }
+
+ return 0;
}

0 comments on commit a2043a3

Please sign in to comment.