diff --git a/group/lib.php b/group/lib.php index 8574a538817..7ff6984fcb0 100644 --- a/group/lib.php +++ b/group/lib.php @@ -278,8 +278,15 @@ function groups_create_group($data, $editform = false, $editoroptions = false) { // Group conversation messaging. if (\core_message\api::can_create_group_conversation($USER->id, $context)) { if (!empty($data->enablemessaging)) { - \core_message\api::create_conversation_area('core_group', 'groups', $group->id, $group->name, $context->id, - \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED); + \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $group->name, + \core_message\api::MESSAGE_CONVERSATION_ENABLED, + 'core_group', + 'groups', + $group->id, + $context->id); } } @@ -423,18 +430,26 @@ function groups_update_group($data, $editform = false, $editoroptions = false) { // Group conversation messaging. if (\core_message\api::can_create_group_conversation($USER->id, $context)) { - if ($conversationarea = \core_message\api::get_conversation_area('core_group', 'groups', $group->id, $context->id)) { - if ($data->enablemessaging && $data->enablemessaging != $conversationarea->enabled) { - \core_message\api::enable_conversation_area($conversationarea->id); + if ($conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $group->id, $context->id)) { + if ($data->enablemessaging && $data->enablemessaging != $conversation->enabled) { + \core_message\api::enable_conversation($conversation->id); } - if (!$data->enablemessaging && $data->enablemessaging != $conversationarea->enabled) { - \core_message\api::disable_conversation_area($conversationarea->id); + if (!$data->enablemessaging && $data->enablemessaging != $conversation->enabled) { + \core_message\api::disable_conversation($conversation->id); } - \core_message\api::update_conversation_name($conversationarea->conversationid, $group->name); + \core_message\api::update_conversation_name($conversation->id, $group->name); } else { if (!empty($data->enablemessaging)) { - \core_message\api::create_conversation_area('core_group', 'groups', $group->id, $group->name, - \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED); + \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $group->name, + \core_message\api::MESSAGE_CONVERSATION_ENABLED, + 'core_group', + 'groups', + $group->id, + $context->id + ); } } } diff --git a/group/tests/lib_test.php b/group/tests/lib_test.php index 01ad4864e20..dd10f512a63 100644 --- a/group/tests/lib_test.php +++ b/group/tests/lib_test.php @@ -530,9 +530,9 @@ class core_group_lib_testcase extends advanced_testcase { } /** - * Test groups_create_group enabling a group of conversation. + * Test groups_create_group enabling a group conversation. */ - public function test_groups_create_group_with_conversation_area() { + public function test_groups_create_group_with_conversation() { global $DB; $this->resetAfterTest(); @@ -544,21 +544,21 @@ class core_group_lib_testcase extends advanced_testcase { $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1)); $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0)); - $conversationareas = $DB->get_records('message_conversation_area', + $conversations = $DB->get_records('message_conversations', [ 'contextid' => $coursecontext1->id, 'component' => 'core_group', 'itemtype' => 'groups', - 'enabled' => \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED + 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED ] ); - $this->assertCount(1, $conversationareas); + $this->assertCount(1, $conversations); - $conversationarea = reset($conversationareas); + $conversation = reset($conversations); // Check groupid was stored in itemid on conversation area. - $this->assertEquals($group1a->id, $conversationarea->itemid); + $this->assertEquals($group1a->id, $conversation->itemid); - $conversations = $DB->get_records('message_conversations', ['id' => $conversationarea->conversationid]); + $conversations = $DB->get_records('message_conversations', ['id' => $conversation->id]); $this->assertCount(1, $conversations); $conversation = reset($conversations); @@ -568,9 +568,9 @@ class core_group_lib_testcase extends advanced_testcase { } /** - * Test groups_update_group enabling and disabling a group of conversation. + * Test groups_update_group enabling and disabling a group conversation. */ - public function test_groups_update_group_conversation_area() { + public function test_groups_update_group_conversation() { global $DB; $this->resetAfterTest(); @@ -582,41 +582,41 @@ class core_group_lib_testcase extends advanced_testcase { $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1)); $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0)); - $conversationareas = $DB->get_records('message_conversation_area', + $conversations = $DB->get_records('message_conversations', [ 'contextid' => $coursecontext1->id, 'component' => 'core_group', 'itemtype' => 'groups', - 'enabled' => \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED + 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED ] ); - $this->assertCount(1, $conversationareas); + $this->assertCount(1, $conversations); // Check that the conversation area is created when group messaging is enabled in the course group. $group1b->enablemessaging = 1; groups_update_group($group1b); - $conversationareas = $DB->get_records('message_conversation_area', + $conversations = $DB->get_records('message_conversations', [ 'contextid' => $coursecontext1->id, 'component' => 'core_group', 'itemtype' => 'groups', - 'enabled' => \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED + 'enabled' => \core_message\api::MESSAGE_CONVERSATION_ENABLED ], 'id ASC'); - $this->assertCount(2, $conversationareas); + $this->assertCount(2, $conversations); - $conversationarea1a = array_shift($conversationareas); - $conversationarea1b = array_shift($conversationareas); + $conversation1a = array_shift($conversations); + $conversation1b = array_shift($conversations); - $conversation1b = $DB->get_record('message_conversations', ['id' => $conversationarea1b->conversationid]); + $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]); // Check for group1b that group name was stored in conversation. $this->assertEquals($group1b->name, $conversation1b->name); $group1b->enablemessaging = 0; groups_update_group($group1b); - $this->assertEquals(0, $DB->get_field("message_conversation_area", "enabled", ['id' => $conversationarea1b->id])); + $this->assertEquals(0, $DB->get_field("message_conversations", "enabled", ['id' => $conversation1b->id])); // Check that the name of the conversation is changed when the name of the course group is updated. $group1b->name = 'New group name'; diff --git a/lib/db/install.xml b/lib/db/install.xml index 3148f1485b6..d78f754bb13 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -619,35 +619,22 @@ + + + + + + + - - - - - - - - - - - - - - - - - - - - - +
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 4c66eabacd7..40924a1c994 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2606,26 +2606,47 @@ function xmldb_main_upgrade($oldversion) { } if ($oldversion < 2018102300.02) { - // Create new 'message_conversation_area' table. - $table = new xmldb_table('message_conversation_area'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $table->add_field('conversationid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id'); - $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'conversationid'); - $table->add_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'component'); - $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'itemtype'); - $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'itemid'); - $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'contextid'); - $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'enabled'); - $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id'], null, null); - $table->add_key('conversationid', XMLDB_KEY_FOREIGN, ['conversationid'], 'message_conversations', ['id']); - $table->add_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']); - $table->add_index('component-itemtype-contextid-itemid', XMLDB_INDEX_UNIQUE, ['component', - 'itemtype', - 'itemid', - 'contextid']); - if (!$dbman->table_exists($table)) { - $dbman->create_table($table); + // Alter 'message_conversations' table to support groups. + $table = new xmldb_table('message_conversations'); + $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'convhash'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('itemtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'itemtype'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'itemid'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'contextid'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'enabled'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Add key. + $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']); + $dbman->add_key($table, $key); + + // Add index. + $index = new xmldb_index('component-itemtype-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, ['component', 'itemtype', + 'itemid', 'contextid']); + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); } upgrade_main_savepoint(true, 2018102300.02); diff --git a/message/classes/api.php b/message/classes/api.php index 5aff3976a33..876a4a69c73 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -76,12 +76,12 @@ class api { /** * The state for an enabled conversation area. */ - const MESSAGE_CONVERSATION_AREA_ENABLED = 1; + const MESSAGE_CONVERSATION_ENABLED = 1; /** * The state for a disabled conversation area. */ - const MESSAGE_CONVERSATION_AREA_DISABLED = 0; + const MESSAGE_CONVERSATION_DISABLED = 0; /** * Handles searching for messages in the message area. @@ -1469,10 +1469,18 @@ class api { * * @param int $type The type of conversation * @param int[] $userids The array of users to add to the conversation - * @param string $name The name of the conversation + * @param string|null $name The name of the conversation + * @param int $enabled Determines if the conversation is created enabled or disabled + * @param string|null $component Defines the Moodle component which the conversation belongs to, if any + * @param string|null $itemtype Defines the type of the component + * @param int|null $itemid The id of the component + * @param int|null $contextid The id of the context * @return \stdClass */ - public static function create_conversation(int $type, array $userids, string $name = null) { + public static function create_conversation(int $type, array $userids, string $name = null, + int $enabled = self::MESSAGE_CONVERSATION_ENABLED, string $component = null, + string $itemtype = null, int $itemid = null, int $contextid = null) { + global $DB; // Sanity check. @@ -1489,7 +1497,13 @@ class api { if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) { $conversation->convhash = helper::get_conversation_hash($userids); } + $conversation->component = $component; + $conversation->itemtype = $itemtype; + $conversation->itemid = $itemid; + $conversation->contextid = $contextid; + $conversation->enabled = $enabled; $conversation->timecreated = time(); + $conversation->timemodified = $conversation->timecreated; $conversation->id = $DB->insert_record('message_conversations', $conversation); // Add users to this conversation. @@ -1986,39 +2000,6 @@ class api { return $DB->count_records('message_conversation_members', ['conversationid' => $convid]); } - /** - * Create message conversation area. - * - * @param string $component Defines the Moodle component which the area was added to - * @param string $itemtype Defines the type of the component - * @param int $itemid The id of the component - * @param int $contextid The id of the context - * @param string $name The main name of the area - * @param int $enabled Enable or disable the conversation area - * @return \stdClass - */ - public static function create_conversation_area(string $component, string $itemtype, int $itemid, int $contextid, string $name, - int $enabled = self::MESSAGE_CONVERSATION_AREA_DISABLED) { - global $DB; - - // Create a conversation. - $conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_GROUP, array(), $name); - - // Create a conversation area. - $conversationarea = new \stdClass; - $conversationarea->conversationid = $conversation->id; - $conversationarea->component = $component; - $conversationarea->itemtype = $itemtype; - $conversationarea->itemid = $itemid; - $conversationarea->contextid = $contextid; - $conversationarea->enabled = $enabled; - $conversationarea->timecreated = time(); - $conversationarea->timemodified = $conversationarea->timecreated; - $conversationarea->id = $DB->insert_record('message_conversation_area', $conversationarea); - - return $conversationarea; - } - /** * Checks whether or not a conversation area is enabled. * @@ -2031,30 +2012,30 @@ class api { public static function is_conversation_area_enabled(string $component, string $itemtype, int $itemid, int $contextid) : bool { global $DB; - return $DB->record_exists('message_conversation_area', + return $DB->record_exists('message_conversations', [ 'itemid' => $itemid, 'contextid' => $contextid, 'component' => $component, 'itemtype' => $itemtype, - 'enabled' => self::MESSAGE_CONVERSATION_AREA_ENABLED + 'enabled' => self::MESSAGE_CONVERSATION_ENABLED ] ); } /** - * Get conversation area. + * Get conversation by area. * * @param string $component Defines the Moodle component which the area was added to. * @param string $itemtype Defines the type of the component. - * @param int $itemid The id of the component. - * @param int $contextid The id of the context. - * @return object message_conversation_area. + * @param int $itemid The id of the component. + * @param int $contextid The id of the context. + * @return \stdClass */ - public static function get_conversation_area(string $component, string $itemtype, int $itemid, int $contextid) { + public static function get_conversation_by_area(string $component, string $itemtype, int $itemid, int $contextid) { global $DB; - return $DB->get_record('message_conversation_area', + return $DB->get_record('message_conversations', [ 'itemid' => $itemid, 'contextid' => $contextid, @@ -2065,41 +2046,41 @@ class api { } /** - * Enable a conversation area. + * Enable a conversation. * - * @param int $conversationareaid The id of a conversation area. + * @param int $conversationid The id of the conversation. * @return void */ - public static function enable_conversation_area(int $conversationareaid) { + public static function enable_conversation(int $conversationid) { global $DB; - $conversationarea = new \stdClass(); - $conversationarea->id = $conversationareaid; - $conversationarea->enabled = self::MESSAGE_CONVERSATION_AREA_ENABLED; - $conversationarea->timemodified = time(); - $DB->update_record('message_conversation_area', $conversationarea); + $conversation = new \stdClass(); + $conversation->id = $conversationid; + $conversation->enabled = self::MESSAGE_CONVERSATION_ENABLED; + $conversation->timemodified = time(); + $DB->update_record('message_conversations', $conversation); } /** - * Disable a conversation area. + * Disable a conversation. * - * @param int $conversationareaid The id of a conversation area. + * @param int $conversationid The id of the conversation. * @return void */ - public static function disable_conversation_area(int $conversationareaid) { + public static function disable_conversation(int $conversationid) { global $DB; - $conversationarea = new \stdClass(); - $conversationarea->id = $conversationareaid; - $conversationarea->enabled = self::MESSAGE_CONVERSATION_AREA_DISABLED; - $conversationarea->timemodified = time(); - $DB->update_record('message_conversation_area', $conversationarea); + $conversation = new \stdClass(); + $conversation->id = $conversationid; + $conversation->enabled = self::MESSAGE_CONVERSATION_DISABLED; + $conversation->timemodified = time(); + $DB->update_record('message_conversations', $conversation); } /** * Update the name of a conversation. * - * @param int $conversationid The id of a conversation. + * @param int $conversationid The id of a conversation. * @param string $name The main name of the area * @return void */ diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 6ba1fdf58b3..5aabfb2ad16 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -3264,91 +3264,113 @@ class core_message_api_testcase extends core_message_messagelib_testcase { } /** - * Test create message conversation area. + * Test create message conversation with area. */ - public function test_create_conversation_area() { - $this->resetAfterTest(); + public function test_create_conversation_with_area() { $contextid = 111; $itemid = 222; $name = 'Name of conversation'; - $conversationarea = \core_message\api::create_conversation_area('core_group', 'groups', $itemid, $contextid, $name); + $conversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $name, + \core_message\api::MESSAGE_CONVERSATION_DISABLED, + 'core_group', + 'groups', + $itemid, + $contextid + ); - $this->assertEquals($itemid, $conversationarea->itemid); - $this->assertEquals($contextid, $conversationarea->contextid); - $this->assertEquals('core_group', $conversationarea->component); - $this->assertEquals('groups', $conversationarea->itemtype); - $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_AREA_DISABLED, $conversationarea->enabled); + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_DISABLED, $conversation->enabled); + $this->assertEquals('core_group', $conversation->component); + $this->assertEquals('groups', $conversation->itemtype); + $this->assertEquals($itemid, $conversation->itemid); + $this->assertEquals($contextid, $conversation->contextid); } /** - * Test get_conversation_area. + * Test get_conversation_by_area. */ - public function test_get_conversation_area() { + public function test_get_conversation_by_area() { $contextid = 111; $itemid = 222; $name = 'Name of conversation'; - \core_message\api::create_conversation_area('core_group', 'groups', $itemid, $contextid, $name, - \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED); - $conversationarea = \core_message\api::get_conversation_area('core_group', 'groups', $itemid, $contextid); + $createconversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $name, + \core_message\api::MESSAGE_CONVERSATION_DISABLED, + 'core_group', + 'groups', + $itemid, + $contextid + ); + $conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $itemid, $contextid); - $this->assertEquals($itemid, $conversationarea->itemid); - $this->assertEquals($contextid, $conversationarea->contextid); - $this->assertEquals('core_group', $conversationarea->component); - $this->assertEquals('groups', $conversationarea->itemtype); - $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED, $conversationarea->enabled); + $this->assertEquals($createconversation->id, $conversation->id); + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_DISABLED, $conversation->enabled); + $this->assertEquals('core_group', $conversation->component); + $this->assertEquals('groups', $conversation->itemtype); + $this->assertEquals($itemid, $conversation->itemid); + $this->assertEquals($contextid, $conversation->contextid); } /** - * Test enable_conversation_area. + * Test enable_conversation. */ - public function test_enable_conversation_area() { + public function test_enable_conversation() { global $DB; - $contextid = 111; - $itemid = 222; $name = 'Name of conversation'; - $conversationarea = \core_message\api::create_conversation_area('core_group', 'groups', $itemid, $contextid, $name); - \core_message\api::enable_conversation_area($conversationarea->id); - $conversationareaenabled = $DB->get_field('message_conversation_area', 'enabled', ['id' => $conversationarea->id]); - $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED, $conversationareaenabled); + $conversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $name, + \core_message\api::MESSAGE_CONVERSATION_DISABLED + ); + + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_DISABLED, $conversation->enabled); + \core_message\api::enable_conversation($conversation->id); + $conversationenabled = $DB->get_field('message_conversations', 'enabled', ['id' => $conversation->id]); + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_ENABLED, $conversationenabled); } /** - * Test disable_conversation_area. + * Test disable_conversation. */ - public function test_disable_conversation_area() { + public function test_disable_conversation() { global $DB; - $contextid = 111; - $itemid = 222; $name = 'Name of conversation'; - $conversationarea = \core_message\api::create_conversation_area('core_group', 'groups', $itemid, $contextid, $name, - \core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED); - $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_AREA_ENABLED, $conversationarea->enabled); - \core_message\api::disable_conversation_area($conversationarea->id); - $conversationareaenabled = $DB->get_field('message_conversation_area', 'enabled', ['id' => $conversationarea->id]); - $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_AREA_DISABLED, $conversationareaenabled); + $conversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, + [], + $name, + \core_message\api::MESSAGE_CONVERSATION_ENABLED + ); + + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_ENABLED, $conversation->enabled); + \core_message\api::disable_conversation($conversation->id); + $conversationenabled = $DB->get_field('message_conversations', 'enabled', ['id' => $conversation->id]); + $this->assertEquals(\core_message\api::MESSAGE_CONVERSATION_DISABLED, $conversationenabled); } + /** * Test update_conversation_name. */ public function test_update_conversation_name() { global $DB; - $contextid = 111; - $itemid = 222; - $name = 'Name of conversation'; - - $conversationarea = \core_message\api::create_conversation_area('core_group', 'groups', $itemid, $contextid, $name); + $conversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP, []); $newname = 'New name of conversation'; - \core_message\api::update_conversation_name($conversationarea->conversationid, $newname); + \core_message\api::update_conversation_name($conversation->id, $newname); $this->assertEquals( $newname, - $DB->get_field('message_conversations', 'name', ['id' => $conversationarea->conversationid]) + $DB->get_field('message_conversations', 'name', ['id' => $conversation->id]) ); }