Skip to content

Commit

Permalink
MDL-60793 mod_chat: Renamed 'system' into 'issystem'
Browse files Browse the repository at this point in the history
'system' is now a reserved word in MySQL 8.0.3+.
  • Loading branch information
scara committed Dec 18, 2017
1 parent e8be9e7 commit 307dce6
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 31 deletions.
4 changes: 3 additions & 1 deletion mod/chat/backup/moodle2/backup_chat_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ protected function define_structure() {

// It is not cool to have two tags with same name, so we need to rename message field to message_text.
$message->set_source_alias('message', 'message_text');
// Renamed 'issystem' into 'system', for backward compatibility: 'system' is now a reserved word in MySQL 8.0.3+.
$message->set_source_alias('issystem', 'system');

// Build the tree.
$chat->add_child($messages);
$messages->add_child($message);
$messages->add_child($message);

// Define sources.
$chat->set_source_table('chat', array('id' => backup::VAR_ACTIVITYID));
Expand Down
1 change: 1 addition & 0 deletions mod/chat/backup/moodle2/restore_chat_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function process_chat_message($data) {
$data->userid = $this->get_mappingid('user', $data->userid);
$data->groupid = $this->get_mappingid('group', $data->groupid);
$data->message = $data->message_text;
$data->issystem = $data->system;

$newitemid = $DB->insert_record('chat_messages', $data);
$this->set_mapping('chat_message', $oldid, $newitemid); // Because of decode.
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/chat_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
foreach ($messages as $n => &$message) {
$tmp = new stdClass();
// When somebody enter room, user list will be updated.
if (!empty($message->system)) {
if (!empty($message->issystem)) {
$senduserlist = true;
}
if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
Expand Down
8 changes: 4 additions & 4 deletions mod/chat/chatd.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function dispatch_sidekick($handle, $type, $sessionid, $customdata) {
$msg->chatid = $this->sets_info[$sessionid]['chatid'];
$msg->userid = $this->sets_info[$sessionid]['userid'];
$msg->groupid = $this->sets_info[$sessionid]['groupid'];
$msg->system = 0;
$msg->issystem = 0;
$msg->message = 'beep '.$customdata['beep'];
$msg->timestamp = time();

Expand Down Expand Up @@ -428,7 +428,7 @@ public function dispatch_sidekick($handle, $type, $sessionid, $customdata) {
$msg->chatid = $this->sets_info[$sessionid]['chatid'];
$msg->userid = $this->sets_info[$sessionid]['userid'];
$msg->groupid = $this->sets_info[$sessionid]['groupid'];
$msg->system = 0;
$msg->issystem = 0;
$msg->message = urldecode($customdata['message']); // Have to undo the browser's encoding.
$msg->timestamp = time();

Expand Down Expand Up @@ -570,7 +570,7 @@ public function promote_final($sessionid, $customdata) {
$msg->chatid = $chatuser->chatid;
$msg->userid = $chatuser->userid;
$msg->groupid = $chatuser->groupid;
$msg->system = 1;
$msg->issystem = 1;
$msg->message = 'enter';
$msg->timestamp = time();

Expand Down Expand Up @@ -785,7 +785,7 @@ public function disconnect_session($sessionid) {
$msg->chatid = $info['chatid'];
$msg->userid = $info['userid'];
$msg->groupid = $info['groupid'];
$msg->system = 1;
$msg->issystem = 1;
$msg->message = 'exit';
$msg->timestamp = time();

Expand Down
2 changes: 1 addition & 1 deletion mod/chat/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public static function get_chat_latest_messages($chatsid, $chatlasttime = 0) {
$returnedmessages[] = array(
'id' => $message->id,
'userid' => $message->userid,
'system' => (bool) $message->system,
'system' => (bool) $message->issystem,
'message' => $messageformatted,
'timestamp' => $message->timestamp,
);
Expand Down
6 changes: 3 additions & 3 deletions mod/chat/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/chat/db" VERSION="20120122" COMMENT="XMLDB file for Moodle mod/chat"
<XMLDB PATH="mod/chat/db" VERSION="20171114" COMMENT="XMLDB file for Moodle mod/chat"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -30,7 +30,7 @@
<FIELD NAME="chatid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="system" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="issystem" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="message" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timestamp" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
Expand All @@ -50,7 +50,7 @@
<FIELD NAME="chatid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="system" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="issystem" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="message" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timestamp" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
Expand Down
31 changes: 30 additions & 1 deletion mod/chat/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
defined('MOODLE_INTERNAL') || die();

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

$dbman = $DB->get_manager();

// Automatically generated Moodle v3.2.0 release upgrade line.
// Put any upgrade step following this.
Expand All @@ -36,5 +38,32 @@ function xmldb_chat_upgrade($oldversion) {
// Automatically generated Moodle v3.4.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2017111301) {
// Rename field 'system' on table 'chat_messages' as it is a reserved word in MySQL 8+.
$table = new xmldb_table('chat_messages');
$field = new xmldb_field('system');
if ($dbman->field_exists($table, $field)) {
$field->set_attributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'groupid');
// Extend the execution time limit of the script to 2 hours.
upgrade_set_timeout(7200);
// Rename it to 'issystem'.
$dbman->rename_field($table, $field, 'issystem');
}

// Rename field 'system' on table 'chat_messages_current' as it is a reserved word in MySQL 8+.
$table = new xmldb_table('chat_messages_current');
$field = new xmldb_field('system');
if ($dbman->field_exists($table, $field)) {
$field->set_attributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'groupid');
// Extend the execution time limit of the script to 5 minutes.
upgrade_set_timeout(300);
// Rename it to 'issystem'.
$dbman->rename_field($table, $field, 'issystem');
}

// Savepoint reached.
upgrade_mod_savepoint(true, 2017111301, 'chat');
}

return true;
}
12 changes: 6 additions & 6 deletions mod/chat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,27 +723,27 @@ function chat_update_chat_times($chatid=0) {
*
* @param object $chatuser The chat user record.
* @param string $messagetext The message to be sent.
* @param bool $system False for non-system messages, true for system messages.
* @param bool $issystem False for non-system messages, true for system messages.
* @param object $cm The course module object, pass it to save a database query when we trigger the event.
* @return int The message ID.
* @since Moodle 2.6
*/
function chat_send_chatmessage($chatuser, $messagetext, $system = false, $cm = null) {
function chat_send_chatmessage($chatuser, $messagetext, $issystem = false, $cm = null) {
global $DB;

$message = new stdClass();
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $chatuser->groupid;
$message->message = $messagetext;
$message->system = $system ? 1 : 0;
$message->issystem = $issystem ? 1 : 0;
$message->timestamp = time();

$messageid = $DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
$message->id = $messageid;

if (!$system) {
if (!$issystem) {

if (empty($cm)) {
$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $chatuser->course);
Expand Down Expand Up @@ -801,7 +801,7 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser

// Start processing the message.

if (!empty($message->system)) {
if (!empty($message->issystem)) {
// System event.
$output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
$output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td>';
Expand Down Expand Up @@ -984,7 +984,7 @@ function chat_format_message_theme ($message, $chatuser, $currentuser, $grouping
" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid\">$message->picture</a>";

// Start processing the message.
if (!empty($message->system)) {
if (!empty($message->issystem)) {
$result->type = 'system';

$senderprofile = $CFG->wwwroot.'/user/view.php?id='.$sender->id.'&amp;course='.$courseid;
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
$sessionend = $message->timestamp;
}
if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session.
if ($message->userid and !$message->system) { // Remember user and count messages.
if ($message->userid and !$message->issystem) { // Remember user and count messages.
if (empty($sessionusers[$message->userid])) {
$sessionusers[$message->userid] = 1;
} else {
Expand Down
24 changes: 12 additions & 12 deletions mod/chat/tests/format_message_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,79 +44,79 @@ public function chat_format_message_manually_provider() {
return [
'Beep everyone' => [
'message' => 'beep all',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepseveryone', 'chat', '__CURRENTUSER__') . ': /',
'refreshusers' => false,
'beep' => true,
],
'Beep the current user' => [
'message' => 'beep __CURRENTUSER__',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepsyou', 'chat', '__CURRENTUSER__') . ': /',
'refreshusers' => false,
'beep' => true,
],
'Beep another user' => [
'message' => 'beep __OTHERUSER__',
'system' => false,
'issystem' => false,
'willreturn' => false,
'expecttext' => null,
'refreshusers' => null,
'beep' => null,
],
'Malformed beep' => [
'message' => 'beep',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: beep$/",
'refreshusers' => false,
'beep' => false,
],
'/me says' => [
'message' => '/me writes a test',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: \*\*\* __CURRENTUSER_FIRST__ writes a test$/",
'refreshusers' => false,
'beep' => false,
],
'Invalid command' => [
'message' => '/help',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: \/help$/",
'refreshusers' => false,
'beep' => false,
],
'To user' => [
'message' => 'To Bernard:I love tests',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
'refreshusers' => false,
'beep' => false,
],
'To user trimmed' => [
'message' => 'To Bernard: I love tests',
'system' => false,
'issystem' => false,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
'refreshusers' => false,
'beep' => false,
],
'System: enter' => [
'message' => 'enter',
'system' => true,
'issystem' => true,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: " . get_string('messageenter', 'chat', '__CURRENTUSER__') . "$/",
'refreshusers' => true,
'beep' => false,
],
'System: exit' => [
'message' => 'exit',
'system' => true,
'issystem' => true,
'willreturn' => true,
'expecttext' => "/^{$dateregexp}: " . get_string('messageexit', 'chat', '__CURRENTUSER__') . "$/",
'refreshusers' => true,
Expand All @@ -128,7 +128,7 @@ public function chat_format_message_manually_provider() {
/**
* @dataProvider chat_format_message_manually_provider
*/
public function test_chat_format_message_manually($messagetext, $system, $willreturn,
public function test_chat_format_message_manually($messagetext, $issystem, $willreturn,
$expecttext, $refreshusers, $expectbeep) {

$this->resetAfterTest();
Expand All @@ -147,7 +147,7 @@ public function test_chat_format_message_manually($messagetext, $system, $willre
$message = (object) [
'message' => $messagetext,
'timestamp' => time(),
'system' => $system,
'issystem' => $issystem,
];

$result = chat_format_message_manually($message, $course->id, $currentuser, $currentuser);
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

$plugin->version = 2017111300; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2017111301; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2017110800; // Requires this Moodle version.
$plugin->component = 'mod_chat'; // Full name of the plugin (used for diagnostics).
$plugin->cron = 300;

0 comments on commit 307dce6

Please sign in to comment.