MDL-35843 implement self enrolment expiry notification

This commit is contained in:
Petr Škoda 2012-10-13 23:01:34 +02:00 committed by Petr Skoda
parent 8c04252cbf
commit 8a486fdb7d
10 changed files with 374 additions and 6 deletions

View File

@ -59,8 +59,11 @@ Example:
$verbose = !empty($options['verbose']);
/** @var $plugin enrol_self_plugin */
$plugin = enrol_get_plugin('self');
$result = $plugin->sync(null, $verbose);
$plugin->send_expiry_notifications($verbose);
exit($result);

View File

@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines message providers for self enrolments.
*
* @package enrol_self
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$messageproviders = array (
'expiry_notification' => array(),
);

45
enrol/self/db/upgrade.php Normal file
View File

@ -0,0 +1,45 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file keeps track of upgrades to the self enrolment plugin
*
* @package enrol_self
* @copyright 2012 Petr Skoda {@link http://skodak.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function xmldb_enrol_self_upgrade($oldversion) {
global $CFG, $DB, $OUTPUT;
$dbman = $DB->get_manager();
// Moodle v2.3.0 release upgrade line
// Put any upgrade step following this
if ($oldversion < 2012101400) {
// Set default expiry threshold to 1 day.
$DB->execute("UPDATE {enrol} SET expirythreshold = 86400 WHERE enrol = 'self' AND expirythreshold = 0");
upgrade_plugin_savepoint(true, 2012101400, 'enrol', 'self');
}
return true;
}

View File

@ -47,6 +47,12 @@ $plugin = enrol_get_plugin('self');
if ($instanceid) {
$instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'self', 'id'=>$instanceid), '*', MUST_EXIST);
// Merge these two settings to one value for the single selection element.
if ($instance->notifyall and $instance->expirynotify) {
$instance->expirynotify = 2;
}
unset($instance->notifyall);
} else {
require_capability('moodle/course:enrolconfig', $context);
// No instance yet, we have to add new instance.
@ -63,6 +69,16 @@ if ($mform->is_cancelled()) {
redirect($return);
} else if ($data = $mform->get_data()) {
if ($data->expirynotify == 2) {
$data->expirynotify = 1;
$data->notifyall = 1;
} else {
$data->notifyall = 0;
}
if (!$data->expirynotify) {
// Keep previous/default value of disabled expirythreshold option.
$data->expirythreshold = $instance->expirythreshold;
}
if ($instance->id) {
$reset = ($instance->status != $data->status);
@ -77,6 +93,9 @@ if ($mform->is_cancelled()) {
$instance->customtext1 = $data->customtext1;
$instance->roleid = $data->roleid;
$instance->enrolperiod = $data->enrolperiod;
$instance->expirynotify = $data->expirynotify;
$instance->notifyall = $data->notifyall;
$instance->expirythreshold = $data->expirythreshold;
$instance->enrolstartdate = $data->enrolstartdate;
$instance->enrolenddate = $data->enrolenddate;
$instance->timemodified = time();
@ -87,9 +106,23 @@ if ($mform->is_cancelled()) {
}
} else {
$fields = array('status'=>$data->status, 'name'=>$data->name, 'password'=>$data->password, 'customint1'=>$data->customint1, 'customint2'=>$data->customint2,
'customint3'=>$data->customint3, 'customint4'=>$data->customint4, 'customint5'=>$data->customint5, 'customtext1'=>$data->customtext1,
'roleid'=>$data->roleid, 'enrolperiod'=>$data->enrolperiod, 'enrolstartdate'=>$data->enrolstartdate, 'enrolenddate'=>$data->enrolenddate);
$fields = array(
'status' => $data->status,
'name' => $data->name,
'password' => $data->password,
'customint1' => $data->customint1,
'customint2' => $data->customint2,
'customint3' => $data->customint3,
'customint4' => $data->customint4,
'customint5' => $data->customint5,
'customtext1' => $data->customtext1,
'roleid' => $data->roleid,
'enrolperiod' => $data->enrolperiod,
'expirynotify' => $data->expirynotify,
'notifyall' => $data->notifyall,
'expirythreshold' => $data->expirythreshold,
'enrolstartdate' => $data->enrolstartdate,
'enrolenddate' => $data->enrolenddate);
$plugin->add_instance($course, $fields);
}

View File

@ -70,6 +70,14 @@ class enrol_self_edit_form extends moodleform {
$mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self');
$options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol'));
$mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
$mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
$mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), array('optional' => false, 'defaultunit' => 86400));
$mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
$mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
$mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
$mform->setDefault('enrolstartdate', 0);
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self');
@ -187,6 +195,10 @@ class enrol_self_edit_form extends moodleform {
}
}
if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
$errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
}
return $errors;
}

View File

@ -46,6 +46,18 @@ $string['enrolstartdate'] = 'Start date';
$string['enrolstartdate_help'] = 'If enabled, users can enrol themselves from this date onward only.';
$string['expiredaction'] = 'Enrolment expiration action';
$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.';
$string['expirymessageenrollersubject'] = 'Self enrolment expiry notification';
$string['expirymessageenrollerbody'] = 'Self enrolment in the course \'{$a->course}\' will expire within the next {$a->threshold} for the following users:
{$a->users}
To extend their enrolment, go to {$a->extendurl}';
$string['expirymessageenrolledsubject'] = 'Self enrolment expiry notification';
$string['expirymessageenrolledbody'] = 'Dear {$a->user},
This is a notification that your enrolment in the course \'{$a->course}\' is due to expire on {$a->timeend}.
If you need help, please contact {$a->enroller}.';
$string['groupkey'] = 'Use group enrolment keys';
$string['groupkey_desc'] = 'Use group enrolment keys by default.';
$string['groupkey_help'] = 'In addition to restricting access to the course to only those who know the key, use of a group enrolment key means users are automatically added to the group when they enrol in the course.
@ -56,6 +68,7 @@ $string['longtimenosee_help'] = 'If users haven\'t accessed a course for a long
$string['maxenrolled'] = 'Max enrolled users';
$string['maxenrolled_help'] = 'Specifies the maximum number of users that can self enrol. 0 means no limit.';
$string['maxenrolledreached'] = 'Maximum number of users allowed to self-enrol was already reached.';
$string['messageprovider:expiry_notification'] = 'Self enrolment expiry notifications';
$string['nopassword'] = 'No enrolment key required.';
$string['password'] = 'Enrolment key';
$string['password_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key.

View File

@ -29,6 +29,9 @@
*/
class enrol_self_plugin extends enrol_plugin {
protected $lasternoller = null;
protected $lasternollerinstanceid = 0;
/**
* Returns optional enrolment information icons.
*
@ -261,12 +264,22 @@ class enrol_self_plugin extends enrol_plugin {
* @return int id of new instance
*/
public function add_default_instance($course) {
$expirynotify = $this->get_config('expirynotify', 0);
if ($expirynotify == 2) {
$expirynotify = 1;
$notifyall = 1;
} else {
$notifyall = 0;
}
$fields = array('customint1' => $this->get_config('groupkey'),
'customint2' => $this->get_config('longtimenosee'),
'customint3' => $this->get_config('maxenrolled'),
'customint4' => $this->get_config('sendcoursewelcomemessage'),
'customint5' => 0,
'enrolperiod' => $this->get_config('enrolperiod', 0),
'expirynotify'=> $expirynotify,
'notifyall' => $notifyall,
'expirythreshold' => $this->get_config('expirythreshold', 86400),
'status' => $this->get_config('status'),
'roleid' => $this->get_config('roleid', 0));
@ -336,6 +349,7 @@ class enrol_self_plugin extends enrol_plugin {
*/
public function cron() {
$this->sync(null, true);
$this->send_expiry_notifications(true);
}
/**
@ -475,7 +489,40 @@ class enrol_self_plugin extends enrol_plugin {
return 0;
}
/**
/**
* Returns the user who is responsible for self enrolments in given instance.
*
* Usually it is the first editing teacher - the person with "highest authority"
* as defined by sort_by_roleassignment_authority() having 'enrol/self:manage'
* capability.
*
* @param int $instanceid enrolment instance id
* @return stdClass user record
*/
protected function get_enroller($instanceid) {
global $DB;
if ($this->lasternollerinstanceid == $instanceid and $this->lasternoller) {
return $this->lasternoller;
}
$instance = $DB->get_record('enrol', array('id'=>$instanceid, 'enrol'=>$this->get_name()), '*', MUST_EXIST);
$context = context_course::instance($instance->courseid);
if ($users = get_enrolled_users($context, 'enrol/self:manage')) {
$users = sort_by_roleassignment_authority($users, $context);
$this->lasternoller = reset($users);
unset($users);
} else {
$this->lasternoller = parent::get_enroller($instanceid);
}
$this->lasternollerinstanceid = $instanceid;
return $this->lasternoller;
}
/**
* Gets an array of the user enrolment actions.
*
* @param course_enrolment_manager $manager

View File

@ -47,6 +47,12 @@ if ($ADMIN->fulltree) {
);
$settings->add(new admin_setting_configselect('enrol_self/expiredaction', get_string('expiredaction', 'enrol_self'), get_string('expiredaction_help', 'enrol_self'), ENROL_EXT_REMOVED_KEEP, $options));
$options = array();
for ($i=0; $i<24; $i++) {
$options[$i] = $i;
}
$settings->add(new admin_setting_configselect('enrol_self/expirynotifyhour', get_string('expirynotifyhour', 'core_enrol'), '', 6, $options));
//--- enrol instance defaults ----------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_self_defaults',
get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin')));
@ -75,6 +81,13 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configduration('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'), 0));
$options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol'));
$settings->add(new admin_setting_configselect('enrol_self/expirynotify',
get_string('expirynotify', 'core_enrol'), get_string('expirynotify_help', 'core_enrol'), 0, $options));
$settings->add(new admin_setting_configduration('enrol_self/expirythreshold',
get_string('expirythreshold', 'core_enrol'), get_string('expirythreshold_help', 'core_enrol'), 86400, 86400));
$options = array(0 => get_string('never'),
1800 * 3600 * 24 => get_string('numdays', '', 1800),
1000 * 3600 * 24 => get_string('numdays', '', 1000),

View File

@ -261,4 +261,177 @@ class enrol_self_testcase extends advanced_testcase {
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
}
public function test_send_expiry_notifications() {
global $DB, $CFG;
$this->resetAfterTest();
$this->preventResetByRollback(); // Messaging does not like transactions...
/** @var $selfplugin enrol_self_plugin */
$selfplugin = enrol_get_plugin('self');
/** @var $manualplugin enrol_manual_plugin */
$manualplugin = enrol_get_plugin('manual');
$now = time();
$admin = get_admin();
// Note: hopefully nobody executes the unit tests the last second before midnight...
$selfplugin->set_config('expirynotifylast', $now - 60*60*24);
$selfplugin->set_config('expirynotifyhour', 0);
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
$this->assertNotEmpty($editingteacherrole);
$managerrole = $DB->get_record('role', array('shortname'=>'manager'));
$this->assertNotEmpty($managerrole);
$user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1'));
$user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2'));
$user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3'));
$user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4'));
$user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5'));
$user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
$user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
$user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
$course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1'));
$course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2'));
$course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3'));
$course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4'));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'self')));
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
$instance1->expirythreshold = 60*60*24*4;
$instance1->expirynotify = 1;
$instance1->notifyall = 1;
$instance1->status = ENROL_INSTANCE_ENABLED;
$DB->update_record('enrol', $instance1);
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
$instance2->expirythreshold = 60*60*24*1;
$instance2->expirynotify = 1;
$instance2->notifyall = 1;
$instance2->status = ENROL_INSTANCE_ENABLED;
$DB->update_record('enrol', $instance2);
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
$instance3->expirythreshold = 60*60*24*1;
$instance3->expirynotify = 1;
$instance3->notifyall = 0;
$instance3->status = ENROL_INSTANCE_ENABLED;
$DB->update_record('enrol', $instance3);
$maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'self'), '*', MUST_EXIST);
$instance4->expirythreshold = 60*60*24*1;
$instance4->expirynotify = 0;
$instance4->notifyall = 0;
$instance4->status = ENROL_INSTANCE_ENABLED;
$DB->update_record('enrol', $instance4);
$selfplugin->enrol_user($instance1, $user1->id, $studentrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified.
$selfplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5); // Above threshold are not notified.
$selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60); // Less than one day after threshold - should be notified.
$selfplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3); // Less than one day after threshold - should be notified.
$selfplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60); // Should have been already notified.
$selfplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60); // Already expired.
$manualplugin->enrol_user($maninstance1, $user7->id, $editingteacherrole->id);
$manualplugin->enrol_user($maninstance1, $user8->id, $managerrole->id); // Highest role --> enroller.
$selfplugin->enrol_user($instance2, $user1->id, $studentrole->id);
$selfplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3); // Above threshold are not notified.
$selfplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified.
$manualplugin->enrol_user($maninstance3, $user1->id, $editingteacherrole->id);
$selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); // Above threshold are not notified.
$selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified.
$manualplugin->enrol_user($maninstance4, $user4->id, $editingteacherrole->id);
$selfplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);
$selfplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);
// The notification is sent out in fixed order first individual users,
// then summary per course by enrolid, user lastname, etc.
$this->assertGreaterThan($instance1->id, $instance2->id);
$this->assertGreaterThan($instance2->id, $instance3->id);
$sink = $this->redirectMessages();
$selfplugin->send_expiry_notifications(false);
$messages = $sink->get_messages();
$this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages));
// First individual notifications from course1.
$this->assertEquals($user3->id, $messages[0]->useridto);
$this->assertEquals($user8->id, $messages[0]->useridfrom);
$this->assertContains('xcourse1', $messages[0]->fullmessagehtml);
$this->assertEquals($user4->id, $messages[1]->useridto);
$this->assertEquals($user8->id, $messages[1]->useridfrom);
$this->assertContains('xcourse1', $messages[1]->fullmessagehtml);
// Then summary for course1.
$this->assertEquals($user8->id, $messages[2]->useridto);
$this->assertEquals($admin->id, $messages[2]->useridfrom);
$this->assertContains('xcourse1', $messages[2]->fullmessagehtml);
$this->assertNotContains('xuser1', $messages[2]->fullmessagehtml);
$this->assertNotContains('xuser2', $messages[2]->fullmessagehtml);
$this->assertContains('xuser3', $messages[2]->fullmessagehtml);
$this->assertContains('xuser4', $messages[2]->fullmessagehtml);
$this->assertContains('xuser5', $messages[2]->fullmessagehtml);
$this->assertNotContains('xuser6', $messages[2]->fullmessagehtml);
// First individual notifications from course2.
$this->assertEquals($user3->id, $messages[3]->useridto);
$this->assertEquals($admin->id, $messages[3]->useridfrom);
$this->assertContains('xcourse2', $messages[3]->fullmessagehtml);
// Then summary for course2.
$this->assertEquals($admin->id, $messages[4]->useridto);
$this->assertEquals($admin->id, $messages[4]->useridfrom);
$this->assertContains('xcourse2', $messages[4]->fullmessagehtml);
$this->assertNotContains('xuser1', $messages[4]->fullmessagehtml);
$this->assertNotContains('xuser2', $messages[4]->fullmessagehtml);
$this->assertContains('xuser3', $messages[4]->fullmessagehtml);
$this->assertNotContains('xuser4', $messages[4]->fullmessagehtml);
$this->assertNotContains('xuser5', $messages[4]->fullmessagehtml);
$this->assertNotContains('xuser6', $messages[4]->fullmessagehtml);
// Only summary in course3.
$this->assertEquals($user1->id, $messages[5]->useridto);
$this->assertEquals($admin->id, $messages[5]->useridfrom);
$this->assertContains('xcourse3', $messages[5]->fullmessagehtml);
$this->assertNotContains('xuser1', $messages[5]->fullmessagehtml);
$this->assertNotContains('xuser2', $messages[5]->fullmessagehtml);
$this->assertContains('xuser3', $messages[5]->fullmessagehtml);
$this->assertNotContains('xuser4', $messages[5]->fullmessagehtml);
$this->assertNotContains('xuser5', $messages[5]->fullmessagehtml);
$this->assertNotContains('xuser6', $messages[5]->fullmessagehtml);
// Make sure that notifications are not repeated.
$sink->clear();
$selfplugin->send_expiry_notifications(false);
$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);
$this->assertEquals(0, $sink->count());
$selfplugin->set_config('expirynotifyhour', '0');
$selfplugin->send_expiry_notifications(false);
$this->assertEquals(6, $sink->count());
}
}

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012091500; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012091400; // Requires this Moodle version
$plugin->version = 2012101400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012100500; // Requires this Moodle version
$plugin->component = 'enrol_self'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 600;