Skip to content

Commit

Permalink
new notification settings to determine which users get a recompletion…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
davidpesce authored and danmarsden committed May 18, 2024
1 parent b477de7 commit 12b8596
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
18 changes: 18 additions & 0 deletions classes/recompletion_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class local_recompletion_recompletion_form extends moodleform {
/** @var string */
const RECOMPLETION_TYPE_SCHEDULE = 'schedule';

/** @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 @@ -72,6 +81,15 @@ public function definition() {
$mform->addHelpButton('recompletionemailenable', 'recompletionemailenable', 'local_recompletion');
$mform->hideIf('recompletionemailenable', 'recompletiontype', 'eq', '');

$mform->addElement('select', 'recompletionnotify', get_string('recompletionnotify', '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->disabledIf('recompletionnotify', 'recompletionemailenable', 'notchecked');

$mform->addElement('checkbox', 'recompletionunenrolenable', get_string('recompletionunenrolenable', 'local_recompletion'));
$mform->setDefault('recompletionunenrolenable', $config->unenrolenable);
$mform->addHelpButton('recompletionunenrolenable', 'recompletionunenrolenable', 'local_recompletion');
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 @@ -295,11 +291,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 ($config->recompletionemailenable) {
// 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
10 changes: 10 additions & 0 deletions lang/en/local_recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
$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 all users (with a completion record)';
$string['recompletionnotify:enrolled'] = 'Send to all users (with an enrollment)';
$string['recompletionnotify:activeenrolled'] = 'Send to all users (with an active enrollment)';
$string['recompletionnotify'] = 'Recompletion user notification';
$string['recompletionnotify_help'] = 'Determines which users are notified of recompletion.
* Send to all users (with a completion record) - If a course completion record exists for a user, they will be notified.
* Send to all users (with an enrollment) - If a user is enrolled (active, suspended, etc) in the course, they will be notified.
* Send to all users (with an active enrollment) - If a user is actively enrolled, 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
1 change: 1 addition & 0 deletions recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
'deletegradedata',
'archivecompletiondata',
'recompletionemailenable',
'recompletionnotify',
'recompletionunenrolenable',
'recompletionemailsubject',
'recompletionemailbody',
Expand Down
17 changes: 17 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@
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_COMPLETED_USERS => get_string(
'recompletionnotify:completed',
'local_recompletion',
),
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_ENROLLED_USERS => get_string(
'recompletionnotify:enrolled',
'local_recompletion',
),
local_recompletion_recompletion_form::RECOMPLETION_NOTIFY_ACTIVE_ENROLLED_USERS => get_string(
'recompletionnotify:activeenrolled',
'local_recompletion',
),
]));

$settings->add(new admin_setting_configtext('local_recompletion/emailsubject',
new lang_string('recompletionemailsubject', 'local_recompletion'),
new lang_string('recompletionemailsubject_help', 'local_recompletion'), '', PARAM_TEXT));
Expand Down

0 comments on commit 12b8596

Please sign in to comment.