MDL-63281 core_message: Sync course group members with conversations
authorcescobedo <carlos.escobedo@gmail.com>
Fri, 26 Oct 2018 02:30:52 +0000 (10:30 +0800)
committerMark Nelson <markn@moodle.com>
Fri, 26 Oct 2018 02:55:59 +0000 (10:55 +0800)
group/lib.php
group/tests/lib_test.php

index 7ff6984..c2edce9 100644 (file)
@@ -107,6 +107,11 @@ function groups_add_member($grouporid, $userorid, $component=null, $itemid=0) {
     // Invalidate the group and grouping cache for users.
     cache_helper::invalidate_by_definition('core', 'user_group_groupings', array(), array($userid));
 
+    // Group conversation messaging.
+    if ($conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $groupid, $context->id)) {
+        \core_message\api::add_members_to_conversation([$userid], $conversation->id);
+    }
+
     // Trigger group event.
     $params = array(
         'context' => $context,
@@ -211,6 +216,12 @@ function groups_remove_member($grouporid, $userorid) {
     // Invalidate the group and grouping cache for users.
     cache_helper::invalidate_by_definition('core', 'user_group_groupings', array(), array($userid));
 
+    // Group conversation messaging.
+    $context = context_course::instance($group->courseid);
+    if ($conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $groupid, $context->id)) {
+        \core_message\api::remove_members_from_conversation([$userid], $conversation->id);
+    }
+
     // Trigger group event.
     $params = array(
         'context' => context_course::instance($group->courseid),
@@ -440,7 +451,7 @@ function groups_update_group($data, $editform = false, $editoroptions = false) {
             \core_message\api::update_conversation_name($conversation->id, $group->name);
         } else {
             if (!empty($data->enablemessaging)) {
-                \core_message\api::create_conversation(
+                $conversation = \core_message\api::create_conversation(
                     \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
                     [],
                     $group->name,
@@ -450,6 +461,17 @@ function groups_update_group($data, $editform = false, $editoroptions = false) {
                     $group->id,
                     $context->id
                 );
+
+                // Add members to conversation if they exists in the group.
+                if ($groupmemberroles = groups_get_members_by_role($group->id, $group->courseid, 'u.id')) {
+                    $users = [];
+                    foreach ($groupmemberroles as $roleid => $roledata) {
+                        foreach ($roledata->users as $member) {
+                            $users[] = $member->id;
+                        }
+                    }
+                    \core_message\api::add_members_to_conversation($users, $conversation->id);
+                }
             }
         }
     }
index dd10f51..c572272 100644 (file)
@@ -624,4 +624,144 @@ class core_group_lib_testcase extends advanced_testcase {
         $conversation1b = $DB->get_record('message_conversations', ['id' => $conversation1b->id]);
         $this->assertEquals($group1b->name, $conversation1b->name);
     }
+
+    /**
+     * Test groups_add_member to conversation.
+     */
+    public function test_groups_add_member_conversation() {
+        global $DB;
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+
+        $course1 = $this->getDataGenerator()->create_course();
+        $coursecontext1 = context_course::instance($course1->id);
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+        $user3 = $this->getDataGenerator()->create_user();
+
+        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
+
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
+
+        // Add users to group1.
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
+
+        $conversation = \core_message\api::get_conversation_by_area(
+            'core_group',
+            'groups',
+            $group1->id,
+            $coursecontext1->id
+        );
+
+        // Check if the users has been added to the conversation.
+        $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+
+        // Check if the user has been added to the conversation when the conversation is disabled.
+        \core_message\api::disable_conversation($conversation->id);
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
+        $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+    }
+
+    /**
+     * Test groups_remove_member to conversation.
+     */
+    public function test_groups_remove_member_conversation() {
+        global $DB;
+
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+
+        $course1 = $this->getDataGenerator()->create_course();
+        $coursecontext1 = context_course::instance($course1->id);
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+        $user3 = $this->getDataGenerator()->create_user();
+
+        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
+
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
+
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
+
+        $conversation = \core_message\api::get_conversation_by_area(
+            'core_group',
+            'groups',
+            $group1->id,
+            $coursecontext1->id
+        );
+
+        // Check if there are three users in the conversation.
+        $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+
+        // Check if after removing one member in the conversation there are two members.
+        groups_remove_member($group1->id, $user1->id);
+        $this->assertEquals(2, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+
+        // Check if the user has been removed from the conversation when the conversation is disabled.
+        \core_message\api::disable_conversation($conversation->id);
+        groups_remove_member($group1->id, $user2->id);
+        $this->assertEquals(1, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+    }
+
+    /**
+     * Test if you enable group messaging in a group with members these are added to the conversation.
+     */
+    public function test_add_members_group_updated_conversation_enabled() {
+        global $DB;
+
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+
+        $course1 = $this->getDataGenerator()->create_course();
+        $coursecontext1 = context_course::instance($course1->id);
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+        $user3 = $this->getDataGenerator()->create_user();
+
+        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
+
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
+
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
+
+        $conversation = \core_message\api::get_conversation_by_area(
+            'core_group',
+            'groups',
+            $group1->id,
+            $coursecontext1->id
+        );
+
+        // No conversation should exist as 'enablemessaging' was set to 0.
+        $this->assertFalse($conversation);
+
+        // Check that the three users are in the conversation when group messaging is enabled in the course group.
+        $group1->enablemessaging = 1;
+        groups_update_group($group1);
+
+        $conversation = \core_message\api::get_conversation_by_area(
+            'core_group',
+            'groups',
+            $group1->id,
+            $coursecontext1->id
+        );
+
+        $this->assertEquals(3, $DB->count_records('message_conversation_members', ['conversationid' => $conversation->id]));
+    }
 }