Skip to content

Commit

Permalink
Merge branch 'MDL-72767' of https://github.com/NeillM/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Nov 2, 2021
2 parents c1a43e8 + 653af35 commit b4fbfb5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
7 changes: 5 additions & 2 deletions mod/forum/classes/task/cron_task.php
Expand Up @@ -336,16 +336,19 @@ protected function queue_user_tasks() {
if (!empty($digestpostdata)) {
// Insert all of the records for the digest.
$DB->insert_records('forum_queue', $digestpostdata);
$digesttime = usergetmidnight($timenow, $sitetimezone) + ($CFG->digestmailtime * 3600);
$servermidnight = usergetmidnight($timenow, $sitetimezone);
$digesttime = $servermidnight + ($CFG->digestmailtime * 3600);

if ($digesttime < $timenow) {
// Digest time is in the past. Get a new time for tomorrow.
$digesttime = usergetmidnight($timenow + DAYSECS, $sitetimezone) + ($CFG->digestmailtime * 3600);
$servermidnight = usergetmidnight($timenow + DAYSECS, $sitetimezone);
$digesttime = $servermidnight + ($CFG->digestmailtime * 3600);
}

$task = new \mod_forum\task\send_user_digests();
$task->set_userid($user->id);
$task->set_component('mod_forum');
$task->set_custom_data(['servermidnight' => $servermidnight]);
$task->set_next_run_time($digesttime);
\core\task\manager::reschedule_or_queue_adhoc_task($task);
$usercounts['digests']++;
Expand Down
31 changes: 30 additions & 1 deletion mod/forum/db/upgrade.php
Expand Up @@ -43,7 +43,7 @@
defined('MOODLE_INTERNAL') || die();

function xmldb_forum_upgrade($oldversion) {
global $DB;
global $CFG, $DB;

$dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.

Expand Down Expand Up @@ -255,5 +255,34 @@ function xmldb_forum_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2020072100, 'forum');
}

if ($oldversion < 2021101100) {
// Add custom data to digest tasks to stop duplicates being created after this patch.
$timenow = time();

$sitetimezone = \core_date::get_server_timezone();
$servermidnight = usergetmidnight($timenow, $sitetimezone);
$digesttime = $servermidnight + ($CFG->digestmailtime * 3600);
if ($digesttime < $timenow) {
// Digest time is in the past. set for tomorrow.
$servermidnight = usergetmidnight($timenow + DAYSECS, $sitetimezone);
}

$customdata = json_encode(['servermidnight' => $servermidnight]);

$params = [
'component' => 'mod_forum',
'classname' => '\mod_forum\task\send_user_digests',
'customdata' => '', // We do not want to overwrite any tasks that already have the custom data.
];

$textfield = $DB->sql_compare_text('customdata', 1);

$sql = "component = :component AND classname = :classname AND $textfield = :customdata";

$DB->set_field_select('task_adhoc', 'customdata', $customdata, $sql, $params);

upgrade_mod_savepoint(true, 2021101100, 'forum');
}

return true;
}
69 changes: 68 additions & 1 deletion mod/forum/tests/maildigest_test.php
Expand Up @@ -655,7 +655,7 @@ public function test_cron_digest_previous_day() {
}

/**
* The digest being in the past is queued til the next day.
* The digest being in the future is queued for today.
*/
public function test_cron_digest_same_day() {
global $DB, $CFG;
Expand Down Expand Up @@ -695,6 +695,73 @@ public function test_cron_digest_same_day() {
$this->assertLessThanOrEqual($digesttime, $task->nextruntime);
}

/**
* Tests that if a new message is posted after the days digest time,
* but before that days digests are sent a new task is created.
*/
public function test_cron_digest_queue_next_before_current_processed() {
global $DB, $CFG;

$this->resetAfterTest(true);

// Set up a basic user enrolled in a course.
$userhelper = $this->helper_setup_user_in_course();
$user = $userhelper->user;
$forum1 = $userhelper->forums->forum1;

// Add 1 discussions to forum 1.
$this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);

// Set the tested user's default maildigest setting.
$DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);

// Set the digest time to the future (magic, shouldn't work).
$CFG->digestmailtime = 25;
// One digest e-mail should be sent, and no individual notifications.
$expect = [
(object) [
'userid' => $user->id,
'digests' => 1,
],
];
$this->queue_tasks_and_assert($expect);

// Set the digest time to midnight.
$CFG->digestmailtime = 0;

// Add another discussions to forum 1.
$this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);

// One digest e-mail should be sent, and no individual notifications.
$expect = [
(object) [
'userid' => $user->id,
'digests' => 1,
],
];
$this->queue_tasks_and_assert($expect);

// There should now be two tasks queued.
$tasks = $DB->get_records('task_adhoc');
$this->assertCount(2, $tasks);

// Add yet another another discussions to forum 1.
$this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);

// One digest e-mail should be sent, and no individual notifications.
$expect = [
(object) [
'userid' => $user->id,
'digests' => 1,
],
];
$this->queue_tasks_and_assert($expect);

// There should still be two tasks queued.
$tasks = $DB->get_records('task_adhoc');
$this->assertCount(2, $tasks);
}

/**
* The sending of a digest marks posts as read if automatic message read marking is set.
*/
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/version.php
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2021052501; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2021101100; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2021052500; // Requires this Moodle version.
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)

0 comments on commit b4fbfb5

Please sign in to comment.