From 653af355dc61dadc122820e1c1a129dbb20ef829 Mon Sep 17 00:00:00 2001 From: Neill Magill Date: Mon, 11 Oct 2021 09:32:00 +0100 Subject: [PATCH] MDL-72767 forum: Stop duplicate digests being created This change is to stop there being a possibility of 2 digests tasks being created for a user on the day that MDL-72767 is applied. --- mod/forum/db/upgrade.php | 31 ++++++++++++++++++++++++++++++- mod/forum/version.php | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index bdb360370db..81ec6a9e29e 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -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. @@ -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; } diff --git a/mod/forum/version.php b/mod/forum/version.php index 9320fa781b9..f439dc21aa0 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -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)