Skip to content

Commit

Permalink
Merge pull request #196 from aneno-m-e/notifications
Browse files Browse the repository at this point in the history
 Notification setting to determine who gets recompletion message
  • Loading branch information
danmarsden authored Nov 22, 2024
2 parents f4034d6 + a88f6bc commit 8d8fdbf
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 24 deletions.
10 changes: 10 additions & 0 deletions backup/moodle2/restore_local_recompletion_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public function process_recompletion($data) {

$data = (object) $data;
$data->course = $this->task->get_courseid();
if ($data->name == 'recompletionemailenable') {
// Old format of email setting.
if (empty($data->value)) {
// Old value stored as 0 - new one stored as empty string.
$data->value = '';
} else {
$data->value = 'completed';
}
$data->name = 'recompletionnotify';
}

$DB->insert_record('local_recompletion_config', $data);
}
Expand Down
31 changes: 24 additions & 7 deletions classes/recompletion_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ class local_recompletion_recompletion_form extends moodleform {
/** @var string */
const RECOMPLETION_TYPE_SCHEDULE = 'schedule';

/** @var string */
const RECOMPLETION_NOTIFY_DISABLED = '';

/** @var string */
const RECOMPLETION_NOTIFY_COMPLETED_USERS = 'completed';

/** @var string */
const RECOMPLETION_NOTIFY_ENROLLED_USERS = 'enrolled';

/** @var string */
const RECOMPLETION_NOTIFY_ACTIVE_ENROLLED_USERS = 'activeenrolled';

/**
* Defines the form fields.
*/
Expand Down Expand Up @@ -67,15 +79,20 @@ public function definition() {
$mform->setDefault('recompletiontype', self::RECOMPLETION_TYPE_DISABLED);
$mform->addHelpButton('recompletiontype', 'recompletiontype', 'local_recompletion');

$mform->addElement('checkbox', 'recompletionemailenable', get_string('recompletionemailenable', 'local_recompletion'));
$mform->setDefault('recompletionemailenable', $config->recompletionemailenable);
$mform->addHelpButton('recompletionemailenable', 'recompletionemailenable', 'local_recompletion');
$mform->hideIf('recompletionemailenable', 'recompletiontype', 'eq', '');
$mform->addElement('select', 'recompletionnotify', get_string('recompletionnotify', 'local_recompletion'), [
self::RECOMPLETION_NOTIFY_DISABLED => get_string('recompletiontype:disabled', 'local_recompletion'),
self::RECOMPLETION_NOTIFY_COMPLETED_USERS => get_string('recompletionnotify:completed', 'local_recompletion'),
self::RECOMPLETION_NOTIFY_ENROLLED_USERS => get_string('recompletionnotify:enrolled', 'local_recompletion'),
self::RECOMPLETION_NOTIFY_ACTIVE_ENROLLED_USERS => get_string('recompletionnotify:activeenrolled', 'local_recompletion'),
]);
$mform->setDefault('recompletionnotify', $config->recompletionnotify ?? '');
$mform->addHelpButton('recompletionnotify', 'recompletionnotify', 'local_recompletion');
$mform->hideIf('recompletionnotify', 'recompletiontype', 'eq', self::RECOMPLETION_TYPE_DISABLED);

$mform->addElement('checkbox', 'recompletionunenrolenable', get_string('recompletionunenrolenable', 'local_recompletion'));
$mform->setDefault('recompletionunenrolenable', $config->recompletionunenrolenable);
$mform->addHelpButton('recompletionunenrolenable', 'recompletionunenrolenable', 'local_recompletion');
$mform->hideIf('recompletionunenrolenable', 'recompletiontype', 'eq', '');
$mform->hideIf('recompletionunenrolenable', 'recompletiontype', 'eq', self::RECOMPLETION_TYPE_DISABLED);

$options = ['optional' => false, 'defaultunit' => 86400];
$mform->addElement('duration', 'recompletionduration', get_string('recompletionrange', 'local_recompletion'), $options);
Expand Down Expand Up @@ -104,15 +121,15 @@ public function definition() {
$mform->setType('recompletionemailsubject', PARAM_TEXT);
$mform->addHelpButton('recompletionemailsubject', 'recompletionemailsubject', 'local_recompletion');
$mform->disabledIf('recompletionemailsubject', 'recompletiontype', 'eq', '');
$mform->disabledIf('recompletionemailsubject', 'recompletionemailenable', 'notchecked');
$mform->disabledIf('recompletionemailsubject', 'recompletionnotify', 'eq', self::RECOMPLETION_NOTIFY_DISABLED);
$mform->setDefault('recompletionemailsubject', $config->recompletionemailsubject);

$mform->addElement('editor', 'recompletionemailbody', get_string('recompletionemailbody', 'local_recompletion'),
$editoroptions);
$mform->setDefault('recompletionemailbody', ['text' => $config->recompletionemailbody, 'format' => FORMAT_HTML]);
$mform->addHelpButton('recompletionemailbody', 'recompletionemailbody', 'local_recompletion');
$mform->disabledIf('recompletionemailbody', 'recompletiontype', 'eq', '');
$mform->disabledIf('recompletionemailbody', 'recompletionemailenable', 'notchecked');
$mform->disabledIf('recompletionemailbody', 'recompletionnotify', 'eq', self::RECOMPLETION_NOTIFY_DISABLED);

// Advanced recompletion settings.
// Delete data section.
Expand Down
24 changes: 17 additions & 7 deletions classes/task/check_recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ protected function reset_completions($userid, $course, $config) {
protected function notify_user($userid, $course, $config) {
global $DB, $CFG;

if (!$config->recompletionemailenable) {
return;
}

$userrecord = $DB->get_record('user', ['id' => $userid]);
$context = \context_course::instance($course->id);
$from = get_admin();
Expand Down Expand Up @@ -296,11 +292,25 @@ public function reset_user($userid, $course, $config = null) {
}
}

// Now notify user.
$this->notify_user($userid, $course, $config);
$context = \context_course::instance($course->id);

// Determine if user should be notified.
if (!empty($config->recompletionnotify)) {
// If user has a completion record, notify user.
if ($config->recompletionnotify == 'completed') {
$this->notify_user($userid, $course, $config);
} else if ($config->recompletionnotify == 'enrolled') { // Active or suspended enrollment, notify user.
if (is_enrolled($context, $userid)) {
$this->notify_user($userid, $course, $config);
}
} else if ($config->recompletionnotify == 'activeenrolled') { // Active enrollment only, notify user.
if (is_enrolled($context, $userid, '', true)) {
$this->notify_user($userid, $course, $config);
}
}
}

// Trigger completion reset event for this user.
$context = \context_course::instance($course->id);
$event = \local_recompletion\event\completion_reset::create(
[
'objectid' => $course->id,
Expand Down
18 changes: 18 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,5 +1098,23 @@ function xmldb_local_recompletion_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024071100, 'local', 'recompletion');
}

if ($oldversion < 2024090300) {
// Convert old recompletionemailenable setting to recompletionnotify.
$emailsettings = $DB->get_records('local_recompletion_config', ['name' => 'recompletionemailenable']);
foreach ($emailsettings as $esetting) {
if (empty($esetting->value)) {
// old value set to 0 now an empty string.
$esetting->value = '';
} else {
$esetting->value = 'completed';
}
$esetting->name = 'recompletionnotify';
$DB->update_record('local_recompletion_config', $esetting);
}

// Recompletion savepoint reached.
upgrade_plugin_savepoint(true, 2024090300, 'local', 'recompletion');

}
return true;
}
12 changes: 10 additions & 2 deletions lang/en/local_recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@
$string['recompletiontask'] = 'Check for users that need to recomplete';
$string['completionnotenabled'] = 'Completion is not enabled in this course';
$string['recompletionnotenabledincourse'] = 'Recompletion is not enabled in courseid: {$a}';
$string['recompletionemailenable'] = 'Send recompletion message';
$string['recompletionemailenable_help'] = 'Enable email messaging to notifiy users that recompletion is required';

$string['recompletionnotify:completed'] = 'Send to completed users';
$string['recompletionnotify:enrolled'] = 'Send to completed users with an enrollment';
$string['recompletionnotify:activeenrolled'] = 'Send to completed users with an active enrollment';
$string['recompletionnotify'] = 'Recompletion message';
$string['recompletionnotify_help'] = 'Determines which users are notified of recompletion.
* Send to completed users - If a course completion record exists for a user, they will be notified.
* Send to completed users with an active enrollment - If a user has an active enrollment they will be notified.
* Send to completed users with an enrollment - If a user has an active or suspended enrollment they will be notified.';

$string['recompletionemailsubject'] = 'Recompletion message subject';
$string['recompletionemailsubject_help'] = 'A custom recompletion email subject may be added as plain text
Expand Down
2 changes: 1 addition & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function local_recompletion_get_config($course) {
'recompletionschedule' => '',
'assignevent' => null,
'archivecompletiondata' => 0,
'recompletionemailenable' => 0,
'recompletionnotify' => '',
'recompletionunenrolenable' => 0,
'recompletionemailbody' => '',
'recompletionemailbody_format' => FORMAT_HTML,
Expand Down
2 changes: 1 addition & 1 deletion recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
'recompletionschedule',
'deletegradedata',
'archivecompletiondata',
'recompletionemailenable',
'recompletionnotify',
'recompletionunenrolenable',
'recompletionemailsubject',
'recompletionemailbody',
Expand Down
23 changes: 20 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,26 @@
new lang_string('recompletionrange', 'local_recompletion'),
new lang_string('recompletionrange_help', 'local_recompletion'), YEARSECS, PARAM_INT));

$settings->add(new admin_setting_configcheckbox('local_recompletion/recompletionemailenable',
new lang_string('recompletionemailenable', 'local_recompletion'),
new lang_string('recompletionemailenable_help', 'local_recompletion'), 1));
$settings->add(new admin_setting_configselect('local_recompletion/recompletionnotify',
new lang_string('recompletionnotify', 'local_recompletion'),
new lang_string('recompletionnotify_help', 'local_recompletion'), 'range', [
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_DISABLED => get_string(
'recompletiontype:disabled',
'local_recompletion',
),
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_COMPLETED_USERS => get_string(
'recompletionnotify:completed',
'local_recompletion',
),
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_ACTIVE_ENROLLED_USERS => get_string(
'recompletionnotify:activeenrolled',
'local_recompletion',
),
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_ENROLLED_USERS => get_string(
'recompletionnotify:enrolled',
'local_recompletion',
),
]));

$settings->add(new admin_setting_configtext('local_recompletion/recompletionemailsubject',
new lang_string('recompletionemailsubject', 'local_recompletion'),
Expand Down
2 changes: 1 addition & 1 deletion tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function set_up_recompletion(int $courseid, array $config = []): void
'recompletionunenrolenable' => 1,
'archivecompletiondata' => 0,
'deletegradedata' => 1,
'recompletionemailenable' => 0,
'recompletionnotify' => '',
];

$config = array_merge($defaultconfig, $config);
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2024071103;
$plugin->release = 2024071103;
$plugin->version = 2024090300;
$plugin->release = 2024090300;
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2023100900; // Requires 4.3.
$plugin->component = 'local_recompletion';
Expand Down

0 comments on commit 8d8fdbf

Please sign in to comment.