Skip to content

Commit

Permalink
MDL-34864 add verbose option to CLI enrol_category sync
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 18, 2012
1 parent 3d7f23c commit a2043a3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
30 changes: 28 additions & 2 deletions enrol/category/cli/sync.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,9 +36,35 @@


require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php'); require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
require_once("$CFG->dirroot/enrol/category/locallib.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')) { 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);
28 changes: 25 additions & 3 deletions enrol/category/locallib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ function enrol_category_sync_course($course) {
} }
} }


function enrol_category_sync_full() { function enrol_category_sync_full($verbose = false) {
global $DB; global $DB;




if (!enrol_is_enabled('category')) { if (!enrol_is_enabled('category')) {
return; return 2;
} }


// we may need a lot of time here // we may need a lot of time here
Expand All @@ -251,12 +251,22 @@ function enrol_category_sync_full() {
// any interesting roles worth synchronising? // any interesting roles worth synchronising?
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) { if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
// yay, nothing to do, so let's remove all leftovers // 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'))) { if ($instances = $DB->get_records('enrol', array('enrol'=>'category'))) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
if ($verbose) {
mtrace(" deleting category enrol instance from course {$instance->courseid}");
}
$plugin->delete_instance($instance); $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'); list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
Expand Down Expand Up @@ -325,6 +335,9 @@ function enrol_category_sync_full() {
unset($instance->userid); unset($instance->userid);
unset($instance->estart); unset($instance->estart);
$plugin->enrol_user($instance, $userid, null, $estart); $plugin->enrol_user($instance, $userid, null, $estart);
if ($verbose) {
mtrace(" enrolling: user $userid ==> course $instance->courseid");
}
} }
$rs->close(); $rs->close();


Expand All @@ -343,6 +356,15 @@ function enrol_category_sync_full() {
$userid = $instance->userid; $userid = $instance->userid;
unset($instance->userid); unset($instance->userid);
$plugin->unenrol_user($instance, $userid); $plugin->unenrol_user($instance, $userid);
if ($verbose) {
mtrace(" unenrolling: user $userid ==> course $instance->courseid");
}
} }
$rs->close(); $rs->close();

if ($verbose) {
mtrace('...user enrolment synchronisation finished.');
}

return 0;
} }

0 comments on commit a2043a3

Please sign in to comment.