Skip to content

Commit

Permalink
StudentQuiz: switch stored notification to use JSON #812359
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Hunt <[email protected]>
  • Loading branch information
Khoa Nguyen Dang and timhunt committed Aug 7, 2024
1 parent 8f4fe71 commit 2287469
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
7 changes: 7 additions & 0 deletions backup/moodle2/restore_studentquiz_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 8 additions & 8 deletions classes/task/send_digest_notification_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="studentquizid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="StudentQuiz id"/>
<FIELD NAME="content" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Notification content"/>
<FIELD NAME="content" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Notification content JSON-encoded"/>
<FIELD NAME="recipientid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Recipient Id"/>
<FIELD NAME="status" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Status of the notification"/>
<FIELD NAME="timetosend" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Time to send the notification"/>
Expand Down
28 changes: 28 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion tests/cron_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2287469

Please sign in to comment.