mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
Merge branch 'w51_MDL-37299_m25_manselfprogress' of git://github.com/skodak/moodle
This commit is contained in:
commit
655b566b9e
7 changed files with 114 additions and 109 deletions
|
@ -29,8 +29,8 @@
|
|||
|
||||
define('CLI_SCRIPT', true);
|
||||
|
||||
require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
require(__DIR__.'/../../../config.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'));
|
||||
|
@ -49,20 +49,27 @@ Options:
|
|||
-h, --help Print out this help
|
||||
|
||||
Example:
|
||||
\$sudo -u www-data /usr/bin/php enrol/self/manual/sync.php
|
||||
\$ sudo -u www-data /usr/bin/php enrol/self/manual/sync.php
|
||||
";
|
||||
|
||||
echo $help;
|
||||
die;
|
||||
}
|
||||
|
||||
$verbose = !empty($options['verbose']);
|
||||
if (!enrol_is_enabled('manual')) {
|
||||
cli_error('enrol_manual plugin is disabled, synchronisation stopped', 2);
|
||||
}
|
||||
|
||||
if (empty($options['verbose'])) {
|
||||
$trace = new null_progress_trace();
|
||||
} else {
|
||||
$trace = new text_progress_trace();
|
||||
}
|
||||
|
||||
/** @var $plugin enrol_manual_plugin */
|
||||
$plugin = enrol_get_plugin('manual');
|
||||
|
||||
$result = $plugin->sync(null, $verbose);
|
||||
|
||||
$plugin->send_expiry_notifications($verbose);
|
||||
$result = $plugin->sync($trace, null);
|
||||
$plugin->send_expiry_notifications($trace);
|
||||
|
||||
exit($result);
|
||||
|
|
|
@ -279,21 +279,23 @@ class enrol_manual_plugin extends enrol_plugin {
|
|||
* @return void
|
||||
*/
|
||||
public function cron() {
|
||||
$this->sync(null, true);
|
||||
$this->send_expiry_notifications(true);
|
||||
$trace = new text_progress_trace();
|
||||
$this->sync($trace, null);
|
||||
$this->send_expiry_notifications($trace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync all meta course links.
|
||||
*
|
||||
* @param progress_trace $trace
|
||||
* @param int $courseid one course, empty mean all
|
||||
* @param bool $verbose verbose CLI output
|
||||
* @return int 0 means ok, 1 means error, 2 means plugin disabled
|
||||
*/
|
||||
public function sync($courseid = null, $verbose = false) {
|
||||
public function sync(progress_trace $trace, $courseid = null) {
|
||||
global $DB;
|
||||
|
||||
if (!enrol_is_enabled('manual')) {
|
||||
$trace->finished();
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -301,9 +303,7 @@ class enrol_manual_plugin extends enrol_plugin {
|
|||
@set_time_limit(0);
|
||||
raise_memory_limit(MEMORY_HUGE);
|
||||
|
||||
if ($verbose) {
|
||||
mtrace('Verifying manual enrolment expiration...');
|
||||
}
|
||||
$trace->output('Verifying manual enrolment expiration...');
|
||||
|
||||
$params = array('now'=>time(), 'useractive'=>ENROL_USER_ACTIVE, 'courselevel'=>CONTEXT_COURSE);
|
||||
$coursesql = "";
|
||||
|
@ -332,9 +332,7 @@ class enrol_manual_plugin extends enrol_plugin {
|
|||
// Always remove all manually assigned roles here, this may break enrol_self roles but we do not want hardcoded hacks here.
|
||||
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
|
||||
$this->unenrol_user($instance, $ue->userid);
|
||||
if ($verbose) {
|
||||
mtrace(" unenrolling expired user $ue->userid from course $instance->courseid");
|
||||
}
|
||||
$trace->output("unenrolling expired user $ue->userid from course $instance->courseid", 1);
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
@ -357,9 +355,7 @@ class enrol_manual_plugin extends enrol_plugin {
|
|||
// Always remove all manually assigned roles here, this may break enrol_self roles but we do not want hardcoded hacks here.
|
||||
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
|
||||
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
|
||||
if ($verbose) {
|
||||
mtrace(" suspending expired user $ue->userid in course $instance->courseid");
|
||||
}
|
||||
$trace->output("suspending expired user $ue->userid in course $instance->courseid", 1);
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
@ -368,9 +364,8 @@ class enrol_manual_plugin extends enrol_plugin {
|
|||
// ENROL_EXT_REMOVED_KEEP means no changes.
|
||||
}
|
||||
|
||||
if ($verbose) {
|
||||
mtrace('...manual enrolment updates finished.');
|
||||
}
|
||||
$trace->output('...manual enrolment updates finished.');
|
||||
$trace->finished();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -210,6 +210,8 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
/** @var $manualplugin enrol_manual_plugin */
|
||||
$manualplugin = enrol_get_plugin('manual');
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
$now = time();
|
||||
|
||||
// Prepare some data.
|
||||
|
@ -263,19 +265,19 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
// Execute tests.
|
||||
|
||||
$this->assertEquals(ENROL_EXT_REMOVED_KEEP, $manualplugin->get_config('expiredaction'));
|
||||
$manualplugin->sync(null, false);
|
||||
$manualplugin->sync($trace, null);
|
||||
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
||||
|
||||
|
||||
$manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
|
||||
$manualplugin->sync($course2->id, false);
|
||||
$manualplugin->sync($trace, $course2->id);
|
||||
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
||||
|
||||
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
|
||||
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id)));
|
||||
$manualplugin->sync(null, false);
|
||||
$manualplugin->sync($trace, null);
|
||||
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(5, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
|
@ -294,7 +296,7 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
|
||||
|
||||
$manualplugin->sync(null, false);
|
||||
$manualplugin->sync($trace, null);
|
||||
$this->assertEquals(4, $DB->count_records('user_enrolments'));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
||||
|
@ -309,6 +311,8 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
$this->resetAfterTest();
|
||||
$this->preventResetByRollback(); // Messaging does not like transactions...
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
/** @var $manualplugin enrol_manual_plugin */
|
||||
$manualplugin = enrol_get_plugin('manual');
|
||||
$now = time();
|
||||
|
@ -394,7 +398,7 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
|
||||
$sink = $this->redirectMessages();
|
||||
|
||||
$manualplugin->send_expiry_notifications(false);
|
||||
$manualplugin->send_expiry_notifications($trace);
|
||||
|
||||
$messages = $sink->get_messages();
|
||||
|
||||
|
@ -451,18 +455,18 @@ class enrol_manual_lib_testcase extends advanced_testcase {
|
|||
// Make sure that notifications are not repeated.
|
||||
$sink->clear();
|
||||
|
||||
$manualplugin->send_expiry_notifications(false);
|
||||
$manualplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(0, $sink->count());
|
||||
|
||||
// use invalid notification hour to verify that before the hour the notifications are not sent.
|
||||
$manualplugin->set_config('expirynotifylast', time() - 60*60*24);
|
||||
$manualplugin->set_config('expirynotifyhour', '24');
|
||||
|
||||
$manualplugin->send_expiry_notifications(false);
|
||||
$manualplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(0, $sink->count());
|
||||
|
||||
$manualplugin->set_config('expirynotifyhour', '0');
|
||||
$manualplugin->send_expiry_notifications(false);
|
||||
$manualplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(6, $sink->count());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@
|
|||
|
||||
define('CLI_SCRIPT', true);
|
||||
|
||||
require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
require(__DIR__.'/../../../config.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'));
|
||||
|
@ -50,20 +50,27 @@ Options:
|
|||
-h, --help Print out this help
|
||||
|
||||
Example:
|
||||
\$sudo -u www-data /usr/bin/php enrol/self/cli/sync.php
|
||||
\$ sudo -u www-data /usr/bin/php enrol/self/cli/sync.php
|
||||
";
|
||||
|
||||
echo $help;
|
||||
die;
|
||||
}
|
||||
|
||||
$verbose = !empty($options['verbose']);
|
||||
if (!enrol_is_enabled('self')) {
|
||||
cli_error('enrol_self plugin is disabled, synchronisation stopped', 2);
|
||||
}
|
||||
|
||||
if (empty($options['verbose'])) {
|
||||
$trace = new null_progress_trace();
|
||||
} else {
|
||||
$trace = new text_progress_trace();
|
||||
}
|
||||
|
||||
/** @var $plugin enrol_self_plugin */
|
||||
$plugin = enrol_get_plugin('self');
|
||||
|
||||
$result = $plugin->sync(null, $verbose);
|
||||
|
||||
$plugin->send_expiry_notifications($verbose);
|
||||
$result = $plugin->sync($trace, null);
|
||||
$plugin->send_expiry_notifications($trace);
|
||||
|
||||
exit($result);
|
||||
|
|
|
@ -360,21 +360,23 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
* @return void
|
||||
*/
|
||||
public function cron() {
|
||||
$this->sync(null, true);
|
||||
$this->send_expiry_notifications(true);
|
||||
$trace = new text_progress_trace();
|
||||
$this->sync($trace, null);
|
||||
$this->send_expiry_notifications($trace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync all meta course links.
|
||||
*
|
||||
* @param progress_trace $trace
|
||||
* @param int $courseid one course, empty mean all
|
||||
* @param bool $verbose verbose CLI output
|
||||
* @return int 0 means ok, 1 means error, 2 means plugin disabled
|
||||
*/
|
||||
public function sync($courseid = null, $verbose = false) {
|
||||
public function sync(progress_trace $trace, $courseid = null) {
|
||||
global $DB;
|
||||
|
||||
if (!enrol_is_enabled('self')) {
|
||||
$trace->finished();
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -382,9 +384,7 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
@set_time_limit(0);
|
||||
raise_memory_limit(MEMORY_HUGE);
|
||||
|
||||
if ($verbose) {
|
||||
mtrace('Verifying self-enrolments...');
|
||||
}
|
||||
$trace->output('Verifying self-enrolments...');
|
||||
|
||||
$params = array('now'=>time(), 'useractive'=>ENROL_USER_ACTIVE, 'courselevel'=>CONTEXT_COURSE);
|
||||
$coursesql = "";
|
||||
|
@ -408,10 +408,8 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
$userid = $instance->userid;
|
||||
unset($instance->userid);
|
||||
$this->unenrol_user($instance, $userid);
|
||||
if ($verbose) {
|
||||
$days = $instance->customint2 / 60*60*24;
|
||||
mtrace(" unenrolling user $userid from course $instance->courseid as they have did not log in for at least $days days");
|
||||
}
|
||||
$days = $instance->customint2 / 60*60*24;
|
||||
$trace->output("unenrolling user $userid from course $instance->courseid as they have did not log in for at least $days days", 1);
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
|
@ -427,10 +425,8 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
$userid = $instance->userid;
|
||||
unset($instance->userid);
|
||||
$this->unenrol_user($instance, $userid);
|
||||
if ($verbose) {
|
||||
$days = $instance->customint2 / 60*60*24;
|
||||
mtrace(" unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days");
|
||||
}
|
||||
$trace->output("unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days", 1);
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
|
@ -455,9 +451,7 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
|
||||
}
|
||||
$this->unenrol_user($instance, $ue->userid);
|
||||
if ($verbose) {
|
||||
mtrace(" unenrolling expired user $ue->userid from course $instance->courseid");
|
||||
}
|
||||
$trace->output("unenrolling expired user $ue->userid from course $instance->courseid", 1);
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
@ -483,9 +477,7 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
|
||||
}
|
||||
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
|
||||
if ($verbose) {
|
||||
mtrace(" suspending expired user $ue->userid in course $instance->courseid");
|
||||
}
|
||||
$trace->output("suspending expired user $ue->userid in course $instance->courseid", 1);
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
@ -494,9 +486,8 @@ class enrol_self_plugin extends enrol_plugin {
|
|||
// ENROL_EXT_REMOVED_KEEP means no changes.
|
||||
}
|
||||
|
||||
if ($verbose) {
|
||||
mtrace('...user self-enrolment updates finished.');
|
||||
}
|
||||
$trace->output('...user self-enrolment updates finished.');
|
||||
$trace->finished();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -44,9 +44,11 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
|
||||
$selfplugin = enrol_get_plugin('self');
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
// Just make sure the sync does not throw any errors when nothing to do.
|
||||
$selfplugin->sync(NULL, false);
|
||||
$selfplugin->sync($SITE->id, false);
|
||||
$selfplugin->sync($trace, null);
|
||||
$selfplugin->sync($trace, $SITE->id);
|
||||
}
|
||||
|
||||
public function test_longtimnosee() {
|
||||
|
@ -59,6 +61,8 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
|
||||
$now = time();
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
// Prepare some data.
|
||||
|
||||
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
||||
|
@ -128,14 +132,14 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
|
||||
// Execute sync - this is the same thing used from cron.
|
||||
|
||||
$selfplugin->sync($course2->id, false);
|
||||
$selfplugin->sync($trace, $course2->id);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
||||
$selfplugin->sync(null, false);
|
||||
$selfplugin->sync($trace, null);
|
||||
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
|
||||
|
@ -157,6 +161,8 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
|
||||
$now = time();
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
// Prepare some data.
|
||||
|
||||
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
||||
|
@ -221,17 +227,17 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
// Execute tests.
|
||||
|
||||
$this->assertEquals(ENROL_EXT_REMOVED_KEEP, $selfplugin->get_config('expiredaction'));
|
||||
$selfplugin->sync(null, false);
|
||||
$selfplugin->sync($trace, null);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
|
||||
|
||||
$selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
|
||||
$selfplugin->sync($course2->id, false);
|
||||
$selfplugin->sync($trace, $course2->id);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
|
||||
$selfplugin->sync(null, false);
|
||||
$selfplugin->sync($trace, null);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
|
@ -252,7 +258,7 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
|
||||
$selfplugin->sync(null, false);
|
||||
$selfplugin->sync($trace, null);
|
||||
$this->assertEquals(7, $DB->count_records('user_enrolments'));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user2->id)));
|
||||
|
@ -274,6 +280,8 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
$now = time();
|
||||
$admin = get_admin();
|
||||
|
||||
$trace = new null_progress_trace();
|
||||
|
||||
// Note: hopefully nobody executes the unit tests the last second before midnight...
|
||||
|
||||
$selfplugin->set_config('expirynotifylast', $now - 60*60*24);
|
||||
|
@ -363,7 +371,7 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
|
||||
$sink = $this->redirectMessages();
|
||||
|
||||
$selfplugin->send_expiry_notifications(false);
|
||||
$selfplugin->send_expiry_notifications($trace);
|
||||
|
||||
$messages = $sink->get_messages();
|
||||
|
||||
|
@ -420,18 +428,18 @@ class enrol_self_testcase extends advanced_testcase {
|
|||
// Make sure that notifications are not repeated.
|
||||
$sink->clear();
|
||||
|
||||
$selfplugin->send_expiry_notifications(false);
|
||||
$selfplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(0, $sink->count());
|
||||
|
||||
// use invalid notification hour to verify that before the hour the notifications are not sent.
|
||||
$selfplugin->set_config('expirynotifylast', time() - 60*60*24);
|
||||
$selfplugin->set_config('expirynotifyhour', '24');
|
||||
|
||||
$selfplugin->send_expiry_notifications(false);
|
||||
$selfplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(0, $sink->count());
|
||||
|
||||
$selfplugin->set_config('expirynotifyhour', '0');
|
||||
$selfplugin->send_expiry_notifications(false);
|
||||
$selfplugin->send_expiry_notifications($trace);
|
||||
$this->assertEquals(6, $sink->count());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue