diff --git a/README.md b/README.md
index 09796b5..16ea29f 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Moodle - Local Reminders
---
-![Version](https://img.shields.io/badge/version-v2.3.1-blue)
+![Version](https://img.shields.io/badge/version-v2.4-blue)
![Moodle Version](https://img.shields.io/badge/moodle-%3E%3D%203.5-orange)
![License](https://img.shields.io/badge/license-GPL%20v3-green)
[![Build Status](https://github.com/isuru89/moodle-local_reminders/actions/workflows/moodle-ci.yml/badge.svg?branch=release_2.0)](https://github.com/isuru89/moodle-local_reminders/actions/workflows/moodle-ci.yml)
@@ -92,9 +92,13 @@ In addition to above, user can control reminders for calendar event changes per
|---|---|---|
| No reminders once completed | enable/disable sending reminders if a user has completed activity. If checked, he/she won't receive reminders anymore once completed. | true |
| Activity Overdue Reminders | enable/disable sending reminders for users who still have not completed expired events | true |
+ | Explicit Reminder Activation | If checked, teachers or relevant authorities must explicitly enable reminders for each activity under course reminders settings page. | false |
## Changelog
+### v2.4
+ * Ability to explicitly turn on reminders instead of enabling by default for all (#129, #130)
+
### v2.3.1
* Removed hard coded string in course settings page (#124)
* Fixed incorrect argument pass in calendar update events (#126)
diff --git a/coursesettings.php b/coursesettings.php
index 8668110..6811e08 100644
--- a/coursesettings.php
+++ b/coursesettings.php
@@ -37,6 +37,7 @@
$coursesettings = new stdClass();
}
$coursesettings->courseid = $courseid;
+$coursesettings->explicitenable = isset($CFG->local_reminders_explicitenable) && $CFG->local_reminders_explicitenable;
$coursecontext = context_course::instance($course->id);
$activitysettings = $DB->get_records('local_reminders_activityconf', array('courseid' => $courseid));
diff --git a/coursesettings_form.php b/coursesettings_form.php
index 6f66e40..ba9c957 100644
--- a/coursesettings_form.php
+++ b/coursesettings_form.php
@@ -50,17 +50,23 @@ public function definition() {
$mform = $this->_form;
list($coursesettings) = $this->_customdata;
+ $explicitlyenable = $coursesettings->explicitenable;
- $mform->addElement('advcheckbox', 'status_course',
- get_string('courseheading', 'local_reminders'),
- get_string('enabled', 'local_reminders'));
- $mform->setDefault('status_course', 1);
+ if ($explicitlyenable) {
+ $mform->addElement('static', 'descriptionex2sub', '',
+ get_string('activityconfexplicitenablehint', 'local_reminders'));
+ }
$mform->addElement('advcheckbox', 'status_activities',
get_string('dueheading', 'local_reminders'),
get_string('enabled', 'local_reminders'));
$mform->setDefault('status_activities', 1);
+ $mform->addElement('advcheckbox', 'status_course',
+ get_string('courseheading', 'local_reminders'),
+ get_string('enabled', 'local_reminders'));
+ $mform->setDefault('status_course', 1);
+
$mform->addElement('advcheckbox', 'status_group',
get_string('groupheading', 'local_reminders'),
get_string('enabled', 'local_reminders'));
@@ -69,6 +75,9 @@ public function definition() {
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
+ $mform->addElement('hidden', 'explicitenable');
+ $mform->setType('explicitenable', PARAM_INT);
+
foreach ($daysarray as $dkey => $dvalue) {
$mform->addElement('hidden', "activityglobal_$dkey");
$mform->setType("activityglobal_$dkey", PARAM_INT);
@@ -93,6 +102,7 @@ public function definition() {
}
ksort($allactivities);
$upcomingactivities = $allactivities;
+
$mform->addElement('static', 'descriptionsub', '',
get_string('activityconfupcomingactivitiesdesc', 'local_reminders'));
@@ -129,14 +139,16 @@ public function definition() {
$key = "activity_".$activity->id.'_enabled';
$mform->addElement('advcheckbox', $key, get_string('enabled', 'local_reminders'), ' ');
- $mform->setDefault($key, 1);
+ $mform->setDefault($key, $explicitlyenable ? 0 : 1);
$activitydayarray = array();
foreach ($daysarray as $dkey => $dvalue) {
$trefkey = "activityglobal_$dkey";
$daykey = "activity_".$activity->id."_$dkey";
$activitydayarray[] = $mform->createElement('advcheckbox', $daykey, '', $dvalue);
- $mform->disabledIf($daykey, $trefkey, 'eq', 0);
+ if (!$explicitlyenable) {
+ $mform->disabledIf($daykey, $trefkey, 'eq', 0);
+ }
$mform->setDefault($daykey, $coursesettings->$trefkey);
}
$groupkey = 'reminder_'.$activity->id.'_group';
diff --git a/lang/en/local_reminders.php b/lang/en/local_reminders.php
index 269c9c2..8470b83 100644
--- a/lang/en/local_reminders.php
+++ b/lang/en/local_reminders.php
@@ -23,6 +23,9 @@
*/
$string['activityconfduein'] = 'Due In';
+$string['activityconfexplicitenable'] = 'Explicit Reminder Activation';
+$string['activityconfexplicitenabledesc'] = 'If checked, teachers or relevant authorities must explicitly enable reminders for each activity under course reminders settings page. Because of that, all activity reminders will be by default disabled regardless of the schedule defined in below. This configuration will not be impact to the overdue reminders anyway.';
+$string['activityconfexplicitenablehint'] = 'Site administrator(s) have disabled sending activity reminders by default. That means, teachers must explictly enable reminders for activities in this course which they want to sent.';
$string['activityconfupcomingactivities'] = 'Upcoming Activities';
$string['activityconfupcomingactivitiesdesc'] = 'Reminders will not be sent for unchecked activities.';
$string['activityconfnoupcomingactivities'] = 'No upcoming activities for this course.';
diff --git a/lib.php b/lib.php
index fdfa87a..0e4cc4b 100644
--- a/lib.php
+++ b/lib.php
@@ -74,6 +74,7 @@
define('REMINDERS_CALL_TYPE_OVERDUE', 'OVERDUE');
define('REMINDERS_CLEAN_TABLE', 'local_reminders');
+define('REMINDERS_ENABLED_KEY', 'enabled');
/**
* ======== FUNCTIONS =========================================
@@ -173,7 +174,11 @@ function local_reminders_cron_pre($currtime, $timewindowstart) {
$excludedmodules = explode(',', $CFG->local_reminders_excludedmodulenames);
}
+ $explicitactivityenable = isset($CFG->local_reminders_explicitenable)
+ && $CFG->local_reminders_explicitenable;
+
$allemailfailed = true;
+ $triedcount = 0;
foreach ($upcomingevents as $event) {
if (in_array($event->modulename, $excludedmodules)) {
mtrace(" [Local Reminder] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
@@ -237,8 +242,8 @@ function local_reminders_cron_pre($currtime, $timewindowstart) {
}
// This reminder will not be set up to send by configurations.
- if ($options[$aheaddaysindex[$aheadday]] == '0') {
- mtrace(" [Local Reminder] No reminder is due in ahead of $aheadday for eventtype $event->eventtype " .
+ if ($options[$aheaddaysindex[$aheadday]] == '0' && !$explicitactivityenable) {
+ mtrace(" [Local Reminder] Reminders are disabled in ahead of $aheadday days for eventtype $event->eventtype " .
"[event#$event->id is ignored!]...");
continue;
}
@@ -249,7 +254,6 @@ function local_reminders_cron_pre($currtime, $timewindowstart) {
}
$reminderref = null;
- mtrace(" [Local Reminder] Finding out users for event#".$event->id."...");
try {
switch ($event->eventtype) {
@@ -321,6 +325,7 @@ function local_reminders_cron_pre($currtime, $timewindowstart) {
mtrace(" [Local Reminder] Starting sending reminders for $event->id [type: $event->eventtype, mod: $event->modulename]");
$failedcount = 0;
+ $triedcount++;
$sendusers = $reminderref->get_sending_users();
foreach ($sendusers as $touser) {
@@ -351,7 +356,7 @@ function local_reminders_cron_pre($currtime, $timewindowstart) {
$reminderref->cleanup();
}
- if (!$allemailfailed) {
+ if (!$allemailfailed || $triedcount == 0) {
add_flag_record_db($timewindowend, 'sent');
mtrace(' [Local Reminder] Marked this reminder execution as success.');
} else {
diff --git a/locallib.php b/locallib.php
index dd9487b..87218f6 100644
--- a/locallib.php
+++ b/locallib.php
@@ -90,7 +90,7 @@ function fetch_course_activity_settings($courseid, $eventid) {
* @param string $keytocheck key to check for.
* @return bool return true if reminders disabled for activity.
*/
-function has_disabled_reminders_for_activity($courseid, $eventid, $keytocheck='enabled') {
+function has_disabled_reminders_for_activity($courseid, $eventid, $keytocheck=REMINDERS_ENABLED_KEY) {
$activitysettings = fetch_course_activity_settings($courseid, $eventid);
if (array_key_exists($keytocheck, $activitysettings) && !$activitysettings[$keytocheck]) {
return true;
@@ -98,6 +98,51 @@ function has_disabled_reminders_for_activity($courseid, $eventid, $keytocheck='e
return false;
}
+/**
+ * Returns true if reminders can be sent to the given event based on Moodle configured settings.
+ *
+ * @param object $event event instance reference.
+ * @param object $options context options.
+ * @param number $aheadday number of days ahead this activity belongs to.
+ * @return bool true if reminders can sent, otherwise false.
+ */
+function should_run_for_activity($event, $options, $aheadday=null) {
+ global $DB, $CFG;
+
+ $showtrace = $options->showtrace;
+ $aheadday = $options->aheadday;
+ $courseid = $event->courseid;
+ $eventid = $event->id;
+ $aheaddayskey = "days$aheadday";
+ $explicitenable = isset($CFG->local_reminders_explicitenable) && $CFG->local_reminders_explicitenable;
+
+ $activitysettings = fetch_course_activity_settings($courseid, $eventid);
+ if (array_key_exists(REMINDERS_ENABLED_KEY, $activitysettings) && !$activitysettings[REMINDERS_ENABLED_KEY]) {
+ $showtrace && mtrace(" [Local Reminder] Reminders for activity event#$eventid (title=$event->name) ".
+ "have been disabled in the course settings.");
+ return false;
+ } else if (array_key_exists($aheaddayskey, $activitysettings) && !$activitysettings[$aheaddayskey]) {
+ $showtrace && mtrace(" [Local Reminder] Reminders for activity event#$eventid (title=$event->name) ".
+ "have been disabled for $aheadday days ahead.");
+ return false;
+ }
+
+ if ($explicitenable) {
+ // Must be explicitly enabled the reminders to be sent.
+ if (array_key_exists(REMINDERS_ENABLED_KEY, $activitysettings)
+ && $activitysettings[REMINDERS_ENABLED_KEY]
+ && array_key_exists($aheaddayskey, $activitysettings)
+ && $activitysettings[$aheaddayskey]) {
+ return true;
+ }
+
+ $showtrace && mtrace(" [Local Reminder] Reminders for activity event#$eventid (title=$event->name) ".
+ "have explicitly not been enabled in the course settings.");
+ return false;
+ }
+ return true;
+}
+
/**
* This method will filter out all the activity events finished recently
* and send reminders for users who still have not yet completed that activity.
@@ -227,11 +272,7 @@ function handle_course_activity_event($event, $course, $cm, $options) {
if (is_course_hidden_and_denied($course)) {
$showtrace && mtrace(" [Local Reminder] Course is hidden. No reminders will be sent.");
return null;
- } else if (has_disabled_reminders_for_activity($event->courseid, $event->id)) {
- $showtrace && mtrace(" [Local Reminder] Activity event $event->id reminders disabled in the course settings.");
- return null;
- } else if (has_disabled_reminders_for_activity($event->courseid, $event->id, "days$aheadday")) {
- $showtrace && mtrace(" [Local Reminder] Activity event $event->id reminders disabled for $aheadday days ahead.");
+ } else if (!should_run_for_activity($event, $options, $aheadday)) {
return null;
}
@@ -240,6 +281,7 @@ function handle_course_activity_event($event, $course, $cm, $options) {
$sendusers = array();
$reminder = new due_reminder($event, $course, $context, $cm, $aheadday);
+ mtrace(" [Local Reminder] Finding out users for event#".$event->id."...");
if ($event->courseid <= 0 && $event->userid > 0) {
// A user overridden activity.
$showtrace && mtrace(" [Local Reminder] Event #".$event->id." is a user overridden ".$event->modulename." event.");
diff --git a/settings.php b/settings.php
index 8e59296..8ead85f 100644
--- a/settings.php
+++ b/settings.php
@@ -222,6 +222,10 @@
get_string('explainsendactivityreminders', 'local_reminders'),
REMINDERS_ACTIVITY_BOTH, $activitychoices));
+ $settings->add(new admin_setting_configcheckbox('local_reminders_explicitenable',
+ get_string('activityconfexplicitenable', 'local_reminders'),
+ get_string('activityconfexplicitenabledesc', 'local_reminders'), 0));
+
$settings->add(new admin_setting_configmulticheckbox2('local_reminders_duerdays',
get_string('reminderdaysahead', 'local_reminders'),
get_string('explaindueheading', 'local_reminders'),
diff --git a/version.php b/version.php
index e7bca76..30b0651 100644
--- a/version.php
+++ b/version.php
@@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2021052500;
+$plugin->version = 2021062000;
$plugin->requires = 2018051700; // Require moodle 3.5 or higher.
-$plugin->release = '2.3.1';
+$plugin->release = '2.4';
$plugin->maturity = MATURITY_RC;
$plugin->component = 'local_reminders';