From 228746992cf8e38637f3657663bef49636faa9ed Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Dang Date: Thu, 1 Aug 2024 20:38:11 +0100 Subject: [PATCH] StudentQuiz: switch stored notification to use JSON #812359 Co-authored-by: Tim Hunt --- .../moodle2/restore_studentquiz_stepslib.php | 7 +++++ .../task/send_digest_notification_task.php | 16 +++++------ db/install.xml | 2 +- db/upgrade.php | 28 +++++++++++++++++++ locallib.php | 2 +- tests/cron_test.php | 2 +- version.php | 2 +- 7 files changed, 47 insertions(+), 12 deletions(-) diff --git a/backup/moodle2/restore_studentquiz_stepslib.php b/backup/moodle2/restore_studentquiz_stepslib.php index e4051f08..6dcd1248 100644 --- a/backup/moodle2/restore_studentquiz_stepslib.php +++ b/backup/moodle2/restore_studentquiz_stepslib.php @@ -332,6 +332,13 @@ protected function process_notification($data) { $data = (object) $data; $data->studentquizid = $this->get_mappingid('notification', $data->studentquizid); + if (json_decode($data->content) === null) { + // Older versions of StudentQuiz stored this data serialised. We no longer support that. + // Such data is not restored. At worse, this leads to some lost notifications for users + // who have chosen to receive a digest, but to people want notifications relating to restored data? + return; + } + $DB->insert_record('studentquiz_notification', $data); } diff --git a/classes/task/send_digest_notification_task.php b/classes/task/send_digest_notification_task.php index 951d09ba..7a311992 100644 --- a/classes/task/send_digest_notification_task.php +++ b/classes/task/send_digest_notification_task.php @@ -68,14 +68,14 @@ public function execute() { if (!array_key_exists($notificationqueue->recipientid, $recipients)) { $recipients[$notificationqueue->recipientid] = []; } - $recipients[$notificationqueue->recipientid][] = unserialize($notificationqueue->content); + $recipients[$notificationqueue->recipientid][] = json_decode($notificationqueue->content); $recordids[] = $notificationqueue->id; } $notificationqueues->close(); foreach ($recipients as $userid => $datas) { $contentdata = [ - 'recipientname' => $datas[0]['messagedata']->recepientname, + 'recipientname' => $datas[0]->messagedata->recepientname, 'digesttype' => $studentquiz->digesttype == 1 ? $dailystring : $weeklystring, 'modulename' => $studentquiz->name, 'activityurl' => (new moodle_url('/mod/studentquiz/view.php', @@ -87,11 +87,11 @@ public function execute() { $total++; $contentdata['notifications'][] = [ 'seq' => $total, - 'timestamp' => $data['messagedata']->timestamp, - 'questionname' => $data['messagedata']->questionname, - 'actiontype' => $data['eventname'], - 'actorname' => $data['messagedata']->actorname, - 'isstudent' => $data['messagedata']->isstudent, + 'timestamp' => $data->messagedata->timestamp, + 'questionname' => $data->messagedata->questionname, + 'actiontype' => $data->eventname, + 'actorname' => $data->messagedata->actorname, + 'isstudent' => $data->messagedata->isstudent, ]; } $fullmessagehtml = $renderer->render_from_template('mod_studentquiz/digest_email_notification', $contentdata); @@ -111,7 +111,7 @@ public function execute() { message_send($eventdata); $messagetotal++; - mtrace("Notification to {$datas[0]['messagedata']->recepientname} has been sent", 1); + mtrace("Notification to {$datas[0]->messagedata->recepientname} has been sent", 1); } } diff --git a/db/install.xml b/db/install.xml index 15ab9653..0ad7ccfd 100644 --- a/db/install.xml +++ b/db/install.xml @@ -155,7 +155,7 @@ - + diff --git a/db/upgrade.php b/db/upgrade.php index 85ddae64..39445385 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -1723,5 +1723,33 @@ function xmldb_studentquiz_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023081702, 'studentquiz'); } + if ($oldversion < 2024080100) { + + // Change the format of data in studentquiz_notification.content. + $total = $DB->count_records('studentquiz_notification'); + + if ($total > 0) { + $progressbar = new progress_bar('updatenotifications', 500, true); + $transaction = $DB->start_delegated_transaction(); + + $notifications = $DB->get_recordset('studentquiz_notification', null, 'id', 'id, content'); + $count = 0; + foreach ($notifications as $notification) { + $progressbar->update($count, $total, "Update the state for question - {$count}/{$total} - id = $notification->id."); + $count++; + + $DB->set_field('studentquiz_notification', + 'content', json_encode(unserialize($notification->content)), + ['id' => $notification->id]); + } + $progressbar->update($count, $total, "Update the state for question - {$count}/{$total} - DONE!"); + $notifications->close(); + $transaction->allow_commit(); + } + + // Studentquiz savepoint reached. + upgrade_mod_savepoint(true, 2024080100, 'studentquiz'); + } + return true; } diff --git a/locallib.php b/locallib.php index 950f0b12..3ab832cc 100644 --- a/locallib.php +++ b/locallib.php @@ -453,7 +453,7 @@ function mod_studentquiz_send_notification($event, $recipient, $submitter, $data date_default_timezone_set('UTC'); $notificationqueue = new stdClass(); $notificationqueue->studentquizid = $data->moduleid; - $notificationqueue->content = serialize($customdata); + $notificationqueue->content = json_encode($customdata); $notificationqueue->recipientid = $recipient->id; if ($data->digesttype == 1) { $notificationqueue->timetosend = strtotime(date('Y-m-d')); diff --git a/tests/cron_test.php b/tests/cron_test.php index ffa89f61..a2f3e404 100644 --- a/tests/cron_test.php +++ b/tests/cron_test.php @@ -190,7 +190,7 @@ public function test_send_digest_notification_task(string $state) { $notificationqueue = new \stdClass(); $notificationqueue->studentquizid = $notifydata->moduleid; - $notificationqueue->content = serialize($customdata); + $notificationqueue->content = json_encode($customdata); $notificationqueue->recipientid = $this->student2->id; $notificationqueue->timetosend = strtotime('-1 day', strtotime(date('Y-m-d'))); $DB->insert_record('studentquiz_notification', $notificationqueue); diff --git a/version.php b/version.php index dbe9eac8..eaaf6f86 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024011900; +$plugin->version = 2024080100; $plugin->requires = 2022041900; // Version MOODLE_4.0. $plugin->component = 'mod_studentquiz'; $plugin->maturity = MATURITY_STABLE;