Merge branch 'w51_MDL-37299_m25_manselfprogress' of git://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2013-01-08 15:08:05 +08:00
commit 655b566b9e
7 changed files with 114 additions and 109 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -1832,9 +1832,9 @@ abstract class enrol_plugin {
* - upgrade code that sets default thresholds for existing courses (should be 1 day),
* - something that calls this method, such as cron.
*
* @param bool $verbose verbose CLI output
* @param progress_trace $trace (accepts bool for backwards compatibility only)
*/
public function send_expiry_notifications($verbose = false) {
public function send_expiry_notifications($trace) {
global $DB, $CFG;
// Unfortunately this may take a long time, it should not be interrupted,
@ -1849,27 +1849,30 @@ abstract class enrol_plugin {
$expirynotifyhour = $this->get_config('expirynotifyhour');
if (is_null($expirynotifyhour)) {
debugging("send_expiry_notifications() in $name enrolment plugin needs expirynotifyhour setting");
$trace->finished();
return;
}
if (!($trace instanceof progress_trace)) {
$trace = $trace ? new text_progress_trace() : new null_progress_trace();
debugging('enrol_plugin::send_expiry_notifications() now expects progress_trace instance as parameter!', DEBUG_DEVELOPER);
}
$timenow = time();
$notifytime = usergetmidnight($timenow, $CFG->timezone) + ($expirynotifyhour * 3600);
if ($expirynotifylast > $notifytime) {
if ($verbose) {
mtrace($name.' enrolment expiry notifications were already sent today at '.userdate($expirynotifylast, '', $CFG->timezone).'.');
}
$trace->output($name.' enrolment expiry notifications were already sent today at '.userdate($expirynotifylast, '', $CFG->timezone).'.');
$trace->finished();
return;
} else if ($timenow < $notifytime) {
if ($verbose) {
mtrace($name.' enrolment expiry notifications will be sent at '.userdate($notifytime, '', $CFG->timezone).'.');
}
$trace->output($name.' enrolment expiry notifications will be sent at '.userdate($notifytime, '', $CFG->timezone).'.');
$trace->finished();
return;
}
if ($verbose) {
mtrace('Processing '.$name.' enrolment expiration notifications...');
}
$trace->output('Processing '.$name.' enrolment expiration notifications...');
// Notify users responsible for enrolment once every day.
$sql = "SELECT ue.*, e.expirynotify, e.notifyall, e.expirythreshold, e.courseid, c.fullname
@ -1888,7 +1891,7 @@ abstract class enrol_plugin {
foreach($rs as $ue) {
if ($lastenrollid and $lastenrollid != $ue->enrolid) {
$this->notify_expiry_enroller($lastenrollid, $users, $verbose);
$this->notify_expiry_enroller($lastenrollid, $users, $trace);
$users = array();
}
$lastenrollid = $ue->enrolid;
@ -1906,23 +1909,21 @@ abstract class enrol_plugin {
if ($ue->timeend - $ue->expirythreshold + 86400 < $timenow) {
// Notify enrolled users only once at the start of the threshold.
if ($verbose) {
mtrace(" user $ue->userid was already notified that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone));
}
$trace->output("user $ue->userid was already notified that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
continue;
}
$this->notify_expiry_enrolled($user, $ue, $verbose);
$this->notify_expiry_enrolled($user, $ue, $trace);
}
$rs->close();
if ($lastenrollid and $users) {
$this->notify_expiry_enroller($lastenrollid, $users, $verbose);
$this->notify_expiry_enroller($lastenrollid, $users, $trace);
}
if ($verbose) {
mtrace('...notification processing finished.');
}
$trace->output('...notification processing finished.');
$trace->finished();
$this->set_config('expirynotifylast', $timenow);
}
@ -1947,9 +1948,9 @@ abstract class enrol_plugin {
*
* @param stdClass $user
* @param stdClass $ue
* @param bool $verbose
* @param progress_trace $trace
*/
protected function notify_expiry_enrolled($user, $ue, $verbose) {
protected function notify_expiry_enrolled($user, $ue, progress_trace $trace) {
global $CFG, $SESSION;
$name = $this->get_name();
@ -1988,13 +1989,9 @@ abstract class enrol_plugin {
$message->contexturl = (string)new moodle_url('/course/view.php', array('id'=>$ue->courseid));
if (message_send($message)) {
if ($verbose) {
mtrace(" notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone));
}
$trace->output("notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
} else {
if ($verbose) {
mtrace(" error notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone));
}
$trace->output("error notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
}
if ($SESSION->lang !== $sessionlang) {
@ -2012,9 +2009,9 @@ abstract class enrol_plugin {
*
* @param int $eid
* @param array $users
* @param bool $verbose
* @param progress_trace $trace
*/
protected function notify_expiry_enroller($eid, $users, $verbose) {
protected function notify_expiry_enroller($eid, $users, progress_trace $trace) {
global $DB, $SESSION;
$name = $this->get_name();
@ -2061,13 +2058,9 @@ abstract class enrol_plugin {
$message->contexturl = $a->extendurl;
if (message_send($message)) {
if ($verbose) {
mtrace(" notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid");
}
$trace->output("notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
} else {
if ($verbose) {
mtrace(" error notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid");
}
$trace->output("error notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
}
if ($SESSION->lang !== $sessionlang) {