Allows linking of course groups with conversations when group messaging feature is enabled.
$mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
$mform->setType('enrolmentkey', PARAM_RAW);
+ // Group conversation messaging.
+ if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
+ $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
+ $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
+ }
+
$mform->addElement('static', 'currentpicture', get_string('currentpicture'));
$mform->addElement('checkbox', 'deletepicture', get_string('delete'));
$mform = $this->_form;
$groupid = $mform->getElementValue('id');
+ $coursecontext = context_course::instance($COURSE->id);
if ($group = $DB->get_record('groups', array('id' => $groupid))) {
-
+ // If can create group conversation then get if a conversation area exists and it is enabled.
+ if ($mform->elementExists('enablemessaging')) {
+ if (\core_message\helper::get_does_conversation_area_enabled('core_group',
+ 'groups',
+ $groupid,
+ $coursecontext->id)) {
+ $mform->getElement('enablemessaging')->setSelected(1);
+ }
+ }
// Print picture.
if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
$pic = get_string('none');
* @return id of group or false if error
*/
function groups_create_group($data, $editform = false, $editoroptions = false) {
- global $CFG, $DB;
+ global $CFG, $DB, $USER;
//check that courseid exists
$course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
// Invalidate the grouping cache for the course
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($course->id));
+ // 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,
+ $context->id,
+ 1,
+ $group->name);
+ }
+ }
+
// Trigger group event.
$params = array(
'context' => $context,
* @return bool true or exception
*/
function groups_update_group($data, $editform = false, $editoroptions = false) {
- global $CFG, $DB;
+ global $CFG, $DB, $USER;
$context = context_course::instance($data->courseid);
groups_update_group_icon($group, $data, $editform);
}
+ // 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 (!$data->enablemessaging && $data->enablemessaging != $conversationarea->enabled) {
+ \core_message\api::disable_conversation_area($conversationarea->id);
+ }
+ \core_message\api::update_conversation_name($conversationarea->conversationid, $group->name);
+ } else {
+ if (!empty($data->enablemessaging)) {
+ \core_message\api::create_conversation_area('core_group',
+ 'groups',
+ $group->id,
+ $context->id,
+ 1,
+ $group->name);
+ }
+ }
+ }
+
// Trigger group event.
$params = array(
'context' => $context,
}
$this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
}
+
+ /**
+ * Test groups_create_group enabling a group of conversation.
+ */
+ public function groups_create_group_with_conversation_area() {
+ global $DB;
+
+ $this->resetAfterTest();
+ $this->setAdminUser();
+ $course1 = $this->getDataGenerator()->create_course();
+ $coursecontext1 = context_course::instance($course1->id);
+ // Check not exists and conversation area created.
+ $this->assertEquals(
+ 0,
+ $DB->count_records_sql("SELECT COUNT(ca.id)
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'", [$coursecontext1->id])
+ );
+ // Create two groups and only one group with enablemessaging = 1.
+ $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
+ $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
+ // Check exist only one row created in conversation area.
+ $this->assertEquals(
+ 1,
+ $DB->count_records_sql("SELECT COUNT(ca.id)
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'
+ AND ca.enabled = 1", [$coursecontext1->id])
+ );
+ $conversation = $DB->get_record_sql("SELECT c.*
+ FROM {message_conversations} c
+ JOIN {message_conversation_area} ca ON c.id = ca.conversationid
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'", [$coursecontext1->id]);
+ // Check group name and course fullname was stored in conversation.
+ $this->assertEquals($group1a->name, $conversation->name);
+ // Check groupid was stored in itemid on conversation area.
+ $this->assertEquals(
+ $group1a->id,
+ $DB->get_field_sql("SELECT ca.itemid
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'
+ AND ca.conversationid = ?", [$coursecontext1->id, $conversation->id])
+ );
+ }
+
+ /**
+ * Test groups_update_group enabling and disabling a group of conversation.
+ */
+ public function test_groups_update_group_conversation_area() {
+ global $DB;
+
+ $this->resetAfterTest();
+ $this->setAdminUser();
+ $course1 = $this->getDataGenerator()->create_course();
+ $coursecontext1 = context_course::instance($course1->id);
+ // Check not exists and conversation area created.
+ $this->assertEquals(
+ 0,
+ $DB->count_records_sql("SELECT COUNT(ca.id)
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'", [$coursecontext1->id])
+ );
+ // Create two groups and only one group with enablemessaging = 1.
+ $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
+ $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
+ // Check exist only one row created in conversation area.
+ $this->assertEquals(
+ 1,
+ $DB->count_records_sql("SELECT COUNT(ca.id)
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'
+ AND ca.enabled = 1", [$coursecontext1->id])
+ );
+ // Check that the conversation area is created when group messaging is enabled in the course group.
+ $group1b->enablemessaging = 1;
+ groups_update_group($group1b);
+ $this->assertEquals(
+ 2,
+ $DB->count_records_sql("SELECT COUNT(ca.id)
+ FROM {message_conversation_area} ca
+ WHERE ca.contextid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'
+ AND ca.enabled = 1", [$coursecontext1->id])
+ );
+ $conversation1b = $DB->get_record_sql("SELECT c.*
+ FROM {message_conversations} c
+ JOIN {message_conversation_area} ca ON c.id = ca.conversationid
+ WHERE ca.contextid = ?
+ AND ca.itemid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'", [$coursecontext1->id, $group1b->id]);
+ // Check for group1b that group name was stored in conversation.
+ $this->assertEquals($group1b->name, $conversation1b->name);
+ // Check how to is disabled conversation area when group messaging is disabled in the course group.
+ $this->assertEquals(1, $DB->get_field("message_conversation_area", "enabled",
+ ['itemid' => $group1b->id,
+ 'contextid' => $coursecontext1->id]));
+ $group1b->enablemessaging = 0;
+ groups_update_group($group1b);
+ $this->assertEquals(0, $DB->get_field("message_conversation_area", "enabled",
+ ['itemid' => $group1b->id,
+ 'contextid' => $coursecontext1->id]));
+ // Check that the name of the conversation is changed when the name of the course group is updated.
+ $group1b->name = 'New group name';
+ $this->assertNotEquals($group1b->name, $conversation1b->name);
+ groups_update_group($group1b);
+ $conversation1b = $DB->get_record_sql("SELECT c.*
+ FROM {message_conversations} c
+ JOIN {message_conversation_area} ca ON c.id = ca.conversationid
+ WHERE ca.contextid = ?
+ AND ca.itemid = ?
+ AND ca.component = 'core_group'
+ AND ca.itemtype = 'groups'", [$coursecontext1->id, $group1b->id]);
+ $this->assertEquals($group1b->name, $conversation1b->name);
+ }
}
$string['editgroupingsettings'] = 'Edit grouping settings';
$string['editgroupsettings'] = 'Edit group settings';
$string['editusersgroupsa'] = 'Edit groups for "{$a}"';
+$string['enablemessaging'] = 'Group messaging';
+$string['enablemessaging_help'] = 'If enabled, group members can send messages to the others in their group via the messaging drawer.';
$string['enrolmentkey'] = 'Enrolment key';
$string['enrolmentkey_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key. If a group enrolment key is specified, then not only will entering that key let the user into the course, but it will also automatically make them a member of this group.
return $conversationarea;
}
+
+ /**
+ * Get 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.
+ * @return object message_conversation_area.
+ */
+ public static function get_conversation_area(string $component,
+ string $itemtype,
+ int $itemid,
+ int $contextid) {
+ global $DB;
+
+ return $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => $component,
+ 'itemtype' => $itemtype]);
+ }
+
+ /**
+ * Enable a conversation area.
+ *
+ * @param int $conversationareaid The id of a conversation area.
+ * @return void
+ */
+ public static function enable_conversation_area(int $conversationareaid) {
+ global $DB;
+
+ $conversationarea = new \stdClass();
+ $conversationarea->id = $conversationareaid;
+ $conversationarea->enabled = 1;
+ $conversationarea->timeread = time();
+ $DB->update_record('message_conversation_area', $conversationarea);
+ }
+
+ /**
+ * Disable a conversation area.
+ *
+ * @param int $conversationareaid The id of a conversation area.
+ * @return void
+ */
+ public static function disable_conversation_area(int $conversationareaid) {
+ global $DB;
+
+ $conversationarea = new \stdClass();
+ $conversationarea->id = $conversationareaid;
+ $conversationarea->enabled = 0;
+ $conversationarea->timeread = time();
+ $DB->update_record('message_conversation_area', $conversationarea);
+ }
+
+ /**
+ * Update the name of a conversation.
+ *
+ * @param int $conversationid The id of a conversation.
+ * @param string $name The main name of the area
+ * @return void
+ */
+ public static function update_conversation_name(int $conversationid, string $name) {
+ global $DB;
+
+ if ($conversation = $DB->get_record('message_conversations', array('id' => $conversationid))) {
+ if ($name <> $conversation->name) {
+ $conversation->name = $name;
+ $DB->update_record('message_conversations', $conversation);
+ }
+ }
+ }
}
return $messageexists || $messagereadexists;
}
+
+ /**
+ * Get if exists a conversation area and is enabled.
+ *
+ * @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 bool Returns if a conversation area exists and is enabled, false otherwise
+ */
+ public static function get_does_conversation_area_enabled(string $component,
+ string $itemtype,
+ int $itemid,
+ int $contextid) : bool {
+ global $DB;
+
+ return $DB->record_exists('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => $component,
+ 'itemtype' => $itemtype,
+ 'enabled' => 1]);
+ }
}
$DB->get_field('message_conversations', 'name', ['id' => $conversationarea->conversationid])
);
}
+
+ /**
+ * Test get_conversation_area.
+ */
+ public function test_get_conversation_area() {
+ global $DB;
+
+ $contextid = 111;
+ $itemid = 222;
+ $name = 'Name of conversation';
+ \core_message\api::create_conversation_area('core_group',
+ 'groups',
+ $itemid,
+ $contextid,
+ 1,
+ $name);
+ $conversationarea = \core_message\api::get_conversation_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(1, $conversationarea->enabled);
+ }
+
+ /**
+ * Test enable_conversation_area.
+ */
+ public function test_enable_conversation_area() {
+ global $DB;
+
+ $contextid = 111;
+ $itemid = 222;
+ $name = 'Name of conversation';
+ \core_message\api::create_conversation_area('core_group',
+ 'groups',
+ $itemid,
+ $contextid,
+ 0,
+ $name);
+ $conversationarea = $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => 'core_group',
+ 'itemtype' => 'groups']);
+ $this->assertEquals(0, $conversationarea->enabled);
+ \core_message\api::enable_conversation_area($conversationarea->id);
+ $conversationarea = $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => 'core_group',
+ 'itemtype' => 'groups']);
+ $this->assertEquals(1, $conversationarea->enabled);
+ }
+
+ /**
+ * Test disable_conversation_area.
+ */
+ public function test_disable_conversation_area() {
+ global $DB;
+
+ $contextid = 111;
+ $itemid = 222;
+ $name = 'Name of conversation';
+ \core_message\api::create_conversation_area('core_group',
+ 'groups',
+ $itemid,
+ $contextid,
+ 1,
+ $name);
+ $conversationarea = $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => 'core_group',
+ 'itemtype' => 'groups']);
+ $this->assertEquals(1, $conversationarea->enabled);
+ \core_message\api::disable_conversation_area($conversationarea->id);
+ $conversationarea = $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => 'core_group',
+ 'itemtype' => 'groups']);
+ $this->assertEquals(0, $conversationarea->enabled);
+ }
+ /**
+ * Test update_conversation_name.
+ */
+ public function test_update_conversation_name() {
+ global $DB;
+
+ $contextid = 111;
+ $itemid = 222;
+ $name = 'Name of conversation';
+ \core_message\api::create_conversation_area('core_group',
+ 'groups',
+ $itemid,
+ $contextid,
+ 1,
+ $name);
+ $conversationarea = $DB->get_record('message_conversation_area', ['itemid' => $itemid,
+ 'contextid' => $contextid,
+ 'component' => 'core_group',
+ 'itemtype' => 'groups']);
+ $this->assertEquals(
+ $name,
+ $DB->get_field('message_conversations', 'name', ['id' => $conversationarea->conversationid])
+ );
+
+ $newname = 'New name of conversation';
+ \core_message\api::update_conversation_name($conversationarea->conversationid, $newname);
+
+ $this->assertEquals(
+ $newname,
+ $DB->get_field('message_conversations', 'name', ['id' => $conversationarea->conversationid])
+ );
+ }
}