Skip to content

Commit

Permalink
MDL-35843 implement self enrolment expiry notification
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Oct 19, 2012
1 parent 8c04252 commit 8a486fd
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 6 deletions.
3 changes: 3 additions & 0 deletions enrol/self/cli/sync.php
Expand Up @@ -59,8 +59,11 @@

$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);
29 changes: 29 additions & 0 deletions enrol/self/db/messages.php
@@ -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 changes: 45 additions & 0 deletions enrol/self/db/upgrade.php
@@ -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;
}


39 changes: 36 additions & 3 deletions enrol/self/edit.php
Expand Up @@ -47,6 +47,12 @@

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.
Expand All @@ -63,6 +69,16 @@
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);

Expand All @@ -77,6 +93,9 @@
$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();
Expand All @@ -87,9 +106,23 @@
}

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

Expand Down
12 changes: 12 additions & 0 deletions enrol/self/edit_form.php
Expand Up @@ -70,6 +70,14 @@ function definition() {
$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');
Expand Down Expand Up @@ -187,6 +195,10 @@ function validation($data, $files) {
}
}

if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
$errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
}

return $errors;
}

Expand Down
13 changes: 13 additions & 0 deletions enrol/self/lang/en/enrol_self.php
Expand Up @@ -46,6 +46,18 @@
$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.
Expand All @@ -56,6 +68,7 @@
$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.
Expand Down
49 changes: 48 additions & 1 deletion enrol/self/lib.php
Expand Up @@ -29,6 +29,9 @@
*/
class enrol_self_plugin extends enrol_plugin {

protected $lasternoller = null;
protected $lasternollerinstanceid = 0;

/**
* Returns optional enrolment information icons.
*
Expand Down Expand Up @@ -261,12 +264,22 @@ public function enrol_page_hook(stdClass $instance) {
* @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));

Expand Down Expand Up @@ -336,6 +349,7 @@ protected function email_welcome_message($instance, $user) {
*/
public function cron() {
$this->sync(null, true);
$this->send_expiry_notifications(true);
}

/**
Expand Down Expand Up @@ -475,7 +489,40 @@ public function sync($courseid = null, $verbose = false) {
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
Expand Down
13 changes: 13 additions & 0 deletions enrol/self/settings.php
Expand Up @@ -47,6 +47,12 @@
);
$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')));
Expand Down Expand Up @@ -75,6 +81,13 @@
$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),
Expand Down

0 comments on commit 8a486fd

Please sign in to comment.