Merge branch 'MDL-61255_master' of git://github.com/markn86/moodle

This commit is contained in:
Jake Dallimore 2018-04-11 09:53:14 +08:00
commit da16e3e3dc
15 changed files with 729 additions and 16 deletions

View File

@ -71,6 +71,7 @@ $string['messagepreferences'] = 'Message preferences';
$string['message'] = 'Message';
$string['messagepreferences'] = 'Message preferences';
$string['messages'] = 'Messages';
$string['messagingdatahasnotbeenmigrated'] = 'Your messages are temporarily unavailable due to upgrades in the messaging infrastructure. Please wait for them to be migrated.';
$string['messagingdisabled'] = 'Messaging is disabled on this site, emails will be sent instead';
$string['newonlymsg'] = 'Show only new';
$string['newmessage'] = 'New message';
@ -83,6 +84,7 @@ $string['nomessagesfound'] = 'No messages were found';
$string['noreply'] = 'Do not reply to this message';
$string['noncontacts'] = 'Non-contacts';
$string['nonotifications'] = 'You have no notifications';
$string['notificationdatahasnotbeenmigrated'] = 'Your notifications are temporarily unavailable due to upgrades in the notification infrastructure. Please wait for them to be migrated.';
$string['notificationwindow'] = 'Notification window';
$string['notificationpreferences'] = 'Notification preferences';
$string['notificationimage'] = 'Notification image';

View File

@ -4405,7 +4405,7 @@ function authenticate_user_login($username, $password, $ignorelockout=false, &$f
* @return stdClass A {@link $USER} object - BC only, do not use
*/
function complete_user_login($user) {
global $CFG, $USER, $SESSION;
global $CFG, $DB, $USER, $SESSION;
\core\session\manager::login_user($user);
@ -4429,6 +4429,16 @@ function complete_user_login($user) {
);
$event->trigger();
// Queue migrating the messaging data, if we need to.
if (!get_user_preferences('core_message_migrate_data', false, $USER->id)) {
// Check if there are any legacy messages to migrate.
if (\core_message\helper::legacy_messages_exist($USER->id)) {
\core_message\task\migrate_message_data::queue_task($USER->id);
} else {
set_user_preference('core_message_migrate_data', true, $USER->id);
}
}
if (isguestuser()) {
// No need to continue when user is THE guest.
return $USER;

View File

@ -330,4 +330,28 @@ class helper {
sort($ids);
return implode('_', $ids);
}
/**
* Checks if legacy messages exist for a given user.
*
* @param int $userid
* @return bool
*/
public static function legacy_messages_exist($userid) {
global $DB;
$sql = "SELECT id
FROM {message} m
WHERE useridfrom = ?
OR useridto = ?";
$messageexists = $DB->record_exists_sql($sql, [$userid, $userid]);
$sql = "SELECT id
FROM {message_read} m
WHERE useridfrom = ?
OR useridto = ?";
$messagereadexists = $DB->record_exists_sql($sql, [$userid, $userid]);
return $messageexists || $messagereadexists;
}
}

View File

@ -0,0 +1,272 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Adhoc task handling migrating data to the new messaging table schema.
*
* @package core_message
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\task;
defined('MOODLE_INTERNAL') || die();
/**
* Class handling migrating data to the new messaging table schema.
*
* @package core_message
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class migrate_message_data extends \core\task\adhoc_task {
/**
* Run the migration task.
*/
public function execute() {
global $DB;
$userid = $this->get_custom_data()->userid;
// Get the user's preference.
$hasbeenmigrated = get_user_preferences('core_message_migrate_data', false, $userid);
if (!$hasbeenmigrated) {
// To determine if we should update the preference.
$updatepreference = true;
// Get all the users the current user has received a message from.
$sql = "SELECT DISTINCT(useridfrom)
FROM {message} m
WHERE useridto = ?
UNION
SELECT DISTINCT(useridfrom)
FROM {message_read} m
WHERE useridto = ?";
$users = $DB->get_records_sql($sql, [$userid, $userid]);
// Get all the users the current user has messaged.
$sql = "SELECT DISTINCT(useridto)
FROM {message} m
WHERE useridfrom = ?
UNION
SELECT DISTINCT(useridto)
FROM {message_read} m
WHERE useridfrom = ?";
$users = $users + $DB->get_records_sql($sql, [$userid, $userid]);
if (!empty($users)) {
// Loop through each user and migrate the data.
foreach ($users as $otheruserid => $user) {
$ids = [$userid, $otheruserid];
sort($ids);
$key = implode('_', $ids);
// Set the lock data.
$timeout = 5; // In seconds.
$locktype = 'core_message_migrate_data';
// Get an instance of the currently configured lock factory.
$lockfactory = \core\lock\lock_config::get_lock_factory($locktype);
// See if we can grab this lock.
if ($lock = $lockfactory->get_lock($key, $timeout)) {
try {
$transaction = $DB->start_delegated_transaction();
$this->migrate_data($userid, $otheruserid);
$transaction->allow_commit();
} catch (\Throwable $e) {
$updatepreference = false;
}
$lock->release();
} else {
// Couldn't get a lock, move on to next user but make sure we don't update user preference so
// we still try again.
$updatepreference = false;
continue;
}
}
}
if ($updatepreference) {
set_user_preference('core_message_migrate_data', true, $userid);
} else {
// Throwing an exception in the task will mean that it isn't removed from the queue and is tried again.
throw new \moodle_exception('Task failed.');
}
}
}
/**
* Helper function to deal with migrating the data.
*
* @param int $userid The current user id.
* @param int $otheruserid The user id of the other user in the conversation.
* @throws \dml_exception
*/
private function migrate_data($userid, $otheruserid) {
global $DB;
if (!$conversationid = \core_message\api::get_conversation_between_users([$userid, $otheruserid])) {
$conversationid = \core_message\api::create_conversation_between_users([$userid, $otheruserid]);
}
// First, get the rows from the 'message' table.
$select = "(useridfrom = ? AND useridto = ?) OR (useridfrom = ? AND useridto = ?)";
$params = [$userid, $otheruserid, $otheruserid, $userid];
$messages = $DB->get_recordset_select('message', $select, $params, 'id ASC');
foreach ($messages as $message) {
if ($message->notification) {
$this->migrate_notification($message, false);
} else {
$this->migrate_message($conversationid, $message);
}
}
$messages->close();
// Ok, all done, delete the records from the 'message' table.
$DB->delete_records_select('message', $select, $params);
// Now, get the rows from the 'message_read' table.
$messages = $DB->get_recordset_select('message_read', $select, $params, 'id ASC');
foreach ($messages as $message) {
if ($message->notification) {
$this->migrate_notification($message, true);
} else {
$this->migrate_message($conversationid, $message);
}
}
$messages->close();
// Ok, all done, delete the records from the 'message_read' table.
$DB->delete_records_select('message_read', $select, $params);
}
/**
* Helper function to deal with migrating an individual notification.
*
* @param \stdClass $notification
* @param bool $isread Was the notification read?
* @throws \dml_exception
*/
private function migrate_notification($notification, $isread) {
global $DB;
$tabledata = new \stdClass();
$tabledata->useridfrom = $notification->useridfrom;
$tabledata->useridto = $notification->useridto;
$tabledata->subject = $notification->subject;
$tabledata->fullmessage = $notification->fullmessage;
$tabledata->fullmessageformat = $notification->fullmessageformat;
$tabledata->fullmessagehtml = $notification->fullmessagehtml;
$tabledata->smallmessage = $notification->smallmessage;
$tabledata->component = $notification->component;
$tabledata->eventtype = $notification->eventtype;
$tabledata->contexturl = $notification->contexturl;
$tabledata->contexturlname = $notification->contexturlname;
$tabledata->timeread = $notification->timeread ?? null;
$tabledata->timecreated = $notification->timecreated;
$newid = $DB->insert_record('notifications', $tabledata);
// Check if there is a record to move to the new 'message_popup_notifications' table.
if ($mp = $DB->get_record('message_popup', ['messageid' => $notification->id, 'isread' => (int) $isread])) {
$mpn = new \stdClass();
$mpn->notificationid = $newid;
$DB->insert_record('message_popup_notifications', $mpn);
$DB->delete_records('message_popup', ['id' => $mp->id]);
}
}
/**
* Helper function to deal with migrating an individual message.
*
* @param int $conversationid The conversation between the two users.
* @param \stdClass $message The message from either the 'message' or 'message_read' table
* @throws \dml_exception
*/
private function migrate_message($conversationid, $message) {
global $DB;
// Create the object we will be inserting into the database.
$tabledata = new \stdClass();
$tabledata->useridfrom = $message->useridfrom;
$tabledata->conversationid = $conversationid;
$tabledata->subject = $message->subject;
$tabledata->fullmessage = $message->fullmessage;
$tabledata->fullmessageformat = $message->fullmessageformat;
$tabledata->fullmessagehtml = $message->fullmessagehtml;
$tabledata->smallmessage = $message->smallmessage;
$tabledata->timecreated = $message->timecreated;
$messageid = $DB->insert_record('messages', $tabledata);
// Check if we need to mark this message as deleted for the user from.
if ($message->timeuserfromdeleted) {
$mua = new \stdClass();
$mua->userid = $message->useridfrom;
$mua->messageid = $messageid;
$mua->action = \core_message\api::MESSAGE_ACTION_DELETED;
$mua->timecreated = $message->timeuserfromdeleted;
$DB->insert_record('message_user_actions', $mua);
}
// Check if we need to mark this message as deleted for the user to.
if ($message->timeusertodeleted) {
$mua = new \stdClass();
$mua->userid = $message->useridto;
$mua->messageid = $messageid;
$mua->action = \core_message\api::MESSAGE_ACTION_DELETED;
$mua->timecreated = $message->timeusertodeleted;
$DB->insert_record('message_user_actions', $mua);
}
// Check if we need to mark this message as read for the user to (it is always read by the user from).
// Note - we do an isset() check here because this column only exists in the 'message_read' table.
if (isset($message->timeread)) {
$mua = new \stdClass();
$mua->userid = $message->useridto;
$mua->messageid = $messageid;
$mua->action = \core_message\api::MESSAGE_ACTION_READ;
$mua->timecreated = $message->timeread;
$DB->insert_record('message_user_actions', $mua);
}
}
/**
* Queues the task.
*
* @param int $userid
*/
public static function queue_task($userid) {
// Let's set up the adhoc task.
$task = new \core_message\task\migrate_message_data();
$task->set_custom_data(
[
'userid' => $userid
]
);
// Queue it.
\core\task\manager::queue_adhoc_task($task, true);
}
}

View File

@ -147,6 +147,14 @@ $messagearea = new \core_message\output\messagearea\message_area($user1->id, $us
// Now the page contents.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('messages', 'message'));
// Display a message if the messages have not been migrated yet.
if (!get_user_preferences('core_message_migrate_data', false, $user1id)) {
$notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
\core\output\notification::NOTIFY_WARNING);
echo $OUTPUT->render($notify);
}
// Display a message that the user is viewing someone else's messages.
if (!$currentuser) {
$notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),

View File

@ -56,11 +56,6 @@ class api {
$useridto = $USER->id;
}
$params = [
'useridto1' => $useridto,
'useridto2' => $useridto,
];
// Is notification enabled ?
if ($useridto == $USER->id) {
$disabled = $USER->emailstop;
@ -79,11 +74,12 @@ class api {
n.contexturlname, n.timecreated, n.component,
n.eventtype, n.timeread
FROM {notifications} n
WHERE n.useridto = :useridto1
WHERE n.id IN (SELECT notificationid FROM {message_popup_notifications})
AND n.useridto = ?
ORDER BY timecreated $sort, timeread $sort, id $sort";
$notifications = [];
$records = $DB->get_recordset_sql($sql, $params, $offset, $limit);
$records = $DB->get_recordset_sql($sql, [$useridto], $offset, $limit);
foreach ($records as $record) {
$notifications[] = (object) $record;
}
@ -109,7 +105,8 @@ class api {
return $DB->count_records_sql(
"SELECT count(id)
FROM {notifications}
WHERE useridto = ?
WHERE id IN (SELECT notificationid FROM {message_popup_notifications})
AND useridto = ?
AND timeread is NULL",
[$useridto]
);

View File

@ -18,5 +18,15 @@
<INDEX NAME="isread" UNIQUE="false" FIELDS="isread"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_popup_notifications" COMMENT="List of notifications to display in the message output popup">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="notificationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="notificationid" TYPE="foreign" FIELDS="notificationid" REFTABLE="notifications" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

View File

@ -83,5 +83,26 @@ function xmldb_message_popup_upgrade($oldversion) {
// Automatically generated Moodle v3.4.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2018032800) {
// Define table message_popup_notifications to be created.
$table = new xmldb_table('message_popup_notifications');
// Adding fields to table message_popup_notifications.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('notificationid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
// Adding keys to table message_popup_notifications.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('notificationid', XMLDB_KEY_FOREIGN, array('notificationid'), 'notifications', array('id'));
// Conditionally launch create table for message_popup_notifications.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Popup savepoint reached.
upgrade_plugin_savepoint(true, 2018032800, 'message', 'popup');
}
return true;
}

View File

@ -35,12 +35,29 @@ require_once($CFG->dirroot.'/message/output/lib.php');
class message_output_popup extends message_output {
/**
* Do nothing on send_message.
* Adds notifications to the 'message_popup_notifications' table if applicable.
*
* The reason for this is because we may not want to show all notifications in the notification popover. This
* can happen if the popup processor was disabled when the notification was sent. If the processor is disabled this
* function is never called so the notification will never be added to the 'message_popup_notifications' table.
* Essentially this table is used to filter what notifications to display from the 'notifications' table.
*
* @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
* @return true if ok, false if error
*/
public function send_message($eventdata) {
global $DB;
// Prevent users from getting popup notifications from themselves (happens with forum notifications).
if ($eventdata->userfrom->id != $eventdata->userto->id && $eventdata->notification) {
if (!$DB->record_exists('message_popup_notifications', ['notificationid' => $eventdata->savedmessageid])) {
$record = new stdClass();
$record->notificationid = $eventdata->savedmessageid;
$DB->insert_record('message_popup_notifications', $record);
}
}
return true;
}

View File

@ -65,6 +65,14 @@ $context = [
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('notifications', 'message'));
// Display a message if the notifications have not been migrated yet.
if (!get_user_preferences('core_message_migrate_data', false, $userid)) {
$notify = new \core\output\notification(get_string('notificationdatahasnotbeenmigrated', 'message'),
\core\output\notification::NOTIFY_WARNING);
echo $OUTPUT->render($notify);
}
echo $renderer->render_from_template('message_popup/notification_area', $context);
echo $OUTPUT->footer();

View File

@ -50,7 +50,14 @@ trait message_popup_test_helper {
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
return $DB->insert_record('notifications', $record);
$id = $DB->insert_record('notifications', $record);
$popup = new stdClass();
$popup->notificationid = $id;
$DB->insert_record('message_popup_notifications', $popup);
return $id;
}
/**
@ -86,6 +93,10 @@ trait message_popup_test_helper {
// Mark it as read.
\core_message\api::mark_notification_as_read($record);
$popup = new stdClass();
$popup->notificationid = $record->id;
$DB->insert_record('message_popup_notifications', $popup);
return $record->id;
}
}

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2018022000; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2018032800; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2017110800; // Requires this Moodle version
$plugin->component = 'message_popup'; // Full name of the plugin (used for diagnostics)

View File

@ -0,0 +1,333 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests the migrate message data task.
*
* @package core_message
* @category test
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
/**
* Class for testing the migrate message data task.
*
* @package core_message
* @category test
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_message_migrate_message_data_task_testcase extends advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Test migrating legacy messages.
*/
public function test_migrating_messages() {
global $DB;
// Create users to test with.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
// Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
$now = time();
$timedeleted1 = $now - (2 * DAYSECS);
$timedeleted2 = $now - (2 * DAYSECS) + 1;
$timeread1 = $now - DAYSECS;
$timeread2 = $now - DAYSECS + 1;
$timeread3 = $now - DAYSECS + 2;
// Send messages from user 1 to user 2.
$m1 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, false, $timeread1);
$m2 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 2);
$m3 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 3);
// Send messages from user 3 to user 1.
$m4 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, false, $timeread2);
$m5 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 5);
$m6 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 6);
// Send messages from user 3 to user 2.
$m7 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, false, $timeread3);
$m8 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 8);
$m9 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 9);
// Let's delete some messages, not using API here as it does not use the legacy tables.
$messageupdate = new stdClass();
$messageupdate->id = $m1;
$messageupdate->timeusertodeleted = $timedeleted1;
$DB->update_record('message_read', $messageupdate);
$messageupdate = new stdClass();
$messageupdate->id = $m5;
$messageupdate->timeuserfromdeleted = $timedeleted2;
$DB->update_record('message', $messageupdate);
// Now, let's execute the task for user 1.
$task = new \core_message\task\migrate_message_data();
$task->set_custom_data(
[
'userid' => $user1->id
]
);
$task->execute();
// Ok, now we need to confirm all is good.
// Remember - we are only converting the messages related to user 1.
$this->assertEquals(2, $DB->count_records('message'));
$this->assertEquals(1, $DB->count_records('message_read'));
$this->assertEquals(6, $DB->count_records('messages'));
$this->assertEquals(0, $DB->count_records('notifications'));
$this->assertEquals(0, $DB->count_records('message_popup_notifications'));
// Get the conversations.
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
$conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
// Confirm what we have in the messages table is correct.
$messages = $DB->get_records('messages', [], 'timecreated ASC');
$i = 1;
foreach ($messages as $message) {
$useridfrom = $user1->id;
$conversationid = $conversation1;
if ($i > 3) {
$useridfrom = $user3->id;
$conversationid = $conversation2;
}
if ($i == 1) {
$messagereadid1 = $message->id;
$messagedeletedid1 = $message->id;
} else if ($i == 4) {
$messagereadid2 = $message->id;
} else if ($i == 5) {
$messagedeletedid2 = $message->id;
}
$this->assertEquals($useridfrom, $message->useridfrom);
$this->assertEquals($conversationid, $message->conversationid);
$this->assertEquals('Subject ' . $i, $message->subject);
$this->assertEquals('Full message ' . $i, $message->fullmessage);
$this->assertEquals(FORMAT_PLAIN, $message->fullmessageformat);
$this->assertEquals('Full message HTML '. $i, $message->fullmessagehtml);
$this->assertEquals('Small message ' . $i, $message->smallmessage);
$this->assertEquals($i, $message->timecreated);
$i++;
}
// Confirm there are 4 actions.
$this->assertEquals(4, $DB->count_records('message_user_actions'));
// Confirm the messages that were marked as read have actions associated with them.
$muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_READ], 'timecreated DESC');
$this->assertCount(2, $muas);
// Message user action for message read by user 1 (referring to $m4).
$mua1 = array_shift($muas);
// Message user action for message read by user 2 (referring to $m1).
$mua2 = array_shift($muas);
$this->assertEquals($user1->id, $mua1->userid);
$this->assertEquals($messagereadid2, $mua1->messageid);
$this->assertEquals($timeread2, $mua1->timecreated);
$this->assertEquals($user2->id, $mua2->userid);
$this->assertEquals($messagereadid1, $mua2->messageid);
$this->assertEquals($timeread1, $mua2->timecreated);
// Confirm the messages that were deleted have actions associated with them.
$muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_DELETED],
'timecreated DESC');
$this->assertCount(2, $muas);
// Message user action for message deleted by user 3 (referring to $m5).
$mua1 = array_shift($muas);
// Message user action for message deleted by user 2 (referring to $m1).
$mua2 = array_shift($muas);
$this->assertEquals($user3->id, $mua1->userid);
$this->assertEquals($messagedeletedid2, $mua1->messageid);
$this->assertEquals($timedeleted2, $mua1->timecreated);
$this->assertEquals($user2->id, $mua2->userid);
$this->assertEquals($messagedeletedid1, $mua2->messageid);
$this->assertEquals($timedeleted1, $mua2->timecreated);
}
/**
* Test migrating legacy notifications.
*/
public function test_migrating_notifications() {
global $DB;
// Create users to test with.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
// Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
$timeread = time() - DAYSECS;
// Send notifications from user 1 to user 2.
$this->create_legacy_message_or_notification($user1->id, $user2->id, 1, true, $timeread);
$this->create_legacy_message_or_notification($user1->id, $user2->id, 2, true);
$this->create_legacy_message_or_notification($user1->id, $user2->id, 3, true);
// Send notifications from user 3 to user 1.
$this->create_legacy_message_or_notification($user3->id, $user1->id, 4, true, $timeread);
$this->create_legacy_message_or_notification($user3->id, $user1->id, 5, true);
$this->create_legacy_message_or_notification($user3->id, $user1->id, 6, true);
// Send notifications from user 3 to user 2.
$this->create_legacy_message_or_notification($user3->id, $user2->id, 7, true, $timeread);
$this->create_legacy_message_or_notification($user3->id, $user2->id, 8, true);
$this->create_legacy_message_or_notification($user3->id, $user2->id, 9, true);
// Now, let's execute the task for user 1.
$task = new \core_message\task\migrate_message_data();
$task->set_custom_data(
[
'userid' => $user1->id
]
);
$task->execute();
// Ok, now we need to confirm all is good.
// Remember - we are only converting the notifications related to user 1.
$this->assertEquals(2, $DB->count_records('message'));
$this->assertEquals(1, $DB->count_records('message_read'));
$this->assertEquals(3, $DB->count_records('message_popup'));
$this->assertEquals(6, $DB->count_records('notifications'));
$this->assertEquals(6, $DB->count_records('message_popup_notifications'));
// Confirm what we have in the notifications table is correct.
$notifications = $DB->get_records('notifications', [], 'timecreated ASC');
$popupnotifications = $DB->get_records('message_popup_notifications', [], 'notificationid ASC', 'notificationid');
$i = 1;
foreach ($notifications as $notification) {
// Assert the correct id is stored in the 'message_popup_notifications' table.
$this->assertArrayHasKey($notification->id, $popupnotifications);
$useridfrom = $user1->id;
$useridto = $user2->id;
if ($i > 3) {
$useridfrom = $user3->id;
$useridto = $user1->id;
}
$this->assertEquals($useridfrom, $notification->useridfrom);
$this->assertEquals($useridto, $notification->useridto);
$this->assertEquals('Subject ' . $i, $notification->subject);
$this->assertEquals('Full message ' . $i, $notification->fullmessage);
$this->assertEquals(FORMAT_PLAIN, $notification->fullmessageformat);
$this->assertEquals('Full message HTML '. $i, $notification->fullmessagehtml);
$this->assertEquals('Small message ' . $i, $notification->smallmessage);
$this->assertEquals('mod_assign', $notification->component);
$this->assertEquals('assign_notification', $notification->eventtype);
$this->assertEquals('https://www.google.com', $notification->contexturl);
$this->assertEquals('google', $notification->contexturlname);
$this->assertEquals($i, $notification->timecreated);
if (($i == 1) || ($i == 4)) {
$this->assertEquals($timeread, $notification->timeread);
} else {
$this->assertNull($notification->timeread);
}
$i++;
}
}
/**
* Creates a legacy message or notification to be used for testing.
*
* @param int $useridfrom The user id from
* @param int $useridto The user id to
* @param int $timecreated
* @param bool $notification
* @param int|null $timeread The time the message/notification was read, null if it hasn't been.
* @return int The id of the message (in either the message or message_read table)
* @throws dml_exception
*/
private function create_legacy_message_or_notification($useridfrom, $useridto, $timecreated = null,
$notification = false, $timeread = null) {
global $DB;
$tabledata = new \stdClass();
if (is_null($timecreated)) {
$timecreated = time();
}
if (!is_null($timeread)) {
$table = 'message_read';
$tabledata->timeread = $timeread;
} else {
$table = 'message';
}
if ($notification) {
$tabledata->eventtype = 'assign_notification';
$tabledata->component = 'mod_assign';
$tabledata->notification = 1;
$tabledata->contexturl = 'https://www.google.com';
$tabledata->contexturlname = 'google';
} else {
$tabledata->eventtype = 'instantmessage';
$tabledata->component = 'moodle';
$tabledata->notification = 0;
}
$tabledata->useridfrom = $useridfrom;
$tabledata->useridto = $useridto;
$tabledata->subject = 'Subject ' . $timecreated;
$tabledata->fullmessage = 'Full message ' . $timecreated;
$tabledata->fullmessageformat = FORMAT_PLAIN;
$tabledata->fullmessagehtml = 'Full message HTML ' . $timecreated;
$tabledata->smallmessage = 'Small message ' . $timecreated;
$tabledata->timecreated = $timecreated;
$id = $DB->insert_record($table, $tabledata);
// Insert into the legacy 'message_popup' table if it is a notification.
if ($notification) {
$mp = new stdClass();
$mp->messageid = $id;
$mp->isread = (!is_null($timeread)) ? 1 : 0;
$DB->insert_record('message_popup', $mp);
}
return $id;
}
}

View File

@ -37,9 +37,9 @@ information provided here is intended especially for developers.
- message_get_history
- message_get_recent_conversations
* Added new events for when a notification is sent and viewed.
* Removed the database tables 'message_popup' and 'message_working'. The 'message_working' table was introduced when
the messaging system was first introduced in Moodle, so, a long time ago. It served no real purpose. The
'message_popup' table is also no longer necessary now we have a separate table for notifications.
* Removed the database table 'message_working'. The 'message_working' table was introduced when
the messaging system was first introduced in Moodle, so, a long time ago. It was never necessary.
* Replaced the usage of the table 'message_popup' to a new table 'message_popup_notifications'.
=== 3.2 ===

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2018040500.01; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2018040500.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.