Skip to content

Commit

Permalink
Merge pull request #131 from isuru89/feature/explicit-activity-enable
Browse files Browse the repository at this point in the history
Feature/explicit activity enable
  • Loading branch information
isuru89 authored Jun 27, 2021
2 parents c071da6 + b3e553a commit aa45cab
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions coursesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
24 changes: 18 additions & 6 deletions coursesettings_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
Expand All @@ -93,6 +102,7 @@ public function definition() {
}
ksort($allactivities);
$upcomingactivities = $allactivities;

$mform->addElement('static', 'descriptionsub', '',
get_string('activityconfupcomingactivitiesdesc', 'local_reminders'));

Expand Down Expand Up @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions lang/en/local_reminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/

$string['activityconfduein'] = 'Due In';
$string['activityconfexplicitenable'] = 'Explicit Reminder Activation';
$string['activityconfexplicitenabledesc'] = 'If checked, teachers or relevant authorities must <strong>explicitly</strong> 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 <em>explictly</em> 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.';
Expand Down
13 changes: 9 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
define('REMINDERS_CALL_TYPE_OVERDUE', 'OVERDUE');

define('REMINDERS_CLEAN_TABLE', 'local_reminders');
define('REMINDERS_ENABLED_KEY', 'enabled');

/**
* ======== FUNCTIONS =========================================
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
54 changes: 48 additions & 6 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,59 @@ 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;
}
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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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.");
Expand Down
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit aa45cab

Please sign in to comment.